我想获得Git存储库的提交数量,有点像SVN的修订号。

我们的目标是将其用作唯一的、递增的构建号。

我目前这样做,在Unix/Cygwin/msysGit上:

git log --pretty=format:'' | wc -l

但我觉得它有点过时了。

有更好的办法吗?如果我实际上不需要wc甚至Git,那么它就可以在裸Windows上工作,那就太棒了。只要读取一个文件或目录结构…


当前回答

如果您正在为提交寻找一个唯一且可读性强的标识符,那么git describe可能正是适合您的东西。

其他回答

你不是第一个在Git中考虑“修订号”的人,但是“wc”是相当危险的,因为commit可以被删除或压缩,历史可以被重新审视。

“修订号”对于Subversion来说尤其重要,因为在合并时需要它(SVN1.5和1.6在这方面有所改进)。

您可能最终得到一个预提交钩子,它将在注释中包含一个修订号,而算法不需要查找分支的所有历史记录来确定正确的数字。

Bazaar实际上提出了这样一个算法,对于您想要做的事情来说,它可能是一个很好的起点。

(正如Bombe的回答所指出的,Git实际上有自己的算法,基于最新的标记,加上提交的数量,再加上一点SHA-1密钥)。如果他的回答对你有用,你应该看看(并投票)。


为了说明Aaron的想法,您还可以将Git提交散列附加到随应用程序分发的应用程序的“info”文件中。

这样,about框看起来就像这样:

应用程序编号是提交的一部分,但“应用程序的“信息”文件”是在打包过程中生成的,有效地将应用程序构建号与技术修订id链接起来。

如果你只使用一个分支,比如master,我认为这会很好:

git rev-list --full-history --all | wc -l

这将只输出一个数字。你可以将它别名为

git revno

让事情变得更方便。要做到这一点,编辑你的.git/配置文件,并添加到:

[alias]
    revno = "!git rev-list --full-history --all | wc -l"

这将不能在Windows上工作。我不知道该操作系统的“wc”对应的是什么,但编写一个Python脚本来为您进行计数将是一个多平台解决方案。

编辑:获得两次提交之间的计数:


我正在寻找一个答案,该答案将显示如何获得两个任意修订之间的提交数,但没有看到任何答案。

git rev-list --count [older-commit]..[newer-commit]

Git shortlog本身并没有解决提交总数的原始问题(没有按作者分组)

这是事实,git rev-list HEAD—count仍然是最简单的答案。

然而,随着Git 2.29(2020年Q4)的出现,“Git shortlog”(man)变得更加精确。 它已经被教导根据预告行内容来分组提交,比如“审阅者:”,“合著者:”,等等。

参见Jeff King (peff)的commit 63d24fa、commit 56d5dde、commit 87abb96、commit f17b0b9、commit 47beb37、commit f0939a0、commit 92338c4(2020年9月27日)和commit 45d93eb(2020年9月25日)。 (由Junio C Hamano—gitster—在commit 2fa8aac中合并,2020年10月4日)

Shortlog:允许指定多个组 署名:杰夫·金

Now that shortlog supports reading from trailers, it can be useful to combine counts from multiple trailers, or between trailers and authors. This can be done manually by post-processing the output from multiple runs, but it's non-trivial to make sure that each name/commit pair is counted only once. This patch teaches shortlog to accept multiple --group options on the command line, and pull data from all of them. That makes it possible to run: git shortlog -ns --group=author --group=trailer:co-authored-by to get a shortlog that counts authors and co-authors equally. The implementation is mostly straightforward. The "group" enum becomes a bitfield, and the trailer key becomes a list. I didn't bother implementing the multi-group semantics for reading from stdin. It would be possible to do, but the existing matching code makes it awkward, and I doubt anybody cares. The duplicate suppression we used for trailers now covers authors and committers as well (though in non-trailer single-group mode we can skip the hash insertion and lookup, since we only see one value per commit). There is one subtlety: we now care about the case when no group bit is set (in which case we default to showing the author). The caller in builtin/log.c needs to be adapted to ask explicitly for authors, rather than relying on shortlog_init(). It would be possible with some gymnastics to make this keep working as-is, but it's not worth it for a single caller.

Git shortlog现在包含在它的手册页中:

——组= <类型> 分组提交基于<类型>。如果没有——group选项为 指定时,默认为author。<type>是其中之一: 作者,提交按作者分组 提交器,提交按提交器分组(与-c相同) 这是——group=committer的别名。

Git短日志现在还包括在它的手册页:

如果多次指定——group,则每次提交都被计数 值(但同样,在该提交中只对每个惟一值执行一次)。为 例如,git shortlog——group=author——group=trailer:co-author -by 包括作者和合著者。

在我们公司,我们从SVN转移到Git。缺乏修订数字是一个大问题!

执行git svn clone,然后用它的svn修订号标记上一次提交的svn:

export hr=`git svn find-rev HEAD`
git tag "$hr" -f HEAD

然后你可以在的帮助下得到修订号

git describe --tags --long

这个命令会给出如下内容:

7603-3-g7f4610d

含义:最后一个标签是7603 -这是SVN的修订。3 -是从它提交的计数。我们需要把它们加起来。

因此,修订号可以通过以下脚本计算:

expr $(git describe --tags --long | cut -d '-' -f 1) + $(git describe --tags --long | cut -d '-' -f 2)

Git shortlog是获取提交细节的一种方法:

git shortlog -s -n

这将给出提交数和作者名称。s选项删除作者每次提交的所有提交消息。如果还希望看到提交消息,则删除相同的选项。-n选项用于对整个列表进行排序。希望这能有所帮助。