-
Notifications
You must be signed in to change notification settings - Fork 39
/
gitdate
executable file
·309 lines (287 loc) · 11 KB
/
gitdate
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
#!/usr/bin/env bash
#_____________________________________________________________________________
# Git Nowhere
#-----------------------------------------------------------------------------
#
# Use: Run as "$ . ./gitdate" before "$ git commit" to manually set
# the date to UTC in order to obscure timezone-based geodata tracking.
#
# If you would always like your timestamps to be obscured for a specific
# project, then you'll need to either source this file BEFORE EACH COMMIT
# while committing code. You *cannot* set it as a git hook, because git
# hooks open child shell processes which are unable to affect the parent
# shell's environment. To make this easier, however, you could set an
# alias in whichever shell you use:
#
# alias gc=". /path/to/gitdate && git commit -v "
#
# NOTE: If you use "$ git commit -S -v" to create GnuPG signatures for your
# git commits, the timestamp on the GnuPG signature will *NOT* match
# the git AuthorDate and CommitDate lines, giving away that you are
# lying. It would also be suspicious looking if you customarily sign
# your commits, and signature(s) were inexplicably missing.
#
# :authors: Isis Lovecuft <[email protected]> 0xA3ADB67A2CDB8B35
# :version: 0.0.4
# :copyright: (c) 2012-2013 Isis Lovecruft
# :license: AGPLv3, see https://www.gnu.org/licenses/agpl-3.0.txt
#
# v0.0.5: It now knows if it's been sourced or executed, and if it was executed,
# it prints the export commands which need to be run on stdout. Because
# they are printed exacted as they should be run.
#
# We *should* be able to coproc that output from other scripts, piping
# stdout of the coproc into the stdin of the other script, and wrap it
# in an exec call, something like this:
#
# GD=`which gitdate`
# { coproc gitdate { $GD "$@" ;} >&3;} 3>&1
# { echo 'Thu Feb 28 11:47:04 2045 -1000' >&${gitdate[1]} ;} >&0
#
# except that is not right yet and still not working.
#
# v0.0.4: Fix so that timezones east of UTC shift backwards.
# Add better date string parsing and some other fixes.
# v0.0.3: Also changes the months and years, because that would suck if your
# commits were accidentally made last year
# v0.0.2: Changes the days too
# v0.0.1: Changes the hours
#_____________________________________________________________________________
## Uncomment for debugging:
#DEBUG=true
## Uncomment for really verbose debugging, and be sure to uncomment the
## the corresponding line at the end
#set -x -
## Git uses the system time settings through mktime(). Do a "$ git log" to see
## the timezone offset for your system. This script gets your timezone offset
## by doing '$ date +"%z"'. If these two values are different, or for some
## reason you would like to manually specify the offset between timezones to
## change, then you can set this shell environment variable:
##
## TIMEOFFSET=-0400
##
## Which would mean that your current timestamps were made in the UTC-0400
## timezone (i.e. 'America/New_York'), and they will be modified by adding four
## hours to change them into UTC.
#TIMEOFFSET=-0400
if [ "x$0" == 'x-bash' ] ; then
if test "$DEBUG" ; then printf "Detected that we were sourced.\n"; fi
elif test -n "${0#gitdate}" ; then
if test "$DEBUG" ; then printf "We weren't sourced.\n"; fi
fi
## git stores datestrings as: Sun Aug 19 08:34:12 2012 -0400
## date uses: Mon May 6 21:00:18 UTC 2013
if test $# -eq 0 ; then
if test "$DEBUG" ; then
printf "We didn't get a datestring argument.\n"
fi
if [ "$#" = '0' ] ; then
DAY=$(date +"%a")
MONTH=$(date +"%b")
DATE=$(date +"%d")
HOUR=$(date +"%H")
MINSEC=$(date +"%M:%S")
YEAR=$(date +"%Y")
TIMEZONE=$(date +"%z")
fi
elif test $# -eq 1 ; then
if test "$DEBUG" ; then
printf "Converting git datestring: %s\n" "$1"
fi
DAY=$(echo "$1" | cut -d ' ' -f 1)
MONTH=$(echo "$1" | cut -d ' ' -f 2)
DATE=$(echo "$1" | cut -d ' ' -f 3)
TIME=$(echo "$1" | cut -d ' ' -f 4)
YEAR=$(echo "$1" | cut -d ' ' -f 5)
TIMEZONE=$(echo "$1" | cut -d ' ' -f 6)
HOUR=$(echo "$TIME" | cut -d ':' -f 1)
MIN=$(echo "$TIME" | cut -d ':' -f 2)
SEC=$(echo "$TIME" | cut -d ':' -f 3)
MINSEC=$(echo "${MIN}:${SEC}")
unset TIME
unset MIN
unset SEC
else
printf "\nUsage:\nTo change a specific commit's date, run as:\n"
printf " . gitdate 'Mon Dec 29 17:45:54 2011 +0800'\n"
printf "or as:\n"
printf " gitdate 'Mon Dec 29 17:45:54 2011 +0800'\n"
printf "and then run the printed export commands to change your shell's\n"
printf "variables.\n"
fi
for mnth in Jan Mar May Jul Aug Oct Dec ; do
if [[ "$mnth" = "$MONTH" ]]; then LONG_MONTH=true ; fi
done
TIMEOFFSET=${TIMEOFFSET:=$TIMEZONE}
TIMEOFFSET=${TIMEOFFSET%%00}
if [[ "${TIMEOFFSET:0:1}" = '-' ]] ; then
TIMEOFFSET=${TIMEOFFSET:1}
IS_WEST=true
elif [[ "${TIMEOFFSET:0:1}" = '+' ]] ; then
TIMEOFFSET=${TIMEOFFSET:1}
IS_EAST=true
else
printf "Error: TIMEOFFSET variable did not start with '-' or '+'"
fi
## 'let' requires the leading zero stripped
if [[ "${TIMEOFFSET:0:1}" = '0' ]] ; then
TIMEOFFSET=${TIMEOFFSET:1}
fi
if test "$IS_WEST" ; then
let SPILLOVER=24-TIMEOFFSET
if [ "$HOUR" -lt "$SPILLOVER" ]; then
let HOUR+=TIMEOFFSET
else
let TILMIDNIGHT=24-HOUR
let HOUR=TIMEOFFSET-TILMIDNIGHT
FALSEDAWN=true
unset TILMIDNIGHT
fi
unset TIMEOFFSET
unset SPILLOVER
# If the hour is one digit, prepend a zero.
if [ "${#HOUR}" -eq "1" ]; then
HOUR=$(printf "%02d" $HOUR)
fi
# If it is tomorrow in UTC, make sure we increment the day.
if test "$FALSEDAWN" ; then
if [ "$DAY" = "Mon" ]; then NEXTDAY=$(echo "Tue") ;
elif [ "$DAY" = "Tue" ]; then NEXTDAY=$(echo "Wed") ;
elif [ "$DAY" = "Wed" ]; then NEXTDAY=$(echo "Thu") ;
elif [ "$DAY" = "Thu" ]; then NEXTDAY=$(echo "Fri") ;
elif [ "$DAY" = "Fri" ]; then NEXTDAY=$(echo "Sat") ;
elif [ "$DAY" = "Sat" ]; then NEXTDAY=$(echo "Sun") ;
elif [ "$DAY" = "Sun" ]; then NEXTDAY=$(echo "Mon") ;
fi
DAY=$NEXTDAY
unset NEXTDAY
if test "$LONG_MONTH" ; then
if [[ "$DATE" -lt "31" ]]; then let DATE+=1 ;
elif [[ "$DATE" -eq "31" ]]; then
if [[ "$MONTH" = "Jan" ]]; then NEXTMONTH=$(echo "Feb") ;
elif [[ "$MONTH" = "Mar" ]]; then NEXTMONTH=$(echo "Apr") ;
elif [[ "$MONTH" = "May" ]]; then NEXTMONTH=$(echo "Jun") ;
elif [[ "$MONTH" = "Jul" ]]; then NEXTMONTH=$(echo "Aug") ;
elif [[ "$MONTH" = "Aug" ]]; then NEXTMONTH=$(echo "Sep") ;
elif [[ "$MONTH" = "Oct" ]]; then NEXTMONTH=$(echo "Nov") ;
elif [[ "$MONTH" = "Dec" ]]; then
NEXTMONTH=$(echo "Jan")
let YEAR+=1
fi
DATE=1
fi
elif [[ "$MONTH" = "Feb" ]] && [[ "$DATE" -lt "28" ]]; then
let DATE+=1
elif [[ "$MONTH" = "Feb" ]] && [[ "$DATE" -eq "28" ]]; then
NEXTMONTH=$(echo "Mar")
DATE=1
else
if [[ "$DATE" -lt "30" ]]; then let DATE+=1 ;
elif [[ "$DATE" -eq "30" ]]; then
if [[ "$MONTH" = "Apr" ]]; then NEXTMONTH=$(echo "May") ;
elif [[ "$MONTH" = "Jun" ]]; then NEXTMONTH=$(echo "Jul") ;
elif [[ "$MONTH" = "Sep" ]]; then NEXTMONTH=$(echo "Oct") ;
elif [[ "$MONTH" = "Nov" ]]; then NEXTMONTH=$(echo "Dec") ;
fi
DATE=1
fi
fi
fi
unset FALSEDAWN
if [[ -n "$NEXTMONTH" ]]; then
MONTH=$NEXTMONTH
fi
unset NEXTMONTH
elif test "$IS_EAST" ; then
if [ "$HOUR" -lt "$TIMEOFFSET" ]; then
let SPILLOVER=24-TIMEOFFSET
let HOUR=HOUR+SPILLOVER
MAKE_YESTERDAY=true
else
## 'let' requires the leading zero stripped
if [[ "${HOUR:0:1}" = '0' ]] ; then
HOUR=${HOUR:1}
fi
let HOUR=HOUR-TIMEOFFSET
fi
unset TIMEOFFSET
unset SPILLOVER
# If the hour is one digit, prepend a zero.
if [ "${#HOUR}" -eq "1" ]; then
HOUR=$(printf "%02d" "$HOUR")
fi
# If it is yesterday in UTC, make sure we decrement the day.
if test "$MAKE_YESTERDAY" ; then
if [ "$DAY" = "Mon" ]; then LASTDAY=$(echo "Sun") ;
elif [ "$DAY" = "Tue" ]; then LASTDAY=$(echo "Mon") ;
elif [ "$DAY" = "Wed" ]; then LASTDAY=$(echo "Tue") ;
elif [ "$DAY" = "Thu" ]; then LASTDAY=$(echo "Wed") ;
elif [ "$DAY" = "Fri" ]; then LASTDAY=$(echo "Thu") ;
elif [ "$DAY" = "Sat" ]; then LASTDAY=$(echo "Fri") ;
elif [ "$DAY" = "Sun" ]; then LASTDAY=$(echo "Sat") ;
fi
DAY=$LASTDAY
unset LASTDAY
if $LONG_MONTH ; then
if [[ "$DATE" -eq "1" ]]; then
if [[ "$MONTH" = "Jan" ]]; then LASTMONTH=$(echo "Dec");
let YEAR=YEAR-1 ;
elif [[ "$MONTH" = "May" ]]; then LASTMONTH=$(echo "Apr") ;
elif [[ "$MONTH" = "Jul" ]]; then LASTMONTH=$(echo "Jun") ;
elif [[ "$MONTH" = "Aug" ]]; then LASTMONTH=$(echo "Jul") ;
elif [[ "$MONTH" = "Oct" ]]; then LASTMONTH=$(echo "Sep") ;
elif [[ "$MONTH" = "Dec" ]]; then LASTMONTH=$(echo "Nov") ;
fi
DATE=30
if [[ "$MONTH" = "Mar" ]]; then LASTMONTH=$(echo "Feb");
DATE=28 ;
fi
else
let DATE=DATE-1
fi
else
if [[ "$DATE" -eq "1" ]]; then
if [[ "$MONTH" = "Feb" ]]; then LASTMONTH=$(echo "Jan") ;
elif [[ "$MONTH" = "Apr" ]]; then LASTMONTH=$(echo "Mar") ;
elif [[ "$MONTH" = "Jun" ]]; then LASTMONTH=$(echo "May") ;
elif [[ "$MONTH" = "Sep" ]]; then LASTMONTH=$(echo "Aug") ;
elif [[ "$MONTH" = "Nov" ]]; then LASTMONTH=$(echo "Oct") ;
fi
DATE=31
else
let DATE=DATE-1
fi
fi
unset LONGMONTH
fi
unset MAKE_YESTERDAY
if [[ -n "$LASTMONTH" ]]; then
MONTH=$LASTMONTH
fi
unset LASTMONTH
fi
unset IS_EAST
unset IS_WEST
unset LONG_MONTH
export GIT_AUTHOR_DATE=$(echo "$DAY $MONTH $DATE $HOUR:$MINSEC $YEAR +0000")
export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE
## only print what we exported if we were not sourced:
if ! [ "x$0" = 'x-bash' ] ; then
if test -n "${0#gitdate}" ; then
if test "$DEBUG" ; then
printf "\nPlease copy and run the following export commands:\n\n"
fi
printf "export GIT_AUTHOR_DATE='%s'\nexport GIT_COMMITTER_DATE='%s'\n" \
"$GIT_AUTHOR_DATE" "$GIT_COMMITTER_DATE"
fi
fi
unset DAY
unset MONTH
unset DATE
unset HOUR
unset MINSEC
unset YEAR
unset TIMEZONE
## If debugging was enabled at the top, make sure it's off when we return
## to a normal shell:
#set +x