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