shell 循环访问diff并使用唯一的提交消息逐个提交每个文件

ax6ht2ek  于 2022-11-16  发布在  Shell
关注(0)|答案(1)|浏览(121)

我在GitHub Actions中运行了一个脚本,该脚本从另一个源检索并将这些更改加载到Git分支中,然后我想做的是遍历已更改文件的列表并将其添加到提交中,然后添加一条特定的提交消息,如“Auto Commit - {xxxx.xml} file was changed”,其中xxxx.xml是在git diff中获取的文件名。现在我的脚本只是提交--除了我想改变它,循环遍历每个更改,并为每个更改添加我自己的提交消息。这可能吗?

echo "*************************************************************"
echo "GIT diff:"
echo ""
git diff
echo ""
echo "*************************************************************"

git add --all
git commit -m "Auto-commit - updated Salesforce metadata"

git push

附言:请记住我是一个彻头彻尾的菜鸟

1l5u6lss

1l5u6lss1#

假设你的文件都不包含奇怪的字符(制表符,尤其是没有换行符),这应该可以让你开始:
1.首先,获取文件列表
1.重置提交
1.添加第一个/下一个文件
1.创建新提交
1.转到第3步。冲洗。重复。
为了使脚本编写更容易,需要交换1和2
shell脚本可能如下所示:

commit=$(git rev-parse HEAD) # remember commit hash
git reset HEAD^ # reset to previous commit
git diff-tree --no-commit-id --name-only -r "$commit" \
| while IFS= read -r path; do
  git add -- "$path"
  git commit -m 'Auto-commit - '"$path"' file was changed'
done

如果您有GNU xargs,您甚至可以合并diff-tree -z | xargs -0来处理这些特殊路径。

相关问题