From 4d00f10668b767d99f539489062c0e3dd052c645 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 17 Sep 2022 15:18:36 +0200 Subject: [PATCH] Expand `file_pattern`-input to an array (#205) * Expand file_pattern input * Expand file pattern array in git-status * Add Failing Test * Expand INPUT_FILE_PATTERN * Fix Shellcheck Issues * Add test to cover file expansion works when globbing is disabled * Add explanation to README --- README.md | 20 +++++++++++++ entrypoint.sh | 8 ++++-- tests/git-auto-commit.bats | 57 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dad822f..1f77647 100644 --- a/README.md +++ b/README.md @@ -407,6 +407,26 @@ If you're using the Action with a custom `file_pattern` and the Action throws a See [Issue #227](https://github.com/stefanzweifel/git-auto-commit-action/issues/227) for details. +### Custom `file_pattern`, changed files but seeing "Working tree clean. Nothing to commit." in the logs + +If you're using a custom `file_pattern` and the Action does not detect the changes made in your worfklow, you're probably running into a globbing issue. + +Let's imagine you use `file_pattern: '*.md'` to detect and commit changes to all Markdown files in your repository. +If your Workflow now only updates `.md`-files in a subdirectory, but you have an untouched `.md`-file in the root of the repository, the git-auto-commit Action will display "Working tree clean. Nothing to commit." in the Workflow log. + +This is due to the fact, that the `*.md`-glob is expanded before sending it to `git-status`. `git-status` will receive the filename of your untouched `.md`-file in the root of the repository and won't detect any changes; and therefore the Action does nothing. + +To fix this add `disable_globbing: true` to your Workflow. + +```yaml +- uses: stefanzweifel/git-auto-commit-action@v4 + with: + file_pattern: '*.md' + disable_globbing: true +``` + +See [Issue #239](https://github.com/stefanzweifel/git-auto-commit-action/issues/239) for details. + ## Running the tests The Action has tests written in [bats](https://github.com/bats-core/bats-core). Before you can run the test suite locally, you have to install the dependencies with `npm` or `yarn`. diff --git a/entrypoint.sh b/entrypoint.sh index dee54a9..e34e72e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -40,8 +40,11 @@ _git_is_dirty() { echo "INPUT_STATUS_OPTIONS: ${INPUT_STATUS_OPTIONS}"; echo "::debug::Apply status options ${INPUT_STATUS_OPTIONS}"; + echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}"; + read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN"; + # shellcheck disable=SC2086 - [ -n "$(git status -s $INPUT_STATUS_OPTIONS -- $INPUT_FILE_PATTERN)" ] + [ -n "$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})" ] } _switch_to_branch() { @@ -75,9 +78,10 @@ _add_files() { echo "::debug::Apply add options ${INPUT_ADD_OPTIONS}"; echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}"; + read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN"; # shellcheck disable=SC2086 - git add ${INPUT_ADD_OPTIONS} ${INPUT_FILE_PATTERN}; + git add ${INPUT_ADD_OPTIONS} ${INPUT_FILE_PATTERN:+"${INPUT_FILE_PATTERN_EXPANDED[@]}"}; } _local_commit() { diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index 775d24a..819b1e8 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -164,7 +164,6 @@ git_auto_commit() { } @test "It applies INPUT_ADD_OPTIONS when adding files" { - INPUT_FILE_PATTERN="" INPUT_STATUS_OPTIONS="--untracked-files=no" INPUT_ADD_OPTIONS="-u" @@ -177,7 +176,6 @@ git_auto_commit() { assert_line "INPUT_STATUS_OPTIONS: --untracked-files=no" assert_line "INPUT_ADD_OPTIONS: -u" - assert_line "INPUT_FILE_PATTERN: " assert_line "::debug::Push commit to remote branch master" # Assert that PHP files have not been added. @@ -872,3 +870,58 @@ git_auto_commit() { assert_failure; } + +@test "expands file patterns correctly and commits all changed files" { + # Add more .txt files + touch "${FAKE_LOCAL_REPOSITORY}"/new-file-1.txt + mkdir "${FAKE_LOCAL_REPOSITORY}"/subdirectory/ + touch "${FAKE_LOCAL_REPOSITORY}"/subdirectory/new-file-2.txt + touch "${FAKE_LOCAL_REPOSITORY}"/new-file-3.bar + + INPUT_FILE_PATTERN="*.txt *.bar" + + run git_auto_commit + + assert_success + + assert_line --partial "new-file-1.txt" + assert_line --partial "subdirectory/new-file-2.txt" + assert_line --partial "new-file-3.bar" +} + +@test "expands file patterns correctly and commits all changed files when globbing is disabled" { + # Add more .txt files + touch "${FAKE_LOCAL_REPOSITORY}"/new-file-1.txt + mkdir "${FAKE_LOCAL_REPOSITORY}"/subdirectory/ + touch "${FAKE_LOCAL_REPOSITORY}"/subdirectory/new-file-2.txt + touch "${FAKE_LOCAL_REPOSITORY}"/new-file-3.bar + + INPUT_FILE_PATTERN="*.txt *.bar" + INPUT_DISABLE_GLOBBING=true + + run git_auto_commit + + assert_success + + assert_line --partial "new-file-1.txt" + assert_line --partial "subdirectory/new-file-2.txt" + assert_line --partial "new-file-3.bar" +} + +@test "expands file patterns correctly and commits all changed files if dirty files are only in subdirectory" { + # Add more .txt files + mkdir "${FAKE_LOCAL_REPOSITORY}"/subdirectory/ + touch "${FAKE_LOCAL_REPOSITORY}"/subdirectory/new-file-2.txt + mkdir "${FAKE_LOCAL_REPOSITORY}"/another-subdirectory/ + touch "${FAKE_LOCAL_REPOSITORY}"/another-subdirectory/new-file-3.txt + + INPUT_FILE_PATTERN="*.txt" + INPUT_DISABLE_GLOBBING=true + + run git_auto_commit + + assert_success + + assert_line --partial "subdirectory/new-file-2.txt" + assert_line --partial "another-subdirectory/new-file-3.txt" +}