Skip to content

Skip the ReplaceBranch prefix when the message already starts with it - #930

Open
NikitaNazarov1 wants to merge 1 commit into
sds:mainfrom
NikitaNazarov1:task/XXX-1/add-proj-name-commit-msg-hook
Open

NikitaNazarov1 wants to merge 1 commit into
sds:mainfrom
NikitaNazarov1:task/XXX-1/add-proj-name-commit-msg-hook

Conversation

@NikitaNazarov1

Copy link
Copy Markdown

#875 asks for a hook that copies a ticket ID from a branch such as task/PROJ-123/fix-login into the commit message as [PROJ-123] . ReplaceBranch can already do this with branch_pattern and replacement_text. But with the default skipped_commit_types, it runs only for editor commits. When you remove message or commit from that list, the hook adds a second prefix to git commit -m '[PROJ-123] Fix' and to git commit --amend. It also prefixes git commit --fixup, so the subject no longer starts with fixup! and git rebase --autosquash does not find the commit.

Leave the message unchanged when it already starts with the replacement text, or with fixup! , squash! , or amend! . The comparison ignores surrounding whitespace, so [PROJ-123]Fix also counts. A template that ends in a word character must end at a word boundary: PROJ-12 does not match PROJ-123 Fix. The comparison uses bytes, because lstrip raises on a message that is not valid in the default encoding, such as an amended Latin-1 commit, and that would stop the commit. A whitespace-only replacement text is still prepended as before.

The default configuration does not change: it runs the hook only for editor commits, whose message file starts with a blank line and Git comments.

Document a ticket ID configuration in the hook's class comment. It keeps template skipped, because a commit template can start with a # line, and the prefix would turn that comment into the subject. It sets on_warn: pass, so branches without a ticket ID do not show a warning.

Follow-up to #875, where a PR with a configurable hook was invited.

Validation: bundle exec ruby script/parallel_rspec.rb passes all 1884 examples, and RuboCop reports no offenses on the changed files. 8 of the new ReplaceBranch examples fail against main. The others pin behavior that must not change: a longer ticket ID, a whitespace-only template, and a message that is not valid UTF-8. I also ran the documented configuration in a disposable repository with Git 2.50.1:

  • commit -m 'Fix login' gives [PROJ-123] Fix login.
  • --amend -m '[PROJ-123] Fix login form' and --amend --no-edit keep one prefix.
  • --fixup=HEAD gives fixup! [PROJ-123] Fix login form.
  • A Latin-1 commit -F and its amend, under a UTF-8 locale, give [PROJ-123] Änderung am Login.
  • A commit on main stays unchanged and shows no warning.

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant