我正在尝试根据以下条件编辑某个范围的所有提交消息:
换句话说,我希望所有提交都有这样的后缀:
Jobs:
<author_job>
我想出了类似的东西:
#!/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}
但是我无法检测提交消息是否已经有后缀。如果没有if条件,它可以很好地工作,但它会为已经拥有它的消息添加后缀。
在这种情况下,这是过滤器:
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
"
有没有人知道如何绕过它?
我见过有些人在做python脚本来解决类似的问题。这是个好主意吗?
感谢。
答案 0 :(得分:1)
使用[[ \${ORIGINAL_COMMIT} != *\"Jobs:\"* ]]
进行测试,或使用String contains in bash中的任何其他比较。
echo \${ORIGINAL_COMMIT}
行还删除了提交消息中的换行符。您想引用它:echo \"\${ORIGINAL_COMMIT}\"
总的来说,过滤器现在看起来像:
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
\"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
;;
esac
"
答案 1 :(得分:0)
根据@Kristján的建议,可以使用 case 来搜索子字符串。
FILTER="
ORIGINAL_COMMIT=\`cat\` &&
case "\${GIT_AUTHOR_EMAIL}" in
\"user1@company.com\") JOB_ID=\"JOB_ID_USER_1\" ;;
\"user2@company.com\") JOB_ID=\"JOB_ID_USER_2\" ;;
\"user3@company.com\") JOB_ID=\"JOB_ID_USER_3\" ;;
*) JOB_ID=\"UNKNOWN\" ;;
esac &&
case \"\${ORIGINAL_COMMIT}\" in
*Jobs:*) ;;
*)
echo
echo Jobs:
echo \"\t\${JOB_ID}\"
esac
"
也可以使用python来解决这个问题。比上面的解决方案麻烦一点,但是因为你使用python它也会给你更多的力量。
PWD=`pwd`
FILTER="
ORIGINAL_COMMIT=\`cat\` &&
case "\${GIT_AUTHOR_EMAIL}" in
\"user1@company.com\") JOB_ID=\"JOB_ID_USER_1\" ;;
\"user2@company.com\") JOB_ID=\"JOB_ID_USER_2\" ;;
\"user3@company.com\") JOB_ID=\"JOB_ID_USER_3\" ;;
*) JOB_ID=\"UNKNOWN\" ;;
esac
echo \"\${ORIGINAL_COMMIT}\" | ${PWD}/Process_Commit.py \${JOB_ID}
"
python脚本(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