使 git diff --stat 显示完整文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10459374/
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
Making git diff --stat show full file path
提问by Badri
On doing git diff --stat
some files are listed with full path from repository base but some files are listed as:
在做git diff --stat
一些文件时列出了来自存储库库的完整路径,但一些文件列为:
.../short/path/to/filename.
That is the path starts with ...
and only short path is shown.
那是路径开始,...
并且只显示短路径。
I would like git diff
to list full file path for all files for it to be easily processed by a script. Is there some way I can get git diff
to always show full path
我想git diff
列出所有文件的完整文件路径,以便脚本轻松处理。有什么方法可以让我git diff
始终显示完整路径
回答by torek
The git diff
command takes optional values for --stat
:
该git diff
命令采用以下可选值--stat
:
--stat[=<width>[,<name-width>[,<count>]]]
Generate a diffstat. You can override the default output width for
80-column terminal by --stat=<width>. The width of the filename
part can be controlled by giving another width to it separated by a
comma. By giving a third parameter <count>, you can limit the
output to the first <count> lines, followed by ... if there are
more.
These parameters can also be set individually with
--stat-width=<width>, --stat-name-width=<name-width> and
--stat-count=<count>.
(For scripting you might want to use git diff-tree
directly since it's more of a "plumbing" command, although I suspect you'll be fine either way. Note that you need the same extra text with --stat
when using git diff-tree
. The essential difference between using the git diff
"porcelain" front end, and the git diff-tree
plumbing command, is that git diff
looks up your configured settings for options like diff.renames
to decide whether to do rename detection. Well, that, plus the front end git diff
will do the equivalent of git diff-index
if you're comparing a commit with the index, for instance. In other words, git diff
reads your configand invokes the right plumbing automatically.)
(对于脚本,你可能想用git diff-tree
直接,因为它更是一个“管道”的命令,但我怀疑你会没事的两种方式。请注意,您需要使用相同的额外的文本--stat
使用时git diff-tree
,使用的本质区别git diff
“瓷" 前端和git diff-tree
管道命令是git diff
查找您配置的设置diff.renames
以决定是否进行重命名检测等选项。好吧,加上前端git diff
将执行等效于git diff-index
将提交与索引进行比较的操作,例如。换句话说,git diff
读取您的配置并自动调用正确的管道。)
回答by cmbuckley
For script processing, it might be better to use one of the following:
对于脚本处理,最好使用以下方法之一:
# list just the file names
git diff --name-only
path/to/modified/file
path/to/renamed/file
# list the names and change statuses:
git diff --name-status
M path/to/modified/file
R100 path/to/existing/file path/to/renamed/file
# list a diffstat-like output (+ed lines, -ed lines, file name):
git diff --numstat
1 0 path/to/modified/file
0 0 path/to/{existing => renamed}/file
These each become more handy for robust script processing when combined with the -z
option, which uses NUL
as the field terminators.
当与用作字段终止符的-z
选项结合使用时,这些对于强大的脚本处理变得更加方便NUL
。
回答by John Mellor
For Bash users, you can use the $COLUMNS
variable to automatically fill the available terminal width:
对于 Bash 用户,您可以使用该$COLUMNS
变量自动填充可用的终端宽度:
git diff --stat=$COLUMNS
Very long path names might still be truncated; in this case, you can reduce the width of the +++/--- part using --stat-graph-width
, for example this limits it to 1/5 of the terminal width:
很长的路径名可能仍会被截断;在这种情况下,您可以使用 减小 +++/--- 部分的宽度--stat-graph-width
,例如这将其限制为终端宽度的 1/5:
git show --stat=$COLUMNS --stat-graph-width=$(($COLUMNS/5))
For a more generic solution, you could use the output of tput cols
to determine the terminal width.
对于更通用的解决方案,您可以使用 的输出tput cols
来确定终端宽度。
回答by Yevhen Pavliuk
There's an option --name-only
: git diff --name-only
. The option is also supported by other git commands like show
and stash
.
有一个选择--name-only
:git diff --name-only
。其他 git 命令(如show
和)也支持该选项stash
。
Paths don't get shortened with the option.
该选项不会缩短路径。
回答by user151841
I created the following git alias:
我创建了以下 git 别名:
diffstat = ! "gitdiffstat() { git diff --stat=$(tput cols) ${1:-master} ; }; gitdiffstat"
It reads the column count from the tput cols
command. It defaults to diffing against master
, but you can optionally specify another branch.
它从tput cols
命令中读取列数。它默认为 diffing against master
,但您可以选择指定另一个分支。
$ git diffstat
.gitalias | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
回答by Javier Buzzi
A simple solution I found was to do this: (only works on *nix, sorry no osx)
我发现一个简单的解决方案是这样做:(仅适用于 *nix,抱歉没有 osx)
git diff --stat=$COLUMNS --relative | head -n -1 | cut -c 2- | xargs -d '\n' -P4 printf "$(pwd)/%s\n"
This version works for both, but it doesn't look great on osx.
这个版本对两者都适用,但在 osx 上看起来不太好。
git diff --stat=$COLUMNS --relative | sed -e '$ d' | cut -c 2- | xargs -n4 -I{} echo "$(pwd)/{}"
回答by Alex Spurling
I found that the behaviour of diff --stat changed somewhere around git 1.7.10 where previously it would shorten file paths to a fixed width by default - it now displays as much as your terminal window will allow. If you are experiencing this problem, make sure you upgrade to 1.8.0 or newer.
我发现 diff --stat 的行为在 git 1.7.10 附近的某个地方发生了变化,以前默认情况下它会将文件路径缩短为固定宽度 - 现在它显示的数量与您的终端窗口允许的一样多。如果您遇到此问题,请确保升级到 1.8.0 或更高版本。