简体   繁体   English

我怎样才能在 git 中获取文件的每次提交的代码行

[英]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.

相关问题 `git commit -p`:每个字符我可以得到更好的差异吗? - `git commit -p`: can I get better diff, per chars? 如何通过礼物库获取git commit文件的内容? - How can I get the contents of a file of a git commit with the gift library? 我如何获得每次提交的代码行 - How I can get line of code for each commit 如何获取git log来限制每条提交消息的行数? - How can I get git log to limit the number of lines per commit message? 如何将文件匹配到git commit? - How can I match a file to a git commit? 我如何从git压缩的提交中获取最后的提交 - How can i get the last commit from squashed commit in git 如何获得文件所有权,即每个开发人员为应用程序的每次提交添加的代码行 - How to get file ownership i.e lines of code added by each developer per commit for an application 如何获取 git 日志以在一行上打印每个提交的完整 hash 和简短统计信息? - How can I get git log to print the full hash and short stats of each commit on one line? 我如何使用git只在一个文件中提交一行用于提交,所有这些都来自脚本? - How can I use git to stage only one line in a file for commit, all from a script? 如何将 git format-patch SHAx..SHAy 到 output 到单个补丁文件,而不是每次提交一个补丁文件? - How can I `git format-patch SHAx..SHAy` to output to a single patch file, not one per commit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM