0

I have post-merge hook that check a few things after a merge/pull. However, these check are unnecessary after a fast-forward merge and they just wasting several seconds for no good reason. This is rather annoying.

How can I detect that the post-merge script was called for a fast-forward merge, in order to skip it?

According to the documentation the only parameter passed to the script informs whether it was a squash merge. I don't see anything about fast-forwarding there.

(Someone will probably point out that post-merge is not the best place to put checks because it it too late to stop the merge. I know that but specifics of my project cause that running them earlier is not possible.)

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65

1 Answers1

2

I thought just counting parents would do it, but OP pointed out that fast-forwarding to an existing merge would fail that test.

So:

if [[ `git reflog -1` = *Fast-forward ]]
then this is a fast-forward
fi
jthill
  • 55,082
  • 5
  • 77
  • 137
  • Woah, I didn't know much about `reflog`. It's powerful! A link in case someone's looking for it: https://git-scm.com/docs/git-reflog – Piotr Siupa Jul 17 '22 at 19:02
  • Fixed some syntax brainos, this should work now. – jthill Jul 17 '22 at 19:04
  • Btw, backticks are some ancient syntax. You should switch to `$()`. https://stackoverflow.com/q/9449778/3052438 – Piotr Siupa Jul 17 '22 at 19:09
  • 1
    Notice how none of the very real advantages of `$()` mentioned in those posts apply here? Really, this one's borderline for me, for short command substitutions I very much prefer backticks. There's a coding style that makes every little step so in-your-face it gets distracting, to the point of obscuring the intent of the resulting blob. – jthill Jul 17 '22 at 19:14
  • That is a valid argument. Personally, I prefer to use the new syntax to not mix the two when it turns out bactiks are not sufficient for some part of the script. Also, `$()` does not trigger warnings from `shellcheck`. – Piotr Siupa Jul 17 '22 at 19:19
  • I was going to put in the $()'s, then realized the last arg was redundant and shortened it instead. Mindless rule-following has never once in my experience produced good code. shellcheck is a handy tool and calls out things that are often signs of thoughtless code, but it's dumber than the average coder and that's saying a lot. – jthill Jul 17 '22 at 19:25
  • I works! I went with a POSIX version of the if: `if git reflog -1 | grep -q 'Fast-forward$'`. – Piotr Siupa Jul 17 '22 at 19:39