10

Is there a way for a post-merge hook get a list of all the files that were changed by a merge, even if it was a fast-forward?

mckeed
  • 9,719
  • 2
  • 37
  • 41

2 Answers2

14

The right Git command to list the changed files is diff-tree

Also the ORIG_HEAD and HEAD shortcuts may be used:

git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD

(see also: List all the files for a commit in Git)

[upd]

Perhaps it is better to use HEAD@{1} in place of ORIG_HEAD. I.e.:

git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD

In case of git pull --ff-only command, when many commits can be added, HEAD@{1} (inside post-merge hook) gives the last commit before this command, while ORIG_HEAD gives just HEAD^ commit (at least in Git 2.1.4).

ruvim
  • 7,151
  • 2
  • 27
  • 36
  • 1
    remember that ORIG_HEAD and HEAD@{1} are not always the same thing. HEAD@{1} is always the last commit while ORIG_HEAD is the last from what Git considers a dangerous operation. See this answer: https://stackoverflow.com/a/967611/647292 – Kioshiki Oct 14 '18 at 23:11
3

I think your best bet at that point is going to be the reflogs. If you've just fast-forwarded, the first line of HEAD's reflog will look like this:

63e21fb HEAD@{0}: merge origin/master: Fast-forward

So you should be able to print just the first line (git reflog -n 1), check if it matches merge .*: Fast-forward$, and if so, do git diff HEAD@{1} HEAD. (You do want to look at the reflogs to verify that there was a fast-forward merge, probably, unless you can be confident from your script that it's the only possibility now.)

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 7
    Perhaps I'm missing something, but along those lines, why not just do `git diff --name-only HEAD@{1} HEAD`? – Mark Longair Feb 02 '11 at 19:14
  • @Mark: Amended to address that. – Cascabel Feb 02 '11 at 21:04
  • Ah, thanks to both of you. I didn't know about the `@{}` notation. – mckeed Feb 02 '11 at 21:08
  • I was thinking that the diff will still produce sensible results if it was a non-fast-forward merge, so so you don't need to check whether it was a ff at all. – Mark Longair Feb 02 '11 at 22:05
  • @Mark: I guess I was thinking about corner cases, e.g. evil merges, where you can get more complete diff output with `git show `, but you're right, it doesn't matter if you're only looking at the list of changed files. – Cascabel Feb 03 '11 at 00:20