4

Background: I'm working on a pre-commit tool. As a developer it can be quite frustrating to pull another developer's branch and have the pre-commit hook complain loudly about files that I haven't even touched.

What I'd like to be able to do in my implementation is in a merge-commit case only run the hooks on files that were either conflicting or manually edited by me locally during the merge conflict.

What I've tried so far:

  • git diff --staged - This is what the current solution does and isn't correct because it contains all of the files including the ones that merged cleanly.
  • git diff MERGE_HEAD - This is pretty close, but if the branch I am merging in branched from master after I did, this contains all of the changes from master that I haven't yet merged.
  • .git/MERGE_MSG contains the list of conflicting files. This seems like a good starting point but does not contain locally edited files.
  • After committing, git show --name-only gets me exactly what I want. But that's too late (I'm implementing pre-commit after all :D)
4

2 回答 2

4

当你git add解决冲突时,你会抹去冲突的记录。因此,要保留此信息而不必重建它,请保留索引文件:

cp .git/index .git/preserved-merge-index

接着

GIT_INDEX_FILE=.git/preserved-merge-index git ls-files --unmerged

将向您展示冲突,并且

GIT_INDEX_FILE=.git/preserved-merge-index git diff-files --name-only

将向您显示工作树中自记录在合并索引中以来发生更改的所有内容。

从评论中,您也可能直接在合并中添加文件。为了抓住这些,在你解决了所有的合并冲突之后,你可以

GIT_INDEX_FILE=.git/preserved-merge-index git diff-index --name-only `git write-tree`
于 2014-04-07T18:15:34.560 回答
2

我相信解决方案是git diff -m. 我发现这个文档非常混乱,所以这是我的总结。给定命令git diff -m child parent1 parent2 ....,您将看到一个多父差异,显示如何从每个父级到子级。parent1表示在第一列,[ +-]以此类推。这里的主要障碍是您问题中的孩子没有可参考的名字。git write-tree来这里救援;它为当前暂存的文件创建一个名称,并将其打印出来。

请注意,如果有任何未合并的文件,write-tree 将失败,这可能是您想要的,但您需要确保您的系统在这种情况下执行一些可理解的操作。

$ CURRENTLY_ADDED=`git write-tree`
$ git diff -m $CURRENTLY_ADDED HEAD MERGE_HEAD
diff --cc README
index 2ef4a65,8e2e466..be3d46e
--- a/README
+++ b/README
@@@ -1,10 -1,5 +1,10 @@@
 -deleted only in <theirs>
 +added only in <theirs>
- deleted only in <ours>
+ added only in <ours>
--deleted during merge
++added during merge
于 2014-04-07T18:59:09.800 回答