From 1f15756b85326893678e50ee0c48d15693ca2150 Mon Sep 17 00:00:00 2001 From: ChristophShyper <45788587+ChristophShyper@users.noreply.github.com> Date: Wed, 18 Feb 2026 19:58:53 +0100 Subject: [PATCH 1/3] :sparkles: Update branch creation logic to skip when no changes and amend is false --- README.md | 2 +- action.yml | 2 +- entrypoint.sh | 155 ++++++++++++++++++++++++++------------------------ 3 files changed, 84 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 5a2fb47..fd04041 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ This action supports three tag levels for flexible versioning: | `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`. | | `no_edit` | No | `false` | Whether to not edit commit message when using amend (`--no-edit`). | | `organization_domain` | No | `github.com` | GitHub Enterprise domain name. | -| `target_branch` | No | *current branch* | Name of a new branch to push the code into. Creates branch if not existing. | +| `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. | ### 📤 Output Parameters diff --git a/action.yml b/action.yml index 24ff0ca..4a02e2a 100644 --- a/action.yml +++ b/action.yml @@ -39,7 +39,7 @@ inputs: required: false default: github.com target_branch: - description: Name of a new branch to push the code into + description: Name of a new branch to push the code into (skipped when no changes and amend is false) required: false default: "" outputs: diff --git a/entrypoint.sh b/entrypoint.sh index cc76efd..a7ce506 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -40,84 +40,93 @@ else echo -e "\n[INFO] No files changed." fi -# Setting branch name -BRANCH="${INPUT_TARGET_BRANCH:-$(git symbolic-ref --short -q HEAD)}" -# Add timestamp to branch name -if [[ "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then - TIMESTAMP=$(date -u +"%Y-%m-%dT%H-%M-%SZ") - if [[ -n ${BRANCH} ]]; then - BRANCH="${BRANCH}-${TIMESTAMP}" - else - BRANCH="${TIMESTAMP}" - fi +SKIP_BRANCH_CREATION=false +if [[ -z ${FILES_CHANGED} && "${INPUT_AMEND}" != "true" ]]; then + SKIP_BRANCH_CREATION=true + BRANCH="$(git symbolic-ref --short -q HEAD)" + echo -e "\n[INFO] No changes to commit and amend disabled; skipping branch creation." fi -echo -e "\n[INFO] Target branch: ${BRANCH}" - -# Enhanced branch handling with proper remote synchronization -if [[ -n "${INPUT_TARGET_BRANCH}" || "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then - # Fetch latest changes from remote - echo "[INFO] Fetching latest changes from remote..." - git fetch origin || { - echo "[WARNING] Could not fetch from remote. Proceeding with local operations." - } - - # Check if remote branch exists - REMOTE_BRANCH_EXISTS=$(git ls-remote --heads origin "${BRANCH}" 2>/dev/null | wc -l) - - # Improved main branch detection - MAIN_BRANCH="main" - if git show-ref --verify --quiet "refs/remotes/origin/main"; then - MAIN_BRANCH="main" - elif git show-ref --verify --quiet "refs/remotes/origin/master"; then - MAIN_BRANCH="master" - else - # Try to get default branch from remote HEAD - MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo "main") - fi - echo "[INFO] Detected main branch: ${MAIN_BRANCH}" - - if [[ ${REMOTE_BRANCH_EXISTS} -gt 0 ]]; then - echo "[INFO] Remote branch '${BRANCH}' exists, checking out and updating..." - # Check if local branch exists - if git show-ref --verify --quiet "refs/heads/${BRANCH}"; then - echo "[INFO] Local branch '${BRANCH}' exists, switching to it..." - git checkout "${BRANCH}" || { - echo "[ERROR] Failed to checkout branch ${BRANCH}" - exit 1 - } + +if [[ "${SKIP_BRANCH_CREATION}" != "true" ]]; then + # Setting branch name + BRANCH="${INPUT_TARGET_BRANCH:-$(git symbolic-ref --short -q HEAD)}" + # Add timestamp to branch name + if [[ "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then + TIMESTAMP=$(date -u +"%Y-%m-%dT%H-%M-%SZ") + if [[ -n ${BRANCH} ]]; then + BRANCH="${BRANCH}-${TIMESTAMP}" else - echo "[INFO] Creating local branch '${BRANCH}' from remote..." - git checkout -b "${BRANCH}" "origin/${BRANCH}" || { - echo "[ERROR] Failed to create local branch from remote" - exit 1 - } + BRANCH="${TIMESTAMP}" fi + fi + echo -e "\n[INFO] Target branch: ${BRANCH}" + + # Enhanced branch handling with proper remote synchronization + if [[ -n "${INPUT_TARGET_BRANCH}" || "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then + # Fetch latest changes from remote + echo "[INFO] Fetching latest changes from remote..." + git fetch origin || { + echo "[WARNING] Could not fetch from remote. Proceeding with local operations." + } + + # Check if remote branch exists + REMOTE_BRANCH_EXISTS=$(git ls-remote --heads origin "${BRANCH}" 2>/dev/null | wc -l) - # Ensure branch is up-to-date with main/master (only if they're different branches) - if [[ "${BRANCH}" != "${MAIN_BRANCH}" ]] && git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then - echo "[INFO] Rebasing branch onto ${MAIN_BRANCH}..." - git rebase "origin/${MAIN_BRANCH}" || { - echo "[WARNING] Rebase onto ${MAIN_BRANCH} failed. This may indicate conflicts." - echo "[INFO] Attempting to abort the rebase and continue without sync..." - git rebase --abort 2>/dev/null || true - echo "[INFO] Branch will remain at its current state without sync to ${MAIN_BRANCH}" - } + # Improved main branch detection + MAIN_BRANCH="main" + if git show-ref --verify --quiet "refs/remotes/origin/main"; then + MAIN_BRANCH="main" + elif git show-ref --verify --quiet "refs/remotes/origin/master"; then + MAIN_BRANCH="master" + else + # Try to get default branch from remote HEAD + MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo "main") fi - else - echo "[INFO] Remote branch '${BRANCH}' does not exist, creating new branch..." - # Ensure starting from the latest main/master - if git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then - echo "[INFO] Creating branch from latest ${MAIN_BRANCH}..." - git checkout -b "${BRANCH}" "origin/${MAIN_BRANCH}" || { - echo "[ERROR] Failed to create branch from ${MAIN_BRANCH}" - exit 1 - } + echo "[INFO] Detected main branch: ${MAIN_BRANCH}" + + if [[ ${REMOTE_BRANCH_EXISTS} -gt 0 ]]; then + echo "[INFO] Remote branch '${BRANCH}' exists, checking out and updating..." + # Check if local branch exists + if git show-ref --verify --quiet "refs/heads/${BRANCH}"; then + echo "[INFO] Local branch '${BRANCH}' exists, switching to it..." + git checkout "${BRANCH}" || { + echo "[ERROR] Failed to checkout branch ${BRANCH}" + exit 1 + } + else + echo "[INFO] Creating local branch '${BRANCH}' from remote..." + git checkout -b "${BRANCH}" "origin/${BRANCH}" || { + echo "[ERROR] Failed to create local branch from remote" + exit 1 + } + fi + + # Ensure branch is up-to-date with main/master (only if they're different branches) + if [[ "${BRANCH}" != "${MAIN_BRANCH}" ]] && git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then + echo "[INFO] Rebasing branch onto ${MAIN_BRANCH}..." + git rebase "origin/${MAIN_BRANCH}" || { + echo "[WARNING] Rebase onto ${MAIN_BRANCH} failed. This may indicate conflicts." + echo "[INFO] Attempting to abort the rebase and continue without sync..." + git rebase --abort 2>/dev/null || true + echo "[INFO] Branch will remain at its current state without sync to ${MAIN_BRANCH}" + } + fi else - echo "[INFO] Creating branch from current HEAD..." - git checkout -b "${BRANCH}" || { - echo "[ERROR] Failed to create branch from HEAD" - exit 1 - } + echo "[INFO] Remote branch '${BRANCH}' does not exist, creating new branch..." + # Ensure starting from the latest main/master + if git show-ref --verify --quiet "refs/remotes/origin/${MAIN_BRANCH}"; then + echo "[INFO] Creating branch from latest ${MAIN_BRANCH}..." + git checkout -b "${BRANCH}" "origin/${MAIN_BRANCH}" || { + echo "[ERROR] Failed to create branch from ${MAIN_BRANCH}" + exit 1 + } + else + echo "[INFO] Creating branch from current HEAD..." + git checkout -b "${BRANCH}" || { + echo "[ERROR] Failed to create branch from HEAD" + exit 1 + } + fi fi fi fi @@ -168,7 +177,7 @@ if [[ "${INPUT_FORCE}" == "true" ]]; then elif [[ "${INPUT_FORCE_WITH_LEASE}" == "true" ]]; then echo "[INFO] Force pushing changes with lease" git push --force-with-lease origin "${BRANCH}" -elif [[ -n ${FILES_CHANGED} || "${INPUT_AMEND}" == "true" || -n "${INPUT_TARGET_BRANCH}" ]]; then +elif [[ "${SKIP_BRANCH_CREATION}" != "true" && ( -n ${FILES_CHANGED} || "${INPUT_AMEND}" == "true" || -n "${INPUT_TARGET_BRANCH}" ) ]]; then echo "[INFO] Pushing changes" # Check if branch has upstream tracking if git rev-parse --abbrev-ref "${BRANCH}@{upstream}" >/dev/null 2>&1; then From 4436b063f0cbc7af77372a685952ed202cadca7e Mon Sep 17 00:00:00 2001 From: ChristophShyper <45788587+ChristophShyper@users.noreply.github.com> Date: Wed, 18 Feb 2026 20:02:15 +0100 Subject: [PATCH 2/3] fix buildx action --- .github/workflows/auto-create-pull-request.yml | 2 -- .github/workflows/auto-create-release.yml | 2 -- .github/workflows/cron-check-dependencies.yml | 2 -- 3 files changed, 6 deletions(-) diff --git a/.github/workflows/auto-create-pull-request.yml b/.github/workflows/auto-create-pull-request.yml index 20ce420..cf21dde 100644 --- a/.github/workflows/auto-create-pull-request.yml +++ b/.github/workflows/auto-create-pull-request.yml @@ -48,8 +48,6 @@ jobs: - name: Install Docker Buildx uses: docker/setup-buildx-action@v3 - with: - install: true - name: Install QEMU uses: docker/setup-qemu-action@v3 diff --git a/.github/workflows/auto-create-release.yml b/.github/workflows/auto-create-release.yml index 84578e6..4f687a3 100644 --- a/.github/workflows/auto-create-release.yml +++ b/.github/workflows/auto-create-release.yml @@ -40,8 +40,6 @@ jobs: - name: Install Docker Buildx uses: docker/setup-buildx-action@v3 - with: - install: true - name: Install QEMU uses: docker/setup-qemu-action@v3 diff --git a/.github/workflows/cron-check-dependencies.yml b/.github/workflows/cron-check-dependencies.yml index 57f98ea..e676884 100644 --- a/.github/workflows/cron-check-dependencies.yml +++ b/.github/workflows/cron-check-dependencies.yml @@ -27,8 +27,6 @@ jobs: - name: Install Docker Buildx uses: docker/setup-buildx-action@v3 - with: - install: true - name: Install QEMU uses: docker/setup-qemu-action@v3 From e63633ad22358f747fc8aabc68b5705a120bb1c3 Mon Sep 17 00:00:00 2001 From: ChristophShyper <45788587+ChristophShyper@users.noreply.github.com> Date: Wed, 18 Feb 2026 20:45:19 +0100 Subject: [PATCH 3/3] :bug: Fix branch resolving --- entrypoint.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index a7ce506..57d5358 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -31,6 +31,15 @@ git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_ORGAN git config --global user.name "${GITHUB_ACTOR}" git config --global user.email "${GITHUB_ACTOR}@users.noreply.${INPUT_ORGANIZATION_DOMAIN}" +get_current_branch() { + local branch + branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true) + if [[ "${branch}" == "HEAD" ]]; then + branch="" + fi + printf '%s' "${branch}" +} + # Get changed files git add -A FILES_CHANGED=$(git diff --staged --name-status) @@ -43,13 +52,13 @@ fi SKIP_BRANCH_CREATION=false if [[ -z ${FILES_CHANGED} && "${INPUT_AMEND}" != "true" ]]; then SKIP_BRANCH_CREATION=true - BRANCH="$(git symbolic-ref --short -q HEAD)" + BRANCH="$(get_current_branch)" echo -e "\n[INFO] No changes to commit and amend disabled; skipping branch creation." fi if [[ "${SKIP_BRANCH_CREATION}" != "true" ]]; then # Setting branch name - BRANCH="${INPUT_TARGET_BRANCH:-$(git symbolic-ref --short -q HEAD)}" + BRANCH="${INPUT_TARGET_BRANCH:-$(get_current_branch)}" # Add timestamp to branch name if [[ "${INPUT_ADD_TIMESTAMP}" == "true" ]]; then TIMESTAMP=$(date -u +"%Y-%m-%dT%H-%M-%SZ")