-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-todo
executable file
·280 lines (219 loc) · 6.06 KB
/
git-todo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env bash
# option
OPTS_BASE='master'
# setup color
# msysgit has no tput, I am looking for getting color codes for mingw32 env
if [ `expr "$(uname)" : "MING"` = 0 ]; then
underline=$(tput sgr 0 1)
bold=$(tput bold)
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
normal=$(tput sgr0)
fi
get_short_ref_colored() {
local full_ref=$1
if [ `expr "$full_ref" : ".*refs/remotes/"` != 0 ]; then
echo -n "$yellow$(get_short_ref $full_ref)"
elif [ `expr "$full_ref" : ".*refs/tags/"` != 0 ]; then
echo -n "$normal$(get_short_ref $full_ref)"
else
echo -n "$cyan$(get_short_ref $full_ref)"
fi
}
get_short_ref() {
local ref=$1
ref=${ref#refs/heads/} #for local refs
ref=${ref#refs/remotes/} #for remote refs
ref=${ref#refs/tags/} #for tags
echo $ref
}
get_full_ref() {
local short=$1
for full_ref in $all_refs; do
local shorten=$(get_short_ref $full_ref)
if [ "$shorten" = "$short" ]; then
echo $full_ref
break
fi
done
}
init_refs() {
local args_base=$1
all_refs=`git show-ref| awk '{print $2}'`
# get TODO_BASE_FULL
local base_ref=''
if [ "$args_base" = "" ]; then
base_ref=`git config todo.base`
else
base_ref="$args_base"
fi
if [ "$base_ref" = "" ]; then
base_ref=$OPTS_BASE
fi
TODO_BASE_FULL=$(get_full_ref $base_ref)
if [ "$TODO_BASE_FULL" = "" ]; then
echo "invalid base ref name($base_ref)"
exit 1
fi
}
get_distance() {
local base=$1
local ref=$2
local base_mark=""
if [ "$ref" = "$base" ]; then
base_mark="[@]"
fi
local ahead=`echo $(git rev-list $base..$ref | wc -l)`
local behind=`echo $(git rev-list $ref..$base | wc -l)`
if [ "$ahead" = "0" ]; then
ahead=""
else
ahead="[+$ahead]"
fi
if [ "$behind" = "0" ]; then
behind=""
else
behind="[-$behind]"
fi
echo "$base_mark$ahead$behind"
}
######### refs
#result:
refs=""
TODO_REF='local'
init_opts() {
while getopts rta opt; do
case $opt in
r)
if [ "$TODO_REF" != "all" ]; then
TODO_REF="remote"
fi
;;
a) TODO_REF="all";;
t) TODO_REF="tag";;
?) echo "usage: git todo [-rta] base";exit 1;;
*) echo "$opt";exit 1;;
esac
done
shift $(( $OPTIND - 1 ))
ARGS_BASE=$1
}
get_refs() {
local TODO_BASE_FULL=$1
local ret=""
for full_ref in $all_refs; do
has_merge_base=`echo $(git merge-base $TODO_BASE_FULL $full_ref | wc -l)`
if [ "$has_merge_base" = "0" ]; then
continue # ex) gh_pages
fi
case "$TODO_REF" in
"local")
if [ `expr "$full_ref" : ".*refs/remotes/"` != 0 ]; then
continue
fi
if [ `expr "$full_ref" : ".*refs/tags/"` != 0 ]; then
continue
fi
;;
"remote")
if [ `expr "$full_ref" : ".*refs/tags/"` != 0 ]; then
continue
fi
;;
"tag")
if [ `expr "$full_ref" : ".*refs/remotes/"` != 0 ]; then
continue
fi
;;
esac
if [ "$full_ref" != "$TODO_BASE_FULL" ]; then
ret="$ret $full_ref"
fi
done
## make sure base branch first
echo "$TODO_BASE_FULL $ret"
}
########## checkouted
checkouted_ref=`git symbolic-ref HEAD`
get_checkouted() {
local full_ref=$1
if [ "$full_ref" = "$checkouted_ref" ]; then
echo "${green}*${normal}"
else
echo "-"
fi
}
######### distance
#result:
max_head_len=0
distance_store=""
distances() {
local key=$1
local value=$2
if [ "$value" != "" ]; then
distance_store="$distance_store $key:$value"
else
for pair in $distance_store; do
local pair_key=${pair%%:*}
local pair_value=${pair##*:}
if [ "$pair_key" = "$key" ]; then
echo $pair_value
break
fi
done
fi
}
init_distance(){
local refs=$1
for full_ref in $refs; do
#distance
distance=$(get_distance $TODO_BASE_FULL $full_ref)
distances $full_ref $distance
done
}
get_max_head_len() {
local refs=$1
for full_ref in $refs; do
local ref=$(get_short_ref $full_ref)
local distance=$(distances $full_ref)
local head="$distance $ref"
local head_len="${#head}"
if [ $max_head_len -lt $head_len ]; then
local max_head_len=$head_len
fi
done
echo $max_head_len
}
main() {
### init
init_opts "$@"
init_refs $ARGS_BASE
local refs=`get_refs $TODO_BASE_FULL`
init_distance "$refs"
local max_head_len=$(get_max_head_len "$refs")
### main
for full_ref in $refs; do
ref=$(get_short_ref $full_ref)
#last commit info
#msg=`git log --abbrev-commit --date=relative --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s' -n 1 $ref`
local msg=`git log --abbrev-commit --date=relative --pretty=format:'%Cred%h%Creset - %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' -n 1 $full_ref`
#time=`git log --pretty=format:'%Cgreen(%cr)' -n 1 $full_ref`
#who=`git log --pretty=format:'%C(bold blue)<%an>%Creset' -n 1 $full_ref`
distance=$(distances $full_ref)
#echo padding
head="$distance $ref"
head_len=${#head}
padding_len=$(expr $max_head_len - $head_len)
#echo
padding=`printf "%${padding_len}s" ""` #padding
head_colored="${magenta}$distance $(get_short_ref_colored $full_ref)${normal}"
echo -e "$padding$head_colored $(get_checkouted $full_ref) $msg $time $who"
done
}
main "$@"