diff --git a/README.md b/README.md index 945e86b..0e2a290 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ jobs: # Run steps that make changes to the local repo here. # Commit all changed files back to the repository - - uses: planetscale/ghcommit-action@v0.1.4 + - uses: planetscale/ghcommit-action@v0.1.5 with: commit_message: "🤖 fmt" repo: ${{ github.repository }} @@ -56,7 +56,7 @@ jobs: Example showing all options: ```yaml - - uses: planetscale/ghcommit-action@v0.1.4 + - uses: planetscale/ghcommit-action@v0.1.5 with: commit_message: "🤖 fmt" repo: ${{ github.repository }} diff --git a/entrypoint.sh b/entrypoint.sh index d13817a..6987f2d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -15,11 +15,31 @@ deletes=() # shellcheck disable=SC2086 while IFS= read -r -d $'\0' line; do + # Uncomment for debugging: + #echo "line: '$line'" + # Extract the status in the tree and status in the index (first two characters) index_status="${line:0:1}" tree_status="${line:1:1}" + + # Renamed files have status code 'R' and two filenames separated by NUL. We need to read + # an additional chunk (up to the next NUL) to get the new filename. + if [[ "$index_status" == "R" || "$tree_status" == "R" ]]; then + IFS= read -r -d $'\0' new_filename + filename="${line:3}" + + echo "Renamed file detected:" + echo "Old Filename: $filename" + echo "New Filename: $new_filename" + echo "-----------------------------" + adds+=("$new_filename") + deletes+=("$filename") + continue + fi + # Extract the filename by removing the first three characters (two statuses and a whitespace) filename="${line:3}" + echo "Filename: $filename" # Print the parsed information, useful for debugging echo "Index Status: $index_status" @@ -34,8 +54,6 @@ while IFS= read -r -d $'\0' line; do # handle deletes (D): [[ "$tree_status" =~ D || "$index_status" =~ D ]] && deletes+=("$filename") - # TODO: handle renames (R) and copies (C). Example rename status line: - # 'R main.sh -> main.sh.new' done < <(git status -s --porcelain=v1 -z -- $FILE_PATTERN) if [[ "${#adds[@]}" -eq 0 && "${#deletes[@]}" -eq 0 && "$EMPTY" == "false" ]]; then @@ -55,4 +73,4 @@ fi ghcommit_args+=("${adds[@]/#/--add=}") ghcommit_args+=("${deletes[@]/#/--delete=}") -ghcommit "${ghcommit_args[@]}" \ No newline at end of file +ghcommit "${ghcommit_args[@]}" diff --git a/tests/entrypoint.bats b/tests/entrypoint.bats index 34c0b9d..9bfe41f 100644 --- a/tests/entrypoint.bats +++ b/tests/entrypoint.bats @@ -17,12 +17,16 @@ setup() { local empty='false' local file_pattern='.' + # NOTE: we are passing our hand-crafted fixture through `tr` to convert newlines to nulls since + # we run `git status -z` which uses null terminators. The newlines are meant to make the file easier + # to modify and prevent cat from removing the leading space on lines/entries since that is a part + # of the git status output. stub git \ "config --global --add safe.directory $GITHUB_WORKSPACE : echo stubbed" \ - "status -s --porcelain=v1 -z -- . : cat ./tests/fixtures/test-1 | tr '\n' '\0'" + "status -s --porcelain=v1 -z -- . : cat ./tests/fixtures/git-status.out-1 | tr '\n' '\0'" stub ghcommit \ - '-b main -r org/repo -m msg --add=README.md --add=foo.txt --delete=\""a path with spaces oh joy/file.txt\"" : echo Success' + '-b main -r org/repo -m msg --add=README.md --add=foo.txt --add=new.file --delete=old.file --delete=\""a path with spaces oh joy/file.txt\"" : echo Success' run ./entrypoint.sh "$commit_message" "$repo" "$branch" "$empty" "$file_pattern" assert_success diff --git a/tests/fixtures/test-1 b/tests/fixtures/git-status.out-1 similarity index 73% rename from tests/fixtures/test-1 rename to tests/fixtures/git-status.out-1 index b78ef68..d61b1a9 100644 Binary files a/tests/fixtures/test-1 and b/tests/fixtures/git-status.out-1 differ