-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-rewrite-dates
executable file
·52 lines (41 loc) · 1.3 KB
/
git-rewrite-dates
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
#!/usr/bin/env ruby
#
# git-rewrite-dates
# Rewrites the author-date and committer-date on commits you haven't pushed to
# the present time, after prompting for confirmation. Use -q or --quiet to skip
# the confirmation.
#
# Usage:
# git rewrite-dates [-q]
def dirty?
`git diff-files --quiet`
$?.exitstatus.nonzero?
end
def quiet?
abort "Too many arguments" if ARGV.size > 1
quiet = ARGV[0] =~ /^-q|--quiet$/
abort "Unknown argument #{ARGV[0]}" if quiet.nil? && ARGV.any?
quiet
end
def print_notpushed
system "git notpushed" unless quiet?
end
def refs_notpushed_chronologically
`git notpushed --pretty=format:%H`.split("\n").map { |sha| sha.strip }.reverse
end
def rewrite_local_commit_timestamps
commit_count = refs_notpushed_chronologically.size
commit_count.times do |i|
ref = refs_notpushed_chronologically[i]
env_filter = %Q|if [ "$GIT_COMMIT" = "#{ref}" ]; then export GIT_AUTHOR_DATE="#{Time.now + i}" && export GIT_COMMITTER_DATE="#{Time.now + i}"; fi|
`git filter-branch --force --env-filter '#{env_filter}' #{ref}~..HEAD`
end
end
abort "Cannot rewrite branch with a dirty working directory." if dirty?
print_notpushed
unless quiet?
print "\nChange these commits' dates to now? [y/N] "
exit unless gets =~ /y(es)?/i
end
rewrite_local_commit_timestamps
print_notpushed