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
This commit is contained in:
Stefan Zweifel 2022-09-17 15:18:36 +02:00 committed by GitHub
parent 4e7c0d67cd
commit 4d00f10668
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 4 deletions

View File

@ -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`.

View File

@ -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() {

View File

@ -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"
}