11#! /bin/bash
22# #
3- # Synchronizes current branch with remote's master/main.
3+ # Synchronizes branch(es) with remote's master/main.
44#
55# `./git_sync.sh` – updates current branch with changes from remote/master
66# `./git_sync.sh` all – updates all branches with changes from remote/master
@@ -13,6 +13,15 @@ FORMAT_GREEN="\033[32m"
1313FORMAT_YELLOW=" \033[33m"
1414FORMAT_RESET=" \033[0m"
1515
16+ # #
17+ # @param string $1 Message to print
18+ #
19+ # @global FORMAT_ITALIC
20+ # @global FORMAT_GREEN
21+ # @global FORMAT_RESET
22+ #
23+ # @stdout Formatted message
24+ # #
1625print_info () {
1726 local message=" ${1} "
1827
@@ -26,6 +35,15 @@ print_info() {
2635 printf " %b" " ${FORMAT_RESET} "
2736}
2837
38+ # #
39+ # @param string $1 Message to print
40+ #
41+ # @global FORMAT_ITALIC
42+ # @global FORMAT_YELLOW
43+ # @global FORMAT_RESET
44+ #
45+ # @stdout Formatted message
46+ # #
2947print_warning () {
3048 local message=" ${1} "
3149
@@ -39,6 +57,15 @@ print_warning() {
3957 printf " %b" " ${FORMAT_RESET} "
4058}
4159
60+ # #
61+ # @param string $1 Message to print
62+ #
63+ # @global FORMAT_ITALIC
64+ # @global FORMAT_RED
65+ # @global FORMAT_RESET
66+ #
67+ # @stdout Formatted message
68+ # #
4269print_error () {
4370 local message=" ${1} "
4471
@@ -52,6 +79,13 @@ print_error() {
5279 printf " %b" " ${FORMAT_RESET} "
5380}
5481
82+ # #
83+ # @param string... $@ The command and its arguments to execute
84+ #
85+ # @stdout The command and its output
86+ #
87+ # @return Exit code of the executed command
88+ # #
5589print_and_run () {
5690 # Print the command.
5791 print_info " \n➤ ${* } \n"
@@ -63,6 +97,13 @@ print_and_run() {
6397 " ${@ } "
6498}
6599
100+ # #
101+ # @global default_branch
102+ #
103+ # @stdout Error message, when not able to detect the default branch
104+ #
105+ # @exit 1 On not able to detect the default branch
106+ #
66107detect_default_branch () {
67108 if local_branch_exists " master" ; then
68109 default_branch=" master"
@@ -75,29 +116,51 @@ detect_default_branch() {
75116 fi
76117}
77118
119+ # #
120+ # @stdout Number of stashes (integer)
121+ #
78122count_stashes () {
79123 # `wc` generates a value with whitespace.
80124 # `xargs` trims the whitespace.
81125 git stash list | wc -l | xargs
82126}
83127
128+ # #
129+ # @stdout Branch names, one per line
130+ # #
84131list_local_branches () {
85132 git for-each-ref --format=' %(refname:short)' refs/heads/
86133}
87134
135+ # #
136+ # @param string $1 Branch name
137+ #
138+ # @return 0 When branch exists
139+ # @return 1 When branch does not exist
140+ # #
88141local_branch_exists () {
89142 local branch=" ${1} "
90143
91144 git show-ref --verify --quiet " refs/heads/${branch} "
92145}
93146
94- # Returns 0 (true) if the branch exists on origin, 1 (false) otherwise
147+ # #
148+ # @param string $1 Branch name
149+ #
150+ # @return 0 When branch exists
151+ # @return 1 When branch does not exist
152+ # #
95153remote_branch_exists () {
96154 local branch=" ${1} "
97155
98156 git show-ref --verify --quiet " refs/remotes/origin/${branch} "
99157}
100158
159+ # #
160+ # @stdout Combined sync_branch() output
161+ #
162+ # @global default_branch Used to skip syncing the default branch
163+ # #
101164sync_all_branches () {
102165 local branch
103166
@@ -109,13 +172,22 @@ sync_all_branches() {
109172 done
110173}
111174
175+ # #
176+ # @param string $1 Branch name
177+ #
178+ # @stdout Sync process and/or warnings/errors
179+ #
180+ # @return 0 Successful sync
181+ # @return 1 Sync skipped (remote branch exists)
182+ # @return 2 Sync rebase aborted (conflicts)
183+ # #
112184sync_branch () {
113185 local branch=" ${1} "
114186
115187 if remote_branch_exists " ${branch} " ; then
116188 print_warning " \n⚠ Sync aborted on branch \" ${branch} \" as it exists on origin."
117189
118- return
190+ return 1
119191 fi
120192
121193 print_and_run git checkout " ${branch} "
@@ -124,9 +196,16 @@ sync_branch() {
124196 print_and_run git rebase --abort
125197
126198 print_error " ⚠ Rebase aborted on branch \" ${branch} \" due to conflicts!"
199+
200+ return 2
127201 fi
202+
203+ return 0
128204}
129205
206+ # #
207+ # Main script execution
208+ # #
130209detect_default_branch
131210print_info " \nDetected default branch: \" ${default_branch} \" ."
132211
0 commit comments