A
A
Alexander Zemlyanoy2018-04-07 15:00:20
bash
Alexander Zemlyanoy, 2018-04-07 15:00:20

How to use git hook to substitute part of the branch name in the commit message?

Now there is a hook that substitutes the name of the entire branch (found in Google)
if [ -z "$BRANCHES_TO_SKIP" ]; then
  BRANCHES_TO_SKIP=(master develop test)
fi

BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"

BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)

if [ -n "$BRANCH_NAME" ] && !  && ! ; then 
  sed -i.bak -e "1s/^/$BRANCH_NAME /" $1
fi


According to the regulations, branches should be named according to the format
#Project code#-#Task code#_#A couple of words about the essence of the task#
For example: PROJECT-777_fix_request_bug
And in the comment of each commit, you need to substitute the branch name at the beginning, only the description should be separated by a space
For example: PROJECT-777 fix_request_bug Commit comment Can
you tell me how to fix the hook attached above?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2018-04-13
@aol-nnov

well, nothing was said about the OS, we will assume that there is a shell and cut

For example: PROJECT-777_fix_request_bug
That is, BRANCH_NAME=PROJECT-777_fix_request_bug
Then:
PROJECT_NAME=$(echo ${BRANCH_NAME} | cut -d_ -f1)
DESCRIPTION=$(echo ${BRANCH_NAME} | cut -d_ -f2-)

on my own :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question