Skip to content

Commit d896562

Browse files
committed
refactor
1 parent 20c0f0a commit d896562

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

git-branch-status

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -136,66 +136,78 @@ declare -a remote_colors=()
136136

137137
### helpers ###
138138

139-
function get_refs # (a_refs_dir)
139+
function GetRefs # (a_refs_dir)
140140
{
141141
git for-each-ref --format="%(refname:short) %(upstream:short)" $1 2> /dev/null
142142
}
143143

144-
function get_status
144+
function GetLocalRefs
145+
{
146+
GetRefs refs/heads
147+
}
148+
149+
function GetRemoteRefs # (remote_repo)
150+
{
151+
remote_repo=$1
152+
153+
GetRefs refs/remotes/$remote_repo
154+
}
155+
156+
function GetStatus
145157
{
146158
git rev-list --left-right ${local}...${remote} -- 2>/dev/null
147159
}
148160

149-
function current_branch
161+
function GetCurrentBranch
150162
{
151163
git rev-parse --abbrev-ref HEAD
152164
}
153165

154-
function is_current_branch # (a_branch_name)
166+
function IsCurrentBranch # (a_branch_name)
155167
{
156-
current=$(current_branch)
168+
current=$(GetCurrentBranch)
157169
if (($SHOW_DATES))
158-
then this_branch=$(get_head_date $1)$1 ; current=$(get_head_date $current)$current ;
170+
then this_branch=$(GetHeadDate $1)$1 ; current=$(GetHeadDate $current)$current ;
159171
else this_branch=$1
160172
fi
161173

162174
if [ "$this_branch" == "$current" ] ; then echo 1 ; else echo 0 ; fi ;
163175
}
164176

165-
function does_branch_exist # (a_branch_name)
177+
function DoesBranchExist # (a_branch_name)
166178
{
167179
is_known_branch=$(git branch | grep -G "^ $1$") # all but current
168-
[ $(is_current_branch $1) -o "$is_known_branch" ] && echo 1 || echo 0
180+
[ $(IsCurrentBranch $1) -o "$is_known_branch" ] && echo 1 || echo 0
169181
}
170182

171-
function set_filter_or_die # (a_branch_name)
183+
function SetFilterOrDie # (a_branch_name)
172184
{
173-
if (($(does_branch_exist $1)))
185+
if (($(DoesBranchExist $1)))
174186
then branch=$1
175187
else echo "no such branch: '$1'" ; exit ;
176188
fi
177189
}
178190

179-
function get_head_date # (a_commit_ref)
191+
function GetHeadDate # (a_commit_ref)
180192
{
181193
author_date=$(git log -n 1 --format=format:"%ai" $1 2> /dev/null)
182194
(($SHOW_DATES)) && [ "$author_date" ] && echo "${author_date:0:10}$JOIN_CHAR"
183195
}
184196

185-
function get_commit_msg # (a_commit_ref)
197+
function GetCommitMsg # (a_commit_ref)
186198
{
187199
git log -n 1 --format=format:"%s" $1
188200
}
189201

190-
function printHRule # (rule_w)
202+
function PrintHRule # (rule_w)
191203
{
192204
printf " $(head -c $1 < /dev/zero | tr '\0' $HRULE_CHAR)\n"
193205
}
194206

195207

196208
### business ###
197209

198-
function reset
210+
function Reset
199211
{
200212
n_total_differences=0
201213
local_w=0
@@ -212,11 +224,11 @@ function reset
212224
remote_colors=()
213225
}
214226

215-
function report # (a_local_branch_name a_remote_branch_name)
227+
function GenerateReport # (a_local_branch_name a_remote_branch_name)
216228
{
217229
local=$1
218230
remote=$2
219-
does_local_exist=$(does_branch_exist $local_branch)
231+
does_local_exist=$(DoesBranchExist $local_branch)
220232

221233
# filter branches per CLI arg
222234
[ $branch ] && [ "$branch" != "$local" ] && continue
@@ -226,7 +238,7 @@ function report # (a_local_branch_name a_remote_branch_name)
226238

227239
# parse local<->remote sync status
228240
if (($does_local_exist)) && [ $remote ] ; then
229-
status=$(get_status) ; (($?)) && continue ;
241+
status=$(GetStatus) ; (($?)) && continue ;
230242

231243
n_behind=$(echo $status | tr " " "\n" | grep -c '^>')
232244
n_ahead=$( echo $status | tr " " "\n" | grep -c '^<')
@@ -263,8 +275,8 @@ function report # (a_local_branch_name a_remote_branch_name)
263275
fi
264276

265277
# populate lists
266-
local_msg="$(get_head_date $local)$local" ; local_msg="${local_msg:0:$MAX_COL_W}" ;
267-
remote_msg="$(get_head_date $remote)$remote" ; remote_msg="${remote_msg:0:$MAX_COL_W}" ;
278+
local_msg="$(GetHeadDate $local)$local" ; local_msg="${local_msg:0:$MAX_COL_W}" ;
279+
remote_msg="$(GetHeadDate $remote)$remote" ; remote_msg="${remote_msg:0:$MAX_COL_W}" ;
268280
local_msgs=( ${local_msgs[@]} "$local_msg" )
269281
behind_msgs=( ${behind_msgs[@]} "$behind_msg" )
270282
ahead_msgs=( ${ahead_msgs[@]} "$ahead_msg" )
@@ -281,7 +293,7 @@ function report # (a_local_branch_name a_remote_branch_name)
281293
if [ ${#remote_msg} -gt $remote_w ] ; then remote_w=${#remote_msg} ; fi ;
282294
}
283295

284-
function printReportLine
296+
function PrintReportLine
285297
{
286298
# fetch data
287299
local_msg=$( echo ${local_msgs[$result_n]} | sed "$JOIN_REGEX" )
@@ -301,7 +313,7 @@ function printReportLine
301313
end_offset=$(( $remote_w - ${#remote_msg} ))
302314

303315
# build output messages and display
304-
if (($(is_current_branch $local_msg))) ; then star=$STAR ; else star=" " ; fi ;
316+
if (($(IsCurrentBranch $local_msg))) ; then star=$STAR ; else star=" " ; fi ;
305317
local_msg="%$(( $local_offset ))s$star$(echo -e $DELIM $local_color$local_msg$CEND)"
306318
behind_msg="%$(( $behind_offset ))s $( echo -e $DELIM $behind_color$behind_msg$CEND)"
307319
ahead_msg="%$(( $ahead_offset ))s $( echo -e $DELIM $ahead_color$ahead_msg$CEND)"
@@ -310,7 +322,7 @@ function printReportLine
310322
printf "$local_msg$behind_msg$ahead_msg$remote_msg$end_msg\n"
311323
}
312324

313-
function printReport # (header)
325+
function PrintReport # (header)
314326
{
315327
header=$1
316328
n_notable_differences=${#local_msgs[@]}
@@ -319,22 +331,22 @@ function printReport # (header)
319331
printf "\n $header\n"
320332
if [ "$n_notable_differences" != "0" ]
321333
then rule_w=$(($local_w+$behind_w+$ahead_w+$remote_w+13))
322-
printHRule $rule_w
334+
PrintHRule $rule_w
323335
for (( result_n = 0 ; result_n < $n_notable_differences ; result_n++ ))
324-
do printReportLine
336+
do PrintReportLine
325337
done
326-
printHRule $rule_w
338+
PrintHRule $rule_w
327339
fi
328340

329341
# print something if no diffs
330-
if [ "$n_total_differences" == "0" -a "$(get_refs)" ]
342+
if [ "$n_total_differences" == "0" -a "$(GetRefs)" ]
331343
then rule_w=$((${#NO_RESULTS_MSG}+4))
332-
printHRule $rule_w
344+
PrintHRule $rule_w
333345
echo -e " $DELIM $CEVEN$NO_RESULTS_MSG$CEND $DELIM"
334-
printHRule $rule_w
346+
PrintHRule $rule_w
335347
fi
336348

337-
reset
349+
Reset
338350
}
339351

340352

@@ -349,13 +361,13 @@ if [ $1 ] ; then
349361
show_all_remote=0
350362
if [ "$1" == "-a" -o "$1" == "--all" ] ; then show_all=1 ;
351363
elif [ "$1" == "-b" -o "$1" == "--branch" ] ; then
352-
if [ $2 ] ; then set_filter_or_die $2 ; else branch=$(current_branch) ; fi ;
364+
if [ $2 ] ; then SetFilterOrDie $2 ; else branch=$(GetCurrentBranch) ; fi ;
353365
elif [ "$1" == "-d" -o "$1" == "--dates" ] ; then show_dates=1 ;
354366
elif [ "$1" == "-h" -o "$1" == "--help" ] ; then echo "$USAGE" ; exit ;
355367
elif [ "$1" == "-r" -o "$1" == "--remotes" ] ; then show_all_remote=1 ;
356368
elif [ "$1" == "-v" -o "$1" == "--verbose" ] ; then show_all=1 ;
357369
show_dates=1 ;
358-
else set_filter_or_die $1
370+
else SetFilterOrDie $1
359371
fi
360372
readonly SHOW_DATES=$show_dates
361373
readonly SHOW_ALL=$show_all
@@ -366,8 +378,8 @@ fi
366378

367379

368380
# compare local branches status to their upstreams
369-
while read local upstream ; do report $local $upstream ; done < <(get_refs refs/heads) ;
370-
printReport "local <-> upstream"
381+
while read local upstream ; do GenerateReport $local $upstream ; done < <(GetLocalRefs) ;
382+
PrintReport "local <-> upstream"
371383

372384

373385
(($SHOW_ALL_REMOTE)) || exit
@@ -380,8 +392,8 @@ do
380392
local_branch=${remote_branch#$remote_repo/}
381393
upstream_branch=`git rev-parse --abbrev-ref $local_branch@{upstream} 2> /dev/null`
382394

383-
[ "$remote_branch" != "$upstream_branch" ] && report $local_branch $remote_branch
384-
done < <(get_refs refs/remotes/$remote_repo)
395+
[ "$remote_branch" != "$upstream_branch" ] && GenerateReport $local_branch $remote_branch
396+
done < <(GetRemoteRefs $remote_repo)
385397

386-
printReport "local <-> $remote_repo"
398+
PrintReport "local <-> $remote_repo"
387399
done

0 commit comments

Comments
 (0)