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/pre
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.