Skip to content

Commit 674c566

Browse files
committed
add HEAD timestamps
1 parent 6c61363 commit 674c566

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

git-branch-status

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
# this script prints out pretty git branch sync status reports
1010

11-
# TODO: add HEAD dates
12-
# TODO: data header
13-
1411

1512
read -r -d '' USAGE <<-'USAGE'
1613
usage: git-branch-status
@@ -36,13 +33,18 @@ usage: git-branch-status
3633
# show the current branch
3734
$ git-branch-status -b
3835
$ git-branch-status --branch
39-
| current-branch | (behind 0) | (ahead 2) | origin/current-branch |
36+
| current-branch | (behind 0) | (ahead 2) | origin/current-branch |
4037
4138
# show a specific branch
4239
$ git-branch-status specific-branch
4340
$ git-branch-status -b specific-branch
4441
$ git-branch-status --branch specific-branch
45-
| specific-branch | (behind 0) | (ahead 2) | origin/specific-branch |
42+
| specific-branch | (behind 0) | (ahead 2) | origin/specific-branch |
43+
44+
# show the timestamp of each HEAD
45+
$ git-branch-status -d
46+
$ git-branch-status --dates
47+
| 1999-12-31:master | (behind 2) | (ahead 0) | 2000-01-01:origin/master |
4648
4749
# print this usage message
4850
$ git-branch-status -h
@@ -55,42 +57,57 @@ USAGE
5557

5658
function get_refs
5759
{
58-
echo "`git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads 2> /dev/null`"
60+
git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads 2> /dev/null
5961
}
6062

6163
function get_status
6264
{
6365
git rev-list --left-right ${local}...${remote} -- 2>/dev/null
6466
}
6567

66-
function current_branch() { echo $(git rev-parse --abbrev-ref HEAD) ; }
68+
function current_branch
69+
{
70+
git rev-parse --abbrev-ref HEAD
71+
}
6772

68-
function is_current_branch()
73+
function is_current_branch # (a_branch_name)
6974
{
7075
if [ "$1" == "$(current_branch)" ] ; then echo 1 ; else echo 0 ; fi ;
7176
}
7277

73-
function does_branch_exist()
78+
function does_branch_exist # (a_branch_name)
7479
{
75-
is_known_branch=$(git branch | grep -G "^ $1$")
80+
is_known_branch=$(git branch | grep -G "^ $1$") # all but current
7681
[ $(is_current_branch $1) -o $is_known_branch ] && echo 1 || echo 0
7782
}
7883

79-
function set_filter_or_die
84+
function set_filter_or_die # (a_branch_name)
8085
{
8186
if [ $(does_branch_exist $1) ]
8287
then branch=$1
8388
else echo "no such branch: '$1'" ; exit ;
8489
fi
8590
}
8691

92+
function get_head_date # (a_commit_ref)
93+
{
94+
author_date=$(git log -n 1 --format=format:"%ai" $1 2> /dev/null)
95+
(($SHOW_DATES)) && [ "$author_date" ] && echo "${author_date:0:10}:"
96+
}
97+
98+
function get_commit_msg # (a_commit_ref)
99+
{
100+
git log -n 1 --format=format:"%s" $1
101+
}
102+
87103

88104
### switches ###
89105

90106
if [ $1 ] ; then
91107
if [ "$1" == "-a" -o "$1" == "--all" ] ; then readonly SHOW_ALL=1 ;
92108
elif [ "$1" == "-b" -o "$1" == "--branch" ] ; then
93109
if [ $2 ] ; then set_filter_or_die $2 ; else branch=$(current_branch) ; fi ;
110+
elif [ "$1" == "-d" -o "$1" == "--dates" ] ; then readonly SHOW_DATES=1 ;
94111
elif [ "$1" == "-h" -o "$1" == "--help" ] ; then echo "$USAGE" ; exit ;
95112
else set_filter_or_die $1
96113
fi
@@ -101,7 +118,7 @@ fi
101118

102119
readonly SHOW_ALL_LOCAL=$(($SHOW_ALL + 0)) # also show branches that have no upstream
103120
readonly SHOW_ALL_REMOTE=$(($SHOW_ALL + 0)) # also show branches that are up to date
104-
readonly MAX_COL_W=25
121+
readonly MAX_COL_W=27
105122
readonly CWHITE='\033[0;37m'
106123
readonly CGREEN='\033[0;32m'
107124
readonly CYELLOW='\033[1;33m'
@@ -169,10 +186,10 @@ do
169186
fi
170187

171188
# populate lists
172-
local_msg="${local:0:$MAX_COL_W}"
189+
local_msg="$(get_head_date $local)$local" ; local_msg="${local_msg:0:$MAX_COL_W}" ;
173190
behind_msg="$n_behind"
174191
ahead_msg="$n_ahead"
175-
remote_msg="${remote:0:$MAX_COL_W}"
192+
remote_msg="$(get_head_date $remote)$remote" ; remote_msg="${remote_msg:0:$MAX_COL_W}" ;
176193
local_msgs=( ${local_msgs[@]} "$local_msg" )
177194
behind_msgs=( ${behind_msgs[@]} "$behind_msg" )
178195
ahead_msgs=( ${ahead_msgs[@]} "$ahead_msg" )
@@ -190,9 +207,11 @@ do
190207

191208
done < <(get_refs)
192209

193-
# compensate width for "(ahead )" and "(behind )" to be appended
194-
ahead_w=$(( $ahead_w + 8 ))
195-
behind_w=$(( $behind_w + 9 ))
210+
# compensate width for dates and "(ahead )" and "(behind )" to be appended
211+
# local_w=$(( $local_w + 11 ))
212+
ahead_w=$(( $ahead_w + 8 ))
213+
behind_w=$(( $behind_w + 9 ))
214+
# remote_w=$(( $remote_w + 11 ))
196215

197216
# pretty print results
198217
for (( result_n = 0 ; result_n < ${#local_msgs[@]} ; result_n++ ))
@@ -208,6 +227,7 @@ do
208227
remote_color="${remote_colors[$result_n]}"
209228

210229
# format data representation
230+
# echo "this_branch=$this_branch current_branch=$(current_branch)" not_current_branch
211231
if (($(is_current_branch $this_branch))) ; then star=$STAR ; else star=" " ; fi ;
212232
local_msg="$this_branch"
213233
behind_msg="(behind $n_behind)"

0 commit comments

Comments
 (0)