Skip to content

Commit babc521

Browse files
Enhance entrypoint.sh with improved branch handling and synchronization logic
1 parent 5297669 commit babc521

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

entrypoint.sh

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fi
4343
# Setting branch name
4444
BRANCH="${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}"
@@ -54,7 +54,7 @@ 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
57+
if [[ -n "${INPUT_TARGET_BRANCH}" || "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then
5858
# Fetch latest changes from remote
5959
echo "[INFO] Fetching latest changes from remote..."
6060
git fetch origin || {
@@ -64,10 +64,18 @@ if [[ -n "${INPUT_TARGET_BRANCH}" || ("${INPUT_ADD_TIMESTAMP}" == "true" && -n $
6464
# Check if remote branch exists
6565
REMOTE_BRANCH_EXISTS=$(git ls-remote --heads origin "${BRANCH}" 2>/dev/null | wc -l)
6666

67-
MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || \
68-
(git show-ref --verify --quiet "refs/remotes/origin/main" && echo "main") || \
69-
(git show-ref --verify --quiet "refs/remotes/origin/master" && echo "master") || \
70-
echo "main")
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+
7179
if [[ ${REMOTE_BRANCH_EXISTS} -gt 0 ]]; then
7280
echo "[INFO] Remote branch '${BRANCH}' exists, checking out and updating..."
7381
# Check if local branch exists
@@ -85,20 +93,14 @@ if [[ -n "${INPUT_TARGET_BRANCH}" || ("${INPUT_ADD_TIMESTAMP}" == "true" && -n $
8593
}
8694
fi
8795

88-
# Ensure branch is up-to-date with main/master
89-
if git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then
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
9098
echo "[INFO] Rebasing branch onto ${MAIN_BRANCH}..."
9199
git rebase "origin/${MAIN_BRANCH}" || {
92-
echo "[ERROR] Rebase onto ${MAIN_BRANCH} failed. This may indicate conflicts or other issues."
93-
echo "[INFO] Attempting to abort the rebase..."
94-
if git rebase --abort 2>/dev/null; then
95-
echo "[INFO] Rebase aborted successfully. The branch is in its pre-rebase state."
96-
echo "[INFO] Please resolve any conflicts manually and reattempt the rebase if necessary."
97-
else
98-
echo "[ERROR] Failed to abort the rebase. The repository may be in an inconsistent state."
99-
echo "[INFO] Please inspect the repository and resolve any issues manually."
100-
exit 1
101-
fi
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}"
102104
}
103105
fi
104106
else
@@ -166,7 +168,7 @@ if [[ "${INPUT_FORCE}" == "true" ]]; then
166168
elif [[ "${INPUT_FORCE_WITH_LEASE}" == "true" ]]; then
167169
echo "[INFO] Force pushing changes with lease"
168170
git push --force-with-lease origin "${BRANCH}"
169-
elif [[ -n ${FILES_CHANGED} || "${INPUT_AMEND}" == "true" ]]; then
171+
elif [[ -n ${FILES_CHANGED} || "${INPUT_AMEND}" == "true" || -n "${INPUT_TARGET_BRANCH}" ]]; then
170172
echo "[INFO] Pushing changes"
171173
# Check if branch has upstream tracking
172174
if git rev-parse --abbrev-ref "${BRANCH}@{upstream}" >/dev/null 2>&1; then

0 commit comments

Comments
 (0)