From 907063a05728b3c2d77732427ac0417787a2a0e1 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:15:49 -0700 Subject: [PATCH 1/9] fix: bug with adding separator to output --- entrypoint.sh | 71 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index f580faa..c68cc64 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -17,60 +17,73 @@ if [[ "$INPUT_MATCH_GITIGNORE_FILES" == "true" ]]; then GIT_STATUS_EXTRA_ARGS+=" --ignored" fi +function sanitize() { + local filename=$1 + if [[ "$INPUT_SAFE_OUTPUT" == "true" ]]; then + filename=${filename//$/\\$} # Replace $ with \$ + filename=${filename//\(/\\\(} # Replace ( with \( + filename=${filename//\)/\\\)} # Replace ) with \) + filename=${filename//\`/\\\`} # Replace ` with \` + filename=${filename//|/\\|} # Replace | with \| + filename=${filename//&/\\&} # Replace & with \& + filename=${filename//;/\\;} # Replace ; with \; + fi + echo "$filename" +} + if [[ -n "$INPUT_FILES_PATTERN_FILE" ]]; then - TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') # Find untracked changes # shellcheck disable=SC2086 - UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') # Find unstaged deleted files - UNSTAGED_DELETED_FILES=$(git ls-files --deleted | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + UNSTAGED_DELETED_FILES=$(git ls-files --deleted | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') else - TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') # Find untracked changes # shellcheck disable=SC2086 - UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') # Find unstaged deleted files - UNSTAGED_DELETED_FILES=$(git ls-files --deleted | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + UNSTAGED_DELETED_FILES=$(git ls-files --deleted | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') fi -CHANGED_FILES="" +echo "::debug::Tracked changed files: $TRACKED_FILES" +echo "::debug::Untracked/Ignored changed files: $UNTRACKED_OR_IGNORED_FILES" +echo "::debug::Unstaged changed files: $UNSTAGED_DELETED_FILES" -# Function to concatenate non-empty file names with a separator -concatenate() { +# Function to concatenate unique filenames with a specified separator +function concatenate_unique_filenames() { local separator=$1 shift local result="" - for filename in "$@"; do - if [[ "$INPUT_SAFE_OUTPUT" == "true" ]]; then - filename=${filename//$/\\$} # Replace $ with \$ - filename=${filename//\(/\\\(} # Replace ( with \( - filename=${filename//\)/\\\)} # Replace ) with \) - filename=${filename//\`/\\\`} # Replace ` with \` - filename=${filename//|/\\|} # Replace | with \| - filename=${filename//&/\\&} # Replace & with \& - filename=${filename//;/\\;} # Replace ; with \; - fi - if [[ -n $filename ]]; then - if [[ -n $result ]]; then - result+="$separator$filename" - else - result="$filename" + declare -A seen + + for files in "$@"; do + IFS="$separator" read -ra filenames <<< "$files" + for filename in "${filenames[@]}"; do + if [[ -n $filename ]] && [[ -z ${seen[$filename]} ]]; then + seen[$filename]=1 + if [[ -n $result ]]; then + result+="$separator$filename" + else + result="$filename" + fi fi - fi + done done + echo "$result" } -# Concatenate non-empty strings with a '|' separator -CHANGED_FILES=$(concatenate "|" "$TRACKED_FILES" "$UNTRACKED_OR_IGNORED_FILES" "$UNSTAGED_DELETED_FILES") - -CHANGED_FILES=$(echo "$CHANGED_FILES" | awk '{gsub(/\|/,"\n"); print $0;}' | sort -u | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') +# Concatenate non-empty strings with a '|' separator and Remove duplicate entries +CHANGED_FILES=$(concatenate_unique_filenames "|" "$TRACKED_FILES" "$UNTRACKED_OR_IGNORED_FILES" "$UNSTAGED_DELETED_FILES") if [[ -n "$CHANGED_FILES" ]]; then + echo "::debug::Changed files: $CHANGED_FILES" echo "Found uncommitted changes" CHANGED_FILES=$(echo "$CHANGED_FILES" | awk '{gsub(/\|/,"\n"); print $0;}' | awk -v d="$INPUT_SEPARATOR" '{s=(NR==1?s:s d)$0}END{print s}') From efdb01e61b8f35a0b53aea4a0eed78852313f4dd Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:21:39 -0700 Subject: [PATCH 2/9] Update entrypoint.sh --- entrypoint.sh | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index c68cc64..8c0aa45 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -17,38 +17,24 @@ if [[ "$INPUT_MATCH_GITIGNORE_FILES" == "true" ]]; then GIT_STATUS_EXTRA_ARGS+=" --ignored" fi -function sanitize() { - local filename=$1 - if [[ "$INPUT_SAFE_OUTPUT" == "true" ]]; then - filename=${filename//$/\\$} # Replace $ with \$ - filename=${filename//\(/\\\(} # Replace ( with \( - filename=${filename//\)/\\\)} # Replace ) with \) - filename=${filename//\`/\\\`} # Replace ` with \` - filename=${filename//|/\\|} # Replace | with \| - filename=${filename//&/\\&} # Replace & with \& - filename=${filename//;/\\;} # Replace ; with \; - fi - echo "$filename" -} - if [[ -n "$INPUT_FILES_PATTERN_FILE" ]]; then - TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') + TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') # Find untracked changes # shellcheck disable=SC2086 - UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') + UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') # Find unstaged deleted files - UNSTAGED_DELETED_FILES=$(git ls-files --deleted | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') + UNSTAGED_DELETED_FILES=$(git ls-files --deleted | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') else - TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') + TRACKED_FILES=$(git diff --diff-filter=ACMUXTR --name-only | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') # Find untracked changes # shellcheck disable=SC2086 - UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') + UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') # Find unstaged deleted files - UNSTAGED_DELETED_FILES=$(git ls-files --deleted | awk -v d="|" '{s=(NR==1?s:s d)sanitize($0)}END{print s}') + UNSTAGED_DELETED_FILES=$(git ls-files --deleted | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') fi echo "::debug::Tracked changed files: $TRACKED_FILES" From 1127b38586995d6a3545eb6ae09bfe3bcd6c5c37 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:24:43 -0700 Subject: [PATCH 3/9] Update entrypoint.sh --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 8c0aa45..ef394ff 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -51,7 +51,7 @@ function concatenate_unique_filenames() { for files in "$@"; do IFS="$separator" read -ra filenames <<< "$files" for filename in "${filenames[@]}"; do - if [[ -n $filename ]] && [[ -z ${seen[$filename]} ]]; then + if [[ -n $filename ]] && [[ -z ${seen[$filename]-} ]]; then seen[$filename]=1 if [[ -n $result ]]; then result+="$separator$filename" From d7231bf482390fbaa53e2cbeaabb016d0d864eb5 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:32:57 -0700 Subject: [PATCH 4/9] Update entrypoint.sh --- entrypoint.sh | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index ef394ff..66dfc1f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -45,24 +45,22 @@ echo "::debug::Unstaged changed files: $UNSTAGED_DELETED_FILES" function concatenate_unique_filenames() { local separator=$1 shift - local result="" - declare -A seen - + local filenames="" + for files in "$@"; do - IFS="$separator" read -ra filenames <<< "$files" - for filename in "${filenames[@]}"; do - if [[ -n $filename ]] && [[ -z ${seen[$filename]-} ]]; then - seen[$filename]=1 - if [[ -n $result ]]; then - result+="$separator$filename" - else - result="$filename" - fi + if [[ -n $files ]]; then + if [[ -n $filenames ]]; then + filenames+="$separator$files" + else + filenames="$files" fi - done + fi done - echo "$result" + filenames=$(echo "$filenames" | tr "$separator" '\n' | sort -u | tr '\n' "$separator") + filenames=${filenames%$separator} # Remove trailing separator + + echo "$filenames" } # Concatenate non-empty strings with a '|' separator and Remove duplicate entries From 365df37c8051128f35f2284d2695a8399e16026d Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:33:24 -0700 Subject: [PATCH 5/9] Update entrypoint.sh Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 66dfc1f..e17c98b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -58,7 +58,7 @@ function concatenate_unique_filenames() { done filenames=$(echo "$filenames" | tr "$separator" '\n' | sort -u | tr '\n' "$separator") - filenames=${filenames%$separator} # Remove trailing separator + filenames=${filenames%"$separator"} # Remove trailing separator echo "$filenames" } From 2d5fb961fb2457392d3921f39a759dfae1f6d982 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:50:54 -0700 Subject: [PATCH 6/9] Update entrypoint.sh --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index e17c98b..9a87721 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -22,7 +22,7 @@ if [[ -n "$INPUT_FILES_PATTERN_FILE" ]]; then # Find untracked changes # shellcheck disable=SC2086 - UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | grep '??\|!!' | awk '{print $2}' | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') # Find unstaged deleted files UNSTAGED_DELETED_FILES=$(git ls-files --deleted | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') @@ -31,7 +31,7 @@ else # Find untracked changes # shellcheck disable=SC2086 - UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '{print $NF}' | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | grep '??\|!!' | awk '{print $2}' | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') # Find unstaged deleted files UNSTAGED_DELETED_FILES=$(git ls-files --deleted | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') From c566840f3691d74388c6562ea42bb9e64878497f Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:52:20 -0700 Subject: [PATCH 7/9] Update entrypoint.sh --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9a87721..014590a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -set -euo pipefail +set -euxo pipefail INPUT_SEPARATOR="${INPUT_SEPARATOR//\%/%25}" INPUT_SEPARATOR="${INPUT_SEPARATOR//\./%2E}" From 2fb6c5c6ec1f92a087cc9d402c5ea2e0b6f5fd84 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:54:00 -0700 Subject: [PATCH 8/9] Update entrypoint.sh --- entrypoint.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 014590a..3e20393 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -set -euxo pipefail +set -euo pipefail INPUT_SEPARATOR="${INPUT_SEPARATOR//\%/%25}" INPUT_SEPARATOR="${INPUT_SEPARATOR//\./%2E}" @@ -22,7 +22,7 @@ if [[ -n "$INPUT_FILES_PATTERN_FILE" ]]; then # Find untracked changes # shellcheck disable=SC2086 - UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | grep '??\|!!' | awk '{print $2}' | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '/\?\?|!!/ {print $2}' | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') # Find unstaged deleted files UNSTAGED_DELETED_FILES=$(git ls-files --deleted | { grep -x -E -f "$INPUT_FILES_PATTERN_FILE" || true; } | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') @@ -31,7 +31,7 @@ else # Find untracked changes # shellcheck disable=SC2086 - UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | grep '??\|!!' | awk '{print $2}' | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') + UNTRACKED_OR_IGNORED_FILES=$(git status $GIT_STATUS_EXTRA_ARGS | awk '/\?\?|!!/ {print $2}' | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') # Find unstaged deleted files UNSTAGED_DELETED_FILES=$(git ls-files --deleted | perl -pe 's/([$\(\)`|&;])/\\$1/g' | awk -v d="|" '{s=(NR==1?s:s d)$0}END{print s}') From b12351cb33b9f330e5d31ed58f7034cfeb8c95cd Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 3 Jan 2024 21:54:55 -0700 Subject: [PATCH 9/9] Update entrypoint.sh --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 3e20393..1d6748b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -9,7 +9,7 @@ INPUT_SEPARATOR="${INPUT_SEPARATOR//\\r/%0D}" echo "::group::verify-changed-files" -echo "Separator: $INPUT_SEPARATOR" +echo "::debug::Separator: $INPUT_SEPARATOR" GIT_STATUS_EXTRA_ARGS="-u --porcelain"