From ffee0a6384e751486bb4ca2752b6a00527b923ca Mon Sep 17 00:00:00 2001 From: rl1987 Date: Tue, 20 Nov 2018 15:40:52 +0200 Subject: [PATCH 1/5] Add pre-push git hook to prevent fixup and squash commits from ending up in master --- changes/ticket27993 | 3 +++ scripts/maint/pre-push | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 changes/ticket27993 create mode 100755 scripts/maint/pre-push diff --git a/changes/ticket27993 b/changes/ticket27993 new file mode 100644 index 0000000000..78ee7c2054 --- /dev/null +++ b/changes/ticket27993 @@ -0,0 +1,3 @@ + o Minor features (developer tooling): + - Provide git hook script to prevent "fixup!" and "squash!" commits from + ending up in master. Closes ticket 27993. diff --git a/scripts/maint/pre-push b/scripts/maint/pre-push new file mode 100755 index 0000000000..2cf1837b8d --- /dev/null +++ b/scripts/maint/pre-push @@ -0,0 +1,56 @@ +#!/bin/sh + +# git pre-push hook script to prevent "fixup!" and "squash!" commit +# from ending up in master, or in any branch if CUR_BRANCH check is removed. +# It is meant to be placed in .git/hooks directory. +# +# The following sample script was used as starting point: +# https://github.com/git/git/blob/master/templates/hooks--pre-push.sample + +z40=0000000000000000000000000000000000000000 + +CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) +if [ "$CUR_BRANCH" != "master" ] +then + exit 0 +fi + +echo "Running pre-push hook" + +# shellcheck disable=SC2034 +while read -r 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 fixup! commit + commit=$(git rev-list -n 1 --grep '^fixup!' "$range") + if [ -n "$commit" ] + then + echo >&2 "Found fixup! commit in $local_ref, not pushing" + exit 1 + fi + + # Check for squash! commit + commit=$(git rev-list -n 1 --grep '^squash!' "$range") + if [ -n "$commit" ] + then + echo >&2 "Found squash! commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0 + From b2053cfc44f4876a52f8d71f2308077c6e39498d Mon Sep 17 00:00:00 2001 From: rl1987 Date: Tue, 20 Nov 2018 16:37:30 +0200 Subject: [PATCH 2/5] Also disallow fixup/squash commits in maint-* and release-* --- scripts/maint/pre-push | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/maint/pre-push b/scripts/maint/pre-push index 2cf1837b8d..26c48c4e21 100755 --- a/scripts/maint/pre-push +++ b/scripts/maint/pre-push @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # git pre-push hook script to prevent "fixup!" and "squash!" commit # from ending up in master, or in any branch if CUR_BRANCH check is removed. @@ -10,7 +10,8 @@ z40=0000000000000000000000000000000000000000 CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) -if [ "$CUR_BRANCH" != "master" ] +if [ "$CUR_BRANCH" != "master" ] && [[ $CUR_BRANCH != release-* ]] && + [[ $CUR_BRANCH != maint-* ]] then exit 0 fi From e2b418bab5c3249fba7b430b942da67ddf8a43dc Mon Sep 17 00:00:00 2001 From: rl1987 Date: Thu, 29 Nov 2018 10:56:56 +0200 Subject: [PATCH 3/5] Rename to pre-push.git-hook --- scripts/maint/{pre-push => pre-push.git-hook} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename scripts/maint/{pre-push => pre-push.git-hook} (100%) diff --git a/scripts/maint/pre-push b/scripts/maint/pre-push.git-hook similarity index 100% rename from scripts/maint/pre-push rename to scripts/maint/pre-push.git-hook From 7c6dc2888144e587bc8d62c7b2bddae4fb6606b9 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Thu, 29 Nov 2018 11:10:30 +0200 Subject: [PATCH 4/5] Improve comment at the top --- scripts/maint/pre-push.git-hook | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/maint/pre-push.git-hook b/scripts/maint/pre-push.git-hook index 26c48c4e21..11f062b539 100755 --- a/scripts/maint/pre-push.git-hook +++ b/scripts/maint/pre-push.git-hook @@ -1,8 +1,10 @@ #!/bin/bash -# git pre-push hook script to prevent "fixup!" and "squash!" commit -# from ending up in master, or in any branch if CUR_BRANCH check is removed. -# It is meant to be placed in .git/hooks directory. +# To install this script, copy it into .git/hooks/pre-push path in your +# local copy of git repository. Make sure it has permission to execute. +# +# This is git pre-push hook script to prevent "fixup!" and "squash!" commits +# from ending up in upstream branches (master, release-* or maint-*). # # The following sample script was used as starting point: # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample From 9c90bddc42467396909812746d5b4256adcb5d2d Mon Sep 17 00:00:00 2001 From: rl1987 Date: Thu, 29 Nov 2018 11:10:48 +0200 Subject: [PATCH 5/5] Mention --no-verify in error message --- scripts/maint/pre-push.git-hook | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/maint/pre-push.git-hook b/scripts/maint/pre-push.git-hook index 11f062b539..26296023fb 100755 --- a/scripts/maint/pre-push.git-hook +++ b/scripts/maint/pre-push.git-hook @@ -42,6 +42,7 @@ do if [ -n "$commit" ] then echo >&2 "Found fixup! commit in $local_ref, not pushing" + echo >&2 "If you really want to push this, use --no-verify." exit 1 fi @@ -50,6 +51,7 @@ do if [ -n "$commit" ] then echo >&2 "Found squash! commit in $local_ref, not pushing" + echo >&2 "If you really want to push this, use --no-verify." exit 1 fi fi