feat: add support for dir_names_max_depth

This commit is contained in:
Tonye Jack 2022-11-23 17:25:26 -07:00
parent ba788ed4ee
commit 08d0cedfea
4 changed files with 33 additions and 3 deletions

View file

@ -73,6 +73,9 @@ inputs:
default: "false"
description: "Output unique changed directories instead of filenames. **NOTE:** This returns `.` for changed files located in the root of the project."
required: false
dir_names_max_depth:
description: "Maximum depth of directories to output."
required: false
json:
description: "Output list of changed files in a JSON formatted string which can be used for matrix jobs."
required: false
@ -221,6 +224,7 @@ runs:
INPUT_OLD_NEW_FILES_SEPARATOR: ${{ inputs.old_new_files_separator }}
INPUT_DIFF_RELATIVE: ${{ inputs.diff_relative }}
INPUT_DIR_NAMES: ${{ inputs.dir_names }}
INPUT_DIR_NAMES_MAX_DEPTH: ${{ inputs.dir_names_max_depth }}
INPUT_JSON: ${{ inputs.json }}
INPUT_HAS_CUSTOM_PATTERNS: ${{ steps.glob.outputs.has-custom-patterns }}

View file

@ -24,6 +24,31 @@ if [[ -n $INPUT_DIFF_RELATIVE ]]; then
git config --global diff.relative "$INPUT_DIFF_RELATIVE"
fi
function dirname_max_depth() {
local dir="$1"
local dirs=($(echo "$dir" | tr "/" " "))
local max_depth="${#dirs[@]}"
if [[ -n "$INPUT_DIR_NAMES_MAX_DEPTH" && "$INPUT_DIR_NAMES_MAX_DEPTH" -lt "$max_depth" ]]; then
max_depth="$INPUT_DIR_NAMES_MAX_DEPTH"
fi
local output=${dirs[1]}
local depth=2
while [ $depth -le $max_depth ]; do
if [[ -n "${dirs[$depth]}" ]]; then
output="$output/${dirs[$depth]}"
else
break
fi
depth=$((depth+1))
done
echo "$output"
}
function get_diff() {
local base="$1"
local sha="$2"
@ -53,7 +78,7 @@ function get_diff() {
done < <(git submodule | awk '{print $2}')
if [[ "$INPUT_DIR_NAMES" == "true" ]]; then
git diff --diff-filter="$filter" --name-only --ignore-submodules=all "$base$DIFF$sha" | xargs -I {} dirname {} | uniq && exit_status=$? || exit_status=$?
git diff --diff-filter="$filter" --name-only --ignore-submodules=all "$base$DIFF$sha" | xargs -I {} dirname {} | dirname_max_depth | uniq && exit_status=$? || exit_status=$?
if [[ $exit_status -ne 0 ]]; then
echo "::error::Failed to get changed directories between: $base$DIFF$sha"
@ -97,7 +122,7 @@ function get_renames() {
done < <(git submodule | awk '{print $2}')
if [[ "$INPUT_DIR_NAMES" == "true" ]]; then
git log --name-status --ignore-submodules=all "$base" "$sha" | { grep -E "^R" || true; } | awk -F '\t' -v d="$INPUT_OLD_NEW_SEPARATOR" '{print $2d$3}' | xargs -I {} dirname {} | uniq && exit_status=$? || exit_status=$?
git log --name-status --ignore-submodules=all "$base" "$sha" | { grep -E "^R" || true; } | awk -F '\t' -v d="$INPUT_OLD_NEW_SEPARATOR" '{print $2d$3}' | xargs -I {} dirname {} | dirname_max_depth | uniq && exit_status=$? || exit_status=$?
if [[ $exit_status -ne 0 ]]; then
echo "::error::Failed to get renamed directories between: $base$sha"

View file

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

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

@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adip eget, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.