git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Quick command to count commits

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Jan 6, 2009, 19:30 UTC
Message-ID
<alpine.LFD.2.00.0901061120530.3057@localhost.localdomain>
In-Reply-To
<1231267896595-2118851.post@n2.nabble.com>
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
Previous: Shawn O. PearceNext: Ted Pavlic
Message 3 of 4 in “Quick command to count commits”
  1. HenkJan 6, 2009
  2. Shawn O. PearceJan 6, 2009
  3. Linus TorvaldsJan 6, 2009
  4. Ted PavlicJan 6, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.