aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2019-08-22 01:08:10 +1200
committerTom Ryder <tom@sanctum.geek.nz>2019-08-22 01:08:10 +1200
commit08f91e8dd0ff8bf0add7f5ce05f06885c60acd74 (patch)
tree9a60d8cd895078051f4403a46965a7d57eda8923
parent5e862b5c6ee1cb7f167cf4b8a28485d1d3352d19 (diff)
downloaddotfiles-08f91e8dd0ff8bf0add7f5ce05f06885c60acd74.tar.gz
Rewrite Git pre-commit hook, silence bad error
-rw-r--r--ISSUES.md6
-rw-r--r--git/template/hooks/pre-commit.sh22
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 <command> [<revision>...] -- [<file>...]'
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