diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9e252e6d..f1c232f7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -334,6 +334,41 @@ jobs: echo "Expected: (failure) got ${{ steps.changed-files-specific.outcome }}" exit 1 + test-submodules: + name: Test changed-files with submodule + runs-on: ${{ matrix.platform }} + strategy: + fail-fast: false + max-parallel: 4 + matrix: + platform: [ubuntu-latest] + + steps: + - name: Checkout to branch + uses: actions/checkout@v3 + with: + submodules: recursive + + - name: Run changed-files with submodule + id: changed-files + uses: ./ + with: + base_sha: "85bd869" + sha: "adde7bb" + fetch_depth: 60000 + + - name: Verify added files + if: steps.changed-files.outputs.added_files != 'test/demo/test/test.txt' + run: | + echo "Expected: (test/demo/test/test.txt) got ${{ steps.changed-files.outputs.added_files }}" + exit 1 + + - name: Show output + run: | + echo "${{ toJSON(steps.changed-files.outputs) }}" + shell: + bash + test: name: Test changed-files runs-on: ${{ matrix.platform }} diff --git a/diff-sha.sh b/diff-sha.sh index 1f3e370d..f8827ae1 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -4,7 +4,7 @@ set -euo pipefail INITIAL_COMMIT="false" GITHUB_OUTPUT=${GITHUB_OUTPUT:-""} -EXTRA_ARGS="--no-tags --prune --no-recurse-submodules" +EXTRA_ARGS="--no-tags --prune --recurse-submodules" PREVIOUS_SHA="" CURRENT_SHA="" DIFF="..." @@ -60,7 +60,9 @@ if [[ -z $GITHUB_EVENT_PULL_REQUEST_BASE_REF ]]; then if [[ -f .git/shallow ]]; then echo "Fetching remote refs..." # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH" 1>/dev/null || true + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$CURRENT_BRANCH":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null + # shellcheck disable=SC2086 + git submodule foreach git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" || true fi echo "::debug::Getting HEAD SHA..." @@ -218,7 +220,7 @@ else # Fetch more of the target branch history until the merge base is found for i in {1..10}; do # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$TARGET_BRANCH:$TARGET_BRANCH" 1>/dev/null + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" 1>/dev/null if git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null 2>&1; then break fi diff --git a/get-changed-paths.sh b/get-changed-paths.sh index 5cf7fd2f..94fe149e 100755 --- a/get-changed-paths.sh +++ b/get-changed-paths.sh @@ -56,13 +56,13 @@ function get_dirname_max_depth() { } function json_output() { - JQ_ARGS="-sR" + local jq_args="-sR" if [[ "$INPUT_JSON_RAW_FORMAT" == "true" ]]; then - JQ_ARGS="$JQ_ARGS -r" + jq_args="$jq_args -r" fi # shellcheck disable=SC2086 - jq $JQ_ARGS 'split("\n") | map(select(. != "")) | @json' | sed -r 's/^"|"$//g' | tr -s / + jq $jq_args 'split("\n") | map(select(. != "")) | @json' | sed -r 's/^"|"$//g' | tr -s / } function get_diff() { @@ -71,33 +71,33 @@ function get_diff() { local filter="$3" while IFS='' read -r sub; do - sub_commit_pre="$(git diff "$base$DIFF$sha" -- "$sub" | { grep '^[-]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$? + sub_commit_pre="$(git diff "$base" "$sha" -- "$sub" | { grep '^[-]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$? if [[ $exit_status -ne 0 ]]; then - echo "::error::Failed to get previous commit for submodule ($sub) between: $base$DIFF$sha" - exit 1 + echo "::warning::Failed to get previous commit for submodule ($sub) between: $base $sha. Please ensure that submodules are initialized and up to date. See: https://github.com/actions/checkout#usage" >&2 fi - sub_commit_cur="$(git diff "$base$DIFF$sha" -- "$sub" | { grep '^[+]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$? + sub_commit_cur="$(git diff "$base" "$sha" -- "$sub" | { grep '^[+]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$? if [[ $exit_status -ne 0 ]]; then - echo "::error::Failed to get current commit for submodule ($sub) between: $base$DIFF$sha" - exit 1 + echo "::warning::Failed to get current commit for submodule ($sub) between: $base $sha. Please ensure that submodules are initialized and up to date. See: https://github.com/actions/checkout#usage" >&2 fi if [ -n "$sub_commit_cur" ]; then ( cd "$sub" && ( # the strange magic number is a hardcoded "empty tree" commit sha - get_diff "${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904}" "${sub_commit_cur}" "$filter" | awk -v r="$sub" '{ print "" r "/" $0}' + git diff --diff-filter="$filter" --name-only --ignore-submodules=all "${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904}" "${sub_commit_cur}" | awk -v r="$sub" '{ print "" r "/" $0}' 2>/dev/null ) - ) + ) || { + echo "::warning::Failed to get changed files for submodule ($sub) between: ${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904} ${sub_commit_cur}. Please ensure that submodules are initialized and up to date. See: https://github.com/actions/checkout#usage" >&2 + } fi done < <(git submodule | awk '{print $2}') - git diff --diff-filter="$filter" --name-only --ignore-submodules=all --no-merges "$base$DIFF$sha" && exit_status=$? || exit_status=$? + git diff --diff-filter="$filter" --name-only --ignore-submodules=all "$base$DIFF$sha" && exit_status=$? || exit_status=$? if [[ $exit_status -ne 0 ]]; then - echo "::error::Failed to get changed files between: $base$DIFF$sha" - exit 1 + echo "::error::Failed to get changed files between: $base$DIFF$sha" >&2 + return 1 fi } @@ -106,33 +106,33 @@ function get_renames() { local sha="$2" while IFS='' read -r sub; do - sub_commit_pre="$(git diff "$base$DIFF$sha" -- "$sub" | { grep '^[-]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$? + sub_commit_pre="$(git diff "$base" "$sha" -- "$sub" | { grep '^[-]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$? if [[ $exit_status -ne 0 ]]; then - echo "::error::Failed to get previous commit for submodule ($sub) between: $base$DIFF$sha" - exit 1 + echo "::warning::Failed to get previous commit for submodule ($sub) between: $base $sha. Please ensure that submodules are initialized and up to date. See: https://github.com/actions/checkout#usage" >&2 fi - sub_commit_cur="$(git diff "$base$DIFF$sha" -- "$sub" | { grep '^[+]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$? + sub_commit_cur="$(git diff "$base" "$sha" -- "$sub" | { grep '^[+]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$? if [[ $exit_status -ne 0 ]]; then - echo "::error::Failed to get current commit for submodule ($sub) between: $base$DIFF$sha" - exit 1 + echo "::warning::Failed to get current commit for submodule ($sub) between: $base $sha. Please ensure that submodules are initialized and up to date. See: https://github.com/actions/checkout#usage" >&2 fi if [ -n "$sub_commit_cur" ]; then ( cd "$sub" && ( # the strange magic number is a hardcoded "empty tree" commit sha - get_renames "${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904}" "${sub_commit_cur}" | awk -v r="$sub" '{ print "" r "/" $0}' + git log --name-status --ignore-submodules=all "${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904}" "${sub_commit_cur}" | { grep -E "^R" || true; } | awk -F '\t' -v d="$INPUT_OLD_NEW_SEPARATOR" '{print $2d$3}' | awk -v r="$sub" '{ print "" r "/" $0}' ) - ) + ) || { + echo "::warning::Failed to get renamed files for submodule ($sub) between: ${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904} ${sub_commit_cur}. Please ensure that submodules are initialized and up to date. See: https://github.com/actions/checkout#usage" >&2 + } fi done < <(git submodule | awk '{print $2}') 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}' && exit_status=$? || exit_status=$? if [[ $exit_status -ne 0 ]]; then - echo "::error::Failed to get renamed files between: $base → $sha" - exit 1 + echo "::error::Failed to get renamed files between: $base → $sha" >&2 + return 1 fi } diff --git a/test/demo b/test/demo index 3b8549eb..83ccca9b 160000 --- a/test/demo +++ b/test/demo @@ -1 +1 @@ -Subproject commit 3b8549eb19fb6d4ff2017cbe27d2cec353cace93 +Subproject commit 83ccca9b633cc41dc1f04e690e4a3a8026a01359