Skip to contentSkip to conversation

Quick command to count commits

DISCUSSION Henk 4 participants 4 messages Jun 15, 2016

lore
Share & tools
lore

Henk

author
#0 lore raw

Hi,

For GitExtensions (windows git ui) I need a command to count all commits. I now use this command:
git.cmd rev-list --all --abbrev-commit | wc -l

This works perfect but its very slow in big repositories. Is there a faster way to count the commits?

Henk

Signature · 2 lines
View this message in context: http://n2.nabble.com/Quick-command-to-count-commits-tp2118851p2118851.html
Sent from the git mailing list archive at Nabble.com.
#1 lore raw
Quoted 6 lines · Henk

For GitExtensions (windows git ui) I need a command to count all commits. I now use this command:
git.cmd rev-list --all --abbrev-commit | wc -l

This works perfect but its very slow in big repositories. Is there a faster way to count the commits?

No. But drop the --abbrev-commit flag, abbreviating the object ids would take longer than just dumping the full 40 bytes to stdout. It requires a scan of parts of the object database for every commit to determine what is a unique abbreviation.

This isn't a very common operation. Its expensive to do, as you can see. What's the reason for supplying it, and needing it so quickly? Nobody really cares how many commits are in their history, unless they are trying to produce stats to describe how big their repository is. In which case the size of the .git/objects/pack *.pack and *.idx files is much more relevant, especially the .idx files as it gives a really quick estimate on the number of total objects in the repository.

Signature · 1 line
Shawn.
#2 lore raw
In reply to Henk

I doubt it's going to be much faster, but it's easier to type

git shortlog -s|numsum

I *think* that should give you the same number, and it forces git to do more of the counting (rather than wc).

Strangely, on one repo that I test that on, I get slightly different numbers from that command and yours. I'm not quite sure why (I guess shortlog doesn't count all commits?).

--Ted

Quoted 11 lines · Henk

Hi,

For GitExtensions (windows git ui) I need a command to count all commits. I now use this command:
git.cmd rev-list --all --abbrev-commit | wc -l

This works perfect but its very slow in big repositories. Is there a faster way to count the commits?

Henk

Signature · 1 line
Ted Pavlic <[email protected]>
#3 lore raw
In reply to Henk
Quoted 7 lines · Henk

For GitExtensions (windows git ui) I need a command to count all commits. I now use this command:
git.cmd rev-list --all --abbrev-commit | wc -l

This works perfect but its very slow in big repositories. Is there a faster way to count the commits?

Nope. Possibly drop the --abbrev-commit part, but it's not going to hurt _that_ much, and maybe avoiding piping the data can be a win in some cases.

Basically, to get commit counts, you need to traverse the whole history, or at least cache it. So the only way to speed things up is

- make sure your repository is well-packed. That will speed things up by an absolutely huge amount, if they weren't well-packed before. Just a single large pack-file, not lots of small packs, and not lots of loose objects.

- you can certainly cache it. Just index by the sha1sum of all the heads, and you have a great cache. Just keep a single entry. So _if_ the repository seldom changes, and you do this a lot, you'll at least only pay the price once.

IOW, do something like this:

	#!/bin/sh
	revs=$(git rev-parse --all)
	index=$(echo "$revs" | sha1sum | cut -d' ' -f1)
	cached=$(cat .git/commit_nr_cache)
	cached_index=$(echo "$cached" | cut -d' ' -f1)
	if [ "$index" == "$cached_index" ]; then
		echo "$cached" | cut -d' ' -f2
		exit
	fi
	nr=$(git rev-list $revs | wc -l)
	echo "$index $nr" > .git/commit_nr_cache
	echo $nr

and you now have a stupid single-entry cache.

Totally untested. You'll need to do _some_ work yourself ;)

Linus