4343# Setting branch name
4444BRANCH=" ${INPUT_TARGET_BRANCH:- $(git symbolic-ref --short -q HEAD)} "
4545# Add timestamp to branch name
46- if [[ " ${INPUT_ADD_TIMESTAMP} " == " true" && -n ${FILES_CHANGED} ]]; then
46+ if [[ " ${INPUT_ADD_TIMESTAMP} " == " true" ]]; then
4747 TIMESTAMP=$( date -u +" %Y-%m-%dT%H-%M-%SZ" )
4848 if [[ -n ${BRANCH} ]]; then
4949 BRANCH=" ${BRANCH} -${TIMESTAMP} "
@@ -53,9 +53,73 @@ if [[ "${INPUT_ADD_TIMESTAMP}" == "true" && -n ${FILES_CHANGED} ]]; then
5353fi
5454echo -e " \n[INFO] Target branch: ${BRANCH} "
5555
56- # Create a new branch
57- if [[ (-n " ${INPUT_TARGET_BRANCH} " || " ${INPUT_ADD_TIMESTAMP} " == " true" ) && -n ${FILES_CHANGED} ]]; then
58- git checkout -b " ${BRANCH} "
56+ # Enhanced branch handling with proper remote synchronization
57+ if [[ -n " ${INPUT_TARGET_BRANCH} " || " ${INPUT_ADD_TIMESTAMP} " == " true" ]]; then
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+ }
63+
64+ # Check if remote branch exists
65+ REMOTE_BRANCH_EXISTS=$( git ls-remote --heads origin " ${BRANCH} " 2> /dev/null | wc -l)
66+
67+ # Improved main branch detection
68+ MAIN_BRANCH=" main"
69+ if git show-ref --verify --quiet " refs/remotes/origin/main" ; then
70+ MAIN_BRANCH=" main"
71+ elif git show-ref --verify --quiet " refs/remotes/origin/master" ; then
72+ MAIN_BRANCH=" master"
73+ else
74+ # Try to get default branch from remote HEAD
75+ MAIN_BRANCH=$( git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null | sed ' s@^refs/remotes/origin/@@' || echo " main" )
76+ fi
77+ echo " [INFO] Detected main branch: ${MAIN_BRANCH} "
78+
79+ if [[ ${REMOTE_BRANCH_EXISTS} -gt 0 ]]; then
80+ echo " [INFO] Remote branch '${BRANCH} ' exists, checking out and updating..."
81+ # Check if local branch exists
82+ if git show-ref --verify --quiet " refs/heads/${BRANCH} " ; then
83+ echo " [INFO] Local branch '${BRANCH} ' exists, switching to it..."
84+ git checkout " ${BRANCH} " || {
85+ echo " [ERROR] Failed to checkout branch ${BRANCH} "
86+ exit 1
87+ }
88+ else
89+ echo " [INFO] Creating local branch '${BRANCH} ' from remote..."
90+ git checkout -b " ${BRANCH} " " origin/${BRANCH} " || {
91+ echo " [ERROR] Failed to create local branch from remote"
92+ exit 1
93+ }
94+ fi
95+
96+ # Ensure branch is up-to-date with main/master (only if they're different branches)
97+ if [[ " ${BRANCH} " != " ${MAIN_BRANCH} " ]] && git show-ref --verify --quiet " refs/remotes/origin/${MAIN_BRANCH} " ; then
98+ echo " [INFO] Rebasing branch onto ${MAIN_BRANCH} ..."
99+ git rebase " origin/${MAIN_BRANCH} " || {
100+ echo " [WARNING] Rebase onto ${MAIN_BRANCH} failed. This may indicate conflicts."
101+ echo " [INFO] Attempting to abort the rebase and continue without sync..."
102+ git rebase --abort 2> /dev/null || true
103+ echo " [INFO] Branch will remain at its current state without sync to ${MAIN_BRANCH} "
104+ }
105+ fi
106+ else
107+ echo " [INFO] Remote branch '${BRANCH} ' does not exist, creating new branch..."
108+ # Ensure starting from the latest main/master
109+ if git show-ref --verify --quiet " refs/remotes/origin/${MAIN_BRANCH} " ; then
110+ echo " [INFO] Creating branch from latest ${MAIN_BRANCH} ..."
111+ git checkout -b " ${BRANCH} " " origin/${MAIN_BRANCH} " || {
112+ echo " [ERROR] Failed to create branch from ${MAIN_BRANCH} "
113+ exit 1
114+ }
115+ else
116+ echo " [INFO] Creating branch from current HEAD..."
117+ git checkout -b " ${BRANCH} " || {
118+ echo " [ERROR] Failed to create branch from HEAD"
119+ exit 1
120+ }
121+ fi
122+ fi
59123fi
60124
61125# Create an auto commit
@@ -104,9 +168,16 @@ if [[ "${INPUT_FORCE}" == "true" ]]; then
104168elif [[ " ${INPUT_FORCE_WITH_LEASE} " == " true" ]]; then
105169 echo " [INFO] Force pushing changes with lease"
106170 git push --force-with-lease origin " ${BRANCH} "
107- elif [[ -n ${FILES_CHANGED} || " ${INPUT_AMEND} " == " true" ]]; then
171+ elif [[ -n ${FILES_CHANGED} || " ${INPUT_AMEND} " == " true" || -n " ${INPUT_TARGET_BRANCH} " ]]; then
108172 echo " [INFO] Pushing changes"
109- git push origin " ${BRANCH} "
173+ # Check if branch has upstream tracking
174+ if git rev-parse --abbrev-ref " ${BRANCH} @{upstream}" > /dev/null 2>&1 ; then
175+ echo " [INFO] Branch has upstream, pushing normally"
176+ git push origin " ${BRANCH} "
177+ else
178+ echo " [INFO] Branch has no upstream, setting upstream on push"
179+ git push -u origin " ${BRANCH} "
180+ fi
110181fi
111182
112183# Finish
0 commit comments