2

I'm writing a tool for my team to help auto-forward commits to "future" branches, and one of the things I would like to detect is whether a merge was done via '-s ours'. I plan to treat these kinds of forwards different from other merges, since they represent a choice not to forward a commit.

Is there any programmatic way to detect this?

The best thing I can think of is to check if

git diff REF^1 REF

is empty.

Jared Grubb
  • 1,139
  • 9
  • 17
  • What do you mean by "auto-forward commits to future branches"? –  Aug 10 '14 at 23:53
  • By "auto-forward": imagine that you have "v1", "v2", and "v3", and you do a bug fix in v1 and want to merge the fix into the other branches, in order. – Jared Grubb Aug 11 '14 at 00:12

2 Answers2

3

Your "best thing" is the thing to do. You can't literally tell if the merge was done that way (there's nothing recording the strategy option used), but in the end, the result is the same whether someone does:

git merge --no-commit otherbranch
git checkout HEAD -- .
git commit

or:

git merge --no-commit otherbranch
vi foo.c # and remove all otherbranch's changes
git commit

or whatever.

Of course, just because no changes were brought in from some other branch doesn't mean the other branch's changes are being discarded. It's possible, for instance, that all the changes in that other branch made it in to your branch's code some other way. But that's likely to be pretty rare.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Yes, I imagine I could try to recreate each merge to detect if someone had to force it to be empty. But since an empty merge represents a merge that had no net effect on the destination branch, I'm not sure I care about *why* or how a merge ended up empty... yet, at least! – Jared Grubb Aug 11 '14 at 00:29
0

To identify all effectively--s ours merges in a batch, once for the whole repository,

git log --all --merges --pretty='%h:%x0a%h^: %h' \
| git cat-file --batch-check='%(objectname) %(rest)' \
| awk '{tree=$1} getline && tree==$1 { print $2 }

and to verify what it lists, check whether git rev-parse x: x^: says the same tree twice.

Doing this for the entire linux history takes 8 seconds hot-cache, 36 seconds completely cold on an ordinary consumer HDD.

jthill
  • 55,082
  • 5
  • 77
  • 137