Skip to content

Commit

Permalink
feat: Add support for glob pattern matching (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackton1 authored Mar 12, 2022
1 parent 22506bb commit c09bcad
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
25 changes: 14 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,30 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Test sql file has no changes an no warning with autocrlf
- name: Test files has no changes an no warning with autocrlf
uses: ./
id: changed_files_not_expected_autocrlf
with:
autocrlf: true
files: |
test/new.txt
test/new.sql
test/new/.(sql|txt)
test/*.txt
test/*.sql
test/**/*.txt
test/**/*.sql
- name: Display changed files
if: steps.changed_files_not_expected_autocrlf.outputs.files_changed == 'true'
run: |
echo "Changed files (Not expected): ${{ steps.changed_files_not_expected_autocrlf.outputs.changed_files }}"
exit 1
- name: Test sql file has no changes
- name: Test files has no changes
uses: ./
id: changed_files_not_expected
with:
files: |
test/new.txt
test/new.sql
test/new/.(sql|txt)
test/*.txt
test/*.sql
test/**/*.txt
test/**/*.sql
- name: Display changed files
if: steps.changed_files_not_expected.outputs.files_changed == 'true'
run: |
Expand All @@ -64,9 +66,10 @@ jobs:
id: changed_files_expected
with:
files: |
test/new.txt
test/new.sql
test/new/.(sql|txt)
test/*.txt
test/*.sql
test/**/*.txt
test/**/*.sql
- name: Display changed files
if: steps.changed_files_expected.outputs.files_changed == 'true'
run: |
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Verify that certain files or directories did or did not change during the workfl
* Boolean output for detecting uncommited changes.
* List all files that changed during the workflow execution.
* Restrict change detection to a subset of files.
* [Regex pattern](https://www.gnu.org/software/grep/manual/grep.html#Regular-Expressions) matching on a subset of files.
* Using [Glob pattern](https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet) matching.

## Usage

Expand Down Expand Up @@ -50,11 +50,12 @@ Verify that certain files or directories did or did not change during the workfl
id: verify-changed-files
with:
files: |
new.txt
*.txt
test_directory
.(py|jpeg)$
\.sql$
^(mynewfile|custom)
action.yml
**/*.py
**/*.jpeg
*.sql
- name: Run step only when files change.
if: steps.verify-changed-files.outputs.files_changed == 'true'
Expand Down
8 changes: 7 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ outputs:
runs:
using: 'composite'
steps:
- name: Glob match
uses: tj-actions/[email protected]
id: glob
with:
files: ${{ inputs.files }}
separator: "|"
- run: |
bash $GITHUB_ACTION_PATH/entrypoint.sh
id: verify-changed-files
Expand All @@ -39,7 +45,7 @@ runs:
# INPUT_<VARIABLE_NAME> is not available in Composite run steps
# https://github.community/t/input-variable-name-is-not-available-in-composite-run-steps/127611
INPUT_TOKEN: ${{ inputs.token }}
INPUT_FILES: ${{ inputs.files }}
INPUT_FILES: ${{ steps.glob.outputs.paths }}
INPUT_AUTO_CRLF: ${{ inputs.autocrlf }}
INPUT_SEPARATOR: ${{ inputs.separator }}
Expand Down
10 changes: 3 additions & 7 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ set -e

git config --local core.autocrlf "$INPUT_AUTO_CRLF"

IFS=" " read -r -a ALL_FILES <<< "$(echo "${INPUT_FILES[@]}" | sort -u | tr "\n" " ")"
echo "Checking for file changes: \"${INPUT_FILES}\"..."

FILES=$(echo "${ALL_FILES[*]}" | awk '{gsub(/ /,"\n"); print $0;}' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}')

echo "Checking for file changes: \"${FILES}\"..."

TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | grep -E "(${FILES})" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}')
TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | grep -E "(${INPUT_FILES})" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}')

# Find untracked changes
UNTRACKED_FILES=$(git ls-files --others --exclude-standard | grep -E "(${FILES})" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}')
UNTRACKED_FILES=$(git ls-files --others --exclude-standard | grep -E "(${INPUT_FILES})" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}')

CHANGED_FILES=""

Expand Down

0 comments on commit c09bcad

Please sign in to comment.