@@ -10,6 +10,7 @@ ARTIFACTS=()
1010PUSH_FAILED=" "
1111TERMINATED=false
1212COMMIT_ID=" "
13+ TARGET_BRANCH_IS_NEW=false
1314
1415# Path where x2a-convertor writes error details on failure
1516export X2A_ERROR_FILE=" /tmp/x2a-error.txt"
@@ -148,12 +149,22 @@ Job: ${JOB_ID}
148149
149150Co-Authored-By: ${GIT_AUTHOR_NAME} <${GIT_AUTHOR_EMAIL} >
150151" || true
151- git_target_repo pull --rebase origin " ${TARGET_REPO_BRANCH} " 2> /dev/null || true
152- COMMIT_ID=$( git rev-parse HEAD 2> /dev/null || echo " " )
153- if ! git_target_repo push origin " ${TARGET_REPO_BRANCH} " ; then
154- PUSH_FAILED=" Failed to push to ${TARGET_REPO_URL} branch ${TARGET_REPO_BRANCH} "
155- echo " ERROR: ${PUSH_FAILED} "
152+
153+ if [ " ${TARGET_BRANCH_IS_NEW} " = " true" ]; then
154+ # New branch — no remote tracking branch to rebase against
155+ if ! git_target_repo push -u origin " ${TARGET_REPO_BRANCH} " ; then
156+ PUSH_FAILED=" Failed to push new branch '${TARGET_REPO_BRANCH} ' to ${TARGET_REPO_URL} "
157+ echo " ERROR: ${PUSH_FAILED} "
158+ fi
159+ else
160+ # Existing branch — rebase on remote changes before pushing
161+ git_target_repo pull --rebase origin " ${TARGET_REPO_BRANCH} " 2> /dev/null || true
162+ if ! git_target_repo push origin " ${TARGET_REPO_BRANCH} " ; then
163+ PUSH_FAILED=" Failed to push to ${TARGET_REPO_URL} branch ${TARGET_REPO_BRANCH} "
164+ echo " ERROR: ${PUSH_FAILED} "
165+ fi
156166 fi
167+ COMMIT_ID=$( git rev-parse HEAD 2> /dev/null || echo " " )
157168 fi
158169
159170 if [ " $TERMINATED " = true ]; then
@@ -178,13 +189,14 @@ git_clone_repos() {
178189 if git_target_repo clone --depth=1 --single-branch \
179190 --branch=" ${TARGET_REPO_BRANCH} " " ${TARGET_REPO_URL} " /workspace/target 2> /dev/null; then
180191 # Repo and branch exist — cloned successfully
181- :
192+ TARGET_BRANCH_IS_NEW=false
182193 elif git_target_repo clone --depth=1 \
183194 " ${TARGET_REPO_URL} " /workspace/target 2> /dev/null; then
184195 # Repo exists but branch doesn't — create target branch locally
185196 echo " Branch '${TARGET_REPO_BRANCH} ' not found on remote, creating it"
186197 cd /workspace/target
187198 git checkout -b " ${TARGET_REPO_BRANCH} "
199+ TARGET_BRANCH_IS_NEW=true
188200 else
189201 # Repo doesn't exist or can't be accessed — init empty
190202 echo " Target repo doesn't exist, initializing empty repo"
@@ -193,6 +205,7 @@ git_clone_repos() {
193205 git init
194206 git checkout -b " ${TARGET_REPO_BRANCH} "
195207 git remote add origin " ${TARGET_REPO_URL} "
208+ TARGET_BRANCH_IS_NEW=true
196209 fi
197210
198211 ERROR_MESSAGE=" "
0 commit comments