-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub
executable file
·2625 lines (2225 loc) · 72.7 KB
/
hub
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
#
# This file is generated code. DO NOT send patches for it.
#
# Original source files with comments are at:
# https://github.com/defunkt/hub
#
module Hub
Version = VERSION = '1.10.6'
end
module Hub
class Args < Array
attr_accessor :executable
def initialize(*args)
super
@executable = ENV["GIT"] || "git"
@skip = @noop = false
@original_args = args.first
@chain = [nil]
end
def after(cmd_or_args = nil, args = nil, &block)
@chain.insert(-1, normalize_callback(cmd_or_args, args, block))
end
def before(cmd_or_args = nil, args = nil, &block)
@chain.insert(@chain.index(nil), normalize_callback(cmd_or_args, args, block))
end
def chained?
@chain.size > 1
end
def commands
chain = @chain.dup
chain[chain.index(nil)] = self.to_exec
chain
end
def skip!
@skip = true
end
def skip?
@skip
end
def noop!
@noop = true
end
def noop?
@noop
end
def to_exec(args = self)
Array(executable) + args
end
def add_exec_flags(flags)
self.executable = Array(executable).concat(flags)
end
def words
reject { |arg| arg.index('-') == 0 }
end
def flags
self - words
end
def changed?
chained? or self != @original_args
end
def has_flag?(*flags)
pattern = flags.flatten.map { |f| Regexp.escape(f) }.join('|')
!grep(/^#{pattern}(?:=|$)/).empty?
end
private
def normalize_callback(cmd_or_args, args, block)
if block
block
elsif args
[cmd_or_args].concat args
elsif Array === cmd_or_args
self.to_exec cmd_or_args
elsif cmd_or_args
cmd_or_args
else
raise ArgumentError, "command or block required"
end
end
end
end
module Hub
class SshConfig
CONFIG_FILES = %w(~/.ssh/config /etc/ssh_config /etc/ssh/ssh_config)
def initialize files = nil
@settings = Hash.new {|h,k| h[k] = {} }
Array(files || CONFIG_FILES).each do |path|
file = File.expand_path path
parse_file file if File.exist? file
end
end
def get_value hostname, key
key = key.to_s.downcase
@settings.each do |pattern, settings|
if pattern.match? hostname and found = settings[key]
return found
end
end
yield
end
class HostPattern
def initialize pattern
@pattern = pattern.to_s.downcase
end
def to_s() @pattern end
def ==(other) other.to_s == self.to_s end
def matcher
@matcher ||=
if '*' == @pattern
Proc.new { true }
elsif @pattern !~ /[?*]/
lambda { |hostname| hostname.to_s.downcase == @pattern }
else
re = self.class.pattern_to_regexp @pattern
lambda { |hostname| re =~ hostname }
end
end
def match? hostname
matcher.call hostname
end
def self.pattern_to_regexp pattern
escaped = Regexp.escape(pattern)
escaped.gsub!('\*', '.*')
escaped.gsub!('\?', '.')
/^#{escaped}$/i
end
end
def parse_file file
host_patterns = [HostPattern.new('*')]
IO.foreach(file) do |line|
case line
when /^\s*(#|$)/ then next
when /^\s*(\S+)\s*=/
key, value = $1, $'
else
key, value = line.strip.split(/\s+/, 2)
end
next if value.nil?
key.downcase!
value = $1 if value =~ /^"(.*)"$/
value.chomp!
if 'host' == key
host_patterns = value.split(/\s+/).map {|p| HostPattern.new p }
else
record_setting key, value, host_patterns
end
end
end
def record_setting key, value, patterns
patterns.each do |pattern|
@settings[pattern][key] ||= value
end
end
end
end
require 'uri'
require 'yaml'
require 'forwardable'
require 'fileutils'
module Hub
class GitHubAPI
attr_reader :config, :oauth_app_url
def initialize config, options
@config = config
@oauth_app_url = options.fetch(:app_url)
end
module Exceptions
def self.===(exception)
exception.class.ancestors.map {|a| a.to_s }.include? 'Net::HTTPExceptions'
end
end
def api_host host
host = host.downcase
'github.com' == host ? 'api.github.com' : host
end
def repo_info project
get "https://%s/repos/%s/%s" %
[api_host(project.host), project.owner, project.name]
end
def repo_exists? project
repo_info(project).success?
end
def fork_repo project
res = post "https://%s/repos/%s/%s/forks" %
[api_host(project.host), project.owner, project.name]
res.error! unless res.success?
end
def create_repo project, options = {}
is_org = project.owner.downcase != config.username(api_host(project.host)).downcase
params = { :name => project.name, :private => !!options[:private] }
params[:description] = options[:description] if options[:description]
params[:homepage] = options[:homepage] if options[:homepage]
if is_org
res = post "https://%s/orgs/%s/repos" % [api_host(project.host), project.owner], params
else
res = post "https://%s/user/repos" % api_host(project.host), params
end
res.error! unless res.success?
res.data
end
def pullrequest_info project, pull_id
res = get "https://%s/repos/%s/%s/pulls/%d" %
[api_host(project.host), project.owner, project.name, pull_id]
res.error! unless res.success?
res.data
end
def create_pullrequest options
project = options.fetch(:project)
params = {
:base => options.fetch(:base),
:head => options.fetch(:head)
}
if options[:issue]
params[:issue] = options[:issue]
else
params[:title] = options[:title] if options[:title]
params[:body] = options[:body] if options[:body]
end
res = post "https://%s/repos/%s/%s/pulls" %
[api_host(project.host), project.owner, project.name], params
res.error! unless res.success?
res.data
end
def statuses project, sha
res = get "https://%s/repos/%s/%s/statuses/%s" %
[api_host(project.host), project.owner, project.name, sha]
res.error! unless res.success?
res.data
end
module HttpMethods
module ResponseMethods
def status() code.to_i end
def data?() content_type =~ /\bjson\b/ end
def data() @data ||= JSON.parse(body) end
def error_message?() data? and data['errors'] || data['message'] end
def error_message() error_sentences || data['message'] end
def success?() Net::HTTPSuccess === self end
def error_sentences
data['errors'].map do |err|
case err['code']
when 'custom' then err['message']
when 'missing_field'
%(Missing field: "%s") % err['field']
when 'invalid'
%(Invalid value for "%s": "%s") % [ err['field'], err['value'] ]
when 'unauthorized'
%(Not allowed to change field "%s") % err['field']
end
end.compact if data['errors']
end
end
def get url, &block
perform_request url, :Get, &block
end
def post url, params = nil
perform_request url, :Post do |req|
if params
req.body = JSON.dump params
req['Content-Type'] = 'application/json;charset=utf-8'
end
yield req if block_given?
req['Content-Length'] = byte_size req.body
end
end
def byte_size str
if str.respond_to? :bytesize then str.bytesize
elsif str.respond_to? :length then str.length
else 0
end
end
def post_form url, params
post(url) {|req| req.set_form_data params }
end
def perform_request url, type
url = URI.parse url unless url.respond_to? :host
require 'net/https'
req = Net::HTTP.const_get(type).new request_uri(url)
http = configure_connection(req, url) do |host_url|
create_connection host_url
end
req['User-Agent'] = "Hub #{Hub::VERSION}"
apply_authentication(req, url)
yield req if block_given?
begin
res = http.start { http.request(req) }
res.extend ResponseMethods
return res
rescue SocketError => err
raise Context::FatalError, "error with #{type.to_s.upcase} #{url} (#{err.message})"
end
end
def request_uri url
str = url.request_uri
str = '/api/v3' << str if url.host != 'api.github.com'
str
end
def configure_connection req, url
if ENV['HUB_TEST_HOST']
req['Host'] = url.host
url = url.dup
url.scheme = 'http'
url.host, test_port = ENV['HUB_TEST_HOST'].split(':')
url.port = test_port.to_i if test_port
end
yield url
end
def apply_authentication req, url
user = url.user || config.username(url.host)
pass = config.password(url.host, user)
req.basic_auth user, pass
end
def create_connection url
use_ssl = 'https' == url.scheme
proxy_args = []
if proxy = config.proxy_uri(use_ssl)
proxy_args << proxy.host << proxy.port
if proxy.userinfo
require 'cgi'
proxy_args.concat proxy.userinfo.split(':', 2).map {|a| CGI.unescape a }
end
end
http = Net::HTTP.new(url.host, url.port, *proxy_args)
if http.use_ssl = use_ssl
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
return http
end
end
module OAuth
def apply_authentication req, url
if (req.path =~ /\/authorizations$/)
super
else
refresh = false
user = url.user || config.username(url.host)
token = config.oauth_token(url.host, user) {
refresh = true
obtain_oauth_token url.host, user
}
if refresh
res = get "https://#{url.host}/user"
res.error! unless res.success?
config.update_username(url.host, user, res.data['login'])
end
req['Authorization'] = "token #{token}"
end
end
def obtain_oauth_token host, user
res = get "https://#{user}@#{host}/authorizations"
res.error! unless res.success?
if found = res.data.find {|auth| auth['app']['url'] == oauth_app_url }
found['token']
else
res = post "https://#{user}@#{host}/authorizations",
:scopes => %w[repo], :note => 'hub', :note_url => oauth_app_url
res.error! unless res.success?
res.data['token']
end
end
end
include HttpMethods
include OAuth
class FileStore
extend Forwardable
def_delegator :@data, :[], :get
def_delegator :@data, :[]=, :set
def initialize filename
@filename = filename
@data = Hash.new {|d, host| d[host] = [] }
load if File.exist? filename
end
def fetch_user host
unless entry = get(host).first
user = yield
return nil if user.nil? or user.empty?
entry = entry_for_user(host, user)
end
entry['user']
end
def fetch_value host, user, key
entry = entry_for_user host, user
entry[key.to_s] || begin
value = yield
if value and !value.empty?
entry[key.to_s] = value
save
value
else
raise "no value"
end
end
end
def entry_for_user host, username
entries = get(host)
entries.find {|e| e['user'] == username } or
(entries << {'user' => username}).last
end
def load
existing_data = File.read(@filename)
@data.update YAML.load(existing_data) unless existing_data.strip.empty?
end
def save
FileUtils.mkdir_p File.dirname(@filename)
File.open(@filename, 'w', 0600) {|f| f << YAML.dump(@data) }
end
end
class Configuration
def initialize store
@data = store
@password_cache = {}
end
def normalize_host host
host = host.downcase
'api.github.com' == host ? 'github.com' : host
end
def username host
return ENV['GITHUB_USER'] unless ENV['GITHUB_USER'].to_s.empty?
host = normalize_host host
@data.fetch_user host do
if block_given? then yield
else prompt "#{host} username"
end
end
end
def update_username host, old_username, new_username
entry = @data.entry_for_user(normalize_host(host), old_username)
entry['user'] = new_username
@data.save
end
def api_token host, user
host = normalize_host host
@data.fetch_value host, user, :api_token do
if block_given? then yield
else prompt "#{host} API token for #{user}"
end
end
end
def password host, user
return ENV['GITHUB_PASSWORD'] unless ENV['GITHUB_PASSWORD'].to_s.empty?
host = normalize_host host
@password_cache["#{user}@#{host}"] ||= prompt_password host, user
end
def oauth_token host, user, &block
@data.fetch_value normalize_host(host), user, :oauth_token, &block
end
def prompt what
print "#{what}: "
$stdin.gets.chomp
end
def prompt_password host, user
print "#{host} password for #{user} (never stored): "
if $stdin.tty?
password = askpass
puts ''
password
else
$stdin.gets.chomp
end
end
NULL = defined?(File::NULL) ? File::NULL :
File.exist?('/dev/null') ? '/dev/null' : 'NUL'
def askpass
tty_state = `stty -g 2>#{NULL}`
system 'stty raw -echo -icanon isig' if $?.success?
pass = ''
while char = getbyte($stdin) and !(char == 13 or char == 10)
if char == 127 or char == 8
pass[-1,1] = '' unless pass.empty?
else
pass << char.chr
end
end
pass
ensure
system "stty #{tty_state}" unless tty_state.empty?
end
def getbyte(io)
if io.respond_to?(:getbyte)
io.getbyte
else
io.getc
end
end
def proxy_uri(with_ssl)
env_name = "HTTP#{with_ssl ? 'S' : ''}_PROXY"
if proxy = ENV[env_name] || ENV[env_name.downcase] and !proxy.empty?
proxy = "http://#{proxy}" unless proxy.include? '://'
URI.parse proxy
end
end
end
end
end
require 'shellwords'
require 'forwardable'
require 'uri'
module Hub
module Context
extend Forwardable
NULL = defined?(File::NULL) ? File::NULL : File.exist?('/dev/null') ? '/dev/null' : 'NUL'
class GitReader
attr_reader :executable
def initialize(executable = nil, &read_proc)
@executable = executable || 'git'
read_proc ||= lambda { |cache, cmd|
result = %x{#{command_to_string(cmd)} 2>#{NULL}}.chomp
cache[cmd] = $?.success? && !result.empty? ? result : nil
}
@cache = Hash.new(&read_proc)
end
def add_exec_flags(flags)
@executable = Array(executable).concat(flags)
end
def read_config(cmd, all = false)
config_cmd = ['config', (all ? '--get-all' : '--get'), *cmd]
config_cmd = config_cmd.join(' ') unless cmd.respond_to? :join
read config_cmd
end
def read(cmd)
@cache[cmd]
end
def stub_config_value(key, value, get = '--get')
stub_command_output "config #{get} #{key}", value
end
def stub_command_output(cmd, value)
@cache[cmd] = value.nil? ? nil : value.to_s
end
def stub!(values)
@cache.update values
end
private
def to_exec(args)
args = Shellwords.shellwords(args) if args.respond_to? :to_str
Array(executable) + Array(args)
end
def command_to_string(cmd)
full_cmd = to_exec(cmd)
full_cmd.respond_to?(:shelljoin) ? full_cmd.shelljoin : full_cmd.join(' ')
end
end
module GitReaderMethods
extend Forwardable
def_delegator :git_reader, :read_config, :git_config
def_delegator :git_reader, :read, :git_command
def self.extended(base)
base.extend Forwardable
base.def_delegators :'self.class', :git_config, :git_command
end
end
class Error < RuntimeError; end
class FatalError < Error; end
private
def git_reader
@git_reader ||= GitReader.new ENV['GIT']
end
include GitReaderMethods
private :git_config, :git_command
def local_repo(fatal = true)
@local_repo ||= begin
if is_repo?
LocalRepo.new git_reader, current_dir
elsif fatal
raise FatalError, "Not a git repository"
end
end
end
repo_methods = [
:current_branch,
:current_project, :upstream_project,
:repo_owner, :repo_host,
:remotes, :remotes_group, :origin_remote
]
def_delegator :local_repo, :name, :repo_name
def_delegators :local_repo, *repo_methods
private :repo_name, *repo_methods
def master_branch
if local_repo(false)
local_repo.master_branch
else
Branch.new nil, 'refs/heads/master'
end
end
class LocalRepo < Struct.new(:git_reader, :dir)
include GitReaderMethods
def name
if project = main_project
project.name
else
File.basename(dir)
end
end
def repo_owner
if project = main_project
project.owner
end
end
def repo_host
project = main_project and project.host
end
def main_project
remote = origin_remote and remote.project
end
def upstream_project
if branch = current_branch and upstream = branch.upstream and upstream.remote?
remote = remote_by_name upstream.remote_name
remote.project
end
end
def current_project
upstream_project || main_project
end
def current_branch
if branch = git_command('symbolic-ref -q HEAD')
Branch.new self, branch
end
end
def master_branch
Branch.new self, 'refs/heads/master'
end
def remotes
@remotes ||= begin
list = git_command('remote').to_s.split("\n")
main = list.delete('origin') and list.unshift(main)
list.map { |name| Remote.new self, name }
end
end
def remotes_group(name)
git_config "remotes.#{name}"
end
def origin_remote
remotes.first
end
def remote_by_name(remote_name)
remotes.find {|r| r.name == remote_name }
end
def known_hosts
hosts = git_config('hub.host', :all).to_s.split("\n")
hosts << default_host
hosts << "ssh.#{default_host}"
end
def self.default_host
ENV['GITHUB_HOST'] || main_host
end
def self.main_host
'github.com'
end
extend Forwardable
def_delegators :'self.class', :default_host, :main_host
def ssh_config
@ssh_config ||= SshConfig.new
end
end
class GithubProject < Struct.new(:local_repo, :owner, :name, :host)
def self.from_url(url, local_repo)
if local_repo.known_hosts.include? url.host
_, owner, name = url.path.split('/', 4)
GithubProject.new(local_repo, owner, name.sub(/\.git$/, ''), url.host)
end
end
attr_accessor :repo_data
def initialize(*args)
super
self.name = self.name.tr(' ', '-')
self.host ||= (local_repo || LocalRepo).default_host
self.host = host.sub(/^ssh\./i, '') if 'ssh.github.com' == host.downcase
end
def private?
repo_data ? repo_data.fetch('private') :
host != (local_repo || LocalRepo).main_host
end
def owned_by(new_owner)
new_project = dup
new_project.owner = new_owner
new_project
end
def name_with_owner
"#{owner}/#{name}"
end
def ==(other)
name_with_owner == other.name_with_owner
end
def remote
local_repo.remotes.find { |r| r.project == self }
end
def web_url(path = nil)
project_name = name_with_owner
if project_name.sub!(/\.wiki$/, '')
unless '/wiki' == path
path = if path =~ %r{^/commits/} then '/_history'
else path.to_s.sub(/\w+/, '_\0')
end
path = '/wiki' + path
end
end
"https://#{host}/" + project_name + path.to_s
end
def git_url(options = {})
if options[:https] then "https://#{host}/"
elsif options[:private] or private? then "git@#{host}:"
else "git://#{host}/"
end + name_with_owner + '.git'
end
end
class GithubURL < URI::HTTPS
extend Forwardable
attr_reader :project
def_delegator :project, :name, :project_name
def_delegator :project, :owner, :project_owner
def self.resolve(url, local_repo)
u = URI(url)
if %[http https].include? u.scheme and project = GithubProject.from_url(u, local_repo)
self.new(u.scheme, u.userinfo, u.host, u.port, u.registry,
u.path, u.opaque, u.query, u.fragment, project)
end
rescue URI::InvalidURIError
nil
end
def initialize(*args)
@project = args.pop
super(*args)
end
def project_path
path.split('/', 4)[3]
end
end
class Branch < Struct.new(:local_repo, :name)
alias to_s name
def short_name
name.sub(%r{^refs/(remotes/)?.+?/}, '')
end
def master?
short_name == 'master'
end
def upstream
if branch = local_repo.git_command("rev-parse --symbolic-full-name #{short_name}@{upstream}")
Branch.new local_repo, branch
end
end
def remote?
name.index('refs/remotes/') == 0
end
def remote_name
name =~ %r{^refs/remotes/([^/]+)} and $1 or
raise Error, "can't get remote name from #{name.inspect}"
end
end
class Remote < Struct.new(:local_repo, :name)
alias to_s name
def ==(other)
other.respond_to?(:to_str) ? name == other.to_str : super
end
def project
urls.each_value { |url|
if valid = GithubProject.from_url(url, local_repo)
return valid
end
}
nil
end
def urls
return @urls if defined? @urls
@urls = {}
local_repo.git_command('remote -v').to_s.split("\n").map do |line|
next if line !~ /^(.+?)\t(.+) \((.+)\)$/
remote, uri, type = $1, $2, $3
next if remote != self.name
if uri =~ %r{^[\w-]+://} or uri =~ %r{^([^/]+?):}
uri = "ssh://#{$1}/#{$'}" if $1
begin
@urls[type] = uri_parse(uri)
rescue URI::InvalidURIError
end
end
end
@urls
end
def uri_parse uri
uri = URI.parse uri
uri.host = local_repo.ssh_config.get_value(uri.host, 'hostname') { uri.host }
uri.user = local_repo.ssh_config.get_value(uri.host, 'user') { uri.user }
uri
end
end
def github_project(name, owner = nil)
if owner and owner.index('/')
owner, name = owner.split('/', 2)
elsif name and name.index('/')
owner, name = name.split('/', 2)
else
name ||= repo_name
owner ||= github_user
end
if local_repo(false) and main_project = local_repo.main_project
project = main_project.dup
project.owner = owner
project.name = name
project
else
GithubProject.new(local_repo(false), owner, name)
end
end
def git_url(owner = nil, name = nil, options = {})
project = github_project(name, owner)
project.git_url({:https => https_protocol?}.update(options))
end
def resolve_github_url(url)
GithubURL.resolve(url, local_repo) if url =~ /^https?:/
end
def http_clone?
git_config('--bool hub.http-clone') == 'true'
end
def https_protocol?
git_config('hub.protocol') == 'https' or http_clone?
end
def git_alias_for(name)
git_config "alias.#{name}"
end
def rev_list(a, b)
git_command("rev-list --cherry-pick --right-only --no-merges #{a}...#{b}")
end
PWD = Dir.pwd
def current_dir
PWD
end
def git_dir
git_command 'rev-parse -q --git-dir'
end
def is_repo?
!!git_dir
end