-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit-mergeup
executable file
·81 lines (64 loc) · 1.76 KB
/
git-mergeup
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
#!/usr/bin/env ruby
def usage(error = nil)
STDERR.puts "#{error}\n\n" if error
STDERR.puts <<-EOF
Usage: #{$0}
Ensures that the remote, origin, has changes up through production and
staging branches.
To set the mergeup path:
git config mergeup.path master,next
Script must be invoked from the base directory of a git repository.
EOF
end
if ARGV[0] == "--help"
usage
exit 1
end
class FailedToExecute < RuntimeError; end
def run(cmd)
puts "Executing: `#{cmd}`"
system(cmd)
raise(FailedToExecute, "Failed to execute `#{cmd}`") unless $? == 0
true
end
git_directory =
case
when File.file?(".git")
File.read(".git").gsub("gitdir: ", "").strip
when File.directory?(".git")
".git"
else
STDERR.puts "ERROR: You must run this command from the root directory of a git repository\n\n"
exit 1
end
merge_path = %x{git config mergeup.path}.chomp.split(",")
if merge_path.empty?
STDERR.puts <<-EOF
You must set config variable mergeup.path.
IE:
git config mergeup.path master,next
EOF
exit 2
end
current_branch = File.read("#{git_directory}/HEAD").strip.gsub("ref: refs/heads/", "")
puts "current branch is #{current_branch}"
unless %x{git diff --ignore-submodules=untracked}.strip.empty?
STDERR.puts "You have existing changes. Stash them and rerun."
exit 1
end
begin
run("git fetch")
run("git checkout #{merge_path.first}")
run("git merge origin/#{merge_path.first}")
merge_path[1..-1].zip(merge_path[0..-2]) do |merge_to, merge_from|
run("git checkout #{merge_to}")
run("git merge origin/#{merge_to}")
run("git merge #{merge_from}")
end
run("git push origin #{merge_path * ' '}")
run("git checkout #{current_branch}")
rescue FailedToExecute => e
puts e
puts "Please resolve the issue and try again"
exit 1
end