0

I have a script running in GitHub Actions that runs a retrieve from another source and loads those changes into a Git branch what I then want to do is iterate through the list of changed files and add them to a commit and add a specific commit message such as "Auto Commit - {xxxx.xml} file was changed" with xxxx.xml being the file name that was picked up in the git diff. At the moment my script just commits --all but I'd like to change this to loop through each change and add my own commit message per change. Is this possible?

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

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

git push

P.S please bare in mind I'm a complete noob

knittl
  • 246,190
  • 53
  • 318
  • 364

1 Answers1

0

Assuming none of your files contain funky characters (tabulator and especially no line breaks), this should get you started:

  1. First, get a list of files
  2. Reset the commit
  3. Add the first/next file
  4. Create the new commit
  5. Go to step 3. Rinse. Repeat.

To make scripting easier, 1 and 2 need to be swapped

A shell script could look like this:

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

If you have GNU xargs, you might combine diff-tree -z | xargs -0 to handle even those special paths.

knittl
  • 246,190
  • 53
  • 318
  • 364