Skip to content

Commit 235683b

Browse files
committed
fix: r
Signed-off-by: Ashing Zheng <[email protected]>
1 parent c8c87bb commit 235683b

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

.github/scripts/backport-commit.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ fi
5252

5353
echo -e "${YELLOW}Backporting commit ${COMMIT_SHA} from ${SOURCE_REPO}${NC}"
5454

55+
ORIGINAL_REF=""
56+
ORIGINAL_COMMIT=""
57+
if ORIGINAL_REF=$(git symbolic-ref --quiet HEAD 2>/dev/null); then
58+
ORIGINAL_REF=${ORIGINAL_REF#refs/heads/}
59+
else
60+
ORIGINAL_COMMIT=$(git rev-parse HEAD)
61+
fi
62+
63+
restore_original_ref() {
64+
if [[ -n "$ORIGINAL_REF" ]]; then
65+
git checkout "$ORIGINAL_REF" >/dev/null 2>&1 || true
66+
elif [[ -n "$ORIGINAL_COMMIT" ]]; then
67+
git checkout --detach "$ORIGINAL_COMMIT" >/dev/null 2>&1 || true
68+
fi
69+
}
70+
5571
if ! git cat-file -e "${COMMIT_SHA}^{commit}" 2>/dev/null; then
5672
die "Commit $COMMIT_SHA is not available locally - fetch upstream before running this script"
5773
fi
@@ -110,6 +126,7 @@ if ! git push -u origin "$BRANCH_NAME"; then
110126
git push -u origin "$BRANCH_NAME" --force-with-lease || {
111127
git checkout "$TARGET_BRANCH"
112128
git branch -D "$BRANCH_NAME" || true
129+
restore_original_ref
113130
die "Unable to push branch ${BRANCH_NAME}"
114131
}
115132
fi
@@ -169,17 +186,18 @@ if [[ $PR_EXIT_CODE -ne 0 ]]; then
169186
if grep -q "already exists" <<<"$PR_RESPONSE"; then
170187
echo -e "${YELLOW}Detected existing PR, assuming success.${NC}"
171188
git checkout "$TARGET_BRANCH"
189+
restore_original_ref
172190
exit 0
173191
fi
174192
git checkout "$TARGET_BRANCH"
175193
git push origin --delete "$BRANCH_NAME" || true
176194
git branch -D "$BRANCH_NAME" || true
195+
restore_original_ref
177196
die "PR creation failed"
178197
fi
179198

180199
echo -e "${GREEN}Pull request created successfully:${NC} ${PR_RESPONSE}"
181200

182-
git checkout "$TARGET_BRANCH"
201+
restore_original_ref
183202

184203
echo -e "${GREEN}Backport finished for ${COMMIT_SHA}.${NC}"
185-

0 commit comments

Comments
 (0)