如何获取 diff 中包含的文件列表?
我有一个补丁文件,其中包含 git diff 的输出。我想根据补丁文件获得所有已添加或修改的文件的摘要。我可以使用什么命令来实现此目的?
I have a patch file containing the output from git diff
. I want to get a summary of all the files that, according to the patch file, have been added or modified. What command can I use to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
patchutils
包含一个lsdiff
实用程序。patchutils
includes alsdiff
utility.grep '+++' mydiff.patch
似乎可以解决问题。我还可以使用 git diff --names-only ,这可能是更好的方法。
grep '+++' mydiff.patch
seems to do the trick.I can also use
git diff --names-only
which is probably the better approach.详细信息:
git diff 生成以下格式的输出
因此,如果您按照 Nathan 建议使用 grep
,您将获得受影响文件的列表,前面带有“+++”(3 个加号和一个空格)。
我经常需要进一步处理文件,并发现每行一个文件名而不需要任何其他内容很方便。这可以通过以下命令来实现,其中 perl/regex 删除这些加号和空格。
对于使用 diff -Naur 生成的补丁文件,mydiff.patch 文件包含带有文件名和日期的条目(表示制表符空白字符)
要为此提取文件名,请使用
Details:
git diff produces output in the format
So if you're using grep as Nathan suggested
You'll have the list of affected files, prepended by '+++ ' (3 plus signs and a space).
I often need to further process files and find it convenient to have one filename per line without anything else. This can be achieved with the following command, where perl/regex removes these plus signs and the space.
For patch files generated with diff -Naur, the mydiff.patch file contains entries with filename and date ( is indicating the tabulator whitespace character)
To extract the filenames for this, use
一个不错的方法是使用 --stat 标志(或 --summary 标志,如果您出于某种原因只需要新的/删除的/重命名的文件)。
例子:
A decent way to do this is to use the --stat flag (or the --summary flag, if you need only new / deleted / renamed files for some reason).
Example:
当您解析由
git format-patch
或其他包含有关编辑的行数的附加信息生成的补丁时,搜索^+++
(在代码的开头)至关重要。行)而不仅仅是+++
。例如:
grep '^+++' *.patch | sed -e 's#+++ [ab]/##'
将输出开头不带
a/
或b/
的路径。When you parse patches generated by
git format-patch
or others containing additional information about number of lines edited, it's crucial to search for^+++
(at the start of the line) rather than just+++
.For example:
grep '^+++' *.patch | sed -e 's#+++ [ab]/##'
will output paths without
a/
orb/
at the begin.