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_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, #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.



154
155
156
157
# File 'lib/squared/workspace/project/git.rb', line 154

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

Class Method Details

.batchargsObject



125
126
127
# File 'lib/squared/workspace/project/git.rb', line 125

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

.config?(val) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
# File 'lib/squared/workspace/project/git.rb', line 129

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

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

.populate(ws) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/squared/workspace/project/git.rb', line 96

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



121
122
123
# File 'lib/squared/workspace/project/git.rb', line 121

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

Instance Method Details

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



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

def branch(flag, names = [], target: nil, ref: nil, opts: [])
  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
    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 = names.partition { |val| val =~ /^[\^~]/ }
    force.each do |val|
      dr = val[0..2]
      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)
    names.compact.each { |val| cmd << shell_quote(val) }
    stdout = true
  when :edit
    cmd << '--edit-description'
  when :current
    cmd << '--show-current'
  else
    opts, pat = option_partition(opts, OPT_BRANCH, no: NO_BRANCH)
    grep = []
    opts.each do |opt|
      if opt =~ /^(v+)$/
        cmd << "-#{$1}"
      elsif opt =~ pat
        case $1
        when 'column', 'format'
          cmd << quote_option($1, $2)
        when 'abbrev'
          cmd << basic_option($1, $2) if $2.to_i > 0
        else
          cmd << shell_option($1, $2)
        end
      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, files = [], branch: nil, origin: nil, create: nil, commit: nil, detach: nil) ⇒ Object



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

def checkout(flag, files = [], branch: nil, origin: nil, create: nil, commit: nil, detach: nil)
  cmd = git_session 'checkout'
  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 << "--#{flag}" << commit
  else
    cmd << "--#{flag}"
    append_first %w[ours theirs]
    append_head
    append_pathspec files
  end
  source
end

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



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

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
  if cmd.include?('--quiet') || cmd.include?('-q')
    quiet = true
  elsif verbose
    quiet = false
  else
    cmd << '--quiet'
    quiet = true
  end
  append_value([data[0], path], delim: true, escape: false)
  source(banner: sync && !quiet, multiple: !sync || quiet)
end

#clone?Boolean

Returns:

  • (Boolean)


917
918
919
# File 'lib/squared/workspace/project/git.rb', line 917

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

#commit(flag, files = [], message: nil, pass: false) ⇒ 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
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
# File 'lib/squared/workspace/project/git.rb', line 708

def commit(flag, files = [], 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 && files.size == 1 && files.first == '*')
               '--all'
             else
               raise_error('commit', 'pathspec', hint: 'missing') if (files = projectmap(files)).empty?
               "-- #{files.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]
    sha = data[2]
    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 (data = /^#{sha} \(HEAD -> #{Regexp.escape(branch)}, (.+?)\)$/.match(out))
          split_escape(data[1]).each do |s|
            next unless s.end_with?("/#{branch}")

            origin = s[0..s.size - branch.size - 2]
            break
          end
        end
      end
      upstream = true if origin
    elsif (data = Regexp.new("^(.+)/#{Regexp.escape(branch)}$").match(data[3]))
      origin = data[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, files = [], branch: nil, index: 0, range: []) ⇒ Object



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

def diff(flag, files = [], branch: nil, index: 0, range: [])
  cmd = git_session 'diff'
  sha = []
  case flag
  when :files, :between, :contain
    cmd.delete('--cached')
  else
    items = files.dup
    files.clear
    items.each do |val|
      if (hash = commithash(val))
        sha << hash
      else
        files << val
      end
    end
  end
  if (val = option('unified')).to_i > 0
    cmd << basic_option('unified', val)
  end
  append_nocolor
  case flag
  when :cached
    cmd << '--cached'
    cmd << '--merge-base' if option('merge-base')
  when :branch
    cmd << branch
  when :files
    cmd << '--no-index'
  when :between, :contain
    cmd << shell_quote(range.join(flag == :between ? '..' : '...'))
  else
    if (val = option('index')) || index > 0
      cmd << "HEAD~#{val || index}"
    elsif !sha.empty? && option('merge-base')
      cmd << '--merge-base'
    end
  end
  unless sha.empty?
    if cmd.include?('--cached')
      raise_error('diff', sha.join(', '), hint: 'one commit') if sha.size > 1
      cmd << sha.first
    else
      cmd.merge(sha)
    end
  end
  append_pathspec(files, parent: flag == :files)
  source(exception: cmd.include?('--exit-code'))
end

#enabled?(**kwargs) ⇒ Boolean

Returns:

  • (Boolean)


921
922
923
# File 'lib/squared/workspace/project/git.rb', line 921

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

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



489
490
491
492
493
494
495
# File 'lib/squared/workspace/project/git.rb', line 489

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

#generate(keys = []) ⇒ Object



436
437
438
439
# File 'lib/squared/workspace/project/git.rb', line 436

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

#ls_files(flag, grep: nil) ⇒ Object



909
910
911
912
913
914
915
# File 'lib/squared/workspace/project/git.rb', line 909

def ls_files(flag, grep: nil)
  git_session 'ls-files', "--#{flag}"
  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, grep: nil) ⇒ Object



901
902
903
904
905
906
907
# File 'lib/squared/workspace/project/git.rb', line 901

def ls_remote(flag, grep: nil)
  git_session 'ls-remote', "--#{flag}", '--refs'
  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



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

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
              if @@task_desc
                format_desc(action, flag, ['refspec=s'] + (action == 'pull' ? OPT_PULL : OPT_FETCH),
                            before: 'remote')
              end
              task flag, [:remote] do |_, args|
                remote = param_guard(action, flag, args: args, key: :remote)
                __send__(action, flag, remote: remote, opts: args.extras)
              end
            else
              format_desc(action, flag, action == 'pull' ? OPT_PULL : OPT_FETCH)
              task flag do |_, args|
                __send__(action, flag, opts: args.to_a)
              end
            end
          when 'commit', 'restore'
            case flag
            when :all
              format_desc action, flag, 'message?'
              task flag, [:message] do |_, args|
                commit(flag, message: args.fetch(:message, nil))
              end
            when :source
              format_desc action, flag, 'tree,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, 'pathspec+'
              task flag do |_, args|
                files = param_guard(action, flag, args: args.to_a)
                __send__ action, flag, files
              end
            end
          when 'tag'
            case flag
            when :list
              format_desc action, flag, 'pattern*'
              task flag do |_, args|
                tag flag, args.to_a
              end
            when :merged
              format_desc action, flag, 'commit,pattern*'
              task flag, [:commit] do |_, args|
                commit = param_guard(action, flag, args: args, key: :commit)
                tag(flag, args.extras, commit: commit)
              end
            when :delete
              format_desc action, flag, 'name+'
              task flag do |_, args|
                name = param_guard(action, flag, args: args.to_a)
                tag flag, name
              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, [name], message: args.message, commit: args.commit)
              end
            end
          when 'stash'
            if flag == :push
              format_desc action, flag, 'pathspec*'
              task flag do |_, args|
                stash flag, args.to_a
              end
            else
              format_desc action, flag, 'commit?'
              task flag, [:commit] do |_, args|
                stash(flag, commit: args.commit)
              end
            end
          when 'diff'
            case flag
            when :head
              format_desc action, flag, 'index?=0,pathspec*'
              task flag do |_, args|
                files = args.to_a
                diff(flag, files, index: /\A\d+\z/.match?(files[0]) && !option('index') ? files.shift.to_i : 0)
              end
            when :cached
              format_desc action, flag, 'pathspec*'
              task flag do |_, args|
                diff flag, args.to_a
              end
            when :branch
              format_desc action, flag, 'name,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, [path1, path2]
              end
            else
              format_desc action, flag, 'commit1,commit2,pathspec*'
              task flag, [:commit1, :commit2] do |_, args|
                commit1 = param_guard(action, flag, args: args, key: :commit1)
                commit2 = param_guard(action, flag, args: args, key: :commit2)
                diff(flag, args.extras, range: [commit1, commit2])
              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 :detach
              format_desc action, flag, 'branch/commit?'
              task flag, [:commit] do |_, args|
                checkout(flag, commit: args.commit)
              end
            else
              format_desc action, flag, '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|
                name = param_guard(action, flag, args: args, key: :name)
                branch(flag, target: name, 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|
                name = param_guard(action, flag, args: args.to_a)
                branch flag, name
              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, OPT_BRANCH, before: 'pattern*')
              task flag do |_, args|
                branch(flag, opts: args.to_a)
              end
            when :current
              format_desc action, flag
              task flag do
                branch flag
              end
            else
              format_desc action, flag, 'newbranch,oldbranch?'
              task flag, [:newbranch, :oldbranch] do |_, args|
                newbranch = param_guard(action, flag, args: args, key: :newbranch)
                branch flag, [args.oldbranch, newbranch]
              end
            end
          when 'reset'
            case flag
            when :mode
              if @@task_desc
                format_desc(action, flag, %w[soft mixed hard merge keep {no-}submodules], after: 'ref?=HEAD',
                                                                                          arg: false)
              end
              task flag, [:mode, :ref] do |_, args|
                mode = param_guard(action, flag, args: args, key: :mode)
                reset(flag, mode: mode, ref: args.ref)
              end
            else
              format_desc action, flag, 'ref,pathspec+'
              task flag, [:ref] do |_, args|
                files = param_guard(action, flag, args: args.extras)
                reset(flag, files, ref: args.ref)
              end
            end
          when 'show'
            case flag
            when :oneline
              format_desc action, flag, 'object*'
              task flag do |_, args|
                show(args.to_a, format: 'oneline', opts: { 'abbrev-commit': true })
              end
            when :format
              format_desc action, flag, 'format?,object*'
              task flag, [:format] do |_, args|
                show(args.extras, format: args.format)
              end
            end
          when 'rebase'
            case flag
            when :onto
              format_desc action, flag, '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 :root
              format_desc action, flag, 'branch,onto?'
              task flag, [:branch, :onto] do |_, args|
                branch = param_guard(action, flag, args: args, key: :branch)
                rebase(flag, commit: args.onto, branch: branch)
              end
            when :send
              format_desc(action, flag, OPT_REBASE, arg: nil)
              task flag, [:command] do |_, args|
                command = param_guard(action, flag, args: args, key: :command)
                rebase(flag, command: command)
              end
            end
          when 'rev'
            format_desc(action, flag, "ref?=HEAD#{flag == :commit ? ',size?' : ''}")
            task flag, [:ref, :size] do |_, args|
              rev_parse(flag, ref: args.ref, size: args.size)
            end
          when 'refs', 'files'
            format_desc action, flag, 'grep?=pattern'
            task flag, [:grep] do |_, args|
              __send__(action == 'refs' ? :ls_remote : :ls_files, flag, grep: args.fetch(:grep, nil))
            end
          end
        end
      end
    end
  end
end

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



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/squared/workspace/project/git.rb', line 441

def pull(flag = nil, sync: invoked_sync?('pull', flag), remote: nil, opts: [])
  cmd = git_session 'pull', flag && "--#{flag}"
  if (val = option('rebase', ignore: false))
    cmd << case val
           when '0'
             '--no-rebase'
           else
             VAL_REBASE.include?(val) ? basic_option('rebase', val) : '--rebase'
           end
  end
  append_pull(opts, OPT_PULL + OPT_FETCH - (FOR_FETCH + FNO_FETCH), NO_PULL + NO_FETCH, 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, sync: invoked_sync?('rebase', flag), command: nil, commit: nil, upstream: nil, branch: nil) ⇒ Object



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

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

  cmd = git_session 'rebase'
  cmd << '--interactive' unless flag == :send || !option('interactive', 'i')
  case flag
  when :onto
    return unless upstream

    cmd << shell_option('onto', commit)
    cmd << shell_escape(upstream)
    append_head branch
  when :root
    return unless branch

    cmd << shell_option('onto', commit) if commit
    cmd << '--root' << shell_escape(branch)
  when :send
    return unless OPT_REBASE.include?(command)

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

#refObject



159
160
161
# File 'lib/squared/workspace/project/git.rb', line 159

def ref
  Git.ref
end

#reset(flag, files = nil, mode: nil, ref: nil) ⇒ Object



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

def reset(flag, files = nil, mode: nil, ref: nil)
  cmd = git_session 'reset'
  case flag
  when :mode
    case mode
    when 'mixed'
      cmd << '--mixed'
      cmd << '-N' if option('n')
      cmd << '--no-refresh' if option('refresh', equals: '0')
    when 'soft', 'hard', 'merge', 'keep'
      cmd << "--#{mode}"
    when 'submodules'
      cmd << '--recurse-submodules'
    when 'no-submodules'
      cmd << '--no-recurse-submodules'
    else
      return
    end
  when :patch
    cmd << '--patch'
  end
  append_commit ref
  append_pathspec files if files
  source
end

#restore(flag, files, tree: nil) ⇒ Object



857
858
859
860
861
862
863
864
# File 'lib/squared/workspace/project/git.rb', line 857

def restore(flag, files, tree: nil)
  source = flag == :source
  cmd = git_session 'restore', source && files.empty? ? '--patch' : nil, shell_option(flag, tree)
  append_option %w[staged worktree]
  append_first %w[ours theirs]
  append_pathspec(files, expect: !cmd.include?('--patch'))
  source(stdout: !source)
end

#rev_parse(flag, ref: nil, size: nil) ⇒ Object



891
892
893
894
895
896
897
898
899
# File 'lib/squared/workspace/project/git.rb', line 891

def rev_parse(flag, ref: nil, size: nil)
  git_session 'rev-parse', if flag == :branch
                             '--abbrev-ref'
                           else
                             (n = size.to_i) > 0 ? basic_option('short', [n, 5].max) : '--verify'
                           end
  append_commit ref
  source
end

#show(objs, format: nil, opts: nil) ⇒ Object



866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'lib/squared/workspace/project/git.rb', line 866

def show(objs, format: nil, opts: nil)
  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
        objs << format
      end
    end
  end
  if opts
    append_hash opts
    banner = format != 'oneline'
  else
    cmd << basic_option('abbrev', val) if (val = option('abbrev')) && val.to_i > 0
    banner = true
  end
  append_value(objs, delim: true)
  source(exception: false, banner: banner)
end

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



526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/squared/workspace/project/git.rb', line 526

def stash(flag = nil, files = [], sync: invoked_sync?('stash', flag), commit: nil)
  cmd = git_session 'stash', flag || 'push'
  case flag
  when :apply, :pop
    cmd << '--index' if option('index')
    cmd << commit
  else
    append_option %w[all staged include-untracked].freeze
    append_message option('message', 'm', ignore: false)
    append_pathspec files
  end
  source(sync: sync, **threadargs)
end

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



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

def status(*, sync: invoked_sync?('status'), **)
  cmd = git_session 'status'
  cmd << (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, refs, message: nil, commit: nil) ⇒ Object



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/squared/workspace/project/git.rb', line 633

def tag(flag, refs, message: nil, commit: nil)
  cmd = git_session 'tag'
  case flag
  when :add
    if option('sign')
      cmd << '--sign'
    else
      out = cmd.to_s
      cmd << '--annotate' if %w[-s --sign -u --local-user].none? { |val| out.include?(" #{val}") }
    end
    if !commit && message && (hash = commithash(message))
      commit = hash
    else
      append_message message
    end
    append_value(refs, escape: false)
    append_head commit
  when :delete, :list, :merged
    cmd << shell_option(flag, commit)
    append_option(%w[contains sort] + (flag == :list ? ['merged'] : []), equals: true) unless flag == :delete
    append_value(refs, escape: false)
  end
  source
end