88# this script prints out pretty git branch sync status reports
99
1010
11- # ## constants ###
12-
13- readonly USAGE=<< USAGE
11+ read -r -d ' ' USAGE << -'USAGE '
1412usage: git-branch-status
1513 git-branch-status [-a | --all]
1614 git-branch-status [-h | --help]
1715 git-branch-status [-b | --branch [branch-name]] [branch-name]
1816
17+ examples:
18+
1919 $ git-branch-status
2020 | dns_check | (ahead 1) | (behind 112) | origin/dns_check |
2121 | master | (ahead 2) | (behind 0) | origin/master |
@@ -41,10 +41,23 @@ usage: git-branch-status
4141USAGE
4242
4343
44+ # ## helpers ###
45+
46+ function current_branch() { echo $( git rev-parse --abbrev-ref HEAD) ; }
47+
48+ function set_filter_or_die
49+ {
50+ if [ " $( current_branch) " == " $1 " ] || [ $( git branch | grep -G " ^ $1 $" ) ] ; then
51+ branch=$1
52+ else echo " no such branch: '$1 '" ; exit ;
53+ fi
54+ }
55+
56+
4457# ## switches ###
4558
4659if [ $1 ] ; then
47- if [ " $1 " == " -h" -o " $1 " == " --help" ] ; then echo $USAGE ; exit ;
60+ if [ " $1 " == " -h" -o " $1 " == " --help" ] ; then echo " $USAGE " ; exit ;
4861 elif [ " $1 " == " -a" -o " $1 " == " --all" ] ; then
4962 readonly SHOW_ALL=1
5063 elif [ " $1 " == " -b" -o " $1 " == " --branch" ] ; then
@@ -54,6 +67,8 @@ if [ $1 ] ; then
5467fi
5568
5669
70+ # ## constants ###
71+
5772readonly SHOW_ALL_REMOTE=$(( $SHOW_ALL + 0 )) # also show branches that are up to date
5873readonly SHOW_ALL_LOCAL=$(( $SHOW_ALL + 0 )) # also show branches that have no remote counterpart
5974readonly MAX_COL_W=25
@@ -83,19 +98,6 @@ declare -a ahead_colors=()
8398declare -a behind_colors=()
8499
85100
86- # ## helpers ###
87-
88- function current_branch() { echo $( git rev-parse --abbrev-ref HEAD) ; }
89-
90- function set_filter_or_die
91- {
92- if [ " $( current_branch) " == " $1 " ] || [ $( git branch | grep -G " ^ $1 $" ) ] ; then
93- branch=$1
94- else echo " no such branch: '$1 '" ; exit ;
95- fi
96- }
97-
98-
99101# loop over all branches
100102while read local remote
101103do
142144done < <( git for-each-ref --format=" %(refname:short) %(upstream:short)" refs/heads)
143145
144146# compensate for "(ahead )" and "(behind )" to be appended
145- ahead_col_w=$(( $ahead_col_w + 8 ))
146- behind_col_w=$(( $behind_col_w + 10 ))
147+ ahead_col_w=$(( $ahead_col_w + 8 ))
148+ behind_col_w=$(( $behind_col_w + 9 ))
147149
148150# pretty print results
149151for (( result_n = 0 ; result_n < ${# local_msgs[@]} ; result_n++ ))
0 commit comments