I have a range of commits in my local git repository with the following messages:
Added source.h
Broken link fixed
Corrected unit test
Deleted deprecated algorithm
...
Before pushing them to the server, I would like to prepend ticket information to every commit message:
Ticket #9999 (1): Added source.h
Ticket #9999 (2): Broken link fixed
Ticket #9999 (3): Corrected unit test
Ticket #9999 (4): Deleted deprecated algorithm
...
Of course I could git rebase
and reword
every sigle commit, but I would like to automate the process as it's so repetitive.
I think it should somehow be possible with git filter-branch
, but the environment variables only provide
GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GIT_AUTHOR_DATE, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL, and GIT_COMMITTER_DATE
, but not the message.
How can I access and automatically manipulate the commit messages?
EDIT: Note that the ticket number is always the same, but the number in the brackets is incremented successively.
Following the hint from Mark to use the --msg-filter
option of filter-branch
, the full solution is:
If the commit range is XXX..HEAD
, the bracketed number can be expressed with git rev-list --count XXX..$GIT_COMIIT
.
Thus the messages can be transformed with sed
:
git filter-branch --msg-filter '
i=`git rev-list --count XXX..$GIT_COMIIT`
sed "1s/^/Ticket #9999 ($i): /"
' XXX..HEAD