Use git check-ignore instead of a (incomplete) list of ignored files

The list-based method breaks down in case of ignored folders:
If .gitignore contains foo/, foo/bar.py is still checked.

Signed-off-by: Christoph Höger <christoph.hoeger@piano.io>
This commit is contained in:
Christoph Höger 2022-11-18 13:41:42 +01:00 committed by Brett Logan
parent d0002cc016
commit adbf28f42d

View file

@ -228,26 +228,6 @@ function BuildFileList() {
fi
fi
###############################
# Load the ignored files list #
###############################
debug "Loading the files list that Git ignores..."
mapfile -t GIT_IGNORED_FILES < <(git -C "${GITHUB_WORKSPACE}" status --ignored --porcelain=v1 --short --untracked-files=normal | grep '!!' | awk -F ' ' '{print $2}' | sed -e 's#^#'"${GITHUB_WORKSPACE}"/'#' | sort)
debug "GIT_IGNORED_FILES contents: ${GIT_IGNORED_FILES[*]}"
# Build an associative array to avoid looping throug the ignored files list
local i
declare -g -A GIT_IGNORED_FILES_INDEX
for i in "${!GIT_IGNORED_FILES[@]}"; do
eval GIT_IGNORED_FILES_INDEX["${GIT_IGNORED_FILES[$i]}"]="$i"
done
debug "--- GIT_IGNORED_FILES_INDEX contents ---"
debug "-----------------------"
for i in "${!GIT_IGNORED_FILES_INDEX[@]}"; do
debug "key: $i, value: ${GIT_IGNORED_FILES_INDEX[$i]}"
done
debug "---------------------------------------------"
#########################################
# Check if the Ansible directory exists #
#########################################
@ -316,7 +296,7 @@ function BuildFileList() {
###################################################
# Filter files if FILTER_REGEX_EXCLUDE is not set #
###################################################
if [ "${GIT_IGNORED_FILES_INDEX[$FILE]}" ] && [ "${IGNORE_GITIGNORED_FILES}" == "true" ]; then
if git check-ignore "$FILE" && [ "${IGNORE_GITIGNORED_FILES}" == "true" ]; then
debug "${FILE} is ignored by Git. Skipping ${FILE}"
continue
fi