Skip to content

Commit b477871

Browse files
fix: bug getting diff for submodules and fetching more history (#980)
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
1 parent 8efe373 commit b477871

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

.github/workflows/test.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,41 @@ jobs:
334334
echo "Expected: (failure) got ${{ steps.changed-files-specific.outcome }}"
335335
exit 1
336336
337+
test-submodules:
338+
name: Test changed-files with submodule
339+
runs-on: ${{ matrix.platform }}
340+
strategy:
341+
fail-fast: false
342+
max-parallel: 4
343+
matrix:
344+
platform: [ubuntu-latest]
345+
346+
steps:
347+
- name: Checkout to branch
348+
uses: actions/checkout@v3
349+
with:
350+
submodules: recursive
351+
352+
- name: Run changed-files with submodule
353+
id: changed-files
354+
uses: ./
355+
with:
356+
base_sha: "85bd869"
357+
sha: "adde7bb"
358+
fetch_depth: 60000
359+
360+
- name: Verify added files
361+
if: steps.changed-files.outputs.added_files != 'test/demo/test/test.txt'
362+
run: |
363+
echo "Expected: (test/demo/test/test.txt) got ${{ steps.changed-files.outputs.added_files }}"
364+
exit 1
365+
366+
- name: Show output
367+
run: |
368+
echo "${{ toJSON(steps.changed-files.outputs) }}"
369+
shell:
370+
bash
371+
337372
test:
338373
name: Test changed-files
339374
runs-on: ${{ matrix.platform }}

diff-sha.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -euo pipefail
44

55
INITIAL_COMMIT="false"
66
GITHUB_OUTPUT=${GITHUB_OUTPUT:-""}
7-
EXTRA_ARGS="--no-tags --prune --no-recurse-submodules"
7+
EXTRA_ARGS="--no-tags --prune --recurse-submodules"
88
PREVIOUS_SHA=""
99
CURRENT_SHA=""
1010
DIFF="..."
@@ -60,7 +60,9 @@ if [[ -z $GITHUB_EVENT_PULL_REQUEST_BASE_REF ]]; then
6060
if [[ -f .git/shallow ]]; then
6161
echo "Fetching remote refs..."
6262
# shellcheck disable=SC2086
63-
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH" 1>/dev/null || true
63+
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$CURRENT_BRANCH":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null
64+
# shellcheck disable=SC2086
65+
git submodule foreach git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" || true
6466
fi
6567

6668
echo "::debug::Getting HEAD SHA..."
@@ -218,7 +220,7 @@ else
218220
# Fetch more of the target branch history until the merge base is found
219221
for i in {1..10}; do
220222
# shellcheck disable=SC2086
221-
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$TARGET_BRANCH:$TARGET_BRANCH" 1>/dev/null
223+
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" 1>/dev/null
222224
if git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null 2>&1; then
223225
break
224226
fi

get-changed-paths.sh

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ function get_dirname_max_depth() {
5656
}
5757

5858
function json_output() {
59-
JQ_ARGS="-sR"
59+
local jq_args="-sR"
6060
if [[ "$INPUT_JSON_RAW_FORMAT" == "true" ]]; then
61-
JQ_ARGS="$JQ_ARGS -r"
61+
jq_args="$jq_args -r"
6262
fi
6363

6464
# shellcheck disable=SC2086
65-
jq $JQ_ARGS 'split("\n") | map(select(. != "")) | @json' | sed -r 's/^"|"$//g' | tr -s /
65+
jq $jq_args 'split("\n") | map(select(. != "")) | @json' | sed -r 's/^"|"$//g' | tr -s /
6666
}
6767

6868
function get_diff() {
@@ -71,33 +71,33 @@ function get_diff() {
7171
local filter="$3"
7272

7373
while IFS='' read -r sub; do
74-
sub_commit_pre="$(git diff "$base$DIFF$sha" -- "$sub" | { grep '^[-]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$?
74+
sub_commit_pre="$(git diff "$base" "$sha" -- "$sub" | { grep '^[-]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$?
7575
if [[ $exit_status -ne 0 ]]; then
76-
echo "::error::Failed to get previous commit for submodule ($sub) between: $base$DIFF$sha"
77-
exit 1
76+
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
7877
fi
7978

80-
sub_commit_cur="$(git diff "$base$DIFF$sha" -- "$sub" | { grep '^[+]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$?
79+
sub_commit_cur="$(git diff "$base" "$sha" -- "$sub" | { grep '^[+]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$?
8180
if [[ $exit_status -ne 0 ]]; then
82-
echo "::error::Failed to get current commit for submodule ($sub) between: $base$DIFF$sha"
83-
exit 1
81+
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
8482
fi
8583

8684
if [ -n "$sub_commit_cur" ]; then
8785
(
8886
cd "$sub" && (
8987
# the strange magic number is a hardcoded "empty tree" commit sha
90-
get_diff "${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904}" "${sub_commit_cur}" "$filter" | awk -v r="$sub" '{ print "" r "/" $0}'
88+
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
9189
)
92-
)
90+
) || {
91+
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
92+
}
9393
fi
9494
done < <(git submodule | awk '{print $2}')
9595

96-
git diff --diff-filter="$filter" --name-only --ignore-submodules=all --no-merges "$base$DIFF$sha" && exit_status=$? || exit_status=$?
96+
git diff --diff-filter="$filter" --name-only --ignore-submodules=all "$base$DIFF$sha" && exit_status=$? || exit_status=$?
9797

9898
if [[ $exit_status -ne 0 ]]; then
99-
echo "::error::Failed to get changed files between: $base$DIFF$sha"
100-
exit 1
99+
echo "::error::Failed to get changed files between: $base$DIFF$sha" >&2
100+
return 1
101101
fi
102102
}
103103

@@ -106,33 +106,33 @@ function get_renames() {
106106
local sha="$2"
107107

108108
while IFS='' read -r sub; do
109-
sub_commit_pre="$(git diff "$base$DIFF$sha" -- "$sub" | { grep '^[-]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$?
109+
sub_commit_pre="$(git diff "$base" "$sha" -- "$sub" | { grep '^[-]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$?
110110
if [[ $exit_status -ne 0 ]]; then
111-
echo "::error::Failed to get previous commit for submodule ($sub) between: $base$DIFF$sha"
112-
exit 1
111+
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
113112
fi
114113

115-
sub_commit_cur="$(git diff "$base$DIFF$sha" -- "$sub" | { grep '^[+]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$?
114+
sub_commit_cur="$(git diff "$base" "$sha" -- "$sub" | { grep '^[+]Subproject commit' || true; } | awk '{print $3}')" && exit_status=$? || exit_status=$?
116115
if [[ $exit_status -ne 0 ]]; then
117-
echo "::error::Failed to get current commit for submodule ($sub) between: $base$DIFF$sha"
118-
exit 1
116+
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
119117
fi
120118

121119
if [ -n "$sub_commit_cur" ]; then
122120
(
123121
cd "$sub" && (
124122
# the strange magic number is a hardcoded "empty tree" commit sha
125-
get_renames "${sub_commit_pre:-4b825dc642cb6eb9a060e54bf8d69288fbee4904}" "${sub_commit_cur}" | awk -v r="$sub" '{ print "" r "/" $0}'
123+
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}'
126124
)
127-
)
125+
) || {
126+
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
127+
}
128128
fi
129129
done < <(git submodule | awk '{print $2}')
130130

131131
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=$?
132132

133133
if [[ $exit_status -ne 0 ]]; then
134-
echo "::error::Failed to get renamed files between: $base$sha"
135-
exit 1
134+
echo "::error::Failed to get renamed files between: $base$sha" >&2
135+
return 1
136136
fi
137137
}
138138

test/demo

Submodule demo updated 1 file

0 commit comments

Comments
 (0)