|
40 | 40 | echo -e "\n[INFO] No files changed." |
41 | 41 | fi |
42 | 42 |
|
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." |
53 | 48 | 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}" |
88 | 58 | 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}" |
94 | 60 | 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) |
95 | 74 |
|
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") |
105 | 84 | 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 |
115 | 114 | 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 |
121 | 130 | fi |
122 | 131 | fi |
123 | 132 | fi |
@@ -168,7 +177,7 @@ if [[ "${INPUT_FORCE}" == "true" ]]; then |
168 | 177 | elif [[ "${INPUT_FORCE_WITH_LEASE}" == "true" ]]; then |
169 | 178 | echo "[INFO] Force pushing changes with lease" |
170 | 179 | 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 |
172 | 181 | echo "[INFO] Pushing changes" |
173 | 182 | # Check if branch has upstream tracking |
174 | 183 | if git rev-parse --abbrev-ref "${BRANCH}@{upstream}" >/dev/null 2>&1; then |
|
0 commit comments