Reputation: 828
My current branch naming convention is as such:
ticket-45-my-new-feature-branch-description
I am currently using this code in my .git/hooks/prepare-commit-msg file to prepend every commit message with the branch name as such:
BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "master" ]; then
echo "[$BRANCH_NAME] $(cat $1)" > $1
fi
End result:
[ticket-45-my-new-feature-branch-description] test commit
What I'm trying to accomplish is output like this:
[ticket-45] test commit
Brownie points if we can capitalize it:
[TICKET-45] test commit
I would love to keep my descriptive branch names, but truncate the prepended text in commit messages. I'm sure I have to use some regex, but I really don't know how to accomplish that. I should mention that we have several projects going on at once, so branch names are different, like so:
ticket-123-branch-name
abc-22-my-branch
ua-11-my-feature
The only thing in common is the fact that I need everything before the second '-'.
Any help is greatly appreciated!!!
Upvotes: 14
Views: 5724
Reputation: 490078
Ok, first, this:
BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
is major overkill :-) Use:
branch=$(git symbolic-ref --short HEAD) || ...
to get the current branch name. The part after ||
is "what to do if you're not on a branch" (i.e., if you're in "detached head" mode)—you'll have to decide that for yourself. (Your current code sets BRANCH_NAME to the empty string; to do that, you don't even need the ||
part, but you may want to add -q
, or a 2>/dev/null
, to avoid the "fatal:" message from symbolic-ref.)
The rest is just basic scripting. In bash you can use regex's directly, in old sh you can invoke expr
or sed
. Both sed
and tr
can upper-case-ify, but sed can do a regex too, so it looks like a good candidate:
$ trimmed=$(echo $branch | sed -e 's:^\([^-]*-[^-]*\)-.*:\1:' -e \
'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
$ echo $trimmed
TICKET-45
Last, it's slightly dangerous to do:
echo "some stuff $(cat $1)" > $1
as you're depending on the shell to expand the $(cat $1)
before it truncates the output file for the > $1
part. (Obviously it works, but you're subject to shell vagaries.) Better to use a temporary file, or maybe another sed
but in-place:
sed -i .bak -e "1s:^:[$trimmed] :" $1
# or use -i '', but note minor warning in sed man pages
The above is only tested piecemeal, but should work.
Upvotes: 22