Skip to content

Commit

Permalink
fix: bug with adding separator to output (#360)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
jackton1 and github-actions[bot] committed Jan 4, 2024
1 parent 2a93ea6 commit 4321d85
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -18,59 +18,56 @@ if [[ "$INPUT_MATCH_GITIGNORE_FILES" == "true" ]]; then
fi

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; } | 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)$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; } | 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; } | 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)$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)$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 | awk -v d="|" '{s=(NR==1?s:s d)$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

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"
local filenames=""

for files in "$@"; do
if [[ -n $files ]]; then
if [[ -n $filenames ]]; then
filenames+="$separator$files"
else
result="$filename"
filenames="$files"
fi
fi
done
echo "$result"
}

# Concatenate non-empty strings with a '|' separator
CHANGED_FILES=$(concatenate "|" "$TRACKED_FILES" "$UNTRACKED_OR_IGNORED_FILES" "$UNSTAGED_DELETED_FILES")
filenames=$(echo "$filenames" | tr "$separator" '\n' | sort -u | tr '\n' "$separator")
filenames=${filenames%"$separator"} # Remove trailing separator

echo "$filenames"
}

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}')
Expand Down

0 comments on commit 4321d85

Please sign in to comment.