http://stackoverflow.com/questions/4138285/block-push-of-trivial-merge-to-git-server/
Created
December 12, 2010 04:22
-
-
Save meonkeys/737842 to your computer and use it in GitHub Desktop.
Tests for stackoverflow question #4138285
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
test: | |
@bash $@ | |
.PHONY: test |
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
#!/bin/bash | |
set -e | |
echo -n "Non-trivial merge allowed on branch 'master'..." | |
exec 1>>/tmp/gtest.log | |
exec 2>&1 | |
set -x | |
base=`mktemp -d` | |
bash setup $base | |
exitcode=0 | |
cd $base | |
cd clone1 | |
echo blah > b | |
git add b | |
git commit -m '2nd' | |
git push | |
cd .. | |
cd clone2 | |
echo foo > b | |
git add b | |
git commit -m '3rd' | |
git pull || true | |
echo foo > b | |
git add b | |
git commit -m 'resolved conflicts (nontrivial merge)' | |
git log --oneline --graph --decorate # debugging | |
if ! git push | |
then | |
echo "SHOULD HAVE WORKED!" | |
exitcode=1 | |
fi | |
set +x | |
cd /tmp | |
if [[ $exitcode -eq 0 ]] | |
then | |
rm -rf $base | |
else | |
echo "Failed! Workspace preserved, see $base" | |
fi | |
exit $exitcode |
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
#!/bin/bash | |
set -e | |
echo -n "Trivial merge allowed on branch 'wip'..." | |
exec 1>>/tmp/gtest.log | |
exec 2>&1 | |
set -x | |
base=`mktemp -d` | |
bash setup $base | |
exitcode=0 | |
cd $base | |
cd clone1 | |
git checkout -b wip | |
git push origin wip | |
cd ../clone2 | |
git pull | |
git checkout wip | |
cd ../clone1 | |
echo blah > b | |
git add b | |
git commit -m '2nd' | |
git push --set-upstream origin wip | |
cd .. | |
cd clone2 | |
echo blah > c | |
git add c | |
git commit -m '3rd' | |
git pull | |
git log --oneline --graph --decorate # debugging | |
if ! git push | |
then | |
echo "SHOULD HAVE WORKED!" | |
exitcode=1 | |
fi | |
set +x | |
cd /tmp | |
if [[ $exitcode -eq 0 ]] | |
then | |
rm -rf $base | |
else | |
echo "Failed! Workspace preserved, see $base" | |
fi | |
exit $exitcode |
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
#!/bin/bash | |
set -e | |
echo -n "Rebase allowed on branch 'master'..." | |
exec 1>>/tmp/gtest.log | |
exec 2>&1 | |
set -x | |
base=`mktemp -d` | |
bash setup $base | |
exitcode=0 | |
cd $base | |
cd clone1 | |
echo blah > b | |
git add b | |
git commit -m '2nd' | |
git push | |
cd .. | |
cd clone2 | |
echo blah > c | |
git add c | |
git commit -m '3rd' | |
git pull --rebase | |
git log --oneline --graph --decorate # debugging | |
if ! git push | |
then | |
echo "SHOULD HAVE WORKED!" | |
exitcode=1 | |
fi | |
set +x | |
cd /tmp | |
if [[ $exitcode -eq 0 ]] | |
then | |
rm -rf $base | |
else | |
echo "Failed! Workspace preserved, see $base" | |
fi | |
exit $exitcode |
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
#!/bin/bash | |
set -ex | |
initial_dir=`pwd` | |
base=$1 | |
mkdir $base/init | |
cd $base/init | |
git init . | |
echo blah > a | |
git add a | |
git commit -m '1st' | |
git checkout -b hotfixes | |
git checkout -b dev | |
git checkout -b qa | |
cd .. | |
git clone --bare init repo | |
rm -rf init | |
cp $initial_dir/update repo/hooks/update | |
cd repo | |
git config hooks.notrivial.branches "hotfixes dev qa master" | |
cd .. | |
git clone repo clone1 | |
git clone repo clone2 |
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
#!/bin/bash | |
tests=0 | |
fails=0 | |
skips=0 | |
exitcode=0 | |
stop_on_failure=yes | |
for test in `\ls t` | |
do | |
((tests += 1)) | |
if [[ $stop_on_failure == "yes" && $fails -gt 0 ]] | |
then | |
((skips += 1)) | |
echo SKIPPED t/$test | |
continue | |
fi | |
rm -f /tmp/gtest.log | |
echo -n "t/$test: " | |
bash t/$test | |
if [ $? -ne 0 ] | |
then | |
((fails += 1)) | |
echo FAIL | |
exitcode=1 | |
else | |
echo OK | |
fi | |
done | |
echo "$tests total test(s), $fails failure(s). $skips test(s) skipped." | |
echo "See /tmp/gtest.log for details." | |
exit $exitcode |
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
#!/bin/bash | |
set -e | |
echo -n "Trivial merge blocked on branch 'master'..." | |
exec 1>>/tmp/gtest.log | |
exec 2>&1 | |
set -x | |
base=`mktemp -d` | |
bash setup $base | |
exitcode=0 | |
cd $base | |
cd clone1 | |
echo blah > c | |
git add b | |
git commit -m '2nd' | |
git push | |
cd .. | |
cd clone2 | |
echo blah > c | |
git add c | |
git commit -m '3rd' | |
git pull | |
git log --oneline --graph --decorate # debugging | |
if git push | |
then | |
echo "SHOULD HAVE FAILED!" | |
exitcode=1 | |
fi | |
set +x | |
cd /tmp | |
if [[ $exitcode -eq 0 ]] | |
then | |
rm -rf $base | |
else | |
echo "Failed! Workspace preserved, see $base" | |
fi | |
exit $exitcode |
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
#!/bin/bash | |
# | |
# This git update hook will refuse unnecessary merge commits caused by pulling | |
# from being pushed to a shared repository. These commits make following the | |
# history of a project difficult and are always avoidable with rebasing. | |
# | |
# by Scott Kyle (appden) | |
# modified by Olivier Refalo (orefalo) | |
refname="$1" | |
oldrev="$2" | |
newrev="$3" | |
# if this is not run as a hook, you may have messed up | |
if [ -z "$GIT_DIR" -o -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then | |
echo "Usage: GIT_DIR=<path> $0 <ref> <oldrev> <newrev>" >&2 | |
exit 1 | |
fi | |
branches="$(git config hooks.notrivial.branches)" | |
cont="no" | |
for branch in $branches; do | |
if [ "${refname#refs/heads/}" = "$branch" ]; then | |
cont="yes" | |
fi | |
done | |
if [ "$cont" = "no" ]; then | |
#echo -Debug- bailing out: Refname: ${refname#refs/heads/} | |
exit 0 | |
fi | |
#echo -Debug- continuing: Refname: ${refname#refs/heads/} | |
# if the new revision is all 0's then it's a commit to delete a ref | |
zero="0000000000000000000000000000000000000000" | |
# also check if the new revision is not a commit or is not a fast forward | |
# detect branch deletion, branch creation... and more | |
if [ "$newrev" = "$zero" ] || [ "$oldrev" = "$zero" ] || [ $(git cat-file -t $newrev) != "commit" ] || [ $(git merge-base $oldrev $newrev) != "$oldrev" ]; then | |
exit 0 | |
fi | |
# loop through merges in the push only following first parents | |
#echo -Debug- Plz send [email protected] the following: git rev-list --first-parent --merges $oldrev..$newrev END | |
for merge in $(git rev-list --first-parent --merges $oldrev..$newrev --); do | |
# lazily create the revision list of this branch prior to the push | |
[ -z "$revlist" ] && revlist=$(git rev-list $oldrev) | |
# check if the second parent of the merge is already in this branch | |
if grep -q $(git rev-parse $merge^2) <<< "$revlist"; then | |
cat >&2 <<-EOF | |
*** PUSH REJECTED *** | |
*** TRIVIAL merge detected on local branch ${refname#refs/heads/} | |
*** To fix: git rebase origin/${refname#refs/heads/} | |
*** | |
*** Next time use: git pull --rebase | |
*** | |
*** Permanent fix: git config [--global] branch.autosetuprebase always | |
*** Then for existing branches: git config branch.<name>.rebase true | |
EOF | |
exit 1 | |
fi | |
done | |
echo -Info- Clean history successfully preserved! | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment