Class: Squared::Workspace::Project::Git

Inherits:
Base
  • Object
show all
Extended by:
Rake::DSL
Defined in:
lib/squared/workspace/project/git.rb

Direct Known Subclasses

Node, Python, Ruby

Constant Summary

Constants included from Common

Common::ARG, Common::PATH

Instance Attribute Summary

Attributes inherited from Base

#dependfile, #exception, #group, #name, #parent, #path, #pipe, #project, #theme, #verbose, #workspace

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add, aliasargs, #allref, #as, as_path, bannerargs, #basepath, #build, #build?, #clean, #clean?, #copy, #copy?, #depend, #depend?, #dependtype, #dev?, #doc, #doc?, #error, #event, #exclude?, #first, #graph, #graph?, #has?, #initialize_build, #initialize_env, #initialize_events, #initialize_logger, #initialize_ref, #inject, #inspect, #last, #lint, #lint?, #localname, #log, #prod?, ref, #ref?, #script?, #task_include?, #test, #test?, to_s, #to_s, #to_sym, #variable_set, #version, #with

Methods included from Common::Format

#enable_aixterm

Constructor Details

#initializeGit

Returns a new instance of Git.



243
244
245
246
# File 'lib/squared/workspace/project/git.rb', line 243

def initialize(*, **)
  super
  initialize_ref(Git.ref) if gitpath.exist?
end

Class Method Details

.batchargsObject



213
214
215
# File 'lib/squared/workspace/project/git.rb', line 213

def batchargs
  [ref, { 'pull+s': %i[stash pull], 'rebase+s': %i[stash rebase] }]
end

.config?(val) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
220
221
# File 'lib/squared/workspace/project/git.rb', line 217

def config?(val)
  return false unless (val = as_path(val))

  val.join('.git').directory?
end

.populate(ws) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/squared/workspace/project/git.rb', line 184

def populate(ws, **)
  return if ws.series[:pull].empty?

  namespace(name = ws.task_name('git')) do
    all = ws.task_join(name, 'all')

    ws.format_desc(all, %w[stash|rebase depend])
    task 'all' do |_, args|
      opts = args.to_a
      cmd = if opts.include?('stash')
              [ws.task_sync('stash'), ws.task_sync('pull')]
            elsif opts.include?('rebase')
              [ws.task_sync('rebase')]
            else
              [ws.task_sync('pull')]
            end
      cmd << ws.task_sync('depend') if opts.include?('depend') && !ws.series[:depend].empty?
      cmd << ws.task_sync('build')
      Common::Utils.task_invoke(*cmd, **ws.invokeargs)
    end
    ws.series.sync << all
    ws.series.multiple << all
  end
end

.tasksObject



209
210
211
# File 'lib/squared/workspace/project/git.rb', line 209

def tasks
  %i[pull rebase fetch clone stash status].freeze
end

Instance Method Details

#branch(flag, opts = [], refs: [], ref: nil, target: nil) ⇒ Object



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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/squared/workspace/project/git.rb', line 948

def branch(flag, opts = [], refs: [], ref: nil, target: nil)
  cmd = git_session 'branch'
  stdout = false
  case flag
  when :create
    if (arg = option('track', ignore: false))
      cmd << case arg
             when '0'
               '--no-track'
             when 'direct', 'inherit'
               basic_option('track', arg)
             else
               '--track'
             end
    end
    cmd << '--force' if option('force')
  when :set
    return unless ref

    if ref.start_with?('^')
      cmd << '--unset-upstream' << shell_escape(arg[1..-1])
      target = nil
      stdout = true
    else
      cmd << quote_option('set-upstream-to', ref)
    end
    ref = nil
  when :delete
    force, list = refs.partition { |val| val =~ /^[\^~]/ }
    force.each do |val|
      dr = val[0, 3]
      d = dr.include?('^') ? '-D' : '-d'
      r = dr.include?('~') ? '-r' : nil
      source git_output('branch', d, r, shell_quote(val.sub(/^[\^~]+/, '')))
    end
    return if list.empty?

    cmd << '-d'
    list.each { |val| cmd << shell_quote(val) }
  when :move, :copy
    flag = "-#{flag.to_s[0]}"
    cmd << (option('force') ? flag.upcase : flag)
    refs.compact.each { |val| cmd << shell_quote(val) }
    stdout = true
  when :edit
    cmd << '--edit-description'
  when :current
    cmd << '--show-current'
  else
    opts = option_sanitize(opts, OPT_GIT[:branch], no: OPT_GIT[:no][:branch]).first
    grep = []
    opts.each do |opt|
      if opt =~ /^(v+)$/
        cmd << "-#{$1}"
      else
        grep << opt
      end
    end
    cmd << '--list'
    grep.each { |val| cmd << shell_quote(val) }
    out, banner, from = source(io: true)
    print_item banner
    ret = write_lines(out, sub: [
      { pat: /^(\*\s+)(\S+)(\s*)$/, styles: :green, index: 2 },
      { pat: %r{^(\s*)(remotes/\S+)(.*)$}, styles: :red, index: 2 }
    ])
    list_result(ret, 'branches', from: from)
    return
  end
  cmd << shell_escape(target) if target
  cmd << shell_escape(ref) if ref
  source(stdout: stdout)
end

#checkout(flag, opts = [], branch: nil, origin: nil, create: nil, commit: nil, detach: nil) ⇒ Object



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
# File 'lib/squared/workspace/project/git.rb', line 740

def checkout(flag, opts = [], branch: nil, origin: nil, create: nil, commit: nil, detach: nil)
  cmd = git_session 'checkout'
  append_option 'force', 'merge'
  case flag
  when :branch
    cmd << '--detach' if detach == 'd' || option('detach')
    if (val = option('track'))
      cmd << shell_option('track', val)
    end
    cmd << if create
             shell_option(create, branch)
           else
             branch
           end
    cmd << commit
  when :track
    if branch
      if branch.start_with?('^')
        opt = 'B'
        branch = branch[1..-1]
      end
      cmd << shell_option(opt || 'b', branch)
    end
    cmd << '--track' << origin
  when :detach
    cmd << '--detach' << commit
  else
    out = option_sanitize(opts, OPT_GIT[:checkout], no: OPT_GIT[:no][:checkout]).first
    if flag == :commit
      append_value(commit, delim: true)
      option_clear out
    else
      append_head
      append_pathspec out
    end
  end
  source
end

#clone(sync: invoked_sync?('clone')) ⇒ Object



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/squared/workspace/project/git.rb', line 628

def clone(*, sync: invoked_sync?('clone'), **)
  return unless clone? && (data = workspace.git_repo(name))

  cmd = git_session('clone', worktree: false)
  opts = data[1].dup
  if (val = option('depth', ignore: false))
    if (n = val.to_i) > 0
      opts[:depth] = n
    else
      opts.delete(:depth)
    end
  end
  opts[:origin] = val if (val = option('origin', ignore: false))
  opts[:branch] = val if (val = option('branch', strict: true))
  opts[:local] = val != '0' if (val = option('local', strict: true))
  opts.delete(:'recurse-submodules') || opts.delete(:'no-recurse-submodules') if append_submodules(:clone)
  append_hash opts
  cmd << '--quiet' unless verbose
  append_value(data[0], path, delim: true)
  source(banner: sync && !quiet?, multiple: !sync || quiet?)
end

#clone?Boolean

Returns:

  • (Boolean)


1103
1104
1105
# File 'lib/squared/workspace/project/git.rb', line 1103

def clone?
  ref?(workspace.baseref) && workspace.git_clone?(path, name) ? 1 : false
end

#commit(flag, refs: [], message: nil, pass: false) ⇒ Object



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
# File 'lib/squared/workspace/project/git.rb', line 880

def commit(flag, *, refs: [], message: nil, pass: false)
  message ||= option('message', 'm', prefix: 'git', ignore: false)
  amend = flag.to_s.start_with?('amend')
  if !message && !amend
    return if pass

    raise_error('commit', 'GIT_MESSAGE="description"', hint: 'missing')
  end
  pathspec = if flag == :all || (amend && refs.size == 1 && refs.first == '*')
               '--all'
             elsif (refs = projectmap(refs)).empty?
               raise_error('commit', 'pathspec', hint: 'missing')
             else
               "-- #{refs.join(' ')}"
             end
  origin = nil
  branch = nil
  upstream = nil
  source(git_output('fetch', '--no-tags', '--quiet'), io: true, banner: false)
  source(git_output('branch', '-vv', '--list'), io: true, banner: false).first.each do |val|
    next unless (data = /^\*\s(\S+)\s+(\h+)(?:\s\[(.+?)(?=\]\s)\])?\s/.match(val))

    branch = data[1]
    if !data[3]
      unless (origin = option('repository', prefix: 'git', ignore: false))
        out = source(git_output('log', '-n1', '--format=%h%d'), io: true, stdout: true, banner: false).first
        if out =~ /^#{data[2]} \(HEAD -> #{Regexp.escape(branch)}, (.+?)\)$/
          split_escape($1).each do |val|
            next unless val.end_with?("/#{branch}")

            origin = val[0, val.size - branch.size - 1]
            break
          end
        end
      end
      upstream = true if origin
    elsif data[3] =~ %r{^(.+)/#{Regexp.escape(branch)}$}
      origin = $1
    end
    break
  end
  raise_error('commit', 'work tree is not usable') unless origin && branch
  cmd = git_session('commit', option('dry-run') && '--dry-run', options: false)
  if amend
    cmd << '--amend'
  else
    cmd.delete('--amend')
  end
  if message
    append_message message
  elsif flag == :'amend-orig' || option('no-edit')
    cmd << '--no-edit'
  end
  a = git_output 'add', '--verbose'
  b = git_output 'push', upstream && '--set-upstream'
  if dryrun?
    a << '--dry-run'
    b << '--dry-run'
  end
  a << pathspec
  b << '--force' if amend
  b << origin << branch
  puts if pass
  source a
  source cmd
  source b
end

#diff(flag, opts = [], refs: [], branch: nil, range: []) ⇒ Object



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
# File 'lib/squared/workspace/project/git.rb', line 829

def diff(flag, opts = [], refs: [], branch: nil, range: [])
  cmd = git_session 'diff'
  files = option_sanitize(opts, collect_hash(OPT_GIT[:diff]) + OPT_GIT[:log][:diff],
                          no: OPT_GIT[:no][:log][:diff]).first
  case flag
  when :files, :view, :between, :contain
    cmd.delete('--cached')
  else
    items = files.dup
    sha = nil
    files.clear
    items.each do |val|
      if (s = commithash(val))
        (sha ||= []).push(s)
      else
        files << val
      end
    end
  end
  append_nocolor
  if flag == :files
    cmd << '--no-index'
    append_pathspec(refs, parent: true)
  else
    case flag
    when :view
      cmd << '--merge-base' if option('merge-base')
      cmd << shell_quote(range.first, quote: true) << shell_quote(range.last, quote: true)
    when :between, :contain
      cmd.delete('--merge-base')
      cmd << shell_quote(range.join(flag == :between ? '..' : '...'))
    else
      cmd << '--cached' if flag == :cached
      cmd << '--merge-base' if option('merge-base')
      cmd << shell_quote(branch) if branch
      if sha
        if session_arg?('cached')
          raise_error('diff', sha.join(', '), hint: 'one commit') if sha.size > 1
          cmd << sha.first
        else
          cmd.merge(sha)
        end
      elsif (n = option('index'))
        cmd << "HEAD~#{n}"
      end
    end
    append_pathspec files
  end
  source(exception: session_arg?('exit-code'))
end

#enabled?(**kwargs) ⇒ Boolean

Returns:

  • (Boolean)


1107
1108
1109
# File 'lib/squared/workspace/project/git.rb', line 1107

def enabled?(*, **kwargs)
  super || (kwargs[:base] == false && !!clone?)
end

#fetch(flag = nil, opts = [], sync: invoked_sync?('fetch', flag), remote: nil) ⇒ Object



619
620
621
622
623
624
625
626
# File 'lib/squared/workspace/project/git.rb', line 619

def fetch(flag = nil, opts = [], sync: invoked_sync?('fetch', flag), remote: nil)
  cmd = git_session 'fetch'
  cmd << '--all' if !remote && !opts.include?('multiple') && option('all')
  cmd << '--verbose' if verbose && !opts.include?('quiet')
  append_pull(opts, collect_hash(OPT_GIT[:fetch]), no: collect_hash(OPT_GIT[:no][:fetch]),
                                                   remote: remote, flag: flag)
  source(sync: sync, **threadargs)
end

#generate(keys = []) ⇒ Object



562
563
564
565
# File 'lib/squared/workspace/project/git.rb', line 562

def generate(keys = [], **)
  keys << :clone if clone?
  super
end

#logx(flag, opts = [], range: []) ⇒ Object



812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# File 'lib/squared/workspace/project/git.rb', line 812

def logx(flag, opts = [], range: [])
  cmd = git_session 'log'
  files = option_sanitize(opts, collect_hash(OPT_GIT[:log]), no: collect_hash(OPT_GIT[:no][:log])).first
  case flag
  when :between, :contain
    cmd << shell_quote(range.join(flag == :between ? '..' : '...'))
  else
    commit, files = files.partition do |val|
      val.start_with?('^') || (!%r{^[.\\/]}.match?(val) && !%r{[\\/]$}.match?(val)) || commithash(val)
    end
    cmd.merge(commit.map { |val| commithash(val) || shell_quote(val) }) unless commit.empty?
  end
  append_nocolor
  append_pathspec files
  source(exception: false)
end

#ls_files(flag, opts = []) ⇒ Object



1094
1095
1096
1097
1098
1099
1100
1101
# File 'lib/squared/workspace/project/git.rb', line 1094

def ls_files(flag, opts = [])
  git_session 'ls-files', "--#{flag}"
  grep = option_sanitize(opts, OPT_GIT[:ls_files]).first
  out, banner, from = source(io: true)
  print_item banner
  ret = write_lines(out, grep: grep)
  list_result(ret, 'files', from: from, grep: grep)
end

#ls_remote(flag, opts = [], remote: nil) ⇒ Object



1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
# File 'lib/squared/workspace/project/git.rb', line 1083

def ls_remote(flag, opts = [], remote: nil)
  cmd = git_session 'ls-remote', '--refs'
  cmd << "--#{flag}" unless flag == :remote
  grep = option_sanitize(opts, OPT_GIT[:ls_remote]).first
  cmd << shell_quote(remote) if remote
  out, banner, from = source(io: true)
  print_item banner
  ret = write_lines(out, grep: grep)
  list_result(ret, flag.to_s, from: from, grep: grep)
end

#populateObject



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
# File 'lib/squared/workspace/project/git.rb', line 252

def populate(*, **)
  super
  return unless ref?(Git.ref)

  namespace name do
    @@tasks[Git.ref].each do |action, flags|
      next if @pass.include?(action)

      namespace action do
        flags.each do |flag|
          case action
          when 'pull', 'fetch'
            if flag == :remote
              format_desc(action, flag, 'remote,opts*')
              task flag, [:remote] do |_, args|
                remote = param_guard(action, flag, args: args, key: :remote)
                __send__(action, flag, args.extras, remote: remote)
              end
            else
              format_desc action, flag, 'opts*'
              task flag do |_, args|
                __send__ action, flag, args.to_a
              end
            end
          when 'commit'
            case flag
            when :all
              format_desc action, flag, 'message?'
              task flag, [:message] do |_, args|
                commit(flag, message: args.fetch(:message, nil))
              end
            else
              format_desc action, flag, 'pathspec+'
              task flag do |_, args|
                refs = param_guard(action, flag, args: args.to_a)
                commit(flag, refs: refs)
              end
            end
          when 'restore'
            if flag == :source
              format_desc action, flag, 'tree,opts*,pathspec*'
              task flag, [:tree] do |_, args|
                tree = param_guard(action, flag, args: args, key: :tree)
                restore(flag, args.extras, tree: tree)
              end
            else
              format_desc action, flag, 'opts*,pathspec+'
              task flag do |_, args|
                restore flag, args.to_a
              end
            end
          when 'tag'
            case flag
            when :list
              format_desc action, flag, 'opts*,pattern*'
              task flag do |_, args|
                tag flag, args.to_a
              end
            when :delete
              format_desc action, flag, 'name+'
              task flag do |_, args|
                refs = param_guard(action, flag, args: args.to_a)
                tag(flag, refs: refs)
              end
            when :add
              format_desc action, flag, 'name,message?,commit?'
              task flag, [:name, :message, :commit] do |_, args|
                name = param_guard(action, flag, args: args, key: :name)
                tag(flag, refs: [name], message: args.message, commit: args.commit)
              end
            end
          when 'stash'
            if flag == :list
              format_desc action, flag
            else
              format_desc(action, flag, '*opts', after: flag == :push ? 'pathspec*' : 'commit?')
            end
            task flag do |_, args|
              stash flag, args.to_a
            end
          when 'log', 'diff'
            case flag
            when :view, :between, :contain
              if flag == :view && action == 'log'
                format_desc action, flag, '(^)commit*,pathspec*,opts*'
                task flag do |_, args|
                  logx flag, args.to_a
                end
              else
                format_desc action, flag, 'commit1,commit2,pathspec*,opts*'
                task flag, [:commit1, :commit2] do |_, args|
                  commit1 = param_guard(action, flag, args: args, key: :commit1)
                  commit2 = param_guard(action, flag, args: args, key: :commit2)
                  __send__(action == 'log' ? :logx : :diff, flag, args.extras, range: [commit1, commit2])
                end
              end
            when :head, :cached
              format_desc action, flag, 'opts*,pathspec*'
              task flag do |_, args|
                diff flag, args.to_a
              end
            when :branch
              format_desc action, flag, 'name,opts*,pathspec*'
              task flag, [:name] do |_, args|
                branch = param_guard(action, flag, args: args, key: :name)
                diff(flag, args.extras, branch: branch)
              end
            when :files
              format_desc action, flag, 'path1,path2'
              task flag, [:path1, :path2] do |_, args|
                path1 = param_guard(action, flag, args: args, key: :path1)
                path2 = param_guard(action, flag, args: args, key: :path2)
                diff(flag, refs: [path1, path2])
              end
            end
          when 'checkout'
            case flag
            when :branch
              format_desc action, flag, 'name,create?=[bB],commit?,detach?=d'
              task flag, [:name, :create, :commit, :detach] do |_, args|
                branch = param_guard(action, flag, args: args, key: :name)
                create = args.create
                if args.commit == 'd'
                  detach = 'd'
                  commit = nil
                elsif create == 'd'
                  create = nil
                  commit = nil
                  detach = 'd'
                elsif create && create.size > 1
                  commit = create
                  create = nil
                  detach = args.commit
                else
                  detach = args.detach
                  commit = args.commit
                end
                param_guard(action, flag, args: { create: create }, key: :create, pat: /\Ab\z/i) if create
                checkout(flag, branch: branch, create: create, commit: commit, detach: detach)
              end
            when :track
              format_desc action, flag, 'origin,(^)name?'
              task flag, [:origin, :name] do |_, args|
                origin = param_guard(action, flag, args: args, key: :origin)
                checkout(flag, branch: args.name, origin: origin)
              end
            when :commit
              format_desc action, flag, 'branch/commit,opts*'
              task flag, [:commit] do |_, args|
                commit = param_guard(action, flag, args: args, key: :commit)
                checkout(flag, commit: commit)
              end
            when :detach
              format_desc action, flag, 'branch/commit?'
              task flag, [:commit] do |_, args|
                checkout(flag, commit: args.commit)
              end
            when :path
              format_desc action, flag, 'opts*,pathspec*'
              task flag do |_, args|
                checkout flag, args.to_a
              end
            end
          when 'branch'
            case flag
            when :create
              format_desc action, flag, 'name,ref?=HEAD'
              task flag, [:name, :ref] do |_, args|
                target = param_guard(action, flag, args: args, key: :name)
                branch(flag, target: target, ref: args.ref)
              end
            when :set
              format_desc(action, flag, '(^)upstream,name?')
              task flag, [:upstream, :name] do |_, args|
                upstream = param_guard(action, flag, args: args, key: :upstream)
                branch(flag, target: args.name, ref: upstream)
              end
            when :delete
              format_desc action, flag, '(^~)name+'
              task flag, [:name] do |_, args|
                refs = param_guard(action, flag, args: args.to_a)
                branch(flag, refs: refs)
              end
            when :edit
              format_desc action, flag, 'name?'
              task flag, [:name] do |_, args|
                branch(flag, target: args.name)
              end
            when :list
              format_desc action, flag, 'opts*,pattern*'
              task flag do |_, args|
                branch flag, args.to_a
              end
            when :current
              format_desc action, flag
              task flag do
                branch flag
              end
            else
              format_desc action, flag, 'branch,oldbranch?'
              task flag, [:branch, :oldbranch] do |_, args|
                branch = param_guard(action, flag, args: args, key: :branch)
                branch(flag, refs: [args.oldbranch, branch])
              end
            end
          when 'reset'
            case flag
            when :commit
              format_desc action, flag, 'branch/commit,opts*'
              task flag, [:commit] do |_, args|
                commit = param_guard(action, flag, args: args, key: :commit)
                reset(flag, args.extras, commit: commit)
              end
            when :index
              format_desc action, flag, 'opts*,pathspec*'
              task flag do |_, args|
                reset flag, args.to_a
              end
            when :mode
              format_desc action, flag, 'mode,ref?=HEAD'
              task flag, [:mode, :ref] do |_, args|
                mode = param_guard(action, flag, args: args, key: :mode)
                reset(flag, mode: mode, ref: args.ref)
              end
            when :patch
              format_desc action, flag, 'ref,pathspec*'
              task flag, [:ref] do |_, args|
                ref = param_guard(action, flag, args: args, key: :ref)
                reset(flag, refs: args.extras, ref: ref)
              end
            end
          when 'show'
            case flag
            when :oneline
              format_desc action, flag, 'opts*,object*'
              task flag do |_, args|
                show 'oneline', args.to_a.push('abbrev-commit')
              end
            when :format
              format_desc action, flag, 'format?,opts*,object*'
              task flag, [:format] do |_, args|
                show args.format, args.extras
              end
            end
          when 'rebase'
            case flag
            when :branch
              format_desc action, flag, 'opts*,upstream?,branch?'
              task flag do |_, args|
                args = param_guard(action, flag, args: args.to_a)
                rebase flag, args
              end
            when :onto
              format_desc action, flag, 'branch/commit,upstream,branch?=HEAD'
              task flag, [:commit, :upstream, :branch] do |_, args|
                commit = param_guard(action, flag, args: args, key: :commit)
                upstream = param_guard(action, flag, args: args, key: :upstream)
                rebase(flag, commit: commit, upstream: upstream, branch: args.branch)
              end
            when :send
              format_desc(action, flag, VAL_GIT[:rebase][:send], arg: nil)
              task flag, [:command] do |_, args|
                command = param_guard(action, flag, args: args, key: :command)
                rebase(flag, command: command)
              end
            end
          when 'rev'
            case flag
            when :commit
              format_desc action, flag, 'ref?=HEAD,size?'
              task flag, [:ref, :size] do |_, args|
                ref = args.ref
                size = args.size
                if !size && ref.to_i > 0
                  size = ref
                  ref = nil
                end
                rev_parse(flag, ref: ref, size: size)
              end
            when :branch
              format_desc action, flag, 'ref?=HEAD'
              task flag, [:ref] do |_, args|
                rev_parse(flag, ref: args.ref)
              end
            else
              format_desc action, flag, 'opts*,args*'
              task flag do |_, args|
                rev_parse flag, args.to_a
              end
            end
          when 'refs', 'files'
            if flag == :remote
              format_desc action, flag, 'remote,opts*,pattern*'
              task flag, [:remote] do |_, args|
                remote = param_guard(action, flag, args: args, key: :remote)
                ls_remote(flag, args.to_a.drop(1), remote: remote)
              end
            else
              format_desc action, flag, 'opts*,pattern*'
              task flag do |_, args|
                __send__(action == 'refs' ? :ls_remote : :ls_files, flag, args.to_a)
              end
            end
          end
        end
      end
    end
  end
end

#pull(flag = nil, opts = [], sync: invoked_sync?('pull', flag), remote: nil) ⇒ Object



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/squared/workspace/project/git.rb', line 567

def pull(flag = nil, opts = [], sync: invoked_sync?('pull', flag), remote: nil)
  cmd = git_session 'pull'
  if (val = option('rebase', ignore: false))
    cmd << case val
           when '0'
             '--no-rebase'
           else
             VAL_GIT[:rebase][:value].include?(val) ? basic_option('rebase', val) : '--rebase'
           end
  end
  append_pull(opts, OPT_GIT[:pull] + OPT_GIT[:fetch][:pull],
              no: OPT_GIT[:no][:pull] + OPT_GIT[:no][:fetch][:pull], remote: remote, flag: flag)
  source(sync: sync, sub: if verbose
                            [
                              { pat: /^(.+)(\|\s+\d+\s+)([^-]*)(-+)(.*)$/, styles: :red, index: 4 },
                              { pat: /^(.+)(\|\s+\d+\s+)(\++)(-*)(.*)$/, styles: :green, index: 3 }
                            ]
                          end, **threadargs)
end

#rebase(flag = nil, opts = [], sync: invoked_sync?('rebase', flag), commit: nil, upstream: nil, branch: nil, command: nil) ⇒ Object



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
# File 'lib/squared/workspace/project/git.rb', line 587

def rebase(flag = nil, opts = [], sync: invoked_sync?('rebase', flag), commit: nil, upstream: nil, branch: nil,
           command: nil)
  return pull(:rebase, sync: sync) unless flag

  cmd = git_session 'rebase'
  case flag
  when :branch
    branch = option_sanitize(opts, OPT_GIT[:rebase], no: OPT_GIT[:no][:rebase]).first
    case branch.size
    when 0
      append_head
    when 1, 2
      append_value(branch, delim: true)
    else
      append_value([branch.pop, branch.pop].reverse, delim: true)
      option_clear branch
    end
  when :onto
    return unless upstream

    cmd << '--interactive' if option('interactive', 'i')
    cmd << shell_option('onto', commit) if commit
    cmd << shell_escape(upstream)
    append_head branch
  else
    return unless VAL_GIT[:rebase][:send].include?(command)

    cmd << "--#{command}"
  end
  source
end

#refObject



248
249
250
# File 'lib/squared/workspace/project/git.rb', line 248

def ref
  Git.ref
end

#reset(flag, opts = [], refs: nil, ref: nil, mode: nil, commit: nil) ⇒ Object



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
# File 'lib/squared/workspace/project/git.rb', line 708

def reset(flag, opts = [], refs: nil, ref: nil, mode: nil, commit: nil)
  cmd = git_session 'reset'
  case flag
  when :commit, :index
    out = option_sanitize(opts, OPT_GIT[:reset] + VAL_GIT[:reset], no: OPT_GIT[:no][:reset]).first
    if flag == :commit
      append_value(commit, delim: true)
      option_clear out
      ref = false
    else
      (refs ||= []).concat(out)
    end
  when :mode
    return unless VAL_GIT[:reset].include?(mode)

    cmd << "--#{mode}"
    if mode == 'mixed'
      cmd << '-N' if option('n')
      cmd << '--no-refresh' if option('refresh', equals: '0')
    end
  when :patch
    cmd << '--patch'
  else
    return
  end
  unless ref == false
    append_commit ref
    append_pathspec refs if refs
  end
  source
end

#restore(flag, opts = [], tree: nil) ⇒ Object



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/squared/workspace/project/git.rb', line 1022

def restore(flag, opts = [], tree: nil)
  cmd = git_session 'restore'
  refs = option_sanitize(opts, OPT_GIT[:restore], no: OPT_GIT[:no][:restore]).first
  if flag == :source
    cmd << '--patch' if refs.empty?
    cmd << shell_option('source', tree)
  else
    cmd << "--#{flag}"
  end
  if session_arg?('p', 'patch')
    option_clear refs
  else
    append_pathspec(refs, expect: true)
  end
  source(sync: false, stderr: true)
end

#rev_parse(flag, opts = [], ref: nil, size: nil) ⇒ Object



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/squared/workspace/project/git.rb', line 1063

def rev_parse(flag, opts = [], ref: nil, size: nil)
  cmd = git_session 'rev-parse', if flag == :parseopt
                                   '--parseopt'
                                 elsif opts.delete('sq-quote')
                                   '--sq-quote'
                                 end
  case flag
  when :commit
    cmd << ((n = size.to_i) > 0 ? basic_option('short', [n, 5].max) : '--verify')
    append_commit ref
  when :branch
    cmd << '--abbrev-ref'
    append_commit ref
  else
    args = option_sanitize(opts, OPT_GIT[:rev_parse][flag]).first
    append_value(args, escape: session_arg?('sq-quote'))
  end
  source(banner: verbose == 1)
end

#show(format, opts = []) ⇒ Object



1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
# File 'lib/squared/workspace/project/git.rb', line 1039

def show(format, opts = [])
  cmd = git_session 'show'
  if format
    case (val = format.downcase)
    when 'oneline', 'short', 'medium', 'full', 'fuller', 'reference', 'email', 'raw'
      cmd << basic_option('format', val)
    else
      if format =~ /^t?format:/ || format.include?('%')
        cmd << quote_option('pretty', format)
      else
        opts << format
      end
    end
  end
  refs = option_sanitize(opts, OPT_GIT[:show] + OPT_GIT[:diff][:show] + OPT_GIT[:log][:diff],
                         no: OPT_GIT[:no][:show] + collect_hash(OPT_GIT[:no][:log], pass: [:base])).first
  unless val == 'oneline' && session_arg?('abbrev-commit')
    cmd << basic_option('abbrev', val) if (val = option('abbrev')) && val.to_i > 0
    banner = true
  end
  append_value(refs, delim: true)
  source(exception: false, banner: banner)
end

#stash(flag = nil, opts = [], sync: invoked_sync?('stash', flag)) ⇒ Object



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
# File 'lib/squared/workspace/project/git.rb', line 650

def stash(flag = nil, opts = [], sync: invoked_sync?('stash', flag))
  if flag
    cmd = git_session 'stash', flag
    list = OPT_GIT[:stash][:common] + OPT_GIT[:stash].fetch(flag, [])
    refs = option_sanitize(opts, list).first
    case flag
    when :push
      append_pathspec refs
    when :pop, :apply, :drop
      cmd << shell_quote(refs.pop)
      option_clear refs
    when :list
      out, banner, from = source(io: true)
      print_item banner
      ret = write_lines(out)
      list_result(ret, 'objects', from: from)
      return
    end
  else
    git_session 'stash', 'push'
    append_option(%w[all keep-index include-untracked staged].freeze, no: true, ignore: false)
    append_message option('message', 'm', ignore: false)
  end
  source(banner: !quiet?, sync: sync, **threadargs)
end

#status(sync: invoked_sync?('status')) ⇒ Object



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
# File 'lib/squared/workspace/project/git.rb', line 676

def status(*, sync: invoked_sync?('status'), **)
  cmd = git_session 'status', option('long') ? '--long' : '--short'
  if (val = option('ignore-submodules', ignore: false))
    cmd << basic_option('ignore-submodules', case val
                                             when '0', 'none'
                                               'none'
                                             when '1', 'untracked'
                                               'untracked'
                                             when '2', 'dirty'
                                               'dirty'
                                             else
                                               'all'
                                             end)
  end
  append_pathspec
  out, banner, from = source(io: true)
  if sync
    print_item banner
    banner = nil
  end
  ret = write_lines(out, banner: banner, sub: if verbose
                                                [
                                                  { pat: /^(.)([A-Z])(.+)$/, styles: :red, index: 2 },
                                                  { pat: /^([A-Z])(.+)$/, styles: :green },
                                                  { pat: /^(\?\?)(.+)$/, styles: :red },
                                                  { pat: /^(## )(.+)(\.{3})(.+)$/,
                                                    styles: [nil, :green, nil, :red], index: -1 }
                                                ]
                                              end)
  list_result(ret, 'files', from: from, action: 'modified')
end

#tag(flag, opts = [], refs: [], message: nil, commit: nil) ⇒ Object



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
# File 'lib/squared/workspace/project/git.rb', line 779

def tag(flag, opts = [], refs: [], message: nil, commit: nil)
  cmd = git_session 'tag'
  case flag
  when :add
    if option('sign')
      cmd << '--sign'
    elsif !session_arg?('s', 'sign', 'u', 'local-user')
      cmd << '--annotate'
    end
    if !commit && message && (hash = commithash(message))
      commit = hash
    else
      append_message message
    end
    append_value refs
    append_head commit
  when :list
    cmd << '--list'
    grep = option_sanitize(opts, OPT_GIT[:tag], no: OPT_GIT[:no][:tag]).first
    out, banner, from = source(io: true)
    print_item banner
    ret = write_lines(out, grep: grep)
    list_result(ret, 'tags', from: from, grep: grep)
    return
  when :delete
    cmd << '--delete'
    append_value refs
  else
    cmd << shell_option(flag, commit)
  end
  source
end