Class: GitPulls

Inherits:
Object
  • Object
show all
Defined in:
lib/git-pulls.rb

Constant Summary collapse

PULLS_CACHE_FILE =
'.git/pulls_cache.json'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ GitPulls

Returns a new instance of GitPulls.



9
10
11
12
13
# File 'lib/git-pulls.rb', line 9

def initialize(args)
  @command = args.shift
  @user, @repo = repo_info
  @args = args
end

Class Method Details

.start(args) ⇒ Object



15
16
17
# File 'lib/git-pulls.rb', line 15

def self.start(args)
  GitPulls.new(args).run
end

Instance Method Details

#browseObject



117
118
119
120
121
122
123
124
# File 'lib/git-pulls.rb', line 117

def browse
  num = @args.shift
  if p = pull_num(num)
    Launchy.open(p['html_url'])
  else
    puts "No such number"
  end
end

#cache_pull_infoObject



264
265
266
267
# File 'lib/git-pulls.rb', line 264

def cache_pull_info
  response = Octokit.pull_requests("#{@user}/#{@repo}")
  save_data({'pulls' => response}, PULLS_CACHE_FILE)
end

#clean(info) ⇒ Object



216
217
218
# File 'lib/git-pulls.rb', line 216

def clean(info)
  info.to_s.gsub("\n", ' ')
end

#configureObject

PRIVATE REPOSITORIES ACCESS



222
223
224
225
226
227
228
# File 'lib/git-pulls.rb', line 222

def configure
  Octokit.configure do |config|
    config. = 
    config.token = github_token
    config.endpoint = github_endpoint
  end
end

#createObject



157
158
159
160
161
162
163
164
165
# File 'lib/git-pulls.rb', line 157

def create
  repo = "#{@user}/#{@repo}"
  to_branch = 'master'
  from_branch = get_from_branch_title
  title = 'my title'
  body = 'my body'
  
  Octokit.create_pull_request(repo, to_branch, from_branch, title, body)
end

#fetch_stale_forksObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/git-pulls.rb', line 171

def fetch_stale_forks
  puts "Checking for forks in need of fetching"
  pulls = get_pull_info
  repos = {}
  pulls.each do |pull|
    next if pull['head']['repository'].nil? # Fork has been deleted
    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
  if github_credentials_provided?
    endpoint = "[email protected]:"
  else
    endpoint = github_endpoint + "/"
  end
  repos.each do |repo, bool|
    puts "  fetching #{repo}"
    git("fetch #{endpoint}#{repo}.git +refs/heads/*:refs/pr/#{repo}/*")
  end
end

#get_data(file) ⇒ Object



260
261
262
# File 'lib/git-pulls.rb', line 260

def get_data(file)
  data = JSON.parse(File.read(file))
end

#get_from_branch_titleObject



167
168
169
# File 'lib/git-pulls.rb', line 167

def get_from_branch_title
  git('branch', false).match(/\*(.*)/)[0][2..-1]
end

#get_pull_infoObject



256
257
258
# File 'lib/git-pulls.rb', line 256

def get_pull_info
  get_data(PULLS_CACHE_FILE)['pulls']
end

#git(command, chomp = true) ⇒ Object



318
319
320
321
322
# File 'lib/git-pulls.rb', line 318

def git(command, chomp=true)
  s = `git #{command}`
  s.chomp! if chomp
  s
end

#github_credentials_provided?Boolean

API/DATA HELPER FUNCTIONS #

Returns:

  • (Boolean)


249
250
251
252
253
254
# File 'lib/git-pulls.rb', line 249

def github_credentials_provided?
  if github_token.empty? && .empty?
    return false
  end
  true
end

#github_endpointObject



238
239
240
241
242
243
244
245
# File 'lib/git-pulls.rb', line 238

def github_endpoint
  host = git("config --get-all github.host")
  if host.size > 0
    host
  else
    'https://github.com'
  end
end

#github_insteadof_matching(c, u) ⇒ Object



280
281
282
283
284
285
286
287
# File 'lib/git-pulls.rb', line 280

def github_insteadof_matching(c, u)
  first = c.collect {|k,v| [v, /url\.(.*github\.com.*)\.insteadof/.match(k)]}.
            find {|v,m| u.index(v) and m != nil}
  if first
    return first[0], first[1][1]
  end
  return nil, nil
end

#github_loginObject



230
231
232
# File 'lib/git-pulls.rb', line 230

def 
  git("config --get-all github.user")
end

#github_tokenObject



234
235
236
# File 'lib/git-pulls.rb', line 234

def github_token
  git("config --get-all github.token")
end

#github_user_and_proj(u) ⇒ Object



289
290
291
292
293
294
295
296
# File 'lib/git-pulls.rb', line 289

def github_user_and_proj(u)
  # Trouble getting optional ".git" at end to work, so put that logic below
  m = /github\.com.(.*?)\/(.*)/.match(u)
  if m
    return m[1], m[2].sub(/\.git\Z/, "")
  end
  return nil, nil
end

#has_sha(sha) ⇒ Object



196
197
198
199
# File 'lib/git-pulls.rb', line 196

def has_sha(sha)
  git("show #{sha} 2>&1")
  $?.exitstatus == 0
end

#helpObject

COMMANDS ##



37
38
39
40
41
# File 'lib/git-pulls.rb', line 37

def help
  puts "No command: #{@command}"
  puts "Try: update, list, show, merge, browse, create"
  puts "or call with '-h' for usage information"
end

#l(info, size) ⇒ Object

DISPLAY HELPER FUNCTIONS #



208
209
210
# File 'lib/git-pulls.rb', line 208

def l(info, size)
  clean(info)[0, size].ljust(size)
end

#listObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/git-pulls.rb', line 126

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

#mergeObject



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
# File 'lib/git-pulls.rb', line 54

def merge
  num = @args.shift
  option = @args.shift
  if p = pull_num(num)
    if p['head']['repository']
      o = p['head']['repository']['owner']
      r = p['head']['repository']['name']
    else # they deleted the source repo
      o = p['head']['user']['login']
      purl = p['patch_url']
      puts "Sorry, #{o} deleted the source repository, git-pulls doesn't support this."
      puts "You can manually patch your repo by running:"
      puts
      puts "  curl #{purl} | git am"
      puts
      puts "Tell the contributor not to do this."
      return false
    end
    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

#not_merged?(sha) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
204
# File 'lib/git-pulls.rb', line 201

def not_merged?(sha)
  commits = git("rev-list #{sha} ^HEAD 2>&1")
  commits.split("\n").size > 0
end

#pull_num(num) ⇒ Object



275
276
277
278
# File 'lib/git-pulls.rb', line 275

def pull_num(num)
  data = get_pull_info
  data.select { |p| p['number'].to_s == num.to_s }.first
end

#r(info, size) ⇒ Object



212
213
214
# File 'lib/git-pulls.rb', line 212

def r(info, size)
  clean(info)[0, size].rjust(size)
end

#repo_infoObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/git-pulls.rb', line 298

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']

  user, proj = github_user_and_proj(u)
  if !(user and proj)
    short, base = github_insteadof_matching(c, u)
    if short and base
      u = u.sub(short, base)
      user, proj = github_user_and_proj(u)
    end
  end
  [user, proj]
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/git-pulls.rb', line 19

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

#save_data(data, file) ⇒ Object



269
270
271
272
273
# File 'lib/git-pulls.rb', line 269

def save_data(data, file)
  File.open(file, "w+") do |f|
    f.puts data.to_json
  end
end

#showObject



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
# File 'lib/git-pulls.rb', line 89

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

#updateObject



150
151
152
153
154
155
# File 'lib/git-pulls.rb', line 150

def update
  puts "Updating #{@user}/#{@repo}"
  cache_pull_info
  fetch_stale_forks
  list
end

#usageObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/git-pulls.rb', line 43

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>
 or: git pulls create
  USAGE
end