-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-redate
executable file
·213 lines (175 loc) · 4.4 KB
/
git-redate
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
#!/bin/bash
echo "git redate.."
is_git_repo() {
git rev-parse --show-toplevel > /dev/null 2>&1
result=$?
if test $result != 0; then
>&2 echo 'Not a git repo!'
exit $result
fi
}
is_git_repo
make_editor_choice() {
echo "Which editor do you want to use for this repo?\n";
echo "0. vim"
echo "1. vi";
echo "2. nano";
echo "3. Your own"
echo "You Choose: ";
read CHOOSE_EDITOR
}
get_editor_executable() {
echo "What is the path to your prefered test editor?\n";
read EDITOR_PATH
}
is_has_editor() {
SETTINGS_FILE="~/.redate-settings";
if [ -f "$SETTINGS_FILE" ]
then
OUR_EDITOR=$(cat ${SETTINGS_FILE});
elif [ ! -z "$EDITOR" ]
then
OUR_EDITOR="$EDITOR";
else
make_editor_choice;
if [ ${CHOOSE_EDITOR} == 3 ] || [ ${CHOOSE_EDITOR} == "3" ]; then
get_editor_executable
OUR_EDITOR=${EDITOR_PATH}
elif [ ${CHOOSE_EDITOR} == 1 ] || [ ${CHOOSE_EDITOR} == "1" ]; then
OUR_EDITOR="vi";
elif [ ${CHOOSE_EDITOR} == 0 ] || [ ${CHOOSE_EDITOR} == "0" ]; then
OUR_EDITOR="vim";
else
OUR_EDITOR="nano";
fi
echo ${OUR_EDITOR} > ${SETTINGS_FILE}
fi
}
is_has_editor
ALL=0
DEBUG=0
LIMITCHUNKS=20
while [[ $# -ge 1 ]]
do
key="$1"
case $key in
-c| --commits)
COMMITS="$2"
if [ -z "${COMMITS}" ]; then COMMITS="5"; fi;
shift
;;
-l| --limit)
LIMITCHUNKS="$2"
if [ -z "${LIMITCHUNKS}" ]; then LIMITCHUNKS="20"; fi;
shift
;;
-d| --debug)
DEBUG=1
shift
;;
-a| --all)
ALL=1
shift
;;
*)
# unknown option
;;
esac
shift
done
die () {
echo >&2 `basename $0`: $*
exit 1
}
tmpfile=$(mktemp gitblah-XXXX)
[ -f "$tmpfile" ] || die "could not get tmpfile=[$tmpfile]"
trap "rm -f $tmpfile" EXIT
datefmt=%cI
if [ "`git log -n1 --pretty=format:"$datefmt"`" == "$datefmt" ];
then
datefmt=%ci
fi
if [ "${ALL}" -eq 1 ];
then
git log --pretty=format:"$datefmt | %H | %s" > $tmpfile;
else
if [ -n "${COMMITS+set}" ];
then git log -n ${COMMITS} --pretty=format:"$datefmt | %H | %s" > $tmpfile;
else git log -n 5 --pretty=format:"$datefmt | %H | %s" > $tmpfile;
fi
fi
${VISUAL:-${EDITOR:-${OUR_EDITOR}}} $tmpfile
ITER=0
COLITER=0
declare -a COLLECTION
COUNTCOMMITS=$(awk 'END {print NR}' $tmpfile)
while read commit || [ -n "$commit" ]; do
IFS="|" read date hash message <<< "$commit"
shopt -s nocasematch
if [[ "$date" == 'now' ]]; then
date=$(date +%Y-%m-%dT%H:%M:%S%z);
fi
shopt -u nocasematch
if [ "$datefmt" == "%cI" ]
then
DATE_NO_SPACE="$(echo "${date}" | tr -d '[[:space:]]')"
else
DATE_NO_SPACE="$(echo "${date}")"
fi
COMMIT_ENV=$(cat <<-END
if [ \$GIT_COMMIT = $hash ];
then
export GIT_AUTHOR_DATE="$DATE_NO_SPACE"
export GIT_COMMITTER_DATE="$DATE_NO_SPACE";
fi;
END
)
((ITER++))
if [ "${DEBUG}" -eq 1 ] && [ $((ITER % LIMITCHUNKS)) == $((LIMITCHUNKS - 1)) ];
then
echo "Chunk $COLITER Finished"
fi
if [ $((ITER % LIMITCHUNKS)) == 0 ]
then
((COLITER++))
if [ "${DEBUG}" -eq 1 ];
then
echo "Chunk $COLITER Started"
fi
fi
COLLECTION[$COLITER]=${COLLECTION[COLITER]}"$COMMIT_ENV"
if [ "${DEBUG}" -eq 1 ]
then
echo "Commit $ITER/$COUNTCOMMITS Collected"
fi
done < $tmpfile
ITERATOR=0
for each in "${COLLECTION[@]}"
do
((ITERATOR++))
if [ "${ALL}" -eq 1 ];
then
if [ "${DEBUG}" -eq 1 ];
then
echo "Chunk $ITERATOR/"${#COLLECTION[@]}" Started"
git filter-branch -f --env-filter "$each" -- --all
echo "Chunk $ITERATOR/"${#COLLECTION[@]}" Finished"
else
git filter-branch -f --env-filter "$each" -- --all >/dev/null
fi
else
if [ "${DEBUG}" -eq 1 ];
then
echo "Chunk $ITERATOR/"${#COLLECTION[@]}" Started"
git filter-branch -f --env-filter "$each" HEAD~${COMMITS}..HEAD
echo "Chunk $ITERATOR/"${#COLLECTION[@]}" Finished"
else
git filter-branch -f --env-filter "$each" HEAD~${COMMITS}..HEAD >/dev/null
fi
fi
done
if [ $? = 0 ] ; then
echo "Git commit dates updated. Run 'git push -f BRANCH_NAME' to push your changes."
else
echo "Git redate failed. Please make sure you run this on a clean working directory."
fi