-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-merge-from
executable file
·89 lines (74 loc) · 2.45 KB
/
git-merge-from
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
#! /usr/bin/env ruby
# Source: http://gist.github.com/64519
require "open3" # for reading stderr
# A dirt-simple script to merge a pull request from someone on github.
# Andre Lewis, 2009
url=ARGV[0] # Looks like: git://github.com/andre/geokit-gem.git
commit=ARGV[1] # Looks like: 25fe7ea
# quick method to execute a command and print the output
def exe(cmd)
puts "EXECUTING: #{cmd}"
failed=nil
Open3.popen3(cmd) do |stdin, stdout, stderr|
out=stdout.read
err=stderr.read
puts out if out !=''
puts err if err !=''
failed = err=~/^fatal|^error/i
end
# give an option to continue if something went wrong
if failed
puts "Continue? [y|N]"
s=STDIN.gets.chomp.downcase
if s!='y'
puts "STOPPING ..."
exit
end
end
end
# bail if the url is blank or not a git url
if url.nil? || !url =~/^git/
puts <<End
Usage: #{File.basename($0)} git://github.com/foo/bar.git"
Optionally, include a specific commit as the second argument: #{File.basename($0)} git://github.com/foo/bar.git 25fe7ea"
Assumptions:
1. You're currently working on master
2. You've already reviewed the pull request and have decided to include it.
What it does:
1. Adds the remote repo
2. Checks out the remote repo as a local branch
3. Pulls from the remote master or specific commit you provided
4. Switches back to master
5. (with your confirmation) Merges the just-checked-out branch
6. (with your confirmation) Deletes your just-checked-out branch
End
exit
end
# format the commit if the commit argument is non-blank
commit = (commit.nil? || commit == '') ? '' : ":#{commit}"
# extract the branchname, or create it from the url
url=~/^git:\/\/github.com\/(\w+)\//
branch=$1 || url.gsub('git:','').gsub(/\W/i,'')
puts "checking out #{url} as local branch #{branch}"
exe "git remote add -f #{branch} #{url}"
exe "git checkout -b #{branch}/master"
exe "git pull #{branch} master#{commit}"
exe "git checkout master"
merge_command = "git merge #{branch}/master"
delete_branch_command = "git merge #{branch}/master"
puts "Checked out the branch, and now switched back to master. Do you want to merge now? [Y|n]"
s=STDIN.gets.chomp.downcase
if s != 'n'
exe merge_command
puts "merged. delete branch? [Y|n]"
s=STDIN.gets.chomp.downcase
if s != 'n'
exe delete_branch_command
else
puts "to delete: #{delete_branch_command}"
end
else
puts "to merge: #{merge_command}"
puts "to delete_branch: #{delete_branch_command}"
end
puts "to push: git push"