planetscale / ghcommit-action

GitHub Action to commit files to a git branch using the ghcommit utility
Apache License 2.0
30 stars 4 forks source link

implement renamed file handling #12

Closed joemiller closed 1 year ago

joemiller commented 1 year ago

Implement handling of renamed files.

According to the github graphql API docs the createCommitOnBranch method takes a FileChanges object as input which is composed of one or more adds or deletes. A rename action should be modeled as an add of the new filename + delete of the old filename: https://docs.github.com/en/graphql/reference/input-objects#modeling-file-changes

The entrypoint.sh script parses the output git status -s --porcelain=v1 -z to detect each add / delete. The -z flag NUL terminates each "line" and is recommended for programmatic consumption of git status output. The tricky part here is that -z also uses a NUL to separate the old and new file name. We have to account for this extra NUL when an R (rename) action is detected by reading an additional chunk inside the loop to get the new filename.

$ git status -s --porcelain=v1  -- .
 M README.md
R  action.yaml -> action.yaml.new

$ git status -s --porcelain=v1 -z -- . | cat -tve
 M README.md^@R  action.yaml.new^@action.yaml^@%