From fca6373c22c5995bd543fa7fa895172733d54895 Mon Sep 17 00:00:00 2001 From: ZeroRin Date: Wed, 30 Nov 2022 15:30:23 +0800 Subject: [PATCH 1/6] fix crlf test --- tests/git-auto-commit.bats | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index 92a3434e..3993efc2 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -982,15 +982,15 @@ cat_github_output() { assert_line "true" # Add more .txt files - touch "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt - touch "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt + echo -ne "crlf test1\r\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt + echo -ne "crlf test1\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt # Run git-auto-commit to add new files to repository run git_auto_commit # Change control characters in files - sed 's/^M$//' "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt - sed 's/$/^M/' "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt + echo -ne "crlf test1\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt + echo -ne "crlf test1\r\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt # Run git-auto-commit to commit the 2 changes files run git_auto_commit @@ -998,10 +998,8 @@ cat_github_output() { assert_success # Changes are not detected - assert_line --partial "Working tree clean. Nothing to commit." - - refute_line --partial "new-file-2.txt" - refute_line --partial "new-file-3.txt" + run cat_github_output + assert_line "changes_detected=false" } From d9c05e5916d64ca91314fb4fddf2c44fa76b2b88 Mon Sep 17 00:00:00 2001 From: ZeroRin Date: Wed, 30 Nov 2022 15:36:49 +0800 Subject: [PATCH 2/6] add diff check before commit --- entrypoint.sh | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 52449a64..c84851e1 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -25,11 +25,24 @@ _main() { _add_files - _local_commit + if [ -n "$(git diff --staged)" ]; then + _local_commit - _tag_commit + _tag_commit - _push_to_github + _push_to_github + else + + # Check if $GITHUB_OUTPUT is available + # (Feature detection will be removed in late December 2022) + if [ -z ${GITHUB_OUTPUT+x} ]; then + echo "::set-output name=changes_detected::false"; + else + echo "changes_detected=false" >> $GITHUB_OUTPUT; + fi + + echo "Working tree clean. Nothing to commit."; + fi else # Check if $GITHUB_OUTPUT is available From f5fe04079db34f96149cc0ca9fbfafc4c431a40e Mon Sep 17 00:00:00 2001 From: ZeroRin Date: Wed, 30 Nov 2022 15:42:01 +0800 Subject: [PATCH 3/6] add dirty check flag (not sure if needed) --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index c84851e1..c314f565 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -25,7 +25,7 @@ _main() { _add_files - if [ -n "$(git diff --staged)" ]; then + if [ -n "$(git diff --staged)" ] || "$INPUT_SKIP_DIRTY_CHECK"; then _local_commit _tag_commit From 7bd0e439a35dc631773631c13554d459ecb0eb5c Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Wed, 30 Nov 2022 20:33:24 +0100 Subject: [PATCH 4/6] Update test name and add more assertions Update test name to make it clear that the Action no longer fails to detect CRLF changes. --- tests/git-auto-commit.bats | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index 3993efc2..3d903b4f 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -974,7 +974,7 @@ cat_github_output() { assert_line --partial "another-subdirectory/new-file-3.txt" } -@test "fails to detect crlf change in files and does not detect change or commit changes" { +@test "detects if crlf in files change and does not create commit" { # Set autocrlf to true cd "${FAKE_LOCAL_REPOSITORY}" git config core.autocrlf true @@ -997,6 +997,13 @@ cat_github_output() { assert_success + refute_line --partial "2 files changed, 2 insertions(+), 2 deletions(-)" + assert_line --partial "warning: in the working copy of 'new-file-2.txt', LF will be replaced by CRLF the next time Git touches it" + + assert_line --partial "Working tree clean. Nothing to commit." + assert_line --partial "new-file-2.txt" + assert_line --partial "new-file-3.txt" + # Changes are not detected run cat_github_output assert_line "changes_detected=false" From f2936bb4d4cba423b718cb6ee9b88d4245994f3d Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Wed, 30 Nov 2022 20:33:35 +0100 Subject: [PATCH 5/6] Add Comment to explain why we use git-diff again --- entrypoint.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index c314f565..3c858317 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -25,6 +25,10 @@ _main() { _add_files + # Check dirty state of repo again using git-diff. + # (git-diff detects beter if CRLF of files changes and does NOT + # proceed, if only CRLF changes are detected. See #241 and #265 + # for more details.) if [ -n "$(git diff --staged)" ] || "$INPUT_SKIP_DIRTY_CHECK"; then _local_commit From dcbc52dc5972241ccedca50564921e69b22c68a7 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Wed, 30 Nov 2022 21:02:33 +0100 Subject: [PATCH 6/6] Add test to confirm content changes are commited --- tests/git-auto-commit.bats | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index 3d903b4f..ead4db75 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -1009,6 +1009,40 @@ cat_github_output() { assert_line "changes_detected=false" } +@test "detects if crlf in files change and creates commit if the actual content of the files change" { + # Set autocrlf to true + cd "${FAKE_LOCAL_REPOSITORY}" + git config core.autocrlf true + run git config --get-all core.autocrlf + assert_line "true" + + # Add more .txt files + echo -ne "crlf test1\r\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt + echo -ne "crlf test1\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt + + # Run git-auto-commit to add new files to repository + run git_auto_commit + + # Change control characters in files + echo -ne "crlf test2\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt + echo -ne "crlf test2\r\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt + + # Run git-auto-commit to commit the 2 changes files + run git_auto_commit + + assert_success + + assert_line --partial "2 files changed, 2 insertions(+), 2 deletions(-)" + assert_line --partial "warning: in the working copy of 'new-file-2.txt', LF will be replaced by CRLF the next time Git touches it" + + assert_line --partial "new-file-2.txt" + assert_line --partial "new-file-3.txt" + + # Changes are detected + run cat_github_output + assert_line "changes_detected=true" +} + @test "It uses old set-output syntax if GITHUB_OUTPUT environment is not available when changes are committed" { unset GITHUB_OUTPUT