I'm trying to edit all commit messages of a range based on the following criteria:
- If the message contains "Jobs:". Leave the message as is.
- If the message does NOT contain it, add it to the end, with an extra line based on the commit author.
In other words, I want all commits to have a suffix like this:
Jobs:
<author_job>
I came up with something like this:
#!/bin/bash
if [ "$#" -lt "2" ]; then
echo Syntax: $0 \<initial commit\> \<final commit\>
exit
fi
INITIAL_COMMIT=$1
FINAL_COMMIT=$2
FILTER="
ORIGINAL_COMMIT=\`cat\` &&
echo \${ORIGINAL_COMMIT} &&
if [ \"\${ORIGINAL_COMMIT/\"Jobs:\"}\" == \"\${ORIGINAL_COMMIT}\" ]; then
echo
echo Jobs:
case "\${GIT_AUTHOR_EMAIL}" in
\"user1@company.com\") echo \"JOB_ID_USER_1\" ;;
\"user2@company.com\") echo \"JOB_ID_USER_2\" ;;
\"user3@company.com\") echo \"JOB_ID_USER_3\" ;;
*) echo UNKNOWN ;;
esac
fi
"
echo Running git filter branch
git filter-branch --msg-filter "${FILTER}" ${INITIAL_COMMIT}..${FINAL_COMMIT}
However I'm having trouble detecting if the commit message already has the suffix or not. Without the if condition, it works really well, but it will add the suffix to messages that already have it.
This is the filter in this case:
FILTER="
cat &&
echo &&
echo Jobs: &&
case "\${GIT_AUTHOR_EMAIL}" in
\"user1@company.com\") echo \"JOB_ID_USER_1\" ;;
\"user2@company.com\") echo \"JOB_ID_USER_2\" ;;
\"user3@company.com\") echo \"JOB_ID_USER_3\" ;;
*) echo UNKNOWN ;;
esac
"
Does anyone have idea how to get around it?
I've seen some people doing python scripts to solve similar problems. Is it a good idea?
Thanks.