[英]How can i get the line of code per commit for a file in git
i tried using the below statement in git:我尝试在 git 中使用以下语句:
$ wc -l file name
Above statement gives me only for the latest version.以上声明只给了我最新版本。 But i need for each and every commit, so how can i get it?
但是我需要每次提交,那么我怎么才能得到它呢?
The command you posted has nothing in common with git
.您发布的命令与
git
没有任何共同之处。 wc
is a general purpose Unix tool that counts the bytes, characters, words or lines in a file (its name stands for word count
). wc
是一个通用的 Unix 工具,用于计算文件中的字节、字符、单词或行word count
(它的名字代表word count
)。 It cannot be used to count the number of lines of code, is it is language agnostic and cannot tell a line of code from a comment.它不能用于计算代码行数,它是否与语言无关并且无法从注释中分辨出一行代码。
However, if you need the total number of lines in the file you can use the command:但是,如果您需要文件中的总行数,则可以使用以下命令:
git log --numstat --format=oneline -- file.txt | grep file.txt$
to get a history of changes on the specified file.获取指定文件的更改历史记录。
The output contains three columns separated by a variable number of spaces.输出包含由可变数量的空格分隔的三列。 The first column contains the number of lines added on each commit, the second column contains the number of lines removed on that commit, the third column is always the path of the file inside the project.
第一列包含每次提交添加的行数,第二列包含该提交删除的行数,第三列始终是项目内文件的路径。
The commits are displayed in reverse order, as usual;像往常一样,提交以相反的顺序显示; the first line is the most recent commit that modified the file, the last line is the commit when the file was created (and should contain
0
on the second column).第一行是最近修改文件的提交,最后一行是文件创建时的提交(第二列应该包含
0
)。
It's easy to write a simple script (in your preferred scripting language) to parse the output and generate the number of lines of the file on each commit.编写一个简单的脚本(使用您首选的脚本语言)来解析输出并在每次提交时生成文件的行数很容易。
Update更新
The above command line doesn't preserve the commit hashes.上面的命令行不保留提交哈希。 Running only the
git log
part of it generates two lines for each commit.仅运行其中的
git log
部分会为每次提交生成两行。 The first line contains the commit hash and the first line of the commit message, separated by a space.第一行包含提交哈希和提交消息的第一行,用空格分隔。 The second line is the one explained above.
第二行是上面解释的那一行。 Still an easy job for a small parsing script.
对于一个小的解析脚本来说仍然是一件容易的事。
In bash
, the parsing script could look like this:在
bash
,解析脚本可能如下所示:
#!/bin/bash
if [ ! -f $1 ]; then echo Cannot find the file \"$1\".; exit 1; fi
LINES=$(wc -l $1 | sed 's/^ *\([0-9]*\) .*$/\1/')
git log --numstat --format=oneline -- $1 | while read -r HASH COMMENT; do
read -r INSERTED DELETED FILENAME
echo $HASH $LINES
LINES=$(($LINES-$INSERTED+$DELETED))
done
It accepts the file path as its first argument and produces a list of lines;它接受文件路径作为它的第一个参数并生成一个行列表; each line contains the commit hash and the number of lines the input file had after that commit.
每行包含提交哈希和输入文件在提交后的行数。
You can use git log
to get all of the commit hashes for a given $filename
and check these out using a for
-loop in your shell.您可以使用
git log
获取给定$filename
所有提交哈希,并在您的 shell 中使用for
循环检查这些。 For example (in Zsh):例如(在 Zsh 中):
git stash
for sha in $(git log --format=format:%H -- $filename); do
git checkout $sha -- $filename
echo "$(wc -l $filename) @ $sha"
done
git stash pop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.