@@ -55,58 +55,57 @@ echo -e "\n[INFO] Target branch: ${BRANCH}"
5555
5656# Enhanced branch handling with proper remote synchronization
5757if [[ -n " ${INPUT_TARGET_BRANCH} " || " ${INPUT_ADD_TIMESTAMP} " == " true" ]]; then
58- # Proceed with branch operations if we have a target branch OR timestamp is enabled OR we have changes
59- if [[ -n " ${INPUT_TARGET_BRANCH} " || " ${INPUT_ADD_TIMESTAMP} " == " true" ]]; then
60- # Fetch latest changes from remote
61- echo " [INFO] Fetching latest changes from remote..."
62- git fetch origin || {
63- echo " [WARNING] Could not fetch from remote. Proceeding with local operations."
64- }
58+ # Fetch latest changes from remote
59+ echo " [INFO] Fetching latest changes from remote..."
60+ git fetch origin || {
61+ echo " [WARNING] Could not fetch from remote. Proceeding with local operations."
62+ }
6563
66- # Check if remote branch exists
67- REMOTE_BRANCH_EXISTS=$( git ls-remote --heads origin " ${BRANCH} " 2> /dev/null | wc -l)
64+ # Check if remote branch exists
65+ REMOTE_BRANCH_EXISTS=$( git ls-remote --heads origin " ${BRANCH} " 2> /dev/null | wc -l)
6866
69- MAIN_BRANCH=$( git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null | sed ' s@^refs/remotes/origin/@@' || echo " main" )
70- if [[ ${REMOTE_BRANCH_EXISTS} -gt 0 ]]; then
71- echo " [INFO] Remote branch '${BRANCH} ' exists, checking out and updating..."
72- # Check if local branch exists
73- if git show-ref --verify --quiet " refs/heads/${BRANCH} " ; then
74- echo " [INFO] Local branch '${BRANCH} ' exists, switching to it..."
75- git checkout " ${BRANCH} " || {
76- echo " [ERROR] Failed to checkout branch ${BRANCH} "
77- exit 1
78- }
79- else
80- echo " [INFO] Creating local branch '${BRANCH} ' from remote..."
81- git checkout -b " ${BRANCH} " " origin/${BRANCH} " || {
82- echo " [ERROR] Failed to create local branch from remote"
83- exit 1
84- }
85- fi
67+ MAIN_BRANCH=$( git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null | sed ' s@^refs/remotes/origin/@@' || echo " main" )
68+ if [[ ${REMOTE_BRANCH_EXISTS} -gt 0 ]]; then
69+ echo " [INFO] Remote branch '${BRANCH} ' exists, checking out and updating..."
70+ # Check if local branch exists
71+ if git show-ref --verify --quiet " refs/heads/${BRANCH} " ; then
72+ echo " [INFO] Local branch '${BRANCH} ' exists, switching to it..."
73+ git checkout " ${BRANCH} " || {
74+ echo " [ERROR] Failed to checkout branch ${BRANCH} "
75+ exit 1
76+ }
77+ else
78+ echo " [INFO] Creating local branch '${BRANCH} ' from remote..."
79+ git checkout -b " ${BRANCH} " " origin/${BRANCH} " || {
80+ echo " [ERROR] Failed to create local branch from remote"
81+ exit 1
82+ }
83+ fi
8684
87- # Ensure branch is up-to-date with main/master
88- if git show-ref --verify --quiet " refs/remotes/origin/${MAIN_BRANCH} " ; then
89- echo " [INFO] Syncing branch with ${MAIN_BRANCH} ..."
90- git merge " origin/${MAIN_BRANCH} " --no-edit || {
91- echo " [WARNING] Could not auto-merge with ${MAIN_BRANCH} . Branch may have conflicts."
92- }
93- fi
85+ # Ensure branch is up-to-date with main/master
86+ if git show-ref --verify --quiet " refs/remotes/origin/${MAIN_BRANCH} " ; then
87+ echo " [INFO] Rebasing branch onto ${MAIN_BRANCH} ..."
88+ git rebase " origin/${MAIN_BRANCH} " || {
89+ echo " [WARNING] Could not auto-rebase onto ${MAIN_BRANCH} . Branch may have conflicts."
90+ echo " [INFO] Attempting to abort rebase and continue with current state..."
91+ git rebase --abort 2> /dev/null || true
92+ }
93+ fi
94+ else
95+ echo " [INFO] Remote branch '${BRANCH} ' does not exist, creating new branch..."
96+ # Ensure starting from the latest main/master
97+ if git show-ref --verify --quiet " refs/remotes/origin/${MAIN_BRANCH} " ; then
98+ echo " [INFO] Creating branch from latest ${MAIN_BRANCH} ..."
99+ git checkout -b " ${BRANCH} " " origin/${MAIN_BRANCH} " || {
100+ echo " [ERROR] Failed to create branch from ${MAIN_BRANCH} "
101+ exit 1
102+ }
94103 else
95- echo " [INFO] Remote branch '${BRANCH} ' does not exist, creating new branch..."
96- # Ensure starting from the latest main/master
97- if git show-ref --verify --quiet " refs/remotes/origin/${MAIN_BRANCH} " ; then
98- echo " [INFO] Creating branch from latest ${MAIN_BRANCH} ..."
99- git checkout -b " ${BRANCH} " " origin/${MAIN_BRANCH} " || {
100- echo " [ERROR] Failed to create branch from ${MAIN_BRANCH} "
101- exit 1
102- }
103- else
104- echo " [INFO] Creating branch from current HEAD..."
105- git checkout -b " ${BRANCH} " || {
106- echo " [ERROR] Failed to create branch from HEAD"
107- exit 1
108- }
109- fi
104+ echo " [INFO] Creating branch from current HEAD..."
105+ git checkout -b " ${BRANCH} " || {
106+ echo " [ERROR] Failed to create branch from HEAD"
107+ exit 1
108+ }
110109 fi
111110 fi
112111fi
0 commit comments