3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-01-17 09:17:46 +00:00

feat: Added support for returning old and new names of renamed files (#483)

This commit is contained in:
Tonye Jack 2022-05-14 18:05:59 -04:00 committed by GitHub
parent 1e1e79beb1
commit 3baba75579
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 112 additions and 35 deletions

View file

@ -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: ./

View file

@ -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 <br /> of the filenames provided using <br /> the `files` input has changed. <br /> i.e. *using a combination of all added, <br />copied, modified and renamed files (ACMR)* |
| only\_changed | `string` | `true` OR `false` | Returns `true` when only <br /> files provided using <br /> the `files` input has changed. (ACMR) |
| other\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other changed files <br/> not listed in the files input <br /> i.e. *a combination of all added, <br /> copied and modified files (ACMR)* |
| any\_modified | `string` | `true` OR `false` | Returns `true` when any <br /> of the filenames provided using <br /> the `files` input has been modified. <br /> i.e. *using a combination of all added, <br />copied, modified, renamed, and deleted files (ACMRD)* |
| only\_modified | `string` | `true` OR `false` | Returns `true` when only <br /> files provided using <br /> the `files` input has been modified. (ACMRD) |
| other\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other modified files <br/> not listed in the files input <br /> i.e. *a combination of all added, <br /> copied, modified, and deleted files (ACMRD)* |
| any\_deleted | `string` | `true` OR `false` | Returns `true` when any <br /> of the filenames provided using <br /> the `files` input has been deleted. (D) |
| only\_deleted | `string` | `true` OR `false` | Returns `true` when only <br /> files provided using <br /> the `files` input has been deleted. (D) |
| other\_deleted\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other deleted files <br/> not listed in the files input <br /> i.e. *a combination of all deleted files (D)* |
| all\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed files <br /> i.e. *a combination of all added, <br />copied, modified and renamed files (ACMR)* |
| all\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed files <br /> i.e. *a combination of all added, <br />copied, modified, renamed and deleted files (ACMRD)* |
| all\_changed\_and\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed <br /> and modified files <br /> 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 <br /> of the filenames provided using <br /> the `files` input has changed. <br /> i.e. *using a combination of all added, <br />copied, modified and renamed files (ACMR)* |
| only\_changed | `string` | `true` OR `false` | Returns `true` when only <br /> files provided using <br /> the `files` input has changed. (ACMR) |
| other\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other changed files <br/> not listed in the files input <br /> i.e. *a combination of all added, <br /> copied and modified files (ACMR)* |
| any\_modified | `string` | `true` OR `false` | Returns `true` when any <br /> of the filenames provided using <br /> the `files` input has been modified. <br /> i.e. *using a combination of all added, <br />copied, modified, renamed, and deleted files (ACMRD)* |
| only\_modified | `string` | `true` OR `false` | Returns `true` when only <br /> files provided using <br /> the `files` input has been modified. (ACMRD) |
| other\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other modified files <br/> not listed in the files input <br /> i.e. *a combination of all added, <br /> copied, modified, and deleted files (ACMRD)* |
| any\_deleted | `string` | `true` OR `false` | Returns `true` when any <br /> of the filenames provided using <br /> the `files` input has been deleted. (D) |
| only\_deleted | `string` | `true` OR `false` | Returns `true` when only <br /> files provided using <br /> the `files` input has been deleted. (D) |
| other\_deleted\_files | `string` | `'new.txt path/to/file.png ...'` | Select all other deleted files <br/> not listed in the files input <br /> i.e. *a combination of all deleted files (D)* |
| all\_changed\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed files <br /> i.e. *a combination of all added, <br />copied, modified and renamed files (ACMR)* |
| all\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed files <br /> i.e. *a combination of all added, <br />copied, modified, renamed and deleted files (ACMRD)* |
| all\_changed\_and\_modified\_files | `string` | `'new.txt path/to/file.png ...'` | Select all changed <br /> and modified files <br /> 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) <br /> or a repo scoped <br /> [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 <br> using only these <br> list of file(s) <br> (Defaults to the <br> entire repo) |
| files\_separator | string | `false` | `'\n'` | Separator used to split the<br>`files` input |
| files\_from\_source\_file | `string` | `false` | | Source file(s) <br> used to populate <br> the `files` input |
@ -138,9 +141,9 @@ Support this project with a :star:
| sha | `string` | `true` | `${{ github.sha }}` | Specify a different <br> commit SHA <br> used for <br> comparing changes |
| base\_sha | `string` | `false` | | Specify a different <br> base commit SHA <br> used for <br> comparing changes |
| path | `string` | `false` | | Relative path under <br> `GITHUB_WORKSPACE` <br> to the repository |
| since\_last\_remote\_commit | `string` | `false` | `false` | Use the last commit on the remote <br> branch as the `base_sha` <br> (Defaults to the last commit <br> on the target branch for Pull requests <br> or the previous commit <br> on the current branch <br> for push events). <br /> NOTE: This requires <br /> `fetch-depth: 0` <br /> with `actions/checkout@v2` |
| use\_fork\_point | `string` | `false` | `false` | Finds best common ancestor <br /> between two commits <br /> to use in a three-way merge <br /> as the `base_sha` <br /> See: [git merge-base](https://git-scm.com/docs/git-merge-base#Documentation/git-merge-base.txt---fork-point). <br> 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 <br> branch as the `base_sha` <br> (Defaults to the last commit <br> on the target branch for Pull requests <br> or the previous commit <br> on the current branch <br> for push events). <br /> NOTE: This requires <br /> `fetch-depth: 0` <br /> with `actions/checkout@v2` |
| use\_fork\_point | `string` | `false` | `false` | Finds best common ancestor <br /> between two commits <br /> to use in a three-way merge <br /> as the `base_sha` <br /> See: [git merge-base](https://git-scm.com/docs/git-merge-base#Documentation/git-merge-base.txt---fork-point). <br> 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

View file

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

View file

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

1
test/test rename 1.txt Normal file
View file

@ -0,0 +1 @@
This is a test file.

1
test/test rename 2.txt Normal file
View file

@ -0,0 +1 @@
This is a test file.