From a4f8fb288bfaa52b4d3e7a4cc77655c6021592d8 Mon Sep 17 00:00:00 2001 From: Djuuu Date: Sat, 31 May 2025 17:57:19 +0200 Subject: [PATCH 1/3] prepare-commit-msg hook: allow skipping Note: prepare-commit-msg hook can not be bypassed with the --no-verify option. https://git-scm.com/docs/githooks#_prepare_commit_msg --- hooks/prepare-commit-msg | 3 +++ test/git-mr.bats | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/hooks/prepare-commit-msg b/hooks/prepare-commit-msg index 538557a..515d00f 100755 --- a/hooks/prepare-commit-msg +++ b/hooks/prepare-commit-msg @@ -4,6 +4,9 @@ # When on a branch referring to a Jira issue, # ensure commit messages start with the issue reference +# Allow skipping +case ${SKIP:-} in *prepare-commit-msg*) exit 0 ;; esac + JIRA_CODE_PATTERN=${JIRA_CODE_PATTERN:-$(git config --get mr.jira-code-pattern)} if [ -z "$JIRA_CODE_PATTERN" ]; then echo "JIRA_CODE_PATTERN not set - unable to guess issue code" >&2 diff --git a/test/git-mr.bats b/test/git-mr.bats index fe90626..ead97a5 100644 --- a/test/git-mr.bats +++ b/test/git-mr.bats @@ -2715,7 +2715,9 @@ n' assert_line "Prefixing message with issue code: AB-123" assert_line "[feature/AB-123-test-feature $(git rev-parse --short HEAD)] AB-123 Test message 2" + # standard .git directory - teardown git reset --hard HEAD~2 + rm -f .git/hooks/prepare-commit-msg # submodule @@ -2738,4 +2740,26 @@ n' run git commit --allow-empty -m "Sub message 2" assert_line "Prefixing message with issue code: XY-345" assert_line "[feature/XY-345-test $(git rev-parse --short HEAD)] XY-345 Sub message 2" + + # submodule - teardown + cd "${BATS_TEST_DIRNAME}/data" || exit + cd repo; git submodule deinit -f sub; cd .. + rm -rf subrepo repo/sub repo/.gitmodules repo/.git/modules +} + +@test "pre-commit-msg hook can be skipped" { + # setup + git-mr hook + + run git commit --allow-empty -m "Test prefixed message" + assert_line "Prefixing message with issue code: AB-123" + assert_line "[feature/AB-123-test-feature $(git rev-parse --short HEAD)] AB-123 Test prefixed message" + + SKIP=aaa,prepare-commit-msg,zzz run git commit --allow-empty -m "Test unprefixed message" + refute_line "Prefixing message with issue code: AB-123" + assert_line "[feature/AB-123-test-feature $(git rev-parse --short HEAD)] Test unprefixed message" + + # teardown + git reset --hard HEAD~2 + rm -f .git/hooks/prepare-commit-msg } From b071f69002744138da0afd5b6840a7ba927d74e5 Mon Sep 17 00:00:00 2001 From: Djuuu Date: Sat, 31 May 2025 17:58:03 +0200 Subject: [PATCH 2/3] prepare-commit-msg hook: simplify fixup commit case --- hooks/prepare-commit-msg | 7 +++---- test/git-mr.bats | 6 +++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/hooks/prepare-commit-msg b/hooks/prepare-commit-msg index 515d00f..54cb291 100755 --- a/hooks/prepare-commit-msg +++ b/hooks/prepare-commit-msg @@ -7,6 +7,9 @@ # Allow skipping case ${SKIP:-} in *prepare-commit-msg*) exit 0 ;; esac +# Don't alter fixup messages +grep -q "^fixup! " "$1" && exit 0 + JIRA_CODE_PATTERN=${JIRA_CODE_PATTERN:-$(git config --get mr.jira-code-pattern)} if [ -z "$JIRA_CODE_PATTERN" ]; then echo "JIRA_CODE_PATTERN not set - unable to guess issue code" >&2 @@ -21,9 +24,5 @@ current_msg=$(cat "$1") msg_code=$(echo "${current_msg}" | grep -iEo "$JIRA_CODE_PATTERN" | tail -n1) [ -z "$msg_code" ] || exit 0 # existing issue code in message -if case "$current_msg" in "fixup! "*) ;; *) false;; esac; then - exit 0 # don't alter fixup messages -fi - echo "Prefixing message with issue code: $issue_code" >&2 sed -i.bak -e "1s/^/$issue_code /" "$1" diff --git a/test/git-mr.bats b/test/git-mr.bats index ead97a5..125083b 100644 --- a/test/git-mr.bats +++ b/test/git-mr.bats @@ -2715,8 +2715,12 @@ n' assert_line "Prefixing message with issue code: AB-123" assert_line "[feature/AB-123-test-feature $(git rev-parse --short HEAD)] AB-123 Test message 2" + run git commit --allow-empty --fixup HEAD~1 + refute_line "Prefixing message with issue code: AB-123" + assert_line "[feature/AB-123-test-feature $(git rev-parse --short HEAD)] fixup! Test message 1" + # standard .git directory - teardown - git reset --hard HEAD~2 + git reset --hard HEAD~3 rm -f .git/hooks/prepare-commit-msg # submodule From ed652e34404499f32c1bde4e82246715923508de Mon Sep 17 00:00:00 2001 From: Djuuu Date: Sat, 31 May 2025 18:40:32 +0200 Subject: [PATCH 3/3] prepare-commit-msg hook: simplify case where code is present in message header --- hooks/prepare-commit-msg | 7 +++---- test/git-mr.bats | 6 +++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/hooks/prepare-commit-msg b/hooks/prepare-commit-msg index 54cb291..b779506 100755 --- a/hooks/prepare-commit-msg +++ b/hooks/prepare-commit-msg @@ -16,13 +16,12 @@ if [ -z "$JIRA_CODE_PATTERN" ]; then exit 0 fi +# Exit if code already present in message header +head -n 1 "$1" | grep -Eiq "$JIRA_CODE_PATTERN" && exit 0 + current_branch=$(git rev-parse --abbrev-ref HEAD) issue_code=$(echo "${current_branch}" | grep -Eo "$JIRA_CODE_PATTERN" | tail -n1) [ -n "$issue_code" ] || exit 0 # No issue code detected -current_msg=$(cat "$1") -msg_code=$(echo "${current_msg}" | grep -iEo "$JIRA_CODE_PATTERN" | tail -n1) -[ -z "$msg_code" ] || exit 0 # existing issue code in message - echo "Prefixing message with issue code: $issue_code" >&2 sed -i.bak -e "1s/^/$issue_code /" "$1" diff --git a/test/git-mr.bats b/test/git-mr.bats index 125083b..78cd7f3 100644 --- a/test/git-mr.bats +++ b/test/git-mr.bats @@ -2719,8 +2719,12 @@ n' refute_line "Prefixing message with issue code: AB-123" assert_line "[feature/AB-123-test-feature $(git rev-parse --short HEAD)] fixup! Test message 1" + run git commit --allow-empty -m "XY-456 Test message 3" + refute_line "Prefixing message with issue code: AB-123" + assert_line "[feature/AB-123-test-feature $(git rev-parse --short HEAD)] XY-456 Test message 3" + # standard .git directory - teardown - git reset --hard HEAD~3 + git reset --hard HEAD~4 rm -f .git/hooks/prepare-commit-msg # submodule