-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_time.sh
68 lines (58 loc) · 1.76 KB
/
git_time.sh
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
#!/bin/bash
#
# Change the date of a previous commit, or make an empty one at a specific
# date.
#
# WARNING, This method will invalidate all current hashes. You WILL have to
# force push. Don't use this script if there are people working in your git
# repository..
#
# The date must follows the format of the 'approxidate' algorithm described
# in this source file :
# https://github.com/git/git/blob/master/date.c
# Or in this manual page :
# https://www.kernel.org/pub/software/scm/git/docs/git-commit.html
# Here are some values :
# Format : WEEK_DAY MONTH DAY HH:MM:SS YYYY UTC
# Weekdays : Sun, Mon, Tue, Wed, Thu, Fri, Sat
# Months : Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
# UTC :
# * Paris : +0100
# * Chicago : -0600
#
#
# Examples :
# git_time.sh 'Nov 26 19:32:10 2017 +0100'
# git_time.sh 'Nov 26 19:32:10 2017 +0100' 16cb996f1c2150d13dfc3f8a14ca2d8b9299fac7
#
# Sources :
# * https://stackoverflow.com/a/454750
#
# Error codes:
# 1: Wrong number of arguments
readonly USAGE='Usage: git_time.sh <DATE>\n git_time.sh <DATE> <COMMIT_HASH>'
# Check parameters' validity
if [ "$#" -lt 1 ]; then
>&2 echo "Wrong number of arguments"
>&2 printf "$USAGE\n"
exit 1
fi
# Unpack parameters
param_date="$1"
param_commit_hash="$2"
previous_author_date="$GIT_AUTHOR_DATE"
previous_committer_date="$GIT_COMMITTER_DATE"
if [ -z $param_commit_hash ]; then
export GIT_AUTHOR_DATE=$param_date
export GIT_COMMITTER_DATE=$param_date
git commit --allow-empty -m "Empty commit"
else
git filter-branch --env-filter \
"if [ $GIT_COMMIT = $param_commit_hash ]
then
export GIT_AUTHOR_DATE=$param_date
export GIT_COMMITTER_DATE=$param_date
fi"
fi
GIT_AUTHOR_DATE="$previous_author_date"
GIT_COMMITTER_DATE="$previous_committer_date"