-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-timetrack
executable file
·348 lines (297 loc) · 10.5 KB
/
git-timetrack
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/bash
# Copyright (C) 2010 Daniel Garcia Moreno <[email protected]>
# Copyright (C) 2010 Eduardo Robles Elvira <[email protected]>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 3 or any later version, incorporated
# herein by reference.
export GIT_NOTES_DISPLAY_REF="refs/notes/timetracker"
function show_help {
echo "usage: git timetrack command [options]"
echo ""
echo "Current timer commands:"
echo " -s, --start start/continue counting time spent"
echo " -k, --start-hacking start/continue counting time spent, won't stop (only reset) on commit"
echo " -p, --stop stop/pause counting time spent"
echo " -r, --reset reset counting time spent"
echo " -g --add <[-]minutes> add (or substract) time from the timer"
echo " -e, --set <minutes> set time spent in minutes"
echo " -ts, --timesince set time spent since last commit"
echo " -c, --current time spent currently in next commit"
echo ""
echo "Commit related commands:"
echo " -a, --amend [<object>] set currently spent time to last commit or to the given one"
echo " -d, --delete [<object>] delete time-spent to last commit or to the given one"
echo " -u, --summary [<opts>] show summary of total time spent. takes similar options as git log"
echo " -l, --log [<opts>] show git log with notes translates to human time"
echo " -gt, --get-time [<object>] gets time from last commit or the given one"
echo ""
echo "Multiple timers commands:"
echo " -st --save-timer <name> save current timer to a named timer for later reuse"
echo " -ut, --use-timer <name> sets an existent timer as current one"
echo " -dt, --del-timer <name> delete an existent timer"
echo " -ti, --timer-info <name> shows a timer information"
echo " -lt, --list-timers list existing timers"
echo ""
echo "Configuration commands:"
echo " -o, --addhooks adds post-commit hook and post-merge to the project"
echo " -n, --config [<remote>] adds config to the project to pull and push timetrack info"
echo " -i, --init setup timetrack environment, hooks and config for origin"
echo " -m, --merge [<remote>] merge timetracker notes from remote branch (origin by default)"
}
function human_time() {
s=$1
if [ -z $s ]; then s=0; fi
m=0
while [ $s -ge 60 ]
do
m=$(( $m + $s / 60 ))
s=$(( $s % 60 ))
done
h=0
while [ $m -ge 60 ]
do
h=$(( $h + $m / 60 ))
m=$(( $m % 60 ))
done
str=""
if [ $h -ne 0 ]; then str=$str" $h hours"; fi
if [ $m -ne 0 ]; then str=$str" $m minutes"; fi
if [ -z "$str" ]
then
str=$str" $s seconds"
else
if [ $s -ne 0 ]; then str=$str" $s seconds"; fi
fi
echo $str
}
function current_time() {
start_date=$(git config --get timetrack.start)
current_date=$(date +%s)
spent_date=$(git config --get timetrack.spent)
if [ -z $spent_date ]; then spent_date=0; fi
if [ $start_date ]
then
spent=$(( $spent_date + $current_date - $start_date ))
else
if [ $spent_date -gt 0 ]
then
spent=$spent_date
fi
fi
echo $spent
}
function user_time_dedication() {
user=$1
shift
alltimes=$(git log --author="$user" $* | grep "Time-spent: " | cut -d":" -f2)
total=0
for t in $alltimes
do
total=$(( $total + $t ))
done
echo $total
}
case $1 in
-s|--start)
git config timetrack.start $(date +%s)
;;
-k|--start-hacking)
git config timetrack.start $(date +%s)
git config timetrack.hacking "true"
;;
-p|--stop)
start_date=$(git config --get timetrack.start)
current_date=$(date +%s)
spent_date=$(git config --get timetrack.spent)
if [ -z $spent_date ]; then spent_date=0; fi
if [ $start_date ]
then
spent=$(( $spent_date + $current_date - $start_date ))
git config --remove-section timetrack 2>/dev/null
git config timetrack.spent $spent
else
echo "Stop before start? I don't think so."
fi
;;
-c|--current)
spent=$(current_time)
if [ -z $spent ]
then
echo "There's no time information :("
else
echo "time spent in next commit: $(human_time $spent)"
hacking=$(git config --get timetrack.hacking)
start_date=$(git config --get timetrack.start)
[ $start_date ] && echo "timer counting" || echo "timer stopped"
[ $hacking ] && echo "hacking mode activated"
fi
;;
-r|--reset)
git config --remove-section timetrack 2>/dev/null
;;
-e|--set)
git config --remove-section timetrack 2>/dev/null
git config timetrack.spent $(( $2 * 60 ))
;;
-g|--add)
spent_date=$(git config --get timetrack.spent)
if [ -z $spent_date ]; then spent_date=0; fi
git config timetrack.spent $(( $2 * 60 + $spent_date ))
;;
-st|--save-timer)
spent=$(current_time)
git config timetrack.$2.spent $spent
;;
-ut|--use-timer)
spent=$(git config timetrack.$2.spent)
[ -z $spent ] && echo "timer $2 not found" && exit 1
git config --remove-section timetrack 2>/dev/null
git config timetrack.spent $spent
;;
-dt|--del-timer)
spent=$(git config timetrack.$2.spent)
[ -z $spent ] && echo "timer $2 not found" && exit 1
git config --remove-section timetrack.$2 2>/dev/null
;;
-ti|---timer-info)
spent=$(git config timetrack.$2.spent)
[ -z $spent ] && echo "timer $2 not found" && exit 1
echo "time spent in $2: $(human_time $spent)"
;;
-lt|--list-timers)
timers=$(git config -l | grep '^timetrack\..*\.' | cut -d . -f 2 | sort -u)
count=0
echo "Saved timers:"
for i in $timers
do
count=$[ $count + 1 ]
spent=$(git config timetrack.$i.spent)
printf " %-25s %s\n" "$i" "$(human_time $spent)"
done
printf "\n%s timers\n" "$count"
;;
-a|--amend)
git timetrack -p > /dev/null
spent=$(git config timetrack.spent)
hacking=$(git config --get timetrack.hacking)
git config --remove-section timetrack 2> /dev/null
if [ $spent ]
then
git notes --ref timetracker add -f -m "Time-spent: $spent" $2
fi
if [ $hacking ]
then
git config timetrack.start $(date +%s)
git config timetrack.hacking $hacking
fi
;;
-d|--delete)
git notes --ref timetracker remove $2
;;
-u|--summary)
shift
allusers=$(git log --oneline --pretty="format: %an <%ae>" $* | sort | uniq)
alltimes=$(git log $* | grep "Time-spent:" | cut -d":" -f2)
total=0
for t in $alltimes
do
total=$(( $total + $t ))
done
echo "total time spent by authors"
printf "$allusers\n" | while read author
do
dedication=$(user_time_dedication "$author" $*)
[ $dedication -ne 0 ] && printf " %-49s %s\n" "$author" "$(human_time $dedication)"
done
printf "\n%-50s %s\n" "total time spent in this project" "$(human_time $total)"
printf "%-50s %s\n" "time spent in next comit" "$(human_time $(current_time))"
hacking=$(git config --get timetrack.hacking)
if [ $hacking ]
then
echo "hacking mode activated"
fi
;;
-l|--log)
shift
git log --color $* | sed "s/Time-spent: \([0-9]\+\)/git timetrack --human-time \1/e" | less -R
;;
-gt|--get-time)
time=$(git log $2 -1 | grep "Time-spent:" | cut -d":" -f2)
[ -z $time ] && echo "Error, the specified commit does not have a Time-spent annotation" && exit 1
git config --remove-section timetrack 2>/dev/null
git config timetrack.spent $time
;;
--human-time)
echo " Time-spent: "$(human_time $2)
;;
-o|--addhooks)
gitdir="$(git rev-parse --git-dir)/hooks"
if [ -d $gitdir ]
then
githook="$gitdir/post-commit"
echo '#!/bin/bash
hacking=$(git config timetrack.hacking)
git timetrack -p > /dev/null
spent=$(git config timetrack.spent)
git config --remove-section timetrack 2> /dev/null
if [ $spent ]
then
git notes --ref timetracker add -m "Time-spent: $spent"
if [ $hacking ]
then
git config timetrack.start $(date +%s)
git config timetrack.hacking $hacking
fi
fi' > $githook
chmod +x $githook
githook="$gitdir/post-merge"
echo '#!/bin/bash
GITDIR=$(git rev-parse --git-dir)
for remote in $GITDIR/refs/notes/remotes/*
do
git timetrack --merge "$(basename $remote)"
done' > $githook
chmod +x $githook
echo "hooks created"
else
echo "That isn't a git root directory. I wont do anything"
fi
;;
-n|--config)
remote=$2
if [ -z $remote ]
then
remote=origin
fi
git config --add remote.$remote.push "refs/notes/timetracker:refs/notes/timetracker"
git config --add remote.$remote.push "refs/heads/master:refs/heads/master"
git config --add remote.$remote.fetch "+refs/notes/timetracker:refs/notes/remotes/$remote/timetracker"
git config notes.rewriteRef "refs/notes/timetracker"
git config notes.displayRef "refs/notes/timetracker"
echo "git timetrack configured, now, run git pull if you don't want problems"
;;
-i|--init)
git timetrack --addhooks
git timetrack --config
;;
-m|--merge)
remote=$2
if [ -z $remote ]
then
remote=origin
fi
remote_ref="refs/notes/remotes/$remote/timetracker"
git notes --ref timetracker merge $remote_ref
;;
-ts|--timesince)
current_date=$(date +%s)
last=$(git log --date=raw -n 1 | grep Date: | sed 's/ \+/ /g' | cut -d " " -f2)
t=$(( current_date - last ))
git timetrack -s
git timetrack -e $(( $t / 60 ))
;;
*)
show_help
;;
esac