forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.rb
996 lines (879 loc) · 32.8 KB
/
commands.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
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
module Hub
# The Commands module houses the git commands that hub
# lovingly wraps. If a method exists here, it is expected to have a
# corresponding git command which either gets run before or after
# the method executes.
#
# The typical flow is as follows:
#
# 1. hub is invoked from the command line:
# $ hub clone rtomayko/tilt
#
# 2. The Hub class is initialized:
# >> hub = Hub.new('clone', 'rtomayko/tilt')
#
# 3. The method representing the git subcommand is executed with the
# full args:
# >> Commands.clone('clone', 'rtomayko/tilt')
#
# 4. That method rewrites the args as it sees fit:
# >> args[1] = "git://github.com/" + args[1] + ".git"
# => "git://github.com/rtomayko/tilt.git"
#
# 5. The new args are used to run `git`:
# >> exec "git", "clone", "git://github.com/rtomayko/tilt.git"
#
# An optional `after` callback can be set. If so, it is run after
# step 5 (which then performs a `system` call rather than an
# `exec`). See `Hub::Args` for more information on the `after` callback.
module Commands
# We are a blank slate.
instance_methods.each { |m| undef_method(m) unless m =~ /(^__|send|to\?$)/ }
extend self
# provides git interrogation methods
extend Context
NAME_RE = /\w[\w.-]*/
OWNER_RE = /[a-zA-Z0-9-]+/
NAME_WITH_OWNER_RE = /^(?:#{NAME_RE}|#{OWNER_RE}\/#{NAME_RE})$/
CUSTOM_COMMANDS = %w[alias create browse compare fork pull-request]
def run(args)
slurp_global_flags(args)
# Hack to emulate git-style
args.unshift 'help' if args.empty?
cmd = args[0]
if expanded_args = expand_alias(cmd)
cmd = expanded_args[0]
expanded_args.concat args[1..-1]
end
respect_help_flags(expanded_args || args) if custom_command? cmd
# git commands can have dashes
cmd = cmd.gsub(/(\w)-/, '\1_')
if method_defined?(cmd) and cmd != 'run'
args.replace expanded_args if expanded_args
send(cmd, args)
end
rescue Errno::ENOENT
if $!.message.include? "No such file or directory - git"
abort "Error: `git` command not found"
else
raise
end
rescue Context::FatalError => err
abort "fatal: #{err.message}"
end
# $ hub pull-request
# $ hub pull-request "My humble contribution"
# $ hub pull-request -i 92
# $ hub pull-request https://github.com/rtomayko/tilt/issues/92
def pull_request(args)
args.shift
options = { }
force = explicit_owner = false
base_project = local_repo.main_project
head_project = local_repo.current_project
unless base_project
abort "Aborted: the origin remote doesn't point to a GitHub repository."
end
from_github_ref = lambda do |ref, context_project|
if ref.index(':')
owner, ref = ref.split(':', 2)
project = github_project(context_project.name, owner)
end
[project || context_project, ref]
end
while arg = args.shift
case arg
when '-f'
force = true
when '-b'
base_project, options[:base] = from_github_ref.call(args.shift, base_project)
when '-h'
head = args.shift
explicit_owner = !!head.index(':')
head_project, options[:head] = from_github_ref.call(head, head_project)
when '-i'
options[:issue] = args.shift
else
if url = resolve_github_url(arg) and url.project_path =~ /^issues\/(\d+)/
options[:issue] = $1
base_project = url.project
elsif !options[:title] then options[:title] = arg
else
abort "invalid argument: #{arg}"
end
end
end
options[:project] = base_project
options[:base] ||= master_branch.short_name
if tracked_branch = options[:head].nil? && current_branch.upstream
if !tracked_branch.remote?
# The current branch is tracking another local branch. Pretend there is
# no upstream configuration at all.
tracked_branch = nil
elsif base_project == head_project and tracked_branch.short_name == options[:base]
$stderr.puts "Aborted: head branch is the same as base (#{options[:base].inspect})"
warn "(use `-h <branch>` to specify an explicit pull request head)"
abort
end
end
options[:head] ||= (tracked_branch || current_branch).short_name
# when no tracking, assume remote branch is published under active user's fork
user = github_user(head_project.host)
if head_project.owner != user and !tracked_branch and !explicit_owner
head_project = head_project.owned_by(user)
end
remote_branch = "#{head_project.remote}/#{options[:head]}"
options[:head] = "#{head_project.owner}:#{options[:head]}"
if !force and tracked_branch and local_commits = rev_list(remote_branch, nil)
$stderr.puts "Aborted: #{local_commits.split("\n").size} commits are not yet pushed to #{remote_branch}"
warn "(use `-f` to force submit a pull request anyway)"
abort
end
if args.noop?
puts "Would request a pull to #{base_project.owner}:#{options[:base]} from #{options[:head]}"
exit
end
unless options[:title] or options[:issue]
base_branch = "#{base_project.remote}/#{options[:base]}"
commits = rev_list(base_branch, remote_branch).to_s.split("\n")
case commits.size
when 0
default_message = commit_summary = nil
when 1
format = '%w(78,0,0)%s%n%+b'
default_message = git_command "show -s --format='#{format}' #{commits.first}"
commit_summary = nil
else
format = '%h (%aN, %ar)%n%w(78,3,3)%s%n%+b'
default_message = nil
commit_summary = git_command "log --no-color --format='%s' --cherry %s...%s" %
[format, base_branch, remote_branch]
end
options[:title], options[:body] = pullrequest_editmsg(commit_summary) { |msg|
msg.puts default_message if default_message
msg.puts ""
msg.puts "# Requesting a pull to #{base_project.owner}:#{options[:base]} from #{options[:head]}"
msg.puts "#"
msg.puts "# Write a message for this pull request. The first block"
msg.puts "# of text is the title and the rest is description."
}
end
pull = api_client.create_pullrequest(options)
args.executable = 'echo'
args.replace [pull['html_url']]
rescue GitHubAPI::Exceptions
display_api_exception("creating pull request", $!.response)
exit 1
end
# $ hub clone rtomayko/tilt
# > git clone git://github.com/rtomayko/tilt.
#
# $ hub clone -p kneath/hemingway
# > git clone git@github.com:kneath/hemingway.git
#
# $ hub clone tilt
# > git clone git://github.com/YOUR_LOGIN/tilt.
#
# $ hub clone -p github
# > git clone git@github.com:YOUR_LOGIN/hemingway.git
def clone(args)
ssh = args.delete('-p')
has_values = /^(--(upload-pack|template|depth|origin|branch|reference)|-[ubo])$/
idx = 1
while idx < args.length
arg = args[idx]
if arg.index('-') == 0
idx += 1 if arg =~ has_values
else
# $ hub clone rtomayko/tilt
# $ hub clone tilt
if arg =~ NAME_WITH_OWNER_RE and !File.directory?(arg)
# FIXME: this logic shouldn't be duplicated here!
name, owner = arg, nil
owner, name = name.split('/', 2) if name.index('/')
host = ENV['GITHUB_HOST'] || 'github.com'
project = Context::GithubProject.new(nil, owner || github_user(host), name, host)
ssh ||= args[0] != 'submodule' && project.owner == github_user(host) || host != 'github.com'
args[idx] = project.git_url(:private => ssh, :https => https_protocol?)
end
break
end
idx += 1
end
end
# $ hub submodule add wycats/bundler vendor/bundler
# > git submodule add git://github.com/wycats/bundler.git vendor/bundler
#
# $ hub submodule add -p wycats/bundler vendor/bundler
# > git submodule add git@github.com:wycats/bundler.git vendor/bundler
#
# $ hub submodule add -b ryppl ryppl/pip vendor/bundler
# > git submodule add -b ryppl git://github.com/ryppl/pip.git vendor/pip
def submodule(args)
return unless index = args.index('add')
args.delete_at index
branch = args.index('-b') || args.index('--branch')
if branch
args.delete_at branch
branch_name = args.delete_at branch
end
clone(args)
if branch_name
args.insert branch, '-b', branch_name
end
args.insert index, 'add'
end
# $ hub remote add pjhyett
# > git remote add pjhyett git://github.com/pjhyett/THIS_REPO.git
#
# $ hub remote add -p mojombo
# > git remote add mojombo git@github.com:mojombo/THIS_REPO.git
#
# $ hub remote add origin
# > git remote add origin git://github.com/YOUR_LOGIN/THIS_REPO.git
def remote(args)
if %w[add set-url].include?(args[1])
name = args.last
if name =~ /^(#{OWNER_RE})$/ || name =~ /^(#{OWNER_RE})\/(#{NAME_RE})$/
user, repo = $1, $2 || repo_name
end
end
return unless user # do not touch arguments
ssh = args.delete('-p')
if args.words[2] == 'origin' && args.words[3].nil?
# Origin special case triggers default user/repo
user, repo = github_user, repo_name
elsif args.words[-2] == args.words[1]
# rtomayko/tilt => rtomayko
# Make sure you dance around flags.
idx = args.index( args.words[-1] )
args[idx] = user
else
# They're specifying the remote name manually (e.g.
# git remote add blah rtomayko/tilt), so just drop the last
# argument.
args.pop
end
args << git_url(user, repo, :private => ssh)
end
# $ hub fetch mislav
# > git remote add mislav git://github.com/mislav/REPO.git
# > git fetch mislav
#
# $ hub fetch --multiple mislav xoebus
# > git remote add mislav ...
# > git remote add xoebus ...
# > git fetch --multiple mislav xoebus
def fetch(args)
# $ hub fetch --multiple <name1>, <name2>, ...
if args.include?('--multiple')
names = args.words[1..-1]
# $ hub fetch <name>
elsif remote_name = args.words[1]
# $ hub fetch <name1>,<name2>,...
if remote_name =~ /^\w+(,\w+)+$/
index = args.index(remote_name)
args.delete(remote_name)
names = remote_name.split(',')
args.insert(index, *names)
args.insert(index, '--multiple')
else
names = [remote_name]
end
else
names = []
end
projects = names.map { |name|
unless name =~ /\W/ or remotes.include?(name) or remotes_group(name)
project = github_project(nil, name)
project if api_client.repo_exists?(project)
end
}.compact
if projects.any?
projects.each do |project|
args.before ['remote', 'add', project.owner, project.git_url(:https => https_protocol?)]
end
end
end
# $ git checkout https://github.com/defunkt/hub/pull/73
# > git remote add -f -t feature git://github:com/mislav/hub.git
# > git checkout --track -B mislav-feature mislav/feature
def checkout(args)
_, url_arg, new_branch_name = args.words
if url = resolve_github_url(url_arg) and url.project_path =~ /^pull\/(\d+)/
pull_id = $1
pull_data = api_client.pullrequest_info(url.project, pull_id)
args.delete new_branch_name
user, branch = pull_data['head']['label'].split(':', 2)
abort "Error: #{user}'s fork is not available anymore" unless pull_data['head']['repo']
new_branch_name ||= "#{user}-#{branch}"
if remotes.include? user
args.before ['remote', 'set-branches', '--add', user, branch]
args.before ['fetch', user, "+refs/heads/#{branch}:refs/remotes/#{user}/#{branch}"]
else
url = github_project(url.project_name, user).git_url(:private => pull_data['head']['repo']['private'],
:https => https_protocol?)
args.before ['remote', 'add', '-f', '-t', branch, user, url]
end
idx = args.index url_arg
args.delete_at idx
args.insert idx, '--track', '-B', new_branch_name, "#{user}/#{branch}"
end
end
# $ git merge https://github.com/defunkt/hub/pull/73
# > git fetch git://github.com/mislav/hub.git +refs/heads/feature:refs/remotes/mislav/feature
# > git merge mislav/feature --no-ff -m 'Merge pull request #73 from mislav/feature...'
def merge(args)
_, url_arg = args.words
if url = resolve_github_url(url_arg) and url.project_path =~ /^pull\/(\d+)/
pull_id = $1
pull_data = api_client.pullrequest_info(url.project, pull_id)
user, branch = pull_data['head']['label'].split(':', 2)
abort "Error: #{user}'s fork is not available anymore" unless pull_data['head']['repo']
url = github_project(url.project_name, user).git_url(:private => pull_data['head']['repo']['private'],
:https => https_protocol?)
merge_head = "#{user}/#{branch}"
args.before ['fetch', url, "+refs/heads/#{branch}:refs/remotes/#{merge_head}"]
idx = args.index url_arg
args.delete_at idx
args.insert idx, merge_head, '--no-ff', '-m',
"Merge pull request ##{pull_id} from #{merge_head}\n\n#{pull_data['title']}"
end
end
# $ git cherry-pick http://github.com/mislav/hub/commit/a319d88#comments
# > git remote add -f mislav git://github.com/mislav/hub.git
# > git cherry-pick a319d88
#
# $ git cherry-pick mislav@a319d88
# > git remote add -f mislav git://github.com/mislav/hub.git
# > git cherry-pick a319d88
#
# $ git cherry-pick mislav@SHA
# > git fetch mislav
# > git cherry-pick SHA
def cherry_pick(args)
unless args.include?('-m') or args.include?('--mainline')
ref = args.words.last
if url = resolve_github_url(ref) and url.project_path =~ /^commit\/([a-f0-9]{7,40})/
sha = $1
project = url.project
elsif ref =~ /^(#{OWNER_RE})@([a-f0-9]{7,40})$/
owner, sha = $1, $2
project = local_repo.main_project.owned_by(owner)
end
if project
args[args.index(ref)] = sha
if remote = project.remote and remotes.include? remote
args.before ['fetch', remote.to_s]
else
args.before ['remote', 'add', '-f', project.owner, project.git_url(:https => https_protocol?)]
end
end
end
end
# $ hub am https://github.com/defunkt/hub/pull/55
# > curl https://github.com/defunkt/hub/pull/55.patch -o /tmp/55.patch
# > git am /tmp/55.patch
def am(args)
if url = args.find { |a| a =~ %r{^https?://(gist\.)?github\.com/} }
idx = args.index(url)
gist = $1 == 'gist.'
# strip the fragment part of the url
url = url.sub(/#.+/, '')
# strip extra path from "pull/42/files", "pull/42/commits"
url = url.sub(%r{(/pull/\d+)/\w*$}, '\1') unless gist
ext = gist ? '.txt' : '.patch'
url += ext unless File.extname(url) == ext
patch_file = File.join(ENV['TMPDIR'] || '/tmp', "#{gist ? 'gist-' : ''}#{File.basename(url)}")
args.before 'curl', ['-#LA', "hub #{Hub::Version}", url, '-o', patch_file]
args[idx] = patch_file
end
end
# $ hub apply https://github.com/defunkt/hub/pull/55
# > curl https://github.com/defunkt/hub/pull/55.patch -o /tmp/55.patch
# > git apply /tmp/55.patch
alias_method :apply, :am
# $ hub init -g
# > git init
# > git remote add origin git@github.com:USER/REPO.git
def init(args)
if args.delete('-g')
# can't use default_host because there is no local_repo yet
# FIXME: this shouldn't be here!
host = ENV['GITHUB_HOST'] || 'github.com'
project = Context::GithubProject.new(nil, github_user(host), File.basename(current_dir), host)
url = project.git_url(:private => true, :https => https_protocol?)
args.after ['remote', 'add', 'origin', url]
end
end
# $ hub fork
# ... hardcore forking action ...
# > git remote add -f YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git
def fork(args)
unless project = local_repo.main_project
abort "Error: repository under 'origin' remote is not a GitHub project"
end
forked_project = project.owned_by(github_user(project.host))
if api_client.repo_exists?(forked_project)
abort "Error creating fork: %s already exists on %s" %
[ forked_project.name_with_owner, forked_project.host ]
else
api_client.fork_repo(project) unless args.noop?
end
if args.include?('--no-remote')
exit
else
url = forked_project.git_url(:private => true, :https => https_protocol?)
args.replace %W"remote add -f #{forked_project.owner} #{url}"
args.after 'echo', ['new remote:', forked_project.owner]
end
rescue GitHubAPI::Exceptions
display_api_exception("creating fork", $!.response)
exit 1
end
# $ hub create
# ... create repo on github ...
# > git remote add -f origin git@github.com:YOUR_USER/CURRENT_REPO.git
def create(args)
if !is_repo?
abort "'create' must be run from inside a git repository"
elsif owner = github_user
args.shift
options = {}
options[:private] = true if args.delete('-p')
new_repo_name = nil
until args.empty?
case arg = args.shift
when '-d'
options[:description] = args.shift
when '-h'
options[:homepage] = args.shift
else
if arg =~ /^[^-]/ and new_repo_name.nil?
new_repo_name = arg
owner, new_repo_name = new_repo_name.split('/', 2) if new_repo_name.index('/')
else
abort "invalid argument: #{arg}"
end
end
end
new_repo_name ||= repo_name
new_project = github_project(new_repo_name, owner)
if api_client.repo_exists?(new_project)
warn "#{new_project.name_with_owner} already exists on #{new_project.host}"
action = "set remote origin"
else
action = "created repository"
api_client.create_repo(new_project, options) unless args.noop?
end
url = new_project.git_url(:private => true, :https => https_protocol?)
if remotes.first != 'origin'
args.replace %W"remote add -f origin #{url}"
else
args.replace %W"remote -v"
end
args.after 'echo', ["#{action}:", new_project.name_with_owner]
end
rescue GitHubAPI::Exceptions
display_api_exception("creating repository", $!.response)
exit 1
end
# $ hub push origin,staging cool-feature
# > git push origin cool-feature
# > git push staging cool-feature
def push(args)
return if args[1].nil? || !args[1].index(',')
branch = (args[2] ||= current_branch.short_name)
remotes = args[1].split(',')
args[1] = remotes.shift
remotes.each do |name|
args.after ['push', name, branch]
end
end
# $ hub browse
# > open https://github.com/CURRENT_REPO
#
# $ hub browse -- issues
# > open https://github.com/CURRENT_REPO/issues
#
# $ hub browse pjhyett/github-services
# > open https://github.com/pjhyett/github-services
#
# $ hub browse github-services
# > open https://github.com/YOUR_LOGIN/github-services
#
# $ hub browse github-services wiki
# > open https://github.com/YOUR_LOGIN/github-services/wiki
def browse(args)
args.shift
browse_command(args) do
dest = args.shift
dest = nil if dest == '--'
if dest
# $ hub browse pjhyett/github-services
# $ hub browse github-services
project = github_project dest
branch = master_branch
else
# $ hub browse
project = current_project
branch = current_branch && current_branch.upstream || master_branch
end
abort "Usage: hub browse [<USER>/]<REPOSITORY>" unless project
# $ hub browse -- wiki
path = case subpage = args.shift
when 'commits'
"/commits/#{branch.short_name}"
when 'tree', NilClass
"/tree/#{branch.short_name}" if branch and !branch.master?
else
"/#{subpage}"
end
project.web_url(path)
end
end
# $ hub compare 1.0..fix
# > open https://github.com/CURRENT_REPO/compare/1.0...fix
# $ hub compare refactor
# > open https://github.com/CURRENT_REPO/compare/refactor
# $ hub compare myfork feature
# > open https://github.com/myfork/REPO/compare/feature
# $ hub compare -u 1.0...2.0
# "https://github.com/CURRENT_REPO/compare/1.0...2.0"
def compare(args)
args.shift
browse_command(args) do
if args.empty?
branch = current_branch.upstream
if branch and not branch.master?
range = branch.short_name
project = current_project
else
abort "Usage: hub compare [USER] [<START>...]<END>"
end
else
sha_or_tag = /(\w{1,2}|\w[\w.-]+\w)/
# replaces two dots with three: "sha1...sha2"
range = args.pop.sub(/^#{sha_or_tag}\.\.#{sha_or_tag}$/, '\1...\2')
project = if owner = args.pop then github_project(nil, owner)
else current_project
end
end
project.web_url "/compare/#{range}"
end
end
# $ hub hub standalone
# Prints the "standalone" version of hub for an easy, memorable
# installation sequence:
#
# $ gem install hub
# $ hub hub standalone > ~/bin/hub && chmod 755 ~/bin/hub
# $ gem uninstall hub
def hub(args)
return help(args) unless args[1] == 'standalone'
require 'hub/standalone'
Hub::Standalone.build $stdout
exit
rescue LoadError
abort "hub is already running in standalone mode."
rescue Errno::EPIPE
exit # ignore broken pipe
end
def alias(args)
shells = %w[bash zsh sh ksh csh fish]
script = !!args.delete('-s')
shell = args[1] || ENV['SHELL']
abort "hub alias: unknown shell" if shell.nil? or shell.empty?
shell = File.basename shell
unless shells.include? shell
$stderr.puts "hub alias: unsupported shell"
warn "supported shells: #{shells.join(' ')}"
abort
end
if script
puts "alias git=hub"
if 'zsh' == shell
puts "if type compdef >/dev/null; then"
puts " compdef hub=git"
puts "fi"
end
else
profile = case shell
when 'bash' then '~/.bash_profile'
when 'zsh' then '~/.zshrc'
when 'ksh' then '~/.profile'
else
'your profile'
end
puts "# Wrap git automatically by adding the following to #{profile}:"
puts
puts 'eval "$(hub alias -s)"'
end
exit
end
# $ hub version
# > git version
# (print hub version)
def version(args)
args.after 'echo', ['hub version', Version]
end
alias_method "--version", :version
# $ hub help
# (print improved help text)
def help(args)
command = args.words[1]
if command == 'hub'
puts hub_manpage
exit
elsif command.nil? && !args.has_flag?('-a', '--all')
ENV['GIT_PAGER'] = '' unless args.has_flag?('-p', '--paginate') # Use `cat`.
puts improved_help_text
exit
end
end
alias_method "--help", :help
private
#
# Helper methods are private so they cannot be invoked
# from the command line.
#
def api_client
@api_client ||= begin
config_file = ENV['HUB_CONFIG'] || '~/.config/hub'
file_store = GitHubAPI::FileStore.new File.expand_path(config_file)
file_config = GitHubAPI::Configuration.new file_store
GitHubAPI.new file_config, :app_url => 'http://defunkt.io/hub/'
end
end
def github_user host = nil
host ||= local_repo(false) && local_repo.main_host
return nil if host.nil?
api_client.config.username(host) { }
end
def custom_command? cmd
CUSTOM_COMMANDS.include? cmd
end
# Show short usage help for `-h` flag, and open man page for `--help`
def respect_help_flags args
return if args.size > 2
case args[1]
when '-h'
pattern = /(git|hub) #{Regexp.escape args[0].gsub('-', '\-')}/
hub_raw_manpage.each_line { |line|
if line =~ pattern
$stderr.print "Usage: "
$stderr.puts line.gsub(/\\f./, '').gsub('\-', '-')
abort
end
}
abort "Error: couldn't find usage help for #{args[0]}"
when '--help'
puts hub_manpage
exit
end
end
# The text print when `hub help` is run, kept in its own method
# for the convenience of the author.
def improved_help_text
<<-help
usage: git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
[-c name=value] [--help]
<command> [<args>]
Basic Commands:
init Create an empty git repository or reinitialize an existing one
add Add new or modified files to the staging area
rm Remove files from the working directory and staging area
mv Move or rename a file, a directory, or a symlink
status Show the status of the working directory and staging area
commit Record changes to the repository
History Commands:
log Show the commit history log
diff Show changes between commits, commit and working tree, etc
show Show information about commits, tags or files
Branching Commands:
branch List, create, or delete branches
checkout Switch the active branch to another branch
merge Join two or more development histories (branches) together
tag Create, list, delete, sign or verify a tag object
Remote Commands:
clone Clone a remote repository into a new directory
fetch Download data, tags and branches from a remote repository
pull Fetch from and merge with another repository or a local branch
push Upload data, tags and branches to a remote repository
remote View and manage a set of remote repositories
Advanced commands:
reset Reset your staging area or working directory to another point
rebase Re-apply a series of patches in one branch onto another
bisect Find by binary search the change that introduced a bug
grep Print files with lines matching a pattern in your codebase
See 'git help <command>' for more information on a specific command.
help
end
# Extract global flags from the front of the arguments list.
# Makes sure important ones are supplied for calls to subcommands.
#
# Known flags are:
# --version --exec-path=<path> --html-path
# -p|--paginate|--no-pager --no-replace-objects
# --bare --git-dir=<path> --work-tree=<path>
# -c name=value --help
#
# Special: `--version`, `--help` are replaced with "version" and "help".
# Ignored: `--exec-path`, `--html-path` are kept in args list untouched.
def slurp_global_flags(args)
flags = %w[ --noop -c -p --paginate --no-pager --no-replace-objects --bare --version --help ]
flags2 = %w[ --exec-path= --git-dir= --work-tree= ]
# flags that should be present in subcommands, too
globals = []
# flags that apply only to main command
locals = []
while args[0] && (flags.include?(args[0]) || flags2.any? {|f| args[0].index(f) == 0 })
flag = args.shift
case flag
when '--noop'
args.noop!
when '--version', '--help'
args.unshift flag.sub('--', '')
when '-c'
# slurp one additional argument
config_pair = args.shift
# add configuration to our local cache
key, value = config_pair.split('=', 2)
git_reader.stub_config_value(key, value)
globals << flag << config_pair
when '-p', '--paginate', '--no-pager'
locals << flag
else
globals << flag
end
end
git_reader.add_exec_flags(globals)
args.add_exec_flags(globals)
args.add_exec_flags(locals)
end
# Handles common functionality of browser commands like `browse`
# and `compare`. Yields a block that returns params for `github_url`.
def browse_command(args)
url_only = args.delete('-u')
warn "Warning: the `-p` flag has no effect anymore" if args.delete('-p')
url = yield
args.executable = url_only ? 'echo' : browser_launcher
args.push url
end
# Returns the terminal-formatted manpage, ready to be printed to
# the screen.
def hub_manpage
abort "** Can't find groff(1)" unless command?('groff')
require 'open3'
out = nil
Open3.popen3(groff_command) do |stdin, stdout, _|
stdin.puts hub_raw_manpage
stdin.close
out = stdout.read.strip
end
out
end
# The groff command complete with crazy arguments we need to run
# in order to turn our raw roff (manpage markup) into something
# readable on the terminal.
def groff_command
"groff -Wall -mtty-char -mandoc -Tascii"
end
# Returns the raw hub manpage. If we're not running in standalone
# mode, it's a file sitting at the root under the `man`
# directory.
#
# If we are running in standalone mode the manpage will be
# included after the __END__ of the file so we can grab it using
# DATA.
def hub_raw_manpage
if File.exists? file = File.dirname(__FILE__) + '/../../man/hub.1'
File.read(file)
else
DATA.read
end
end
# All calls to `puts` in after hooks or commands are paged,
# git-style.
def puts(*args)
page_stdout
super
end
# http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
def page_stdout
return if not $stdout.tty? or windows?
read, write = IO.pipe
if Kernel.fork
# Parent process, become pager
$stdin.reopen(read)
read.close
write.close
# Don't page if the input is short enough
ENV['LESS'] = 'FSRX'
# Wait until we have input before we start the pager
Kernel.select [STDIN]
pager = ENV['GIT_PAGER'] ||
`git config --get-all core.pager`.split.first || ENV['PAGER'] ||
'less -isr'
pager = 'cat' if pager.empty?
exec pager rescue exec "/bin/sh", "-c", pager
else
# Child process
$stdout.reopen(write)
$stderr.reopen(write) if $stderr.tty?
read.close
write.close
end
end
def pullrequest_editmsg(changes)
message_file = File.join(git_dir, 'PULLREQ_EDITMSG')
File.open(message_file, 'w') { |msg|
yield msg
if changes
msg.puts "#\n# Changes:\n#"
msg.puts changes.gsub(/^/, '# ').gsub(/ +$/, '')
end
}
edit_cmd = Array(git_editor).dup
edit_cmd << '-c' << 'set ft=gitcommit' if edit_cmd[0] =~ /^[mg]?vim$/
edit_cmd << message_file
system(*edit_cmd)
abort "can't open text editor for pull request message" unless $?.success?
title, body = read_editmsg(message_file)
abort "Aborting due to empty pull request title" unless title
[title, body]
end
def read_editmsg(file)
title, body = '', ''
File.open(file, 'r') { |msg|
msg.each_line do |line|
next if line.index('#') == 0
((body.empty? and line =~ /\S/) ? title : body) << line
end
}
title.tr!("\n", ' ')
title.strip!
body.strip!
[title =~ /\S/ ? title : nil, body =~ /\S/ ? body : nil]
end
def expand_alias(cmd)
if expanded = git_alias_for(cmd)
if expanded.index('!') != 0
require 'shellwords' unless defined?(::Shellwords)
Shellwords.shellwords(expanded)
end
end
end
def display_api_exception(action, response)
$stderr.puts "Error #{action}: #{response.message} (HTTP #{response.status})"
if 422 == response.status and response.error_message?
# display validation errors
msg = response.error_message
msg = msg.join("\n") if msg.respond_to? :join
warn msg
end
end
end
end