0

I have a script, which adds a branch name before the commit message. This is my code now

#!/bin/bash
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop)
fi
# Get the current branch name and check if it is excluded
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
# Trim it down to get the parts we're interested in
TRIMMED=$(echo $BRANCH_NAME | sed -e 's:\([a-z]\+\/\)*\([A-Z]\+-[0-9]\+\).\+:\2:')
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if [ -n "$BRANCH_NAME" ] &&  ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
sed -i.bak -e "1s,^,$TRIMMED: ," $1
fi

And this works, but it also attaches the branch name to the commit name over and over again when amending. Which looks like this:

LG-132: LG-132: LG-132: LG-132: Testing

Which makes no sense.

So I want to add a script to prevent this. How can I do this? I tries adding stuff like

IS_AMEND=$(ps -ocommand= -p $PPID | grep -e '--amend');

if [ -n "$IS_AMEND" ]; then
  return;
fi

to the code, but it resulted in the branch name not being the part of the name, even with normal commit. + I get this error:

ps: unknown option -- o

So how can I solve this?

Alex Ironside
  • 4,658
  • 11
  • 59
  • 119

0 Answers0