@@ -31,6 +31,15 @@ git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_ORGAN
3131git config --global user.name " ${GITHUB_ACTOR} "
3232git 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
3544git add -A
3645FILES_CHANGED=$( git diff --staged --name-status)
4049 echo -e " \n[INFO] No files changed."
4150fi
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."
5357fi
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
123141fi
@@ -168,7 +186,7 @@ if [[ "${INPUT_FORCE}" == "true" ]]; then
168186elif [[ " ${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