Reputation: 5049
I need to add the task code from the branch name to every commit. We figured out that this should be done with a hook.
Here's the code:
#!/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
Right now what's happening is this:
LG-132: LG-132: LG-132: LG-132: LG-132: LG-132: LG-132: Merge branch 'develop' of https://git.dw.com/scm/lg/webapp into develop
So the branch code gets added every time I do an amend. Now, I am trying to check if the branch code is included in the commit message, and if so just break/return 0.
This is what I have so far:
#!/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
set BRANCH_NAME|find "$TRIMMED" >nul && shouldAddCode=true || shouldAddCode=false
if $shouldAddCode; then
echo "test"
else
sed -i.bak -e "1s,^,$TRIMMED: ," "$1"
fi
fi
I am getting this in the console:
find: ‘LG-132’: No such file or directory
Now this code just doesn't work. I was using this code as reference:
set "i=hello world"
set i|find "world" >nul && set test=yes || set test=no
echo %test%
pause
What am I missing?
Upvotes: 0
Views: 114
Reputation: 5049
it ended up being a bit chaotic, but this is what I came up with
#!/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-zA-Z]\+\/\)*\([a-zA-Z]\+-[0-9]\+\).\+:\2:')
TRIMMED=${TRIMMED^^}
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
COMMIT_MESSAGE="$(cat "$1" | sed -E "s,^#.*|$TRIMMED: *,,g")"
echo "$COMMIT_MESSAGE" >"$1"
sed -i.bak -e "1s,^,$TRIMMED: ," "$1"
fi
Upvotes: 0
Reputation: 13387
Your reference does not look like bash code, but like a CMD script.
To check in a bash script whether some variable begins with a particular string, the canonical way is to as case
...esac
construct, for example:
case "$BRANCHNAME" in
"$TRIMMED"*)
# yes, it's there
echo "test"
;;
*)
# no, it's not there; add it
if ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
sed -i.bak -e "1s,^,$TRIMMED: ," "$1"
fi
;;
esac
Note the weird case arm syntax, which has just the closing parenthesis after the pattern and ends with a double-semicolon.
Upvotes: 1