使用 git diff,如何添加和修改行号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8259851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using git diff, how can I get added and modified lines numbers?
提问by Mahmoud Khaled
Assuming I have a text file
假设我有一个文本文件
alex
bob
matrix
will be removed
git repo
and I have updated it to be
我已经将其更新为
alex
new line here
another new line
bob
matrix
git
Here, I have added lines number (2,3) and updated line number (6)
在这里,我添加了行号 (2,3) 并更新了行号 (6)
How can I get these line numbers info using git diff or any other git command?
如何使用 git diff 或任何其他 git 命令获取这些行号信息?
采纳答案by Sedrik
git diff --stat
will show you the output you get when committing stuff which is the one you are referring to I guess.
git diff --stat
将向您显示提交内容时获得的输出,我猜这就是您所指的内容。
git diff --stat
For showing exactly the line numbers that has been changed you can use
为了准确显示已更改的行号,您可以使用
git blame -p <file> | grep "Not Committed Yet"
And the line changed will be the last number before the ending parenthesis in the result. Not a clean solution though :(
更改的行将是结果中结束括号之前的最后一个数字。虽然不是一个干净的解决方案:(
回答by John Mellor
Here's a bash function to calculate the resulting line numbers from a diff:
这是一个 bash 函数,用于从差异计算结果行号:
diff-lines() {
local path=
local line=
while read; do
esc=$'3'
if [[ $REPLY =~ ---\ (a/)?.* ]]; then
continue
elif [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]$esc]+).* ]]; then
path=${BASH_REMATCH[2]}
elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,[0-9]+)?\ @@.* ]]; then
line=${BASH_REMATCH[2]}
elif [[ $REPLY =~ ^($esc\[[0-9;]+m)*([\ +-]) ]]; then
echo "$path:$line:$REPLY"
if [[ ${BASH_REMATCH[2]} != - ]]; then
((line++))
fi
fi
done
}
It can produce output such as:
它可以产生输出,例如:
$ git diff | diff-lines
http-fetch.c:1: #include "cache.h"
http-fetch.c:2: #include "walker.h"
http-fetch.c:3:
http-fetch.c:4:-int cmd_http_fetch(int argc, const char **argv, const char *prefix)
http-fetch.c:4:+int main(int argc, const char **argv)
http-fetch.c:5: {
http-fetch.c:6:+ const char *prefix;
http-fetch.c:7: struct walker *walker;
http-fetch.c:8: int commits_on_stdin = 0;
http-fetch.c:9: int commits;
http-fetch.c:19: int get_verbosely = 0;
http-fetch.c:20: int get_recover = 0;
http-fetch.c:21:
http-fetch.c:22:+ prefix = setup_git_directory();
http-fetch.c:23:+
http-fetch.c:24: git_config(git_default_config, NULL);
http-fetch.c:25:
http-fetch.c:26: while (arg < argc && argv[arg][0] == '-') {
fetch.h:1: #include "config.h"
fetch.h:2: #include "http.h"
fetch.h:3:
fetch.h:4:-int cmd_http_fetch(int argc, const char **argv, const char *prefix);
fetch.h:4:+int main(int argc, const char **argv);
fetch.h:5:
fetch.h:6: void start_fetch(const char* uri);
fetch.h:7: bool fetch_succeeded(int status_code);
from a diff like this:
从这样的差异:
$ git diff
diff --git a/builtin-http-fetch.c b/http-fetch.c
similarity index 95%
rename from builtin-http-fetch.c
rename to http-fetch.c
index f3e63d7..e8f44ba 100644
--- a/builtin-http-fetch.c
+++ b/http-fetch.c
@@ -1,8 +1,9 @@
#include "cache.h"
#include "walker.h"
-int cmd_http_fetch(int argc, const char **argv, const char *prefix)
+int main(int argc, const char **argv)
{
+ const char *prefix;
struct walker *walker;
int commits_on_stdin = 0;
int commits;
@@ -18,6 +19,8 @@ int cmd_http_fetch(int argc, const char **argv, const char *prefix)
int get_verbosely = 0;
int get_recover = 0;
+ prefix = setup_git_directory();
+
git_config(git_default_config, NULL);
while (arg < argc && argv[arg][0] == '-') {
diff --git a/fetch.h b/fetch.h
index 5fd3e65..d43e0ca 100644
--- a/fetch.h
+++ b/fetch.h
@@ -1,7 +1,7 @@
#include "config.h"
#include "http.h"
-int cmd_http_fetch(int argc, const char **argv, const char *prefix);
+int main(int argc, const char **argv);
void start_fetch(const char* uri);
bool fetch_succeeded(int status_code);
If you only want to show added/removed/modified lines, and not the surrounding context, you can pass -U0
to git diff:
如果您只想显示添加/删除/修改的行,而不是周围的上下文,您可以传递-U0
给 git diff:
$ git diff -U0 | diff-lines
http-fetch.c:4:-int cmd_http_fetch(int argc, const char **argv, const char *prefix)
http-fetch.c:4:+int main(int argc, const char **argv)
http-fetch.c:6:+ const char *prefix;
http-fetch.c:22:+ prefix = setup_git_directory();
http-fetch.c:23:+
fetch.h:4:-int cmd_http_fetch(int argc, const char **argv, const char *prefix);
fetch.h:4:+int main(int argc, const char **argv);
It's robust against ANSI color codes, so you can pass --color=always
to git diff to get the usual color coding for added/removed lines.
它对 ANSI 颜色代码很健壮,因此您可以传递--color=always
给 git diff 以获得添加/删除行的常用颜色编码。
The output can be easily grepped:
输出可以很容易地grepped:
$ git diff -U0 | diff-lines | grep 'main'
http-fetch.c:4:+int main(int argc, const char **argv)
fetch.h:4:+int main(int argc, const char **argv);
In your case git diff -U0
would give:
在你的情况下git diff -U0
会给出:
$ git diff -U0 | diff-lines
test.txt:2:+new line here
test.txt:3:+another new line
test.txt:6:-will be removed
test.txt:6:-git repo
test.txt:6:+git
If you just want the line numbers, change the echo "$path:$line:$REPLY"
to just echo "$line"
and pipe the output through uniq
.
如果您只需要行号,请将 更改echo "$path:$line:$REPLY"
为 justecho "$line"
并将输出通过uniq
.
回答by Ida
I use the --unified=0
option of git diff
.
我使用--unified=0
的选项git diff
。
For example, git diff --unified=0 commit1 commit2
outputs the diff:
例如,git diff --unified=0 commit1 commit2
输出差异:
Because of the --unified=0
option, the diff output shows 0 context lines; in other words, it shows exactly the changed lines.
由于该--unified=0
选项,diff 输出显示 0 个上下文行;换句话说,它准确地显示了更改后的行。
Now, you can identify the lines that start with '@@', and parse it based on the pattern:
现在,您可以识别以“@@”开头的行,并根据模式对其进行解析:
@@ -startline1,count1 +startline2,count2 @@
@@ -startline1,count1 +startline2,count2 @@
Back to the above example, for the file WildcardBinding.java, start from line 910, 0 lines are deleted. Start from line 911, 4 lines are added.
回到上面的例子,对于文件 WildcardBinding.java,从第 910 行开始,删除了 0 行。从第 911 行开始,添加了 4 行。
回答by Jay
I had this same problem so I wrote a gawk script that changes the output of git diff to prepend the line number for each line. I find it useful sometimes when I need to diff working tree, although it's not limited to that. Maybe it is useful to someone here?
我遇到了同样的问题,所以我写了一个 gawk 脚本,它改变了 git diff 的输出以在每一行前面加上行号。当我需要区分工作树时,我发现它有时很有用,尽管它不仅限于此。也许它对这里的人有用?
$ git diff HEAD~1 |showlinenum.awk
diff --git a/doc.txt b/doc.txt
index fae6176..6ca8c26 100644
--- a/doc.txt
+++ b/doc.txt
@@ -1,3 +1,3 @@
1: red
2: blue
:-green
3:+yellow
You can download it from here:
https://github.com/jay/showlinenum
你可以从这里下载:https:
//github.com/jay/showlinenum
回答by Rich
Line numbers of all uncommitted lines (added/modified):
所有未提交行的行号(添加/修改):
git blame <file> | grep -n '^0\{8\} ' | cut -f1 -d:
Example output:
示例输出:
1
2
8
12
13
14
回答by manojlds
Configure an external diff tool which will show you the line numbers. For example, this is what I have in my git global config:
配置一个外部差异工具,它将显示行号。例如,这就是我的 git 全局配置中的内容:
diff.guitool=kdiff3
difftool.kdiff3.path=c:/Program Files (x86)/KDiff3/kdiff3.exe
difftool.kdiff3.cmd="c:/Program Files (x86)/KDiff3/kdiff3.exe" "$LOCAL" "$REMOTE"
See this answer for more details: https://stackoverflow.com/q/949242/526535
有关更多详细信息,请参阅此答案:https: //stackoverflow.com/q/949242/526535
回答by Jay Carroll
This is probably a fairly accurate count of changed lines:
这可能是更改行的相当准确的计数:
git diff --word-diff <commit> |egrep '(?:\[-)|(?:\{\+)' |wc -l
Also, here is a solution for line numbers in your diff: https://github.com/jay/showlinenum
此外,这里是您的差异中行号的解决方案:https: //github.com/jay/showlinenum
回答by forivall
Here's a bash function I cobbled together:
这是我拼凑的一个 bash 函数:
echo ${f}:
for n in $(git --no-pager blame --line-porcelain |
awk '/author Not Committed Yet/{if (a && a !~ /author Not Committed Yet/) print a} {a=views.py:
403,404,533-538,546-548,550-552,554-559,565-567,580-582
}' |
awk '{print }') ; do
if (( prev_line > -1 )) ; then
if (( "$n" > (prev_line + 1) )) ; then
if (( (prev_line - range_start) > 1 )) ; then
echo -n "$range_start-$prev_line,"
else
echo -n "$range_start,$prev_line,"
fi
range_start=$n
fi
else
range_start=$n
fi
prev_line=$n
done
if (( "$range_start" != "$prev_line" )) ; then
echo "$range_start-$prev_line"
else
echo "$range_start"
fi
And it ends up looking like this:
它最终看起来像这样:
git diff HEAD --shortstat
回答by shahalpk
You can use git diff
coupled with shortstat
parameter to just show the no oflines changed.
您可以使用git diff
与shortstat
参数结合来仅显示更改的行数。
For the no of lines changed (in a file that's already in the repo) since your last commit
对于自上次提交以来更改的行数(在存储库中已经存在的文件中)
1 file changed, 4 insertions(+)
It'll output something similar to
它会输出类似于
##代码##回答by u-punkt
Not exactly what you were asking for, but git blame TEXTFILE
may help.
不完全是你所要求的,但git blame TEXTFILE
可能会有所帮助。