Append ticket number to all commit messages in git history local and remote

Viewed 223

How could I append the ticket number "TIX-321" to all commit messages already existing in my branch test_branch, within a bash script?

It would amount to looping thru all commits and for each git amend commit.

I would like to do it automatically within a shell script, I do not want to do it manually with an interactive rebase.

2 Answers

You could do an interractive rebase of your branch (assuming it comes from main)

git switch test_branch
git rebase -i $(git merge-base --fork-point main)

In the editor automatically opened, you can then change all commit messages, save and quit.
That will rewrite each commit of your branch, which you will have to force push (make sure anyone working on that branch is aware).

You can use the same range of commits to do so programmatically, using git filter-repo, using its new --replace-message option.
As with replace-text, regex: is supported, so you can use an expression.txt file with:

regex:(.*)$==>TIX-321 \1

And the command:

git filter-repo --replace-message expressions.txt $(git merge-base --fork-point main)..test_branch

To append a paragraph containing just the ticket number to the message of every commit in your repo, install git-filter-repo, then run this in a clone of your repository:

git filter-repo --message-callback 'return message + b"\n\nTIX-321"'

This will, for example, change this commit message:

OBS-12: fix frobulator parsing

Make the parser able to handle the non-numeric IDs returned by the Zob API.

To this:

OBS-12: fix frobulator parsing

Make the parser able to handle the non-numeric IDs returned by the Zob API.

TIX-321

Why these arguments for git filter-repo

The --message-callback flag takes a string that will be interpreted as Python that returns the new commit message given a message bytestring. The callbacks above concatenate the message bytestring and another bytestring. (In Python, you will get an error if you try to concatenate a bytestring b"…" and a string "…".)

--message-callback can easily handle multi-line commit messages, unlike --replace-message. --replace-message takes the path to a file with expressions, but a regex: expression in that file can only return a string based on each line of the commit message. --message-callback can examine and edit the whole commit message.

Examples of other commit message templates

Add the ticket to the beginning of every message

git filter-repo --message-callback 'return b"TIX-321: " + message'

Changes this commit message:

Fix frobulator parsing

Make the parser able to handle the non-numeric IDs returned by the Zob API.

To this:

TIX-321: Fix frobulator parsing

Make the parser able to handle the non-numeric IDs returned by the Zob API.

Leave the message unchanged if the ticket number is already mentioned

git filter-repo --message-callback 'return message if "TIX-321" in message else message + b"\n\nTIX-321"'

Will not modify this commit message:

TIX-321: fix frobulator parsing

Make the parser able to handle the non-numeric IDs returned by the Zob API.

Related