3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-01-29 13:34:51 +00:00

Switch to using a bash script. (#39)

This commit is contained in:
Tonye Jack 2021-05-01 15:14:25 -04:00 committed by GitHub
parent 1772038cde
commit 6a4e739596
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 103 additions and 93 deletions

View file

@ -17,6 +17,8 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: shellcheck
uses: reviewdog/action-shellcheck@v1
- name: Run changed-files with defaults
id: changed-files
uses: ./

View file

@ -54,99 +54,11 @@ runs:
steps:
- id: changed_files
run: |
echo "Getting head sha..."
if [[ -z $GITHUB_BASE_REF ]]; then
HEAD_SHA=$(git rev-parse HEAD^1 || true)
else
TARGET_BRANCH=${GITHUB_BASE_REF}
git fetch --depth=1 origin ${TARGET_BRANCH}:${TARGET_BRANCH}
HEAD_SHA=$(git rev-parse ${TARGET_BRANCH} || true)
fi
INPUT_FILES="${{ inputs.files }}"
if [[ -z "$INPUT_FILES" ]]; then
echo "Getting diff..."
ADDED=$(git diff --diff-filter=A --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
COPIED=$(git diff --diff-filter=C --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
DELETED=$(git diff --diff-filter=D --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
MODIFIED=$(git diff --diff-filter=M --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
RENAMED=$(git diff --diff-filter=R --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
CHANGED=$(git diff --diff-filter=T --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
UNMERGED=$(git diff --diff-filter=U --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
UNKNOWN=$(git diff --diff-filter=X --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
ALL_CHANGED=$(git diff --diff-filter='*ACDMRTUX' --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
ALL_MODIFIED_FILES=$(git diff --diff-filter='ACM' --name-only "$HEAD_SHA" | tr "\n" "${{ inputs.separator }}" | sed -E 's/(${{ inputs.separator }})$//')
else
ADDED=()
COPIED=()
DELETED=()
MODIFIED=()
RENAMED=()
CHANGED=()
UNMERGED=()
UNKNOWN=()
ALL_CHANGED=()
ALL_MODIFIED_FILES=()
for path in ${INPUT_FILES}
do
echo "Checking for file changes: \"${path}\"..."
ADDED+=$(git diff --diff-filter=A --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
COPIED+=$(git diff --diff-filter=C --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
DELETED+=$(git diff --diff-filter=D --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
MODIFIED+=$(git diff --diff-filter=M --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
RENAMED+=$(git diff --diff-filter=R --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
CHANGED+=$(git diff --diff-filter=T --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
UNMERGED+=$(git diff --diff-filter=U --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
UNKNOWN+=$(git diff --diff-filter=X --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
ALL_CHANGED+=$(git diff --diff-filter='*ACDMRTUX' --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
ALL_MODIFIED_FILES+=$(git diff --diff-filter='ACM' --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s${{ inputs.separator }}" || true)
done
ADDED=$(echo "$ADDED" | sed -E 's/(${{ inputs.separator }})$//')
COPIED=$(echo $COPIED | sed -E 's/(${{ inputs.separator }})$//')
DELETED=$(echo "$DELETED" | sed -E 's/(${{ inputs.separator }})$//')
MODIFIED=$(echo "$MODIFIED" | sed -E 's/(${{ inputs.separator }})$//')
RENAMED=$(echo "$RENAMED" | sed -E 's/(${{ inputs.separator }})$//')
CHANGED=$(echo "$CHANGED" | sed -E 's/(${{ inputs.separator }})$//')
UNMERGED=$(echo "$UNMERGED" | sed -E 's/(${{ inputs.separator }})$//')
UNKNOWN=$(echo "$UNKNOWN" | sed -E 's/(${{ inputs.separator }})$//')
ALL_CHANGED=$(echo "$ALL_CHANGED" | sed -E 's/(${{ inputs.separator }})$//')
ALL_MODIFIED_FILES=$(echo "$ALL_MODIFIED_FILES" | sed -E 's/(${{ inputs.separator }})$//')
OUTPUT_ALL_MODIFIED_FILES=$(echo $ALL_MODIFIED_FILES | sed "s/${{ inputs.separator }}/ /g")
ALL_INPUT_FILES=$(echo $INPUT_FILES | sed "s/\n/ /g")
IFS=$' ' SORTED_INPUT_FILES=($(sort <<<"${ALL_INPUT_FILES[*]}"))
IFS=$' ' SORTED_OUTPUT_ALL_MODIFIED_FILES=($(sort <<<"${OUTPUT_ALL_MODIFIED_FILES[*]}"))
if [[ "${SORTED_INPUT_FILES[*]}" == "${SORTED_OUTPUT_ALL_MODIFIED_FILES[*]}" ]]; then
echo "::set-output name=all_changed::true"
else
echo "::set-output name=all_changed::false"
fi
if [[ ${#SORTED_OUTPUT_ALL_MODIFIED_FILES[@]} -gt 0 ]]; then
echo "::set-output name=any_changed::true"
else
echo "::set-output name=any_changed::false"
fi
fi
echo "::set-output name=added_files::$ADDED"
echo "::set-output name=copied_files::$COPIED"
echo "::set-output name=deleted_files::$DELETED"
echo "::set-output name=modified_files::$MODIFIED"
echo "::set-output name=renamed_files::$RENAMED"
echo "::set-output name=changed_files::$CHANGED"
echo "::set-output name=unmerged_files::$UNMERGED"
echo "::set-output name=unknown_files::$UNKNOWN"
echo "::set-output name=all_changed_files::$ALL_CHANGED"
echo "::set-output name=all_modified_files::$ALL_MODIFIED_FILES"
export INPUT_FILES="${{ inputs.files }}"
export INPUT_SEPARATOR="${{ inputs.separator }}"
bash entrypoint.sh
unset INPUT_FILES
unset INPUT_SEPARATOR
shell: bash
branding:

96
entrypoint.sh Normal file
View file

@ -0,0 +1,96 @@
#!/usr/bin/env bash
set -e
echo "Getting head sha..."
if [[ -z $GITHUB_BASE_REF ]]; then
HEAD_SHA=$(git rev-parse HEAD^1 || true)
else
TARGET_BRANCH=${GITHUB_BASE_REF}
git fetch --depth=1 origin "${TARGET_BRANCH}":"${TARGET_BRANCH}"
HEAD_SHA=$(git rev-parse "${TARGET_BRANCH}" || true)
fi
if [[ -z "$INPUT_FILES" ]]; then
echo "Getting diff..."
ADDED=$(git diff --diff-filter=A --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
COPIED=$(git diff --diff-filter=C --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
DELETED=$(git diff --diff-filter=D --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
MODIFIED=$(git diff --diff-filter=M --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
RENAMED=$(git diff --diff-filter=R --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
CHANGED=$(git diff --diff-filter=T --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
UNMERGED=$(git diff --diff-filter=U --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
UNKNOWN=$(git diff --diff-filter=X --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
ALL_CHANGED=$(git diff --diff-filter="*ACDMRTUX" --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
ALL_MODIFIED_FILES=$(git diff --diff-filter="ACM" --name-only "$HEAD_SHA" | tr "\n" "$INPUT_SEPARATOR" | sed -E "s/($INPUT_SEPARATOR)$//")
else
ADDED_ARRAY=()
COPIED_ARRAY=()
DELETED_ARRAY=()
MODIFIED_ARRAY=()
RENAMED_ARRAY=()
CHANGED_ARRAY=()
UNMERGED_ARRAY=()
UNKNOWN_ARRAY=()
ALL_CHANGED_ARRAY=()
ALL_MODIFIED_FILES_ARRAY=()
for path in ${INPUT_FILES}
do
echo "Checking for file changes: \"${path}\"..."
IFS=" " read -r -a ADDED_ARRAY <<< "$(git diff --diff-filter=A --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a COPIED_ARRAY <<< "$(git diff --diff-filter=C --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a DELETED_ARRAY <<< "$(git diff --diff-filter=D --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a MODIFIED_ARRAY <<< "$(git diff --diff-filter=M --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a RENAMED_ARRAY <<< "$(git diff --diff-filter=R --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a CHANGED_ARRAY <<< "$(git diff --diff-filter=T --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a UNMERGED_ARRAY <<< "$(git diff --diff-filter=U --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a UNKNOWN_ARRAY <<< "$(git diff --diff-filter=X --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a ALL_CHANGED_ARRAY <<< "$(git diff --diff-filter="*ACDMRTUX" --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
IFS=" " read -r -a ALL_MODIFIED_FILES_ARRAY <<< "$(git diff --diff-filter="ACM" --name-only "$HEAD_SHA" | grep -E "(${path})" | xargs printf "%s$INPUT_SEPARATOR" || true)"
done
ADDED=$(echo "${ADDED_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
COPIED=$(echo "${COPIED_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
DELETED=$(echo "${DELETED_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
MODIFIED=$(echo "${MODIFIED_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
RENAMED=$(echo "${RENAMED_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
CHANGED=$(echo "${CHANGED_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
UNMERGED=$(echo "${UNMERGED_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
UNKNOWN=$(echo "${UNKNOWN_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
ALL_CHANGED=$(echo "${ALL_CHANGED_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
ALL_MODIFIED_FILES=$(echo "${ALL_MODIFIED_FILES_ARRAY[@]}" | sed -E "s/($INPUT_SEPARATOR)$//")
# shellcheck disable=SC2001
OUTPUT_ALL_MODIFIED_FILES=$(echo "$ALL_MODIFIED_FILES" | sed "s/$INPUT_SEPARATOR/ /g")
ALL_INPUT_FILES=${INPUT_FILES//\n/ }
SORTED_INPUT_FILES=()
SORTED_OUTPUT_ALL_MODIFIED_FILES=()
IFS=" " read -r -a SORTED_INPUT_FILES <<< "$(sort <<<"${ALL_INPUT_FILES[*]}")"
IFS=" " read -r -a SORTED_OUTPUT_ALL_MODIFIED_FILES <<< "$(sort <<<"${OUTPUT_ALL_MODIFIED_FILES[*]}")"
if [[ "${SORTED_INPUT_FILES[*]}" == "${SORTED_OUTPUT_ALL_MODIFIED_FILES[*]}" ]]; then
echo "::set-output name=all_changed::true"
else
echo "::set-output name=all_changed::false"
fi
if [[ ${#SORTED_OUTPUT_ALL_MODIFIED_FILES[@]} -gt 0 ]]; then
echo "::set-output name=any_changed::true"
else
echo "::set-output name=any_changed::false"
fi
fi
echo "::set-output name=added_files::$ADDED"
echo "::set-output name=copied_files::$COPIED"
echo "::set-output name=deleted_files::$DELETED"
echo "::set-output name=modified_files::$MODIFIED"
echo "::set-output name=renamed_files::$RENAMED"
echo "::set-output name=changed_files::$CHANGED"
echo "::set-output name=unmerged_files::$UNMERGED"
echo "::set-output name=unknown_files::$UNKNOWN"
echo "::set-output name=all_changed_files::$ALL_CHANGED"
echo "::set-output name=all_modified_files::$ALL_MODIFIED_FILES"