From 08f91e8dd0ff8bf0add7f5ce05f06885c60acd74 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 22 Aug 2019 01:08:10 +1200 Subject: Rewrite Git pre-commit hook, silence bad error --- ISSUES.md | 6 ------ git/template/hooks/pre-commit.sh | 22 +++++++++++++++------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/ISSUES.md b/ISSUES.md index 597d7bd8..103c3a98 100644 --- a/ISSUES.md +++ b/ISSUES.md @@ -30,9 +30,3 @@ Known issues pushed upstream. * The `_text_filenames` completion handler for Bash won't work on files with newlines in their names. Can it be made to? -* First commit to a new repository fails with the Git template hooks because - the HEAD ref doesn't exist yet: - - fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree. - Use '--' to separate paths from revisions, like this: - 'git [...] -- [...]' diff --git a/git/template/hooks/pre-commit.sh b/git/template/hooks/pre-commit.sh index f0e093b3..4d29cb58 100644 --- a/git/template/hooks/pre-commit.sh +++ b/git/template/hooks/pre-commit.sh @@ -1,15 +1,23 @@ # Reject a commit directly to a branch named 'master' if a branch named # 'develop' exists -# Allow commit if it's not to master -[ "$(git rev-parse --abbrev-ref HEAD)" = master ] || exit 0 +# Allow commit if no HEAD ref (new repo), master branch, or develop branch +if ! git show-ref --quiet --verify \ + HEAD refs/heads/develop refs/heads/master ; then + exit 0 +fi -# Allow commit if there's no develop branch -git show-ref --quiet --verify refs/heads/develop || exit 0 +# Allow merge commit +if git show-ref --quiet --verify MERGE_HEAD ; then + exit 0 +fi -# Allow commit if it's a merge. Is there a better way to test this? -! git rev-parse --quiet --verify MERGE_HEAD >/dev/null || exit 0 +# Allow commit if not on master +case $(git rev-parse --abbrev-ref --verify HEAD) in + master) ;; + *) exit 0 ;; +esac -# Throw toys +# Refuse to commit printf >&2 'Branch develop exists, commits to master blocked\n' exit 1 -- cgit v1.2.3