Skip to content

Commit 3ea6ae1

Browse files
Fix "nothing to commit" error with LF/CRLF changes #241 (#265)
* fix crlf test * add diff check before commit * add dirty check flag (not sure if needed) * Update test name and add more assertions Update test name to make it clear that the Action no longer fails to detect CRLF changes. * Add Comment to explain why we use git-diff again * Add test to confirm content changes are commited * Closes #241 Co-authored-by: Stefan Zweifel <[email protected]>
1 parent 976f220 commit 3ea6ae1

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

entrypoint.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,28 @@ _main() {
2525

2626
_add_files
2727

28-
_local_commit
28+
# Check dirty state of repo again using git-diff.
29+
# (git-diff detects beter if CRLF of files changes and does NOT
30+
# proceed, if only CRLF changes are detected. See #241 and #265
31+
# for more details.)
32+
if [ -n "$(git diff --staged)" ] || "$INPUT_SKIP_DIRTY_CHECK"; then
33+
_local_commit
2934

30-
_tag_commit
35+
_tag_commit
3136

32-
_push_to_github
37+
_push_to_github
38+
else
39+
40+
# Check if $GITHUB_OUTPUT is available
41+
# (Feature detection will be removed in late December 2022)
42+
if [ -z ${GITHUB_OUTPUT+x} ]; then
43+
echo "::set-output name=changes_detected::false";
44+
else
45+
echo "changes_detected=false" >> $GITHUB_OUTPUT;
46+
fi
47+
48+
echo "Working tree clean. Nothing to commit.";
49+
fi
3350
else
3451

3552
# Check if $GITHUB_OUTPUT is available

tests/git-auto-commit.bats

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -974,34 +974,73 @@ cat_github_output() {
974974
assert_line --partial "another-subdirectory/new-file-3.txt"
975975
}
976976

977-
@test "fails to detect crlf change in files and does not detect change or commit changes" {
977+
@test "detects if crlf in files change and does not create commit" {
978978
# Set autocrlf to true
979979
cd "${FAKE_LOCAL_REPOSITORY}"
980980
git config core.autocrlf true
981981
run git config --get-all core.autocrlf
982982
assert_line "true"
983983

984984
# Add more .txt files
985-
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt
986-
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt
985+
echo -ne "crlf test1\r\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt
986+
echo -ne "crlf test1\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt
987987

988988
# Run git-auto-commit to add new files to repository
989989
run git_auto_commit
990990

991991
# Change control characters in files
992-
sed 's/^M$//' "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt
993-
sed 's/$/^M/' "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt
992+
echo -ne "crlf test1\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt
993+
echo -ne "crlf test1\r\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt
994994

995995
# Run git-auto-commit to commit the 2 changes files
996996
run git_auto_commit
997997

998998
assert_success
999999

1000-
# Changes are not detected
1000+
refute_line --partial "2 files changed, 2 insertions(+), 2 deletions(-)"
1001+
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"
1002+
10011003
assert_line --partial "Working tree clean. Nothing to commit."
1004+
assert_line --partial "new-file-2.txt"
1005+
assert_line --partial "new-file-3.txt"
1006+
1007+
# Changes are not detected
1008+
run cat_github_output
1009+
assert_line "changes_detected=false"
1010+
}
1011+
1012+
@test "detects if crlf in files change and creates commit if the actual content of the files change" {
1013+
# Set autocrlf to true
1014+
cd "${FAKE_LOCAL_REPOSITORY}"
1015+
git config core.autocrlf true
1016+
run git config --get-all core.autocrlf
1017+
assert_line "true"
1018+
1019+
# Add more .txt files
1020+
echo -ne "crlf test1\r\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt
1021+
echo -ne "crlf test1\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt
10021022

1003-
refute_line --partial "new-file-2.txt"
1004-
refute_line --partial "new-file-3.txt"
1023+
# Run git-auto-commit to add new files to repository
1024+
run git_auto_commit
1025+
1026+
# Change control characters in files
1027+
echo -ne "crlf test2\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-2.txt
1028+
echo -ne "crlf test2\r\n" > "${FAKE_LOCAL_REPOSITORY}"/new-file-3.txt
1029+
1030+
# Run git-auto-commit to commit the 2 changes files
1031+
run git_auto_commit
1032+
1033+
assert_success
1034+
1035+
assert_line --partial "2 files changed, 2 insertions(+), 2 deletions(-)"
1036+
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"
1037+
1038+
assert_line --partial "new-file-2.txt"
1039+
assert_line --partial "new-file-3.txt"
1040+
1041+
# Changes are detected
1042+
run cat_github_output
1043+
assert_line "changes_detected=true"
10051044
}
10061045

10071046

0 commit comments

Comments
 (0)