|
52 | 52 |
|
53 | 53 | echo -e "${YELLOW}Backporting commit ${COMMIT_SHA} from ${SOURCE_REPO}${NC}" |
54 | 54 |
|
| 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 | + |
55 | 71 | if ! git cat-file -e "${COMMIT_SHA}^{commit}" 2>/dev/null; then |
56 | 72 | die "Commit $COMMIT_SHA is not available locally - fetch upstream before running this script" |
57 | 73 | fi |
@@ -110,6 +126,7 @@ if ! git push -u origin "$BRANCH_NAME"; then |
110 | 126 | git push -u origin "$BRANCH_NAME" --force-with-lease || { |
111 | 127 | git checkout "$TARGET_BRANCH" |
112 | 128 | git branch -D "$BRANCH_NAME" || true |
| 129 | + restore_original_ref |
113 | 130 | die "Unable to push branch ${BRANCH_NAME}" |
114 | 131 | } |
115 | 132 | fi |
@@ -169,17 +186,18 @@ if [[ $PR_EXIT_CODE -ne 0 ]]; then |
169 | 186 | if grep -q "already exists" <<<"$PR_RESPONSE"; then |
170 | 187 | echo -e "${YELLOW}Detected existing PR, assuming success.${NC}" |
171 | 188 | git checkout "$TARGET_BRANCH" |
| 189 | + restore_original_ref |
172 | 190 | exit 0 |
173 | 191 | fi |
174 | 192 | git checkout "$TARGET_BRANCH" |
175 | 193 | git push origin --delete "$BRANCH_NAME" || true |
176 | 194 | git branch -D "$BRANCH_NAME" || true |
| 195 | + restore_original_ref |
177 | 196 | die "PR creation failed" |
178 | 197 | fi |
179 | 198 |
|
180 | 199 | echo -e "${GREEN}Pull request created successfully:${NC} ${PR_RESPONSE}" |
181 | 200 |
|
182 | | -git checkout "$TARGET_BRANCH" |
| 201 | +restore_original_ref |
183 | 202 |
|
184 | 203 | echo -e "${GREEN}Backport finished for ${COMMIT_SHA}.${NC}" |
185 | | - |
|
0 commit comments