/
git-pulls.rb
276 lines (238 loc) · 5.92 KB
/
git-pulls.rb
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
require 'json'
require 'launchy'
require 'octokit'
class GitPulls
PULLS_CACHE_FILE = '.git/pulls_cache.json'
def initialize(args)
@command = args.shift
@user, @repo = repo_info
@args = args
end
def self.start(args)
GitPulls.new(args).run
end
def run
configure
if @command && self.respond_to?(@command)
# If the cache file doesn't exist, make sure we run update
# before any other command. git-pulls will otherwise crash
# with an exception.
update unless File.exists?(PULLS_CACHE_FILE) || @command == 'update'
self.send @command
elsif %w(-h --help).include?(@command)
usage
else
help
end
end
## COMMANDS ##
def help
puts "No command: #{@command}"
puts "Try: update, list, show, merge, browse"
puts "or call with '--help' for usage information"
end
def usage
puts <<-USAGE
Usage: git pulls update
or: git pulls list [--reverse]
or: git pulls show <number> [--full]
or: git pulls browse <number>
or: git pulls merge <number>
USAGE
end
def merge
num = @args.shift
option = @args.shift
if p = pull_num(num)
o = p['head']['repository']['owner']
r = p['head']['repository']['name']
s = p['head']['sha']
message = "Merge pull request ##{num} from #{o}/#{r}\n\n---\n\n"
message += p['body'].gsub("'", '')
cmd = ''
if option == '--log'
message += "\n\n---\n\nMerge Log:\n"
puts cmd = "git merge --no-ff --log -m '#{message}' #{s}"
else
puts cmd = "git merge --no-ff -m '#{message}' #{s}"
end
exec(cmd)
else
puts "No such number"
end
end
def show
num = @args.shift
option = @args.shift
if p = pull_num(num)
puts "Number : #{p['number']}"
puts "Label : #{p['head']['label']}"
puts "Created : #{p['created_at']}"
puts "Votes : #{p['votes']}"
puts "Comments : #{p['comments']}"
puts
puts "Title : #{p['title']}"
puts "Body :"
puts
puts p['body']
puts
puts '------------'
puts
if option == '--full'
exec "git diff --color=always HEAD...#{p['head']['sha']}"
else
puts "cmd: git diff HEAD...#{p['head']['sha']}"
puts git("diff --stat --color=always HEAD...#{p['head']['sha']}")
end
else
puts "No such number"
end
end
def browse
num = @args.shift
if p = pull_num(num)
Launchy.open(p['html_url'])
else
puts "No such number"
end
end
def list
option = @args.shift
puts "Open Pull Requests for #{@user}/#{@repo}"
pulls = get_pull_info
pulls.reverse! if option == '--reverse'
count = 0
pulls.each do |pull|
line = []
line << l(pull['number'], 4)
line << l(Date.parse(pull['created_at']).strftime("%m/%d"), 5)
line << l(pull['comments'], 2)
line << l(pull['title'], 35)
line << l(pull['head']['label'], 20)
sha = pull['head']['sha']
if not_merged?(sha)
puts line.join ' '
count += 1
end
end
if count == 0
puts ' -- no open pull requests --'
end
end
def update
puts "Updating #{@user}/#{@repo}"
cache_pull_info
fetch_stale_forks
list
end
def fetch_stale_forks
puts "Checking for forks in need of fetching"
pulls = get_pull_info
repos = {}
pulls.each do |pull|
o = pull['head']['repository']['owner']
r = pull['head']['repository']['name']
s = pull['head']['sha']
if !has_sha(s)
repo = "#{o}/#{r}"
repos[repo] = true
end
end
repos.each do |repo, bool|
puts " fetching #{repo}"
git("fetch #{protocol(get_repo_visibility)}#{github_url}/#{repo}.git +refs/heads/*:refs/pr/#{repo}/*")
end
end
def protocol(is_private)
is_private ? "ssh://git@" : "git://"
end
def get_repo_visibility
# This would fail if the repository was private and user did not provide github token
if github_credentials_provided?
Octokit.repository("#{repo_info[0]}/#{repo_info[1]}").private
end
end
def has_sha(sha)
git("show #{sha} 2>&1")
$?.exitstatus == 0
end
def not_merged?(sha)
commits = git("rev-list #{sha} ^HEAD 2>&1")
commits.split("\n").size > 0
end
# DISPLAY HELPER FUNCTIONS #
def l(info, size)
clean(info)[0, size].ljust(size)
end
def r(info, size)
clean(info)[0, size].rjust(size)
end
def clean(info)
info.to_s.gsub("\n", ' ')
end
# PRIVATE REPOSITORIES ACCESS
def configure
Octokit.configure do |config|
config.login = github_login
config.token = github_token
config.endpoint = github_endpoint
end
end
def github_login
git("config --get-all github.user")
end
def github_token
git("config --get-all github.token")
end
def github_endpoint
host = git("config --get-all github.host")
if host.size > 0
host
else
'https://github.com/'
end
end
# API/DATA HELPER FUNCTIONS #
def github_credentials_provided?
if github_token.empty? && github_login.empty?
return false
end
true
end
def get_pull_info
get_data(PULLS_CACHE_FILE)['pulls']
end
def get_data(file)
data = JSON.parse(File.read(file))
end
def cache_pull_info
response = Octokit.pull_requests("#{@user}/#{@repo}")
save_data({'pulls' => response}, PULLS_CACHE_FILE)
end
def save_data(data, file)
File.open(file, "w+") do |f|
f.puts data.to_json
end
end
def pull_num(num)
data = get_pull_info
data.select { |p| p['number'].to_s == num.to_s }.first
end
def repo_info
c = {}
config = git('config --list')
config.split("\n").each do |line|
k, v = line.split('=')
c[k] = v
end
u = c['remote.origin.url']
if m = /github\.com.(.*?)\/(.*?)\.git/.match(u)
user = m[1]
proj = m[2]
end
[user, proj]
end
def git(command)
`git #{command}`.chomp
end
end