diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3e675d0c..60267d27 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - "**" pull_request: branches: - main @@ -18,7 +19,7 @@ jobs: uses: actions/checkout@v3 - name: shellcheck uses: reviewdog/action-shellcheck@v1.15 - + test-pull-requests-from-forks: name: Test changed-files diff on pull_requests from forks runs-on: ${{ matrix.platform }} @@ -140,7 +141,7 @@ jobs: echo "${{ toJSON(steps.changed-files.outputs) }}" shell: bash - + - name: Run changed-files-specific with non existent base sha id: changed-files-specific uses: ./ @@ -180,7 +181,7 @@ jobs: echo "${{ toJSON(steps.changed-files.outputs) }}" shell: bash - + - name: Run changed-files-specific with non existent sha id: changed-files-specific uses: ./ @@ -293,6 +294,42 @@ jobs: echo '${{ toJSON(steps.changed-files-specific-newline-source-file.outputs) }}' shell: bash + - name: Run changed-files for old new filenames test rename 1 + id: changed-files-all-old-new-renamed-files-1 + uses: ./ + with: + base_sha: d1c0ee4 + sha: 4d04215 + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files-1.outputs) }}' + shell: + bash + - name: Check all_old_new_renamed_files output + if: "!contains(steps.changed-files-all-old-new-renamed-files-1.outputs.all_old_new_renamed_files, 'test/test rename 1.txt,test/test rename-1.txt')" + run: | + echo "Invalid output: Expected to include (test/test rename 1.txt,test/test rename-1.txt) got (${{ steps.changed-files-all-old-new-renamed-files-1.outputs.all_old_new_renamed_files }})" + exit 1 + shell: + bash + - name: Run changed-files for old new filenames test rename 2 + id: changed-files-all-old-new-renamed-files-2 + uses: ./ + with: + base_sha: 4d04215 + sha: fe238e6 + - name: Show output + run: | + echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files-2.outputs) }}' + shell: + bash + - name: Check all_old_new_renamed_files output + if: "!contains(steps.changed-files-all-old-new-renamed-files-2.outputs.all_old_new_renamed_files, 'test/test rename 2.txt,test/test rename-2.txt')" + run: | + echo "Invalid output: Expected to include (test/test rename 2.txt test/test rename-2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2.outputs.all_old_new_renamed_files }})" + exit 1 + shell: + bash - name: Run changed-files with specific files id: changed-files-specific uses: ./ diff --git a/README.md b/README.md index 85fc018c..1802ec44 100644 --- a/README.md +++ b/README.md @@ -100,35 +100,38 @@ Support this project with a :star: | U | Unmerged | | X | Unknown | -| Output | type | example | description | -|:----------------------------------:|:--------:|:--------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| -| any\_changed | `string` | `true` OR `false` | Returns `true` when any
of the filenames provided using
the `files` input has changed.
i.e. *using a combination of all added,
copied, modified and renamed files (ACMR)* | -| only\_changed | `string` | `true` OR `false` | Returns `true` when only
files provided using
the `files` input has changed. (ACMR) | -| other\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other changed files
not listed in the files input
i.e. *a combination of all added,
copied and modified files (ACMR)* | -| any\_modified | `string` | `true` OR `false` | Returns `true` when any
of the filenames provided using
the `files` input has been modified.
i.e. *using a combination of all added,
copied, modified, renamed, and deleted files (ACMRD)* | -| only\_modified | `string` | `true` OR `false` | Returns `true` when only
files provided using
the `files` input has been modified. (ACMRD) | -| other\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other modified files
not listed in the files input
i.e. *a combination of all added,
copied, modified, and deleted files (ACMRD)* | -| any\_deleted | `string` | `true` OR `false` | Returns `true` when any
of the filenames provided using
the `files` input has been deleted. (D) | -| only\_deleted | `string` | `true` OR `false` | Returns `true` when only
files provided using
the `files` input has been deleted. (D) | -| other\_deleted\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other deleted files
not listed in the files input
i.e. *a combination of all deleted files (D)* | -| all\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed files
i.e. *a combination of all added,
copied, modified and renamed files (ACMR)* | -| all\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed files
i.e. *a combination of all added,
copied, modified, renamed and deleted files (ACMRD)* | -| all\_changed\_and\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed
and modified files
i.e. *a combination of (ACMRDTUX)* | -| added\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Added (A) | -| copied\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Copied (C) | -| deleted\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Deleted (D) | -| modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Modified (M) | -| renamed\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Renamed (R) | -| type\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that have their file type changed (T) | -| unmerged\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Unmerged (U) | -| unknown\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Unknown (X) | +| Output | type | example | description | +|:----------------------------------:|:--------:|:--------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| +| any\_changed | `string` | `true` OR `false` | Returns `true` when any
of the filenames provided using
the `files` input has changed.
i.e. *using a combination of all added,
copied, modified and renamed files (ACMR)* | +| only\_changed | `string` | `true` OR `false` | Returns `true` when only
files provided using
the `files` input has changed. (ACMR) | +| other\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other changed files
not listed in the files input
i.e. *a combination of all added,
copied and modified files (ACMR)* | +| any\_modified | `string` | `true` OR `false` | Returns `true` when any
of the filenames provided using
the `files` input has been modified.
i.e. *using a combination of all added,
copied, modified, renamed, and deleted files (ACMRD)* | +| only\_modified | `string` | `true` OR `false` | Returns `true` when only
files provided using
the `files` input has been modified. (ACMRD) | +| other\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other modified files
not listed in the files input
i.e. *a combination of all added,
copied, modified, and deleted files (ACMRD)* | +| any\_deleted | `string` | `true` OR `false` | Returns `true` when any
of the filenames provided using
the `files` input has been deleted. (D) | +| only\_deleted | `string` | `true` OR `false` | Returns `true` when only
files provided using
the `files` input has been deleted. (D) | +| other\_deleted\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other deleted files
not listed in the files input
i.e. *a combination of all deleted files (D)* | +| all\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed files
i.e. *a combination of all added,
copied, modified and renamed files (ACMR)* | +| all\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed files
i.e. *a combination of all added,
copied, modified, renamed and deleted files (ACMRD)* | +| all\_changed\_and\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed
and modified files
i.e. *a combination of (ACMRDTUX)* | +| added\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Added (A) | +| copied\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Copied (C) | +| deleted\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Deleted (D) | +| modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Modified (M) | +| renamed\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Renamed (R) | +| all_old_new_renamed_files | `string` | `'old name.txt,new name.txt old name 2.txt,new name 2.txt...'` | Select only files that are Renamed and list their old and new names (R) | +| type\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that have their file type changed (T) | +| unmerged\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Unmerged (U) | +| unknown\_files | `string` | `'new.txt path/to/file.png ...'` | Select only files that are Unknown (X) | ## Inputs | Input | type | required | default | description | |:---------------------------------:|:----------------------:|:--------:|:---------------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| | token | `string` | `false` | `${{ github.token }}` | [GITHUB\_TOKEN](https://docs.github.com/en/free-pro-team@latest/actions/reference/authentication-in-a-workflow#using-the-github_token-in-a-workflow)
or a repo scoped
[Personal Access Token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token) | -| separator | `string` | `true` | `' '` | Output string separator | +| separator | `string` | `false` | `' '` | Split character for output strings | +| old\_new\_separator | `string` | `false` | `','` | Split character for old and new filename pairs | +| old\_new\_files\_separator | `string` | `false` | `' '` | Split character for multiple old and new filename pairs | | files | `string` OR `string[]` | `false` | | Check for changes
using only these
list of file(s)
(Defaults to the
entire repo) | | files\_separator | string | `false` | `'\n'` | Separator used to split the
`files` input | | files\_from\_source\_file | `string` | `false` | | Source file(s)
used to populate
the `files` input | @@ -138,9 +141,9 @@ Support this project with a :star: | sha | `string` | `true` | `${{ github.sha }}` | Specify a different
commit SHA
used for
comparing changes | | base\_sha | `string` | `false` | | Specify a different
base commit SHA
used for
comparing changes | | path | `string` | `false` | | Relative path under
`GITHUB_WORKSPACE`
to the repository | -| since\_last\_remote\_commit | `string` | `false` | `false` | Use the last commit on the remote
branch as the `base_sha`
(Defaults to the last commit
on the target branch for Pull requests
or the previous commit
on the current branch
for push events).
NOTE: This requires
`fetch-depth: 0`
with `actions/checkout@v2` | -| use\_fork\_point | `string` | `false` | `false` | Finds best common ancestor
between two commits
to use in a three-way merge
as the `base_sha`
See: [git merge-base](https://git-scm.com/docs/git-merge-base#Documentation/git-merge-base.txt---fork-point).
NOTE: This pulls the entire commit history of the base branch | -| quotepath | `string` | `false` | `true` | Output filenames completely verbatim by setting this to `false` | +| since\_last\_remote\_commit | `string` | `false` | `false` | Use the last commit on the remote
branch as the `base_sha`
(Defaults to the last commit
on the target branch for Pull requests
or the previous commit
on the current branch
for push events).
NOTE: This requires
`fetch-depth: 0`
with `actions/checkout@v2` | +| use\_fork\_point | `string` | `false` | `false` | Finds best common ancestor
between two commits
to use in a three-way merge
as the `base_sha`
See: [git merge-base](https://git-scm.com/docs/git-merge-base#Documentation/git-merge-base.txt---fork-point).
NOTE: This pulls the entire commit history of the base branch | +| quotepath | `string` | `false` | `true` | Output filenames completely verbatim by setting this to `false` | ## Example diff --git a/action.yml b/action.yml index d59b9339..fd10f53a 100644 --- a/action.yml +++ b/action.yml @@ -5,11 +5,19 @@ author: tj-actions inputs: token: description: 'Github token' - required: true + required: false default: ${{ github.token }} separator: - description: 'Split character for array output' - required: true + description: 'Split character for output strings' + required: false + default: " " + old_new_separator: + description: 'Split character for old and new filename pairs' + required: false + default: "," + old_new_files_separator: + description: 'Split character for multiple old and new filename pairs' + required: false default: " " files_from_source_file: description: 'Source file(s) to populate the files input' @@ -74,6 +82,9 @@ outputs: renamed_files: description: List of renamed files. value: ${{ steps.changed-files.outputs.renamed_files }} + all_old_new_renamed_files: + description: List of all old and new names of renamed files. + value: ${{ steps.changed-files.outputs.all_old_new_renamed_files }} type_changed_files: description: List of files that had type changes. value: ${{ steps.changed-files.outputs.type_changed_files }} @@ -182,6 +193,8 @@ runs: INPUT_TARGET_BRANCH: ${{ steps.changed-files-diff-sha.outputs.target_branch }} INPUT_CURRENT_BRANCH: ${{ steps.changed-files-diff-sha.outputs.current_branch }} INPUT_QUOTEPATH: ${{ inputs.quotepath }} + INPUT_OLD_NEW_SEPARATOR: ${{ inputs.old_new_separator }} + INPUT_OLD_NEW_FILES_SEPARATOR: ${{ inputs.old_new_files_separator }} branding: icon: file-text diff --git a/entrypoint.sh b/entrypoint.sh index 26d783c5..b3ef8751 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -31,6 +31,24 @@ function get_diff() { git diff --diff-filter="$filter" --name-only --ignore-submodules=all "$base" "$sha" } +function get_renames() { + base="$1" + sha="$2" + while IFS='' read -r sub; do + sub_commit_pre="$(git diff "$base" "$sha" -- "$sub" | grep '^[-]Subproject commit' | awk '{print $3}')" + sub_commit_cur="$(git diff "$base" "$sha" -- "$sub" | grep '^[+]Subproject commit' | awk '{print $3}')" + if [ -n "$sub_commit_cur" ]; then + ( + cd "$sub" && ( + # the strange magic number is a hardcoded "empty tree" commit sha + get_renames "${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904}" "${sub_commit_cur}" | awk -v r="$sub" '{ print "" r "/" $0}' + ) + ) + fi + done < <(git submodule | awk '{print $2}') + git log --name-status --ignore-submodules=all "$base" "$sha" | grep -E "^R" | awk -F '\t' -v d="$INPUT_OLD_NEW_SEPARATOR" '{print $2d$3}' +} + echo "::group::changed-files" echo "Resolving repository path..." @@ -60,6 +78,7 @@ if [[ -z "$INPUT_FILES_PATTERN_FILE" ]]; then ALL_CHANGED_AND_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "*ACDMRTUX" | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') ALL_CHANGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMR" | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') ALL_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMRD" | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') + ALL_OLD_NEW_RENAMED_FILES=$(get_renames "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" | awk -v d="$INPUT_OLD_NEW_FILES_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') else ADDED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" A | grep -x -E -f "$INPUT_FILES_PATTERN_FILE" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') COPIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" C | grep -x -E -f "$INPUT_FILES_PATTERN_FILE" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') @@ -72,6 +91,7 @@ else ALL_CHANGED_AND_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "*ACDMRTUX" | grep -x -E -f "$INPUT_FILES_PATTERN_FILE" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') ALL_CHANGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMR" | grep -x -E -f "$INPUT_FILES_PATTERN_FILE" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') ALL_MODIFIED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMRD" | grep -x -E -f "$INPUT_FILES_PATTERN_FILE" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + ALL_OLD_NEW_RENAMED_FILES=$(get_renames "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" | grep -x -E -f "$INPUT_FILES_PATTERN_FILE" | awk -v d="$INPUT_OLD_NEW_FILES_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') ALL_OTHER_CHANGED=$(get_diff "$INPUT_PREVIOUS_SHA" "$INPUT_CURRENT_SHA" "ACMR" | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') UNIQUE_ALL_CHANGED=$(echo "${ALL_CHANGED}" | awk '{gsub(/\|/,"\n"); print $0;}' | awk '!a[$0]++' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') @@ -82,7 +102,7 @@ else else echo "::set-output name=any_changed::false" fi - + OTHER_CHANGED="" if [[ -n $ALL_OTHER_CHANGED ]]; then @@ -112,7 +132,7 @@ else else echo "::set-output name=any_modified::false" fi - + OTHER_MODIFIED="" if [[ -n $ALL_OTHER_MODIFIED ]]; then @@ -142,7 +162,7 @@ else else echo "::set-output name=any_deleted::false" fi - + OTHER_DELETED="" if [[ -n $ALL_OTHER_DELETED ]]; then @@ -189,6 +209,7 @@ echo "Unknown files: $UNKNOWN" echo "All changed and modified files: $ALL_CHANGED_AND_MODIFIED" echo "All changed files: $ALL_CHANGED" echo "All modified files: $ALL_MODIFIED" +echo "All old & new renamed files: $ALL_OLD_NEW_RENAMED_FILES" echo "::set-output name=added_files::$ADDED" echo "::set-output name=copied_files::$COPIED" @@ -201,5 +222,6 @@ echo "::set-output name=unknown_files::$UNKNOWN" echo "::set-output name=all_changed_and_modified_files::$ALL_CHANGED_AND_MODIFIED" echo "::set-output name=all_changed_files::$ALL_CHANGED" echo "::set-output name=all_modified_files::$ALL_MODIFIED" +echo "::set-output name=all_old_new_renamed_files::$ALL_OLD_NEW_RENAMED_FILES" echo "::endgroup::" diff --git a/test/test rename 1.txt b/test/test rename 1.txt new file mode 100644 index 00000000..6de7b8c6 --- /dev/null +++ b/test/test rename 1.txt @@ -0,0 +1 @@ +This is a test file. diff --git a/test/test rename 2.txt b/test/test rename 2.txt new file mode 100644 index 00000000..6de7b8c6 --- /dev/null +++ b/test/test rename 2.txt @@ -0,0 +1 @@ +This is a test file.