Skip to content

Commit

Permalink
Merge pull request #12 from planetscale/renames
Browse files Browse the repository at this point in the history
  • Loading branch information
joemiller authored Jun 21, 2023
2 parents d36aa24 + 628c564 commit 4452950
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand Down
24 changes: 21 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -55,4 +73,4 @@ fi
ghcommit_args+=("${adds[@]/#/--add=}")
ghcommit_args+=("${deletes[@]/#/--delete=}")

ghcommit "${ghcommit_args[@]}"
ghcommit "${ghcommit_args[@]}"
8 changes: 6 additions & 2 deletions tests/entrypoint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file not shown.

0 comments on commit 4452950

Please sign in to comment.