获取 git merge 中的完整文件路径

发布于 2024-11-01 00:02:24 字数 321 浏览 5 评论 0原文

我正在开发一种工具来分析合并许多主题分支的输出并生成详细的冲突报告。我遇到了一个小问题,有时 Git 会在合并命令的输出中生成截断的文件名,例如

Merge made by recursive.
 .../somepath/anotherpath/toolong/default.css     |    2 +-

当在其他情况下它给出完整路径时,

Auto-merging thefullpath/to/myfile/default.jsp

我希望始终拥有可用的完整路径以匹配文件合并之间。

I'm working on a tool to analyse the output of merging many topic branches and produce a detailed conflict report. I've run into a slight problem in that sometimes Git will produce a truncated file name in the merge command's output, such as

Merge made by recursive.
 .../somepath/anotherpath/toolong/default.css     |    2 +-

When in other cases it gives the full path

Auto-merging thefullpath/to/myfile/default.jsp

I would like to always have the full path available in order to match files between merges.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

像极了他 2024-11-08 00:02:24

我对你的最终目标有点困惑,但我想我无论如何都能提供答案。

如果您想在合并遇到冲突时获得良好的更改报告,即当所有内容仍未提交时,您应该使用 git status --porcelain 。它提供了一个很好的、机器可读的所有文件及其状态(包括合并冲突状态)的列表,并且有详细记录 在手册页中。根据您的用例,您甚至可以使用 git merge --no-commit 来确保您有机会在继续(提交)之前检查合并结果。您还可以使用 git diff HEAD --numstat 来获取按文件添加/删除的行数的机器可读报告。

如果您想在事后检查合并,那么这是 git diff-tree

git diff-tree -c --numstat <commit>

-c 告诉它对父级进行比较合并提交,--numstat 像以前一样为您提供良好的机器可读输出。

最后,如果您想生成极其详细的冲突报告,您可以传递 -p 而不是 --numstat,从而为您提供完整的补丁输出。这是您将在 gitk 中看到的那种补丁,其中每行开头有两个字符,指示该行是否相对于每个父级添加/删除。仅来自一个父级的更改只有一个符号(例如 '+ '' -'、添加引号以使空格可见),而手动更改的行作为冲突的一部分分辨率有两个 (--, ++)。如果确实需要的话,你可以自己解析它。 (不幸的是,我认为没有像 --numstat 这样的东西可以用于合并提交。)

I'm a little confused by your end goal, but I think I can provide an answer anyway.

If you want to get a good report of changes when a merge runs into conflicts, i.e. while everything is still uncommitted, you should use git status --porcelain. It gives a nice, machine-readable list of all files and their statuses (including merge conflict states) and is well-documented in the manpage. Depending on your use case, you might even use git merge --no-commit to ensure you have the chance to inspect the merge result before proceeding (committing). You could also use git diff HEAD --numstat to get a machine-readable report of the added/removed line counts by file.

If you want to examine merges after the fact, it's a very good use case for git diff-tree:

git diff-tree -c --numstat <commit>

The -c tells it to diff merge commits against both parents, and the --numstat gets you a nice machine-readable output as before.

Finally, if you wanted to generate extremely detailed conflict reports, you could pass -p instead of --numstat, giving you full patch output. This is the kind of patch you'll see in gitk, where there are two characters at the start of each line, indicating whether the line was added/removed relative to each parent. Changes coming from just one parent have just one symbol (e.g. '+ ', ' -', quotes added to make spaces visible), while lines changed manually as part of conflict resolution will have two (--, ++). You could parse it yourself if you really had to. (Unfortunately I don't think there's anything like --numstat on steroids for merge commits.)

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