Prevent merging branch into master if it contains commits with specific text

Viewed 124

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?)

1 Answers

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

Related