如何在Git存储库中按作者计数每个文件路径名的提交次数?

问题描述:

git-blame

While git-blame and counting number of lines changed by an author within a git repository are helpful, is there a command that can list all of the pathnames modified in a repo across all commits by either an author or set of authors that scores each file by the number of commits by that author or set of authors? E.g. the output from running such a command in a cloned git repo would be similar to:

1    /path/to/some/file/in/repo/file1
34   /path/to/some/file/in/repo/file2
3    /path/to/some/other/file/in/repo/anotherfile
...

谢谢!

刚刚意识到,如果您使用-仅名称来打印文件名,则将漂亮的格式打印为空字符串,并使用此方法以* nix/OS X中的最高提交次数进行排序,唯一化和排序,您可以使用:>

Just realized that if you use --name-only to print the filenames, pretty format as empty string, and use this method to sort, uniq, and sort by top number of commits, in *nix/OS X, you could use:

git log --name-only --author=John --pretty=format: | sort | uniq -c | sort -nr

请确保您使用的是正确的作者.

Be sure that you are using the right author.

例如如果我们试图在Rails中找到DHH的作者,我们可以这样做:

E.g. if we were trying to find DHH's authors in Rails, we might do:

git log --format='%aN <%aE>' | LC_ALL='C' sort -u | grep avid

,请注意,DHH在Rails git repo中的所有作者都使用了"David Heinemeier Hansson"这个名字.因此,我们可以这样做:

and notice that all of DHH's authors in the Rails git repo use the name "David Heinemeier Hansson". So, then we could do:

git log --name-only --author="David Heinemeier Hansson" --pretty=format: | sort | uniq -c | sort -nr

可能输出:

3624 
 611 actionpack/CHANGELOG
 432 activerecord/CHANGELOG
 329 railties/CHANGELOG
 206 activerecord/lib/active_record/base.rb
 195 activesupport/CHANGELOG
 157 actionpack/lib/action_controller/base.rb
 153 railties/Rakefile
 108 activerecord/lib/active_record/associations.rb
  79 actionpack/lib/action_view/helpers/javascript_helper.rb
  75 activerecord/lib/active_record/validations.rb
  74 activerecord/test/base_test.rb
  69 actionmailer/CHANGELOG
  66 railties/lib/rails_generator/generators/applications/app/app_generator.rb
  66 activerecord/Rakefile
  66 actionpack/lib/action_controller/caching.rb
  60 actionpack/lib/action_controller/routing.rb
  59 railties/lib/initializer.rb
  59 actionpack/Rakefile
  57 actionpack/lib/action_controller/request.rb
  ...

因此,截至2015年2月21日,Rails git存储库中共有3624个文件,看来他从未亲自提交过该文件,该文件的最大提交数量是ActionPack CHANGELOG,共611次提交,其次是ActiveRecord CHANGELOG和ActiveRecord :: Base是他提交最多的Ruby文件.

So, as of 2015-02-21, there were 3624 files in the Rails git repo that it appears he never personally made commits to, the top number of commits for a file was the ActionPack CHANGELOG at 611 commits, followed by the ActiveRecord CHANGELOG, and ActiveRecord::Base was the Ruby file he made the most commits to.

如果要从计数中排除未触及的文件数,请使用-format = 而不是-pretty = format:,例如:>

If you want to exclude the number of files not touched from the counts, use --format= instead of --pretty=format:, e.g.:

git log --name-only --author="David Heinemeier Hansson" --format: | sort | uniq -c | sort -nr