@@ -11,30 +11,72 @@ BACKPORT_PR=$1 |
11 | 11 | echo "Usage:" >&2 |
12 | 12 | echo 'tools/actions/review_backport.sh https://github.com/nodejs/node/pull/<backport-PR-number> | less' >&2 |
13 | 13 | echo 'DIFF_CMD="codium --wait --diff" tools/actions/review_backport.sh https://github.com/nodejs/node/pull/<backport-PR-number>' >&2 |
14 | | - echo "Limitations: This tools only supports PRs that landed as single commit, e.g. with 'commit-queue-squash' label." >&2 |
15 | 14 |
|
16 | 15 | exit 1 |
17 | 16 | } |
18 | 17 |
|
19 | | -SED_CMD='s/^index [a-f0-9]\+..[a-f0-9]\+ \([0-7]\{6\}\)$/index eeeeeeeeee..eeeeeeeeee \1/g;s/^@@ -[0-9]\+,[0-9]\+ +[0-9]\+,[0-9]\+ @@/@@ -111,1 +111,1 @@/' |
| 18 | +SED_CMD= |
| 19 | +SED_CMD="${SED_CMD};"'s/^index [a-f0-9]\+..[a-f0-9]\+\( [0-7]\{6\}\)\?$/index eeeeeeeeee..eeeeeeeeee\1/g' |
| 20 | +SED_CMD="${SED_CMD};"'s/^From [a-f0-9]\{40\} /From 0000000000000000000000000000000000000000 /' |
| 21 | +SED_CMD="${SED_CMD};"'s/^@@ -[0-9]\+,[0-9]\+ +[0-9]\+,[0-9]\+ @@/@@ -111,1 +111,1 @@/' |
| 22 | +SED_CMD="${SED_CMD};"'s#^\(Subject: \[PATCH [0-9]\+\)/[0-9]\+]#\1]#' |
20 | 23 |
|
21 | 24 | set -ex |
22 | 25 |
|
23 | | -BACKPORT_PR_URL=$(gh pr view "$BACKPORT_PR" --json url --jq .url) |
| 26 | +REMOTE=${GIT_REMOTE:-https://github.com/${GH_REPO:-nodejs/node}.git} |
24 | 27 |
|
25 | | -ORIGINAL=$(mktemp) |
26 | | -BACKPORT=$(mktemp) |
27 | | -trap 'set -x; rm -f "$ORIGINAL" "$BACKPORT"; set +x; trap - EXIT; exit' EXIT INT HUP |
| 28 | +case "$BACKPORT_PR" in |
| 29 | + (https://github.com/*/pull/[0-9]*) BACKPORT_PR_URL=$BACKPORT_PR ;; |
| 30 | + (*) BACKPORT_PR_URL=$(gh pr view "$BACKPORT_PR" --json url --jq .url) ;; |
| 31 | +esac |
28 | 32 |
|
29 | | -gh pr view "$BACKPORT_PR" --json commits --jq '.[] | map([ .oid, (.messageBody | match("(?:^|\\n)PR-URL: (https?://.+/pull/\\d+)(?:\\n|$)", "g") | .captures | last | .string)] | @tsv) | .[]' \ |
30 | | -| while read -r LINE; do |
31 | | - COMMIT_SHA=$(echo "$LINE" | cut -f1) |
32 | | - PR_URL=$(echo "$LINE" | cut -f2) |
| 33 | +WORKDIR=$(mktemp -d) |
| 34 | +trap 'set -x; rm -rf "$WORKDIR"; set +x; trap - EXIT; exit' EXIT INT HUP |
| 35 | +ORIGINAL=$WORKDIR/original.patch |
| 36 | +BACKPORT=$WORKDIR/backport.patch |
| 37 | +BUFFER=$WORKDIR/buffer.patch |
33 | 38 |
|
34 | | - curl -fsL "$PR_URL.diff" | sed "$SED_CMD" >> "$ORIGINAL" |
35 | | - curl -fsL "$BACKPORT_PR_URL/commits/$COMMIT_SHA.diff" | sed "$SED_CMD" >> "$BACKPORT" |
36 | | -done |
| 39 | +curl -fsSLo "$BUFFER" "$BACKPORT_PR_URL.patch?full_index=1" |
| 40 | +sed "$SED_CMD" "$BUFFER" > "$BACKPORT" |
37 | 41 |
|
| 42 | +ORIGINAL_COMMITS=$( |
| 43 | + LAST_FETCHED= |
| 44 | + COMMIT_COUNT=$(git mailsplit -o"$WORKDIR" "$BUFFER") |
| 45 | + i=${COMMIT_COUNT:?No commits found in PR $BACKPORT_PR} |
| 46 | + while [ "$i" -gt 0 ]; do |
| 47 | + MAIL=$WORKDIR/$(printf '%04d' "$i") |
| 48 | + i=$((i - 1)) |
| 49 | +
|
| 50 | + read -r _ COMMIT_SHA _ < "$MAIL" |
| 51 | + PR_URL=$(git interpret-trailers --parse "$MAIL" | sed -n '/^PR-URL: /h;$ {x;s/^PR-URL: //p;}') |
| 52 | + [ -n "$PR_URL" ] || { |
| 53 | + echo "Missing PR-URL in commit message for $COMMIT_SHA" >&2 |
| 54 | + continue |
| 55 | + } |
| 56 | +
|
| 57 | + ORIGINAL_BASE=$(gh pr view "$PR_URL" --json baseRefName -q '.baseRefName') |
| 58 | + [ "$ORIGINAL_BASE" = "$LAST_FETCHED" ] || { |
| 59 | + git fetch "$REMOTE" "$ORIGINAL_BASE" |
| 60 | + LAST_FETCHED="$ORIGINAL_BASE" |
| 61 | + } |
| 62 | + COMMIT_TITLE=$(git mailinfo -b --no-scissors /dev/null /dev/null < "$MAIL" | sed -n 's/^Subject: //p') |
| 63 | + GIT_LOG_OUT=$( |
| 64 | + git log FETCH_HEAD --fixed-strings --grep="$COMMIT_TITLE" --format="%H%x09%s%x09%(trailers:key=PR-URL,only,valueonly)" |
| 65 | + ) |
| 66 | + [ -n "$GIT_LOG_OUT" ] || { |
| 67 | + echo "Error: Could not locate landed commit with title '$COMMIT_TITLE' in $REMOTE/$ORIGINAL_BASE" >&2 |
| 68 | + continue |
| 69 | + } |
| 70 | + T=$COMMIT_TITLE P=$PR_URL awk -F'\t' '$2 == ENVIRON["T"] && $3 == ENVIRON["P"] { print $1; exit }' <<EOF |
| 71 | +$GIT_LOG_OUT |
| 72 | +EOF |
| 73 | + done |
| 74 | +) |
| 75 | + |
| 76 | +xargs git format-patch --no-signature --stdout --root --no-walk=unsorted > "$BUFFER" <<EOF |
| 77 | +${ORIGINAL_COMMITS:?Could not find any commit being backported} |
| 78 | +EOF |
| 79 | +sed "$SED_CMD" "$BUFFER" > "$ORIGINAL" |
38 | 80 | ${DIFF_CMD:-diff} "$ORIGINAL" "$BACKPORT" || echo "diff command exited with $?" >&2 |
39 | 81 |
|
40 | 82 | set +x |
|