1

My workflow: I have a master branch and a feature branch. When I'm done with the feature, I merge-squash it into master (then push master to remote, though not immediately, I may work on other features first).

When working on a feature branch I often leave comments like //TODO ... to remind me to do something. For simplicity, I include them in commits (I'm going to merge squash, so they shouldn't appear in master anyway). But sometimes I forget to clean those comments before finishing up the feature, and so they find their way into master.

I read about "pre-commit hooks", but I don't think that helps me because I don't want to prevent committing those lines in a feature branch. What I want to is to prevent merging into master if the feature branch contains those lines.

Is this possible? (And, is it possible using git alone, without a third-party script / tool?)

lonix
  • 14,255
  • 23
  • 85
  • 176

1 Answers1

1

I believe pre-merge-commit is what you're looking for

A sample script is given here:

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git merge" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message to
# stderr if it wants to stop the merge commit.
#
# To enable this hook, rename this file to "pre-merge-commit".

. git-sh-setup
test -x "$GIT_DIR/hooks/pre-commit" &&
        exec "$GIT_DIR/hooks/pre-commit"
:

I believe you should be able to use grep or any other method to recursively search through what would be the result of the merge as it's written:

It takes no parameters, and is invoked after the merge has been carried out successfully and before obtaining the proposed commit log message to make a commit.

You can also get the two branches from stdin and use them as input in your script to only perform this if the merge is occurring from feature to master

eeegnu
  • 436
  • 2
  • 13
  • Thank you! Do you think this works with a fast forward merge as well, because it doesn't seem to work for me? – lonix Oct 07 '21 at 08:11
  • @lonix Fast forward is not a merge at all so merge hooks are not triggered. – phd Oct 07 '21 at 11:13
  • @phd Thanks for confirming. I setup the hook as explained above, but when I do `git merge --squash feature-branch; git commit` the hook isn't called, and the output mentions that a FF was done. So is this hook not applicable to my use case, or have I made a mistake somewhere? – lonix Oct 07 '21 at 11:23
  • @lonix Squash merge is also not a merge, IMO it's the worst kind of merges and I avoid it. To trigger a merge hook you need a real merge: `git merge --no-ff` – phd Oct 07 '21 at 11:25
  • @phd Alright I understand, so this hook doesn't apply to me - thanks for explaining! However your comment about disliking "squashing" commits (and so I assume you disapprove of my workflow mentioned above) is going to send me down a rabbit hole of research... thanks no thanks! :-P – lonix Oct 07 '21 at 11:31
  • @lomix For your use case, the best I can see is https://git-scm.com/docs/githooks#_post_merge which isn't able to abort the merge, but can at least inform you that a TODO comment is still there, and can maybe be called to revert to before the merge. – eeegnu Oct 07 '21 at 13:17
  • @eeegnu Thanks for the tip I'll look into that option as well. – lonix Oct 07 '21 at 16:30