-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an extra hook so that "git push" that is run without making sure what is being pushed is sane can be checked and rejected (as opposed to the user deciding not pushing). * as/pre-push-hook: Add sample pre-push hook script push: Add support for pre-push hooks hooks: Add function to check if a hook exists
- Loading branch information
Showing
10 changed files
with
302 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#!/bin/sh | ||
|
||
test_description='check pre-push hooks' | ||
. ./test-lib.sh | ||
|
||
# Setup hook that always succeeds | ||
HOOKDIR="$(git rev-parse --git-dir)/hooks" | ||
HOOK="$HOOKDIR/pre-push" | ||
mkdir -p "$HOOKDIR" | ||
write_script "$HOOK" <<EOF | ||
cat >/dev/null | ||
exit 0 | ||
EOF | ||
|
||
test_expect_success 'setup' ' | ||
git config push.default upstream && | ||
git init --bare repo1 && | ||
git remote add parent1 repo1 && | ||
test_commit one && | ||
git push parent1 HEAD:foreign | ||
' | ||
write_script "$HOOK" <<EOF | ||
cat >/dev/null | ||
exit 1 | ||
EOF | ||
|
||
COMMIT1="$(git rev-parse HEAD)" | ||
export COMMIT1 | ||
|
||
test_expect_success 'push with failing hook' ' | ||
test_commit two && | ||
test_must_fail git push parent1 HEAD | ||
' | ||
|
||
test_expect_success '--no-verify bypasses hook' ' | ||
git push --no-verify parent1 HEAD | ||
' | ||
|
||
COMMIT2="$(git rev-parse HEAD)" | ||
export COMMIT2 | ||
|
||
write_script "$HOOK" <<'EOF' | ||
echo "$1" >actual | ||
echo "$2" >>actual | ||
cat >>actual | ||
EOF | ||
|
||
cat >expected <<EOF | ||
parent1 | ||
repo1 | ||
refs/heads/master $COMMIT2 refs/heads/foreign $COMMIT1 | ||
EOF | ||
|
||
test_expect_success 'push with hook' ' | ||
git push parent1 master:foreign && | ||
diff expected actual | ||
' | ||
|
||
test_expect_success 'add a branch' ' | ||
git checkout -b other parent1/foreign && | ||
test_commit three | ||
' | ||
|
||
COMMIT3="$(git rev-parse HEAD)" | ||
export COMMIT3 | ||
|
||
cat >expected <<EOF | ||
parent1 | ||
repo1 | ||
refs/heads/other $COMMIT3 refs/heads/foreign $COMMIT2 | ||
EOF | ||
|
||
test_expect_success 'push to default' ' | ||
git push && | ||
diff expected actual | ||
' | ||
|
||
cat >expected <<EOF | ||
parent1 | ||
repo1 | ||
refs/tags/one $COMMIT1 refs/tags/tag1 $_z40 | ||
HEAD~ $COMMIT2 refs/heads/prev $_z40 | ||
EOF | ||
|
||
test_expect_success 'push non-branches' ' | ||
git push parent1 one:tag1 HEAD~:refs/heads/prev && | ||
diff expected actual | ||
' | ||
|
||
cat >expected <<EOF | ||
parent1 | ||
repo1 | ||
(delete) $_z40 refs/heads/prev $COMMIT2 | ||
EOF | ||
|
||
test_expect_success 'push delete' ' | ||
git push parent1 :prev && | ||
diff expected actual | ||
' | ||
|
||
cat >expected <<EOF | ||
repo1 | ||
repo1 | ||
HEAD $COMMIT3 refs/heads/other $_z40 | ||
EOF | ||
|
||
test_expect_success 'push to URL' ' | ||
git push repo1 HEAD && | ||
diff expected actual | ||
' | ||
|
||
# Test that filling pipe buffers doesn't cause failure | ||
# Too slow to leave enabled for general use | ||
if false | ||
then | ||
printf 'parent1\nrepo1\n' >expected | ||
nr=1000 | ||
while test $nr -lt 2000 | ||
do | ||
nr=$(( $nr + 1 )) | ||
git branch b/$nr $COMMIT3 | ||
echo "refs/heads/b/$nr $COMMIT3 refs/heads/b/$nr $_z40" >>expected | ||
done | ||
|
||
test_expect_success 'push many refs' ' | ||
git push parent1 "refs/heads/b/*:refs/heads/b/*" && | ||
diff expected actual | ||
' | ||
fi | ||
|
||
test_done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/bin/sh | ||
|
||
# An example hook script to verify what is about to be pushed. Called by "git | ||
# push" after it has checked the remote status, but before anything has been | ||
# pushed. If this script exits with a non-zero status nothing will be pushed. | ||
# | ||
# This hook is called with the following parameters: | ||
# | ||
# $1 -- Name of the remote to which the push is being done | ||
# $2 -- URL to which the push is being done | ||
# | ||
# If pushing without using a named remote those arguments will be equal. | ||
# | ||
# Information about the commits which are being pushed is supplied as lines to | ||
# the standard input in the form: | ||
# | ||
# <local ref> <local sha1> <remote ref> <remote sha1> | ||
# | ||
# This sample shows how to prevent push of commits where the log message starts | ||
# with "WIP" (work in progress). | ||
|
||
remote="$1" | ||
url="$2" | ||
|
||
z40=0000000000000000000000000000000000000000 | ||
|
||
IFS=' ' | ||
while read local_ref local_sha remote_ref remote_sha | ||
do | ||
if [ "$local_sha" = $z40 ] | ||
then | ||
# Handle delete | ||
else | ||
if [ "$remote_sha" = $z40 ] | ||
then | ||
# New branch, examine all commits | ||
range="$local_sha" | ||
else | ||
# Update to existing branch, examine new commits | ||
range="$remote_sha..$local_sha" | ||
fi | ||
|
||
# Check for WIP commit | ||
commit=`git rev-list -n 1 --grep '^WIP' "$range"` | ||
if [ -n "$commit" ] | ||
then | ||
echo "Found WIP commit in $local_ref, not pushing" | ||
exit 1 | ||
fi | ||
fi | ||
done | ||
|
||
exit 0 |
Oops, something went wrong.