Git Blame コミット統計 質問する

Git Blame コミット統計 質問する

各コミッターからリポジトリに現在何行 (コード) あるのかという統計を取得するために、blame (またはより適切な関数、および/またはシェル コマンドと組み合わせて) を「悪用」するにはどうすればよいでしょうか。

出力例:

Committer 1: 8046 Lines
Committer 2: 4378 Lines

ベストアンサー1

アップデート

git ls-tree -r -z --name-only HEAD -- */*.c  | sed 's/^/.\//' | xargs -0 -n1 git blame \
--line-porcelain HEAD |grep -ae "^author "|sort|uniq -c|sort -nr

途中でいくつか更新しました。

便宜上、これを独自のコマンドにすることもできます。

#!/bin/bash

# save as i.e.: git-authors and set the executable flag
git ls-tree -r -z --name-only HEAD -- $1 | sed 's/^/.\//' | xargs -0 -n1 git blame \
 --line-porcelain HEAD |grep -ae "^author "|sort|uniq -c|sort -nr

これをパスのどこかに保存するか、パスを変更して次のように使用します。

  • git authors '*/*.c' # look for all files recursively ending in .c
  • git authors '*/*.[ch]' # look for all files recursively ending in .c or .h
  • git authors 'Makefile' # just count lines of authors in the Makefile

元の回答

受け入れられた回答は機能しますが、非常に遅いです。

$ git ls-tree --name-only -z -r HEAD|egrep -z -Z -E '\.(cc|h|cpp|hpp|c|txt)$' \
  |xargs -0 -n1 git blame --line-porcelain|grep "^author "|sort|uniq -c|sort -nr

ほぼ瞬時です。

現在追跡されているファイルのリストを取得するには、

git ls-tree --name-only -r HEAD

このソリューションfileでは、ファイルタイプを判別するための呼び出しを回避し、パフォーマンス上の理由から、grep を使用して必要な拡張子を一致させます。すべてのファイルを含める必要がある場合は、行からこれを削除するだけです。

grep -E '\.(cc|h|cpp|hpp|c)$' # for C/C++ files
grep -E '\.py$'               # for Python files

ファイルにスペースが含まれる可能性がある場合 (シェルにとっては好ましくない)、次のように使用できます。

git ls-tree -z --name-only -r HEAD | egrep -Z -z '\.py'|xargs -0 ... # passes newlines as '\0'

ファイルのリストを(パイプ経由で)渡すと、xargs を使用してコマンドを呼び出し、引数を配布できます。複数のファイルを処理できるコマンドは を省略します。-n1この場合、 を呼び出しgit blame --line-porcelain、呼び出しごとに 1 つの引数を使用します。

xargs -n1 git blame --line-porcelain

次に、出力をフィルタリングして「author」の出現箇所を検索し、リストを並べ替えて重複行をカウントします。

grep "^author "|sort|uniq -c|sort -nr

注記

他の回答では、実際には空白のみを含む行がフィルタリングされます。

grep -Pzo "author [^\n]*\n([^\n]*\n){10}[\w]*[^\w]"|grep "author "

上記のコマンドは、少なくとも 1 つの非空白文字を含む行の作成者を出力します。match を使用すると、\w*[^\w#]最初の非空白文字が#(多くのスクリプト言語ではコメント) ではない行も除外できます。

おすすめ記事