Module: GitPr
- Defined in:
- lib/git_pr.rb,
lib/git_pr/diff.rb,
lib/git_pr/merge.rb,
lib/git_pr/github.rb,
lib/git_pr/version.rb,
lib/git_pr/pull_request.rb
Defined Under Namespace
Modules: GitHub
Classes: PullRequest
Constant Summary
collapse
- VERSION =
"0.0.13"
Class Method Summary
collapse
Class Method Details
.diff ⇒ Object
3
4
5
|
# File 'lib/git_pr/diff.rb', line 3
def self.diff
end
|
.ensure_remote_for_project(git, username, ssh_url, git_url) ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/git_pr.rb', line 41
def self.ensure_remote_for_project git, username, ssh_url, git_url
remote = git.remotes.find { |r| [git_url, ssh_url].include? r.url }
unless remote
puts "Adding remote '#{username}' from #{ssh_url}"
remote = git.add_remote username, ssh_url
end
remote
end
|
.ensure_remotes_for_pull_request(git, pull) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/git_pr/merge.rb', line 5
def self.ensure_remotes_for_pull_request git, pull
source_remote = GitPr.ensure_remote_for_project(git,
pull[:head][:user][:login],
pull[:head][:repo][:ssh_url],
pull[:head][:repo][:git_url])
target_remote = GitPr.ensure_remote_for_project(git,
pull[:base][:user][:login],
pull[:base][:repo][:ssh_url],
pull[:base][:repo][:git_url])
[source_remote, target_remote]
end
|
.get_char ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'lib/git_pr.rb', line 26
def self.get_char
state = `stty -g`
`stty raw -echo -icanon isig`
STDIN.getc.chr
ensure
`stty #{state}`
puts ""
end
|
.merge_pull_cleanly(git, pull) ⇒ Object
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/git_pr/merge.rb', line 19
def self.merge_pull_cleanly git, pull
pull_number = pull[:number]
source_branch = pull[:head][:ref]
source_repo_ssh_url = pull[:head][:repo][:git_url]
source_repo_clone_url = pull[:head][:repo][:clone_url]
target_branch = pull[:base][:ref]
target_repo_ssh_url = pull[:base][:repo][:git_url]
target_repo_clone_url = pull[:base][:repo][:clone_url]
puts "Merging #{pull_summary(pull)}".cyan
puts "#{target_repo_ssh_url}/#{target_branch} <= #{source_repo_ssh_url}/#{source_branch}\n".cyan
source_remote, target_remote = self.ensure_remotes_for_pull_request git, pull
puts "Fetching latest changes from '#{source_remote}'"
source_remote.fetch
unless target_remote.name == source_remote.name
puts "Fetching latest changes from '#{target_remote}'"
target_remote.fetch
end
puts "Update branch '#{target_branch}' from remote"
GitPr.run_command "git checkout -q #{target_branch}"
GitPr.run_command "git pull --no-rebase --ff-only", :failure => lambda {
"Unable to update local target branch '#{target_branch}'. Please repair manually before continuing.".red
}
remote_target_branch = "#{target_remote}/#{target_branch}"
if git.diff("remotes/#{remote_target_branch}", target_branch).any?
puts "Local branch '#{target_branch}' differs from remote branch '#{remote_target_branch}'. Please reconcile before continuing.".red
exit -1
end
remote_source_branch = "#{source_remote}/#{source_branch}"
if git.is_local_branch? source_branch and
git.diff("remotes/#{remote_source_branch}", source_branch).any?
puts "Local branch '#{source_branch}' differs from remote branch '#{remote_source_branch}'. Please reconcile before continuing.".red
exit -1
end
rebase_branch = "#{source_branch}-rebase"
puts "Create temporary branch '#{rebase_branch}'"
if git.is_local_branch? rebase_branch
puts "Local rebase branch '#{rebase_branch}' already exists. Please remove before continuing.".red
exit -1
end
GitPr.run_command "git checkout -q -b #{rebase_branch} #{remote_source_branch}"
at_exit do
if git.is_local_branch? rebase_branch
puts "Removing temporary branch #{rebase_branch}" if $verbose
GitPr.run_command "git checkout -q #{target_branch}"
GitPr.run_command "git branch -D #{rebase_branch}"
end
end
puts "Rebasing '#{rebase_branch}' on top of '#{target_branch}'"
GitPr.run_command "git rebase #{target_branch} 2>&1", :failure => lambda {
GitPr.run_command "git rebase --abort"
puts "Unable to automatically rebase #{remote_source_branch} on top of #{target_branch}. Rebase manually and push before trying again.".red
puts "Run: " + "git checkout #{source_branch}".yellow
puts " " + "git rebase #{target_branch}".yellow + " and fix up any conflicts."
puts " " + "git push --force-with-lease".yellow
}
puts "Pushing changes from '#{rebase_branch}' to '#{source_remote.name}/#{source_branch}'"
GitPr.run_command "git push --force-with-lease #{source_remote.name} HEAD:#{source_branch} 2>&1"
puts "Merging changes from '#{rebase_branch}' to '#{target_branch}'"
GitPr.run_command "git checkout -q #{target_branch}"
commit_message = "Merge \#{pull_summary(pull)}\n\n\#{pull[:body]}\n"
GitPr.run_command "git merge --no-ff #{rebase_branch} -m #{Shellwords.escape commit_message}"
puts "\nVerify that the merge looks clean:\n".cyan
origin_parent = `git rev-list --abbrev-commit --parents -n 1 #{target_remote}/#{target_branch}`.split().last
GitPr.run_command "git log --graph --decorate --pretty=oneline --abbrev-commit --color #{target_branch} #{origin_parent}..#{target_branch}", :force_print_output => true
if GitPr.prompt "\nDo you want to proceed with the merge (y/n)? ".cyan
puts "Pushing changes to '#{target_remote}'"
GitPr.run_command "git push #{target_remote} #{target_branch} 2>&1"
if GitPr.prompt "\nDo you want to delete the feature branch (y/n)? ".cyan
source_branch_sha = git.branches["#{source_remote}/#{source_branch}"].gcommit.sha[0..6]
GitPr.run_command "git push #{source_remote} :#{source_branch} 2>&1"
if git.is_local_branch? source_branch
source_branch_sha = git.branches[source_branch].gcommit.sha[0..6]
GitPr.run_command "git branch -D #{source_branch}"
end
puts "Feature branch '#{source_branch}' deleted. To restore it, run: " + "git branch #{source_branch} #{source_branch_sha}".green
end
puts "\nMerge complete!".cyan
else
puts "\nUndoing local merge"
GitPr.run_command "git reset --hard #{target_remote}/#{target_branch}"
end
end
|
.prompt(prompt) ⇒ Object
36
37
38
39
|
# File 'lib/git_pr.rb', line 36
def self.prompt prompt
print prompt
return GitPr.get_char.downcase == 'y'
end
|
.run_command(cmd, args = {
:failure => lambda {},
:force_print_output => false
}) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/git_pr.rb', line 11
def self.run_command(cmd,
args = {
:failure => lambda {},
:force_print_output => false
})
puts cmd.green if $verbose
result = `#{cmd}`
puts result if $verbose || args[:force_print_output]
puts '' if $verbose
if $?.exitstatus != 0
args[:failure].call()
exit -1
end
end
|