-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-pushone
executable file
·61 lines (52 loc) · 1.84 KB
/
git-pushone
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
#!/usr/bin/env ruby
#
# git-pushone
# Rewrites the author-date and committer-date on commits you haven't pushed to
# the present time, and then pushes the first commit. Optionally allows
# non-fast-forwards with --force. To push to a remote other than "origin",
# specify it on the command line. Assumes you're pushing to a remote branch with
# the same name as your current branch.
#
# TODO: if the remote's name is available from git-config's
# branch.<current>.remote, use that instead of assuming "origin"
#
# Usage:
# git pushone [-f] [remote]
#
def get_force_and_remote_from_arguments
abort "Too many arguments" if ARGV.size > 2
force, remote = nil, nil
force = ARGV[0] =~ /^-f|--force$/
if ARGV[1].nil? && force.nil?
remote = ARGV[0]
elsif !force.nil?
remote = ARGV[1]
end
abort "Unknown argument #{ARGV[0]}" if force.nil? && remote.nil? && ARGV.any?
remote = "origin" if remote.nil?
ARGV.clear
[force, remote]
end
def current_branch
`git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||'`.strip
end
def ref_to_push
`git notpushed --pretty=format:%H`.split("\n").map { |sha| sha.strip }.last
end
def bold_text(message = "")
bold, reset = "\e[1m", "\e[0m"
"#{bold}#{message}#{reset}"
end
# Show the commit to be pushed
system "GIT_PAGER=cat git log #{ref_to_push}~..#{ref_to_push} --pretty=tformat:'%Cred%h%Creset %s (%ar)'"
# Get confirmation
force, remote = get_force_and_remote_from_arguments
print "\nRewrite local commit dates and #{'force-' if force}push this commit to #{remote}/#{current_branch}? [y/N] "
exit unless gets =~ /y(es)?/i
# Rewrite commit dates to the present
output = `git rewrite-dates --quiet`
abort output if $?.exitstatus.nonzero?
# Push the commit
command = "git push #{'--force' if force} #{remote} #{ref_to_push}:#{current_branch}".squeeze(" ")
puts "\n" + bold_text(command) + "\n\n"
system(command)