pre-receive hook to prevent reappearance of a bad revision

372 Views Asked by At

Suppose I have had to use git filter-branch to remove a file from revision history. I want to ensure that all my collaborators update all their local copies before they push again. The obvious way to do this seems to be to use a pre-receive hook on the master repo to ensure that the original revision ID of the revision that introduced the problem file never reappears.

How do I write that hook?

1

There are 1 best solutions below

6
Christopher On BEST ANSWER

I strongly support paranoia. That written, I hope your contributors would notice the "your branch has diverged" warnings or the fact their merge commits suddenly pull in hundreds of new SHA1 hashes.

Something like the following should get you most of the way there. I unfortunately cannot test it right now, but git-receive-pack and githooks's man pages were helpful, as was this example:

#!/bin/sh
while read oldrev newrev refname
do
    git rev-list ^$oldrev $newrev | grep "<problem-hash>"
    if test $? = 0; then
        echo "Problematic hash found. Please contact the maintainer."
        exit 1
    fi
done

Searching for the file itself using pre-receive:

#!/bin/sh
while read oldrev newrev refname
do
    git diff $oldrev $newrev --name-only | grep "<full_file_path>"
    if test $? = 0; then
        echo "Problematic hash found. Please contact the maintainer."
        exit 1
    fi
done