如何获取 diff 中包含的文件列表?

发布于 2024-09-27 21:25:09 字数 75 浏览 0 评论 0原文

我有一个补丁文件,其中包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

指尖上得阳光 2024-10-04 21:25:09

patchutils 包含一个 lsdiff 实用程序。

patchutils includes a lsdiff utility.

两相知 2024-10-04 21:25:09

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.

梦初启 2024-10-04 21:25:09
grep '+++' mydiff.patch|perl -pe 's/\+\+\+ //g'

详细信息:

git diff 生成以下格式的输出

+++ b/file

因此,如果您按照 Nathan 建议使用 grep

grep '+++' mydiff.patch

,您将获得受影响文件的列表,前面带有“+++”(3 个加号和一个空格)。

我经常需要进一步处理文件,并发现每行一个文件名而不需要任何其他内容很方便。这可以通过以下命令来实现,其中 perl/regex 删除这些加号和空格。

grep '+++' mydiff.patch|perl -pe 's/\+\+\+ //g'

对于使用 diff -Naur 生成的补丁文件,mydiff.patch 文件包含带有文件名和日期的条目(表示制表符空白字符)

+++ b/file<tab>2013-07-03 13:58:45.000000000 +0200

要为此提取文件名,请使用

grep '+++' mydiff.patch|perl -pe 's/\+\+\+ (.*)\t.*/\1/g'
grep '+++' mydiff.patch|perl -pe 's/\+\+\+ //g'

Details:

git diff produces output in the format

+++ b/file

So if you're using grep as Nathan suggested

grep '+++' mydiff.patch

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.

grep '+++' mydiff.patch|perl -pe 's/\+\+\+ //g'

For patch files generated with diff -Naur, the mydiff.patch file contains entries with filename and date ( is indicating the tabulator whitespace character)

+++ b/file<tab>2013-07-03 13:58:45.000000000 +0200

To extract the filenames for this, use

grep '+++' mydiff.patch|perl -pe 's/\+\+\+ (.*)\t.*/\1/g'
多情癖 2024-10-04 21:25:09

一个不错的方法是使用 --stat 标志(或 --summary 标志,如果您出于某种原因只需要新的/删除的/重命名的文件)。

例子:

git apply --stat peer.diff | awk '{ print $1 }' | sed '$d'
1-js/03-code-quality/index.md
CONTR.md
LICENSE.md
README.md
chat-app.readme.md

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 apply --stat peer.diff | awk '{ print $1 }' | sed '$d'
1-js/03-code-quality/index.md
CONTR.md
LICENSE.md
README.md
chat-app.readme.md
撑一把青伞 2024-10-04 21:25:09

当您解析由 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/ or b/ at the begin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文