threads / discuss / 17024

Quick command to count commits

Subject: Quick command to count commits

## tl;dr

4 messages between Jan 6, 2009 and Jan 6, 2009.

replies: 3people: 4as markdown or json

Henk· Jan 6, 2009, 18:51 UTC · lore
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
-- 
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.
Shawn O. Pearce· Jan 6, 2009, 18:55 UTC · re: Henk · lore

Re: Quick command to count commits

Henk <henk_westhuis@hotmail.com> wrote:
Show 6 quoted lines
> 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.

-- 
Shawn.
Linus Torvalds· Jan 6, 2009, 19:30 UTC · re: Henk · lore

Re: Quick command to count commits

On Tue, 6 Jan 2009, Henk wrote:
Show 7 quoted lines
>
> 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
Ted Pavlic· Jan 6, 2009, 19:55 UTC · re: Henk · lore

Re: Quick command to count commits

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
On 1/6/09 1:51 PM, Henk wrote:
Show 11 quoted lines
>
> 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
-- 
Ted Pavlic <ted@tedpavlic.com>

← back to recent threads