Skip to content

Commit 5131b00

Browse files
add hook to reject certain commits from being pushed to the repository (github#240)
* add hook to reject certain commits from being pushed to the repository * Update pre-receive-hooks/reject-commits.sh Co-Authored-By: larsxschneider <larsxschneider@github.com> Co-authored-by: Thomas Hughes <iamhughes@github.com>
1 parent d08243c commit 5131b00

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pre-receive-hooks/reject-commits.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
#
3+
# Reject certain commits from being pushed to the repository
4+
#
5+
# This can be a useful pre-receive hook [1] if you rewrote the history
6+
# of a repository and you want to ensure nobody pushes the old commits
7+
# again.
8+
#
9+
# Usage: Add the commits you want to reject in the
10+
# "<list commit hashes here>" below.
11+
#
12+
# [1] https://help.github.com/en/enterprise/user/articles/working-with-pre-receive-hooks
13+
#
14+
set -e
15+
16+
zero_commit="0000000000000000000000000000000000000000"
17+
rejected_commits=$(mktemp /tmp/rejected-commits.XXXXXX)
18+
trap "rm -f $rejected_commits" EXIT
19+
cat <<EOF > $rejected_commits
20+
<list commit hashes here>
21+
EOF
22+
23+
while read -r oldrev newrev refname; do
24+
25+
# Branch or tag got deleted, ignore the push
26+
[ "$newrev" = "$zero_commit" ] && continue
27+
28+
# Calculate range for new branch/updated branch
29+
[ "$oldrev" = "$zero_commit" ] && range="$newrev" || range="$oldrev..$newrev"
30+
31+
# Iterate over all new hashes and try to match "rejected hashes"
32+
# Return "success" if there are no matches
33+
match=$(git rev-list "$range" --not --all \
34+
| fgrep --max-count=1 --file=$rejected_commits \
35+
) || continue
36+
37+
echo "ERROR:"
38+
echo "ERROR: Your push was rejected because it contained the commit"
39+
echo "ERROR: '$match' in '${refname#refs/heads/}'."
40+
echo "ERROR:"
41+
echo "ERROR: Please contact your GitHub Enterprise administrator."
42+
echo "ERROR"
43+
exit 1
44+
done

0 commit comments

Comments
 (0)