From 08d0cedfeaebcc4aaf7748d120e9ed3258d8487d Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Wed, 23 Nov 2022 17:25:26 -0700 Subject: [PATCH] feat: add support for dir_names_max_depth --- action.yml | 4 ++++ get-changed-paths.sh | 29 +++++++++++++++++++++++++++-- test/test/test.txt | 2 +- test/test2/test.txt | 1 + 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 test/test2/test.txt diff --git a/action.yml b/action.yml index 160846a6..89e4d66a 100644 --- a/action.yml +++ b/action.yml @@ -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 }} diff --git a/get-changed-paths.sh b/get-changed-paths.sh index 265ac711..e593ecfc 100755 --- a/get-changed-paths.sh +++ b/get-changed-paths.sh @@ -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" diff --git a/test/test/test.txt b/test/test/test.txt index 6de7b8c6..9f4b6d8b 100644 --- a/test/test/test.txt +++ b/test/test/test.txt @@ -1 +1 @@ -This is a test file. +This is a test file diff --git a/test/test2/test.txt b/test/test2/test.txt new file mode 100644 index 00000000..570d0dbf --- /dev/null +++ b/test/test2/test.txt @@ -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. \ No newline at end of file