Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand file_pattern-input to an array #205

Merged
merged 12 commits into from Sep 17, 2022
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -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`.
Expand Down
8 changes: 6 additions & 2 deletions entrypoint.sh
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
57 changes: 55 additions & 2 deletions tests/git-auto-commit.bats
Expand Up @@ -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"

Expand All @@ -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.
Expand Down Expand Up @@ -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"
}