Skip to content

Commit 316c022

Browse files
Improve branch handling with enhanced remote synchronization and error handling
1 parent 3627bc7 commit 316c022

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

entrypoint.sh

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -54,50 +54,61 @@ fi
5454
echo -e "\n[INFO] Target branch: ${BRANCH}"
5555

5656
# Enhanced branch handling with proper remote synchronization
57-
if [[ (-n "${INPUT_TARGET_BRANCH}" || "${INPUT_ADD_TIMESTAMP}" == "true") && -n ${FILES_CHANGED} ]]; then
58-
# Fetch latest changes from remote
59-
echo "[INFO] Fetching latest changes from remote..."
60-
git fetch origin
61-
62-
# Check if remote branch exists
63-
REMOTE_BRANCH_EXISTS=$(git ls-remote --heads origin "${BRANCH}" | wc -l)
64-
65-
MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo "main")
66-
if [[ ${REMOTE_BRANCH_EXISTS} -gt 0 ]]; then
67-
echo "[INFO] Remote branch '${BRANCH}' exists, checking out and updating..."
68-
# Check if local branch exists
69-
if git show-ref --verify --quiet "refs/heads/${BRANCH}"; then
70-
echo "[INFO] Local branch '${BRANCH}' exists, switching to it..."
71-
git checkout "${BRANCH}"
72-
else
73-
echo "[INFO] Creating local branch '${BRANCH}' from remote..."
74-
git checkout -b "${BRANCH}" "origin/${BRANCH}"
75-
fi
76-
77-
# Ensure branch is up-to-date with main/master
78-
if git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then
79-
echo "[INFO] Syncing branch with ${MAIN_BRANCH}..."
80-
git merge "origin/${MAIN_BRANCH}" --no-edit || {
81-
echo "[WARNING] Could not auto-merge with ${MAIN_BRANCH}. Branch may have conflicts."
82-
}
83-
fi
84-
else
85-
echo "[INFO] Remote branch '${BRANCH}' does not exist, creating new branch..."
86-
# Ensure starting from the latest main/master
87-
if git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then
88-
echo "[INFO] Creating branch from latest ${MAIN_BRANCH}..."
89-
git checkout -b "${BRANCH}" "origin/${MAIN_BRANCH}"
57+
if [[ -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+
}
65+
66+
# Check if remote branch exists
67+
REMOTE_BRANCH_EXISTS=$(git ls-remote --heads origin "${BRANCH}" 2>/dev/null | wc -l)
68+
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
86+
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
9094
else
91-
echo "[INFO] Creating branch from current HEAD..."
92-
git checkout -b "${BRANCH}"
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
93110
fi
94111
fi
95-
96-
# Set upstream for the branch
97-
echo "[INFO] Setting upstream for branch '${BRANCH}'..."
98-
git branch --set-upstream-to="origin/${BRANCH}" "${BRANCH}" 2>/dev/null || {
99-
echo "[INFO] Upstream will be set on first push."
100-
}
101112
fi
102113

103114
# Create an auto commit
@@ -148,7 +159,14 @@ elif [[ "${INPUT_FORCE_WITH_LEASE}" == "true" ]]; then
148159
git push --force-with-lease origin "${BRANCH}"
149160
elif [[ -n ${FILES_CHANGED} || "${INPUT_AMEND}" == "true" ]]; then
150161
echo "[INFO] Pushing changes"
151-
git push origin "${BRANCH}"
162+
# Check if branch has upstream tracking
163+
if git rev-parse --abbrev-ref "${BRANCH}@{upstream}" >/dev/null 2>&1; then
164+
echo "[INFO] Branch has upstream, pushing normally"
165+
git push origin "${BRANCH}"
166+
else
167+
echo "[INFO] Branch has no upstream, setting upstream on push"
168+
git push -u origin "${BRANCH}"
169+
fi
152170
fi
153171

154172
# Finish

0 commit comments

Comments
 (0)