Skip to content

Commit ea9308d

Browse files
✨ Update branch creation logic to skip when no changes and amend is false (#182)
* ✨ Update branch creation logic to skip when no changes and amend is false --------- Co-authored-by: ChristophShyper <45788587+ChristophShyper@users.noreply.github.com>
1 parent e6a6e38 commit ea9308d

File tree

6 files changed

+93
-81
lines changed

6 files changed

+93
-81
lines changed

.github/workflows/auto-create-pull-request.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ jobs:
4848

4949
- name: Install Docker Buildx
5050
uses: docker/setup-buildx-action@v3
51-
with:
52-
install: true
5351

5452
- name: Install QEMU
5553
uses: docker/setup-qemu-action@v3

.github/workflows/auto-create-release.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ jobs:
4040
4141
- name: Install Docker Buildx
4242
uses: docker/setup-buildx-action@v3
43-
with:
44-
install: true
4543

4644
- name: Install QEMU
4745
uses: docker/setup-qemu-action@v3

.github/workflows/cron-check-dependencies.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ jobs:
2727

2828
- name: Install Docker Buildx
2929
uses: docker/setup-buildx-action@v3
30-
with:
31-
install: true
3230

3331
- name: Install QEMU
3432
uses: docker/setup-qemu-action@v3

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: 91 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_ORGAN
3131
git config --global user.name "${GITHUB_ACTOR}"
3232
git config --global user.email "${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}"
3333

34+
get_current_branch() {
35+
local branch
36+
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)
37+
if [[ "${branch}" == "HEAD" ]]; then
38+
branch=""
39+
fi
40+
printf '%s' "${branch}"
41+
}
42+
3443
# Get changed files
3544
git add -A
3645
FILES_CHANGED=$(git diff --staged --name-status)
@@ -40,84 +49,93 @@ else
4049
echo -e "\n[INFO] No files changed."
4150
fi
4251

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
52+
SKIP_BRANCH_CREATION=false
53+
if [[ -z ${FILES_CHANGED} && "${INPUT_AMEND}" != "true" ]]; then
54+
SKIP_BRANCH_CREATION=true
55+
BRANCH="$(get_current_branch)"
56+
echo -e "\n[INFO] No changes to commit and amend disabled; skipping branch creation."
5357
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-
}
58+
59+
if [[ "${SKIP_BRANCH_CREATION}" != "true" ]]; then
60+
# Setting branch name
61+
BRANCH="${INPUT_TARGET_BRANCH:-$(get_current_branch)}"
62+
# Add timestamp to branch name
63+
if [[ "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then
64+
TIMESTAMP=$(date -u +"%Y-%m-%dT%H-%M-%SZ")
65+
if [[ -n ${BRANCH} ]]; then
66+
BRANCH="${BRANCH}-${TIMESTAMP}"
8867
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-
}
68+
BRANCH="${TIMESTAMP}"
9469
fi
70+
fi
71+
echo -e "\n[INFO] Target branch: ${BRANCH}"
72+
73+
# Enhanced branch handling with proper remote synchronization
74+
if [[ -n "${INPUT_TARGET_BRANCH}" || "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then
75+
# Fetch latest changes from remote
76+
echo "[INFO] Fetching latest changes from remote..."
77+
git fetch origin || {
78+
echo "[WARNING] Could not fetch from remote. Proceeding with local operations."
79+
}
80+
81+
# Check if remote branch exists
82+
REMOTE_BRANCH_EXISTS=$(git ls-remote --heads origin "${BRANCH}" 2>/dev/null | wc -l)
9583

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

0 commit comments

Comments
 (0)