Skip to content

Commit e69cd61

Browse files
committed
restrict max column width
1 parent e8ed737 commit e69cd61

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

git-branch-status

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
# this script prints out pretty git branch sync status reports
99

1010

11-
USAGE=<<USAGE
11+
### constants ###
12+
13+
readonly USAGE=<<USAGE
1214
usage: git-branch-status
1315
git-branch-status [-a | --all]
1416
git-branch-status [-h | --help]
@@ -39,16 +41,7 @@ usage: git-branch-status
3941
USAGE
4042

4143

42-
# switches
43-
function current_branch() { echo $(git rev-parse --abbrev-ref HEAD) ; }
44-
45-
function set_filter_or_die
46-
{
47-
if [ "$(current_branch)" == "$1" ] || [ $(git branch | grep -G "^ $1$") ] ; then
48-
branch=$1
49-
else echo "no such branch: '$1'" ; exit ;
50-
fi
51-
}
44+
### switches ###
5245

5346
if [ $1 ] ; then
5447
if [ "$1" == "-h" -o "$1" == "--help" ] ; then echo $USAGE ; exit ;
@@ -61,9 +54,9 @@ if [ $1 ] ; then
6154
fi
6255

6356

64-
# constants
6557
readonly SHOW_ALL_REMOTE=$(($SHOW_ALL + 0)) # also show branches that are up to date
6658
readonly SHOW_ALL_LOCAL=$(($SHOW_ALL + 0)) # also show branches that have no remote counterpart
59+
readonly MAX_COL_W=25
6760
readonly CGREEN='\033[0;32m'
6861
readonly CYELLOW='\033[1;33m'
6962
readonly CRED='\033[0;31m'
@@ -75,7 +68,8 @@ readonly CEVEN=$CGREEN
7568
readonly NO_RESULTS_MSG="${CEVEN}Everything is synchronized.$CEND"
7669

7770

78-
# variables
71+
### variables ###
72+
7973
n_total_differences=0
8074
local_col_w=0
8175
ahead_col_w=0
@@ -89,16 +83,30 @@ declare -a ahead_colors=()
8983
declare -a behind_colors=()
9084

9185

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+
9299
# loop over all branches
93100
while read local remote
94101
do
95102
# filter branches by name
96103
[ $branch ] && [ "$branch" != "$local" ] && continue
97104

98105
# parse local<->remote sync status
99-
if [ ! -z "$remote" ] ; then
106+
if [ $remote ] ; then
100107
status=$(git rev-list --left-right ${local}...${remote} -- 2>/dev/null)
101108
[ $(($?)) -eq 0 ] || continue
109+
102110
n_ahead=$( echo $status | tr " " "\n" | grep -c '^<')
103111
n_behind=$(echo $status | tr " " "\n" | grep -c '^>')
104112
n_differences=$(($n_ahead + $n_behind))
@@ -111,11 +119,13 @@ do
111119
if [ "$n_ahead" -ne 0 ] ; then ahead_color=$CAHEAD ; else ahead_color=$CEVEN ; fi ;
112120
if [ "$n_behind" -ne 0 ] ; then behind_color=$CBEHIND ; else behind_color=$CEVEN ; fi ;
113121
elif [ "$SHOW_ALL_LOCAL" -eq 1 ] ; then
122+
# dummy data for locals
114123
n_ahead="X" ; n_behind="X" ; remote="n/a" ; ahead_color="$CEVEN" ; behind_color="$CEVEN"
115124
else continue
116125
fi
117126

118127
# populate lists
128+
local=${local:0:$MAX_COL_W} ; remote=${remote:0:$MAX_COL_W} ;
119129
local_msgs=( ${local_msgs[@]} "$local" )
120130
ahead_msgs=( ${ahead_msgs[@]} "$n_ahead" )
121131
behind_msgs=( ${behind_msgs[@]} "$n_behind" )
@@ -131,7 +141,6 @@ do
131141

132142
done < <(git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads)
133143

134-
135144
# compensate for "(ahead )" and "(behind )" to be appended
136145
ahead_col_w=$(( $ahead_col_w + 8 ))
137146
behind_col_w=$(( $behind_col_w + 10 ))
@@ -159,8 +168,8 @@ do
159168
end_col_offset=$(( $remote_col_w - ${#remote_msg} ))
160169

161170
# build output messages and print
162-
local_msg="%$(($local_col_offset))s $(echo -e $SPACER $local_msg)"
163-
ahead_msg="%$(($ahead_col_offset))s $(echo -e $SPACER $ahead_color$ahead_msg$CEND)"
171+
local_msg="%$(($local_col_offset))s $( echo -e $SPACER $local_msg)"
172+
ahead_msg="%$(($ahead_col_offset))s $( echo -e $SPACER $ahead_color$ahead_msg$CEND)"
164173
behind_msg="%$(($behind_col_offset))s $(echo -e $SPACER $behind_color$behind_msg$CEND)"
165174
remote_msg="%$(($remote_col_offset))s $(echo -e $SPACER $remote_msg)"
166175
end_msg="%$(($end_col_offset))s $SPACER"

0 commit comments

Comments
 (0)