Reputation: 1715
I'm trying to edit all commit messages of a range based on the following criteria:
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
\"[email protected]\") echo \"JOB_ID_USER_1\" ;;
\"[email protected]\") echo \"JOB_ID_USER_2\" ;;
\"[email protected]\") 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
\"[email protected]\") echo \"JOB_ID_USER_1\" ;;
\"[email protected]\") echo \"JOB_ID_USER_2\" ;;
\"[email protected]\") 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.
Upvotes: 0
Views: 696
Reputation: 18813
Use [[ \${ORIGINAL_COMMIT} != *\"Jobs:\"* ]]
for your test, or any of the other comparisons in String contains in bash.
The echo \${ORIGINAL_COMMIT}
line is also removing newlines in the commit message. You'll want to quote that: echo \"\${ORIGINAL_COMMIT}\"
Overall, the filter now looks like:
FILTER="
ORIGINAL_COMMIT=\`cat\` &&
echo \"\${ORIGINAL_COMMIT}\" &&
case \"\${ORIGINAL_COMMIT}\" in
*Jobs:*) ;; # Already present, do nothing
*) # Otherwise, add
echo
echo Jobs:
case "\${GIT_AUTHOR_EMAIL}" in
\"[email protected]\") echo \"JOB_ID_USER_1\" ;;
\"[email protected]\") echo \"JOB_ID_USER_2\" ;;
\"[email protected]\") echo \"JOB_ID_USER_3\" ;;
*) echo UNKNOWN ;;
esac
;;
esac
"
Upvotes: 1
Reputation: 1715
As proposed by @Kristján, it is possible to use case to search for substrings.
FILTER="
ORIGINAL_COMMIT=\`cat\` &&
case "\${GIT_AUTHOR_EMAIL}" in
\"[email protected]\") JOB_ID=\"JOB_ID_USER_1\" ;;
\"[email protected]\") JOB_ID=\"JOB_ID_USER_2\" ;;
\"[email protected]\") JOB_ID=\"JOB_ID_USER_3\" ;;
*) JOB_ID=\"UNKNOWN\" ;;
esac &&
case \"\${ORIGINAL_COMMIT}\" in
*Jobs:*) ;;
*)
echo
echo Jobs:
echo \"\t\${JOB_ID}\"
esac
"
It is also possible to use python to work around this. A bit more troublesome than the solution above, but it also gives you more power since you are using python.
PWD=`pwd`
FILTER="
ORIGINAL_COMMIT=\`cat\` &&
case "\${GIT_AUTHOR_EMAIL}" in
\"[email protected]\") JOB_ID=\"JOB_ID_USER_1\" ;;
\"[email protected]\") JOB_ID=\"JOB_ID_USER_2\" ;;
\"[email protected]\") JOB_ID=\"JOB_ID_USER_3\" ;;
*) JOB_ID=\"UNKNOWN\" ;;
esac
echo \"\${ORIGINAL_COMMIT}\" | ${PWD}/Process_Commit.py \${JOB_ID}
"
The python script (Process_Commit.py):
#!/usr/bin/python
import sys
job_id = sys.argv[1]
adjust = True
for line in sys.stdin:
sys.stdout.write(line)
if line.find("Jobs:") != -1:
adjust = False
if adjust == True:
print
print "Jobs:"
print "\t",job_id
Upvotes: 0