How to count number of commits per file pathname by author in a Git repository?

前端 未结 2 1380
名媛妹妹
名媛妹妹 2021-02-07 10:18

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 a

2条回答
  •  我在风中等你
    2021-02-07 10:54

    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.

    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
    

    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
    

    Which might output:

    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
      ...
    

    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.

    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
    

提交回复
热议问题