Skip to content

Commit 1f15756

Browse files
✨ Update branch creation logic to skip when no changes and amend is false
1 parent e6a6e38 commit 1f15756

File tree

3 files changed

+84
-75
lines changed

3 files changed

+84
-75
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ This action supports three tag levels for flexible versioning:
7474
| `force_with_lease` | No | `false` | Whether to use force push with lease (`--force-with-lease`). Safer than `force` as it checks for remote changes. Set `fetch-depth: 0` for `actions/checkout`. |
7575
| `no_edit` | No | `false` | Whether to not edit commit message when using amend (`--no-edit`). |
7676
| `organization_domain` | No | `github.com` | GitHub Enterprise domain name. |
77-
| `target_branch` | No | *current branch* | Name of a new branch to push the code into. Creates branch if not existing. |
77+
| `target_branch` | No | *current branch* | Name of a new branch to push the code into. Creates branch if not existing unless there are no changes and `amend` is false. |
7878

7979

8080
### 📤 Output Parameters

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ inputs:
3939
required: false
4040
default: github.com
4141
target_branch:
42-
description: Name of a new branch to push the code into
42+
description: Name of a new branch to push the code into (skipped when no changes and amend is false)
4343
required: false
4444
default: ""
4545
outputs:

entrypoint.sh

Lines changed: 82 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -40,84 +40,93 @@ else
4040
echo -e "\n[INFO] No files changed."
4141
fi
4242

43-
# Setting branch name
44-
BRANCH="${INPUT_TARGET_BRANCH:-$(git symbolic-ref --short -q HEAD)}"
45-
# Add timestamp to branch name
46-
if [[ "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then
47-
TIMESTAMP=$(date -u +"%Y-%m-%dT%H-%M-%SZ")
48-
if [[ -n ${BRANCH} ]]; then
49-
BRANCH="${BRANCH}-${TIMESTAMP}"
50-
else
51-
BRANCH="${TIMESTAMP}"
52-
fi
43+
SKIP_BRANCH_CREATION=false
44+
if [[ -z ${FILES_CHANGED} && "${INPUT_AMEND}" != "true" ]]; then
45+
SKIP_BRANCH_CREATION=true
46+
BRANCH="$(git symbolic-ref --short -q HEAD)"
47+
echo -e "\n[INFO] No changes to commit and amend disabled; skipping branch creation."
5348
fi
54-
echo -e "\n[INFO] Target branch: ${BRANCH}"
55-
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-
}
49+
50+
if [[ "${SKIP_BRANCH_CREATION}" != "true" ]]; then
51+
# Setting branch name
52+
BRANCH="${INPUT_TARGET_BRANCH:-$(git symbolic-ref --short -q HEAD)}"
53+
# Add timestamp to branch name
54+
if [[ "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then
55+
TIMESTAMP=$(date -u +"%Y-%m-%dT%H-%M-%SZ")
56+
if [[ -n ${BRANCH} ]]; then
57+
BRANCH="${BRANCH}-${TIMESTAMP}"
8858
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-
}
59+
BRANCH="${TIMESTAMP}"
9460
fi
61+
fi
62+
echo -e "\n[INFO] Target branch: ${BRANCH}"
63+
64+
# Enhanced branch handling with proper remote synchronization
65+
if [[ -n "${INPUT_TARGET_BRANCH}" || "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then
66+
# Fetch latest changes from remote
67+
echo "[INFO] Fetching latest changes from remote..."
68+
git fetch origin || {
69+
echo "[WARNING] Could not fetch from remote. Proceeding with local operations."
70+
}
71+
72+
# Check if remote branch exists
73+
REMOTE_BRANCH_EXISTS=$(git ls-remote --heads origin "${BRANCH}" 2>/dev/null | wc -l)
9574

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-
}
75+
# Improved main branch detection
76+
MAIN_BRANCH="main"
77+
if git show-ref --verify --quiet "refs/remotes/origin/main"; then
78+
MAIN_BRANCH="main"
79+
elif git show-ref --verify --quiet "refs/remotes/origin/master"; then
80+
MAIN_BRANCH="master"
81+
else
82+
# Try to get default branch from remote HEAD
83+
MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo "main")
10584
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-
}
85+
echo "[INFO] Detected main branch: ${MAIN_BRANCH}"
86+
87+
if [[ ${REMOTE_BRANCH_EXISTS} -gt 0 ]]; then
88+
echo "[INFO] Remote branch '${BRANCH}' exists, checking out and updating..."
89+
# Check if local branch exists
90+
if git show-ref --verify --quiet "refs/heads/${BRANCH}"; then
91+
echo "[INFO] Local branch '${BRANCH}' exists, switching to it..."
92+
git checkout "${BRANCH}" || {
93+
echo "[ERROR] Failed to checkout branch ${BRANCH}"
94+
exit 1
95+
}
96+
else
97+
echo "[INFO] Creating local branch '${BRANCH}' from remote..."
98+
git checkout -b "${BRANCH}" "origin/${BRANCH}" || {
99+
echo "[ERROR] Failed to create local branch from remote"
100+
exit 1
101+
}
102+
fi
103+
104+
# Ensure branch is up-to-date with main/master (only if they're different branches)
105+
if [[ "${BRANCH}" != "${MAIN_BRANCH}" ]] && git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then
106+
echo "[INFO] Rebasing branch onto ${MAIN_BRANCH}..."
107+
git rebase "origin/${MAIN_BRANCH}" || {
108+
echo "[WARNING] Rebase onto ${MAIN_BRANCH} failed. This may indicate conflicts."
109+
echo "[INFO] Attempting to abort the rebase and continue without sync..."
110+
git rebase --abort 2>/dev/null || true
111+
echo "[INFO] Branch will remain at its current state without sync to ${MAIN_BRANCH}"
112+
}
113+
fi
115114
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-
}
115+
echo "[INFO] Remote branch '${BRANCH}' does not exist, creating new branch..."
116+
# Ensure starting from the latest main/master
117+
if git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then
118+
echo "[INFO] Creating branch from latest ${MAIN_BRANCH}..."
119+
git checkout -b "${BRANCH}" "origin/${MAIN_BRANCH}" || {
120+
echo "[ERROR] Failed to create branch from ${MAIN_BRANCH}"
121+
exit 1
122+
}
123+
else
124+
echo "[INFO] Creating branch from current HEAD..."
125+
git checkout -b "${BRANCH}" || {
126+
echo "[ERROR] Failed to create branch from HEAD"
127+
exit 1
128+
}
129+
fi
121130
fi
122131
fi
123132
fi
@@ -168,7 +177,7 @@ if [[ "${INPUT_FORCE}" == "true" ]]; then
168177
elif [[ "${INPUT_FORCE_WITH_LEASE}" == "true" ]]; then
169178
echo "[INFO] Force pushing changes with lease"
170179
git push --force-with-lease origin "${BRANCH}"
171-
elif [[ -n ${FILES_CHANGED} || "${INPUT_AMEND}" == "true" || -n "${INPUT_TARGET_BRANCH}" ]]; then
180+
elif [[ "${SKIP_BRANCH_CREATION}" != "true" && ( -n ${FILES_CHANGED} || "${INPUT_AMEND}" == "true" || -n "${INPUT_TARGET_BRANCH}" ) ]]; then
172181
echo "[INFO] Pushing changes"
173182
# Check if branch has upstream tracking
174183
if git rev-parse --abbrev-ref "${BRANCH}@{upstream}" >/dev/null 2>&1; then

0 commit comments

Comments
 (0)