-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathgit-sync-commit-date
executable file
·54 lines (46 loc) · 1.17 KB
/
git-sync-commit-date
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
#!/bin/sh
set -e
usage () {
echo "usage: git sync-commit-date [-fq]" >&2
echo >&2
echo "Rewrites the HEAD commit by setting the commit date to the author date." >&2
echo >&2
echo "Options:" >&2
echo "-f Don't verify" >&2
echo "-q Be quiet" >&2
echo "-h Show this help" >&2
echo >&2
echo "<commit> defaults to HEAD"
}
verbose=1
force=0
while getopts fqh flag; do
case "$flag" in
f) force=1;;
q) verbose=0;;
h) usage; exit 2;;
esac
done
shift $(($OPTIND - 1))
# Enforce we're in a clean work tree
git is-clean -iv
author_date=$(git log --pretty='%aI' -1 HEAD)
commit_date=$(git log --pretty='%cI' -1 HEAD)
if [ "$author_date" = "$commit_date" ]; then
if [ $verbose -eq 1 ]; then
echo "Already the same" >&2
fi
exit 0
fi
if [ $force = 0 ]; then
echo "Reset last commit date to $author_date? [yN] " >&2
read answer
if [ "$answer" != "y" ]; then
exit 1
fi
fi
OLD_SHA=$(git sha -s)
GIT_COMMITTER_DATE="$author_date" git commit --quiet --no-verify --amend --allow-empty -C HEAD
if [ $verbose -eq 1 ]; then
echo "Created $(git sha -s) (previous was ${OLD_SHA})" >&2
fi