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, #archive, #archive?, #as, as_path, bannerargs, #basepath, batchargs, #build, #build?, #clean?, #copy, #copy?, #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?, #rootpath, #script?, #series, subtasks, #task_include?, #test, #test?, to_s, #to_s, #to_sym, #unpack, #variable_set, #version, #with

Methods included from Common::Format

#enable_aixterm, #enable_drawing

Constructor Details

#initializeGit

Returns a new instance of Git.



346
347
348
349
# File 'lib/squared/workspace/project/git.rb', line 346

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

Class Method Details

.config?(val) ⇒ Boolean

Returns:

  • (Boolean)


317
318
319
320
321
# File 'lib/squared/workspace/project/git.rb', line 317

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

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

.populate(ws) ⇒ Object



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

def populate(ws, **)
  return if ws.series.exclude?(:pull, true)

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

    ws.format_desc(all, 'stash|rebase|autostash?,depend?')
    task 'all' do |_, args|
      args = args.to_a
      cmd = if args.include?('stash')
              ['stash', 'pull']
            elsif args.include?('rebase')
              ['rebase']
            elsif args.include?('autostash')
              ['autostash']
            else
              ['pull']
            end
      cmd.map! { |val| ws.task_sync(val) }
      cmd << ws.task_sync('depend') if args.include?('depend') && !ws.series.exclude?(:depend, true)
      cmd << ws.task_sync('build')
      Common::Utils.task_invoke(*cmd, **ws.invokeargs)
    end
    ws.series.sync << all
    ws.series.multiple << all
  end
end

.tasksObject



313
314
315
# File 'lib/squared/workspace/project/git.rb', line 313

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

Instance Method Details

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



969
970
971
# File 'lib/squared/workspace/project/git.rb', line 969

def autostash(*, sync: invoked_sync?('autostash'), **)
  pull(:autostash, sync: sync)
end

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



1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
# File 'lib/squared/workspace/project/git.rb', line 1388

def branch(flag = nil, opts = [], refs: [], ref: nil, target: nil, remote: nil)
  cmd, opts = git_session('branch', opts: opts)
  stdout = false
  case flag
  when :create
    if (arg = option('track', ignore: false))
      cmd << case arg
             when '0', 'false'
               '--no-track'
             when 'direct', 'inherit'
               basic_option('track', arg)
             else
               '--track'
             end
    end
    cmd << '--force' if option('force')
    cmd << shell_quote(target)
    cmd << shell_quote(ref) if ref
  when :track
    raise_error('invalid upstream', hint: ref) unless ref.include?('/')
    if ref.start_with?('^')
      cmd << '--unset-upstream' << shell_quote(ref[1..-1])
      remote = false
      stdout = true
    else
      cmd << quote_option('set-upstream-to', ref)
      cmd << shell_quote(target) if target
    end
  when :delete
    remote&.each do |val|
      source git_output('push', '--delete', *val.split('/', 2).map { |s| shell_quote(s) })
    end
    force, list = refs.partition { |val| val.match?(/^[~^]/) }
    force.each do |val|
      dr = val[0, 2]
      d = dr.include?('^') ? '-D' : '-d'
      r = '-r' if dr.include?('~')
      source git_output('branch', d, r, shell_quote(val.sub(/^[~^]+/, '')))
    end
    return if list.empty?

    cmd << '-d'
    append_value list
    remote = nil
  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 :current
    cmd << '--show-current'
    source(banner: verbosetype > 1, stdout: true)
    return
  when :list
    op = OptionPartition.new(opts, OPT_GIT[:branch], cmd << '--list',
                             project: self, no: OPT_GIT[:no][:branch], single: /\Av+\z/)
    op.each { |val| op << shell_quote(val) }
    out, banner, from = source(io: true)
    print_item banner
    ret = write_lines(out, sub: [
      { pat: /^(\*\s+)(\S+)(.*)$/, styles: color(:green), index: 2 },
      { pat: %r{^(\s*)(remotes/\S+)(.*)$}, styles: color(:red), index: 2 }
    ])
    list_result(ret, 'branches', from: from)
    return
  else
    head = git_spawn('rev-parse --abbrev-ref HEAD').chomp
    if head.empty?
      ret = 0
    else
      git_spawn 'fetch --all --prune --quiet' if option('sync')
      out, banner, from = source(cmd << '-vv --no-abbrev --list', io: true)
      ret = write_lines(out, grep: /^\*\s+#{Regexp.escape(head)}\s/, banner: banner, first: true) do |line|
        next line if stdin?

        data = line.sub(/^\*\s+/, '').split(/\s+/)
        a = sub_style(data[0], styles: theme[:inline])
        b = sub_style(data[1], styles: theme[:extra])
        r = /\A(?:\[(.+?)(?=\]\s)\]\s)?(.+)\z/m.match(data[2..-1].join(' '))
        if (r1 = r[1]) && r1 =~ /^(.+):(?: ([a-z]+) (\d+),)? ([a-z]+) (\d+)$/
          write = ->(s1, s2) { "#{s1.capitalize.rjust(7)}: #{sub_style(s2, styles: theme[:warn])}" }
          r1 = $1
          r2 = $2 && write.call($2, $3)
          r3 = write.call($4, $5)
        end
        r1 = nil if r1 == "origin/#{data[0]}"
        [" Branch: #{a + (r1 ? " (#{r1})" : '')}", r2, r3, " Commit: #{b}", "Message: #{r[2]}"]
          .compact
          .join("\n")
      end
      on :last, from
    end
    if ret == 0
      warn log_message(Logger::WARN, name, 'no ref found', subject: 'branch', hint: 'head', pass: true)
    end
    return
  end
  return unless success?(source(stdout: stdout))

  if !ref
    print_success if flag == :create
  elsif remote && target
    source git_output('push', '-u', shell_quote(ref.split('/', 2).first), shell_quote(target))
  end
end

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



1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
# File 'lib/squared/workspace/project/git.rb', line 1164

def checkout(flag, opts = [], branch: nil, origin: nil, create: nil, commit: nil, detach: nil, merge: false)
  cmd, opts = git_session('checkout', opts: opts)
  append_option 'force', 'merge'
  case flag
  when :branch
    cmd << '--detach' if detach == 'd' || option('detach')
    append_option('track', equals: true)
    cmd << (create ? quote_option(create, branch) : branch) << commit
  when :track
    if branch
      if branch.start_with?('^')
        opt = 'B'
        branch = branch[1..-1]
      end
      cmd << quote_option(opt || 'b', branch)
    end
    cmd << '--track' << shell_quote(origin)
  when :detach
    cmd << '-m' if merge
    cmd << '--detach' << commit
  else
    op = OptionPartition.new(opts, OPT_GIT[:checkout], cmd, project: self, no: OPT_GIT[:no][:checkout],
                                                            first: flag == :path ? matchpathspec : nil)
    if flag == :commit
      op.append(commit)
        .clear(pass: false)
    else
      append_head
      append_pathspec(op.extras, pass: false)
    end
  end
  source
end

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



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

def clean(*, sync: invoked_sync?('clean'), **)
  workspace.rev_clear(name, sync: sync)
  super
end

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



980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File 'lib/squared/workspace/project/git.rb', line 980

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(from: :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)


1648
1649
1650
# File 'lib/squared/workspace/project/git.rb', line 1648

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

#commit(flag, opts = [], refs: [], ref: nil, squash: nil, pick: nil, message: nil, pass: false) ⇒ Object



1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
# File 'lib/squared/workspace/project/git.rb', line 1287

def commit(flag, opts = [], refs: [], ref: nil, squash: nil, pick: nil, message: nil, pass: false)
  fixup = flag == :fixup
  amend = !fixup && flag.to_s.start_with?('amend')
  unless flag == :add || pick == 'reword'
    pathspec = if flag == :all || ((fixup || amend) && refs.size == 1 && refs.first == '*')
                 '--all'
               elsif (refs = projectmap(refs)).empty?
                 raise_error 'no qualified pathspec'
               else
                 "-- #{refs.join(' ')}"
               end
  end
  if fixup
    source git_session('commit', basic_option('fixup', "#{pick ? "#{pick}:" : ''}#{ref}"), pathspec)
    source git_output('rebase --autosquash', squash) if squash.is_a?(String)
    return
  end
  message ||= messageopt
  if !message && !amend
    return if pass

    message = readline('Enter message', force: true)
  end
  branch = nil
  origin = nil
  upstream = nil
  cmd, opts = git_session('add', opts: opts)
  op = OptionPartition.new(opts, OPT_GIT[:add], cmd, project: self, first: matchpathspec)
  op << '--verbose' if verbose
  dryrun = dryrun?
  format = '%(if)%(HEAD)%(then)%(refname:short)...%(upstream:short)...%(upstream:track)%(end)'
  git_spawn 'fetch --no-tags --quiet'
  foreachref('heads', format: format).each do |line|
    next if (line = line.chomp).empty?

    branch, origin, hint = line.split('...')
    if hint && !hint.match?(/^\[(\D+0,\D+0)\]$/)
      raise_error('work tree is not usable', hint: hint[1..-2])
    elsif origin.empty?
      return nil if pass
      break if dryrun

      unless (origin = option('upstream', prefix: 'git', ignore: false))
        if (origin = choice_refs('Choose an upstream', 'remotes', attempts: 1, force: false))
          git_spawn 'branch', quote_option('set-upstream-to', origin)
        else
          origin = readline('Enter an upstream', force: true)
        end
      end
      raise_error('missing remote name', hint: origin) unless origin.include?('/')
      upstream = true
    end
    break
  end
  if pathspec
    op << pathspec
  else
    append_pathspec op.extras
  end
  co = git_session('commit', options: false)
  pu = git_output 'push', upstream && '--set-upstream'
  if dryrun
    op.delete('--dry-run')
    co << '--dry-run'
    pu << '--dry-run'
  end
  co << '--amend' if amend
  if message
    append_message message
  elsif flag == :'amend-orig' || option('edit', equals: '0')
    co << '--no-edit'
  end
  pu << '--force-with-lease' if amend
  pu.merge(repotrack(origin, branch))
  puts if pass
  source op
  source co
  source pu
end

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



905
906
907
908
# File 'lib/squared/workspace/project/git.rb', line 905

def depend(*, sync: invoked_sync?('depend'), **)
  workspace.rev_clear(name, sync: sync)
  super
end

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



1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
# File 'lib/squared/workspace/project/git.rb', line 1247

def diff(flag, opts = [], refs: [], branch: nil, range: [], index: [])
  cmd, opts = git_session('diff', opts: opts)
  op = OptionPartition.new(opts, collect_hash(OPT_GIT[:diff]) + OPT_GIT[:log][:diff], cmd,
                           project: self, no: OPT_GIT[:no][:log][:diff],
                           first: flag == :files ? nil : matchpathspec)
  case flag
  when :files, :view, :between, :contain
    op.delete('--cached')
  end
  append_nocolor
  if flag == :files
    op << '--no-index'
    append_pathspec(refs, parent: true)
  else
    case flag
    when :view
      op << '--merge-base' if option('merge-base')
      op.merge(range)
    when :between, :contain
      op.delete('--merge-base')
      op << shell_quote(range.join(flag == :between ? '..' : '...'))
    else
      op << '--merge-base' if option('merge-base')
      op << shell_quote(branch) if branch
      if !index.empty?
        if op.arg?('cached')
          raise_error("one commit only: #{index.join(', ')}", hint: '--cached') if index.size > 1
          op << index.first
        else
          op.merge(index)
        end
      elsif (n = option('index', ignore: false))
        op << "HEAD~#{n}"
      end
    end
    append_pathspec op.extras
  end
  source(exception: op.arg?('exit-code'))
end

#enabled?(**kwargs) ⇒ Boolean

Returns:

  • (Boolean)


1656
1657
1658
# File 'lib/squared/workspace/project/git.rb', line 1656

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

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



973
974
975
976
977
978
# File 'lib/squared/workspace/project/git.rb', line 973

def fetch(flag = nil, opts = [], sync: invoked_sync?('fetch', flag), remote: nil)
  opts = git_session('fetch', opts: opts).last
  append_pull(opts, collect_hash(OPT_GIT[:fetch]), no: collect_hash(OPT_GIT[:no][:fetch]),
                                                   remote: remote, flag: flag, from: :fetch)
  source(sync: sync, **threadargs)
end

#generate(keys = []) ⇒ Object



900
901
902
903
# File 'lib/squared/workspace/project/git.rb', line 900

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

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



1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
# File 'lib/squared/workspace/project/git.rb', line 1601

def git(flag, opts = [])
  cmd, opts = git_session(flag, opts: opts)
  list = OPT_GIT[:git].fetch(flag, []) + OPT_GIT.fetch(flag, [])
  op = OptionPartition.new(opts, list, cmd, project: self, no: OPT_GIT[:no][flag],
                                            first: flag == :revert ? nil : matchpathspec)
  case flag
  when :revert
    if VAL_GIT[:rebase][:send].any? { |val| op.arg?(val) }
      op.clear
    elsif op.empty?
      raise_error 'no commit given'
    else
      append_commit(*op.extras)
    end
  when :add, :clean
    if flag == :add && !op.arg?('pathspec-from-file')
      grep, list = op.extras.partition { |val| val.start_with?(':') && val.end_with?(':') }
      if list.empty? || !grep.empty?
        red = color(:red)
        grep.map! { |val| Regexp.new(val[1..-2]) }
        files = status_data.map! do |a, b|
          next unless grep.empty? || grep.any? { |pat| pat.match?(a) }

          "#{sub_style(b, styles: red)} #{a}"
        end
        .compact
        unless files.empty?
          files = choice_index('Select files', files, multiple: true, force: true, trim: /^\S+\s/,
                                                      accept: 'Add?')
        end
        op.swap(list + files)
      end
    end
    append_pathspec(op.extras)
    verbose = flag == :add && !op.arg?('verbose')
    print_success if success?(source) && verbose
    return
  when :mv
    refs = projectmap op.extras
    raise_error 'no source/destination' unless refs.size > 1
    op.merge(refs)
  when :rm
    append_pathspec(op.extras, expect: true)
  end
  source(sync: false, stderr: true)
end

#log!(flag, opts = [], range: [], index: []) ⇒ Object



1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
# File 'lib/squared/workspace/project/git.rb', line 1231

def log!(flag, opts = [], range: [], index: [])
  cmd, opts = git_session('log', opts: opts)
  op = OptionPartition.new(opts, collect_hash(OPT_GIT[:log]), cmd,
                           project: self, no: collect_hash(OPT_GIT[:no][:log]),
                           first: matchpathspec)
  case flag
  when :between, :contain
    op << shell_quote(range.join(flag == :between ? '..' : '...'))
  else
    op.merge(index)
  end
  append_nocolor
  append_pathspec op.extras
  source(exception: false)
end

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



1592
1593
1594
1595
1596
1597
1598
1599
# File 'lib/squared/workspace/project/git.rb', line 1592

def ls_files(flag, opts = [])
  cmd, opts = git_session('ls-files', "--#{flag}", opts: opts)
  op = OptionPartition.new(opts, OPT_GIT[:ls_files], cmd, project: self)
  out, banner, from = source(io: true)
  print_item banner
  ret = write_lines(out, grep: op.extras)
  list_result(ret, 'files', from: from, grep: op.extras)
end

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



1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
# File 'lib/squared/workspace/project/git.rb', line 1581

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

#merge(flag, opts = [], command: nil, branch: nil) ⇒ Object



1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'lib/squared/workspace/project/git.rb', line 1367

def merge(flag, opts = [], command: nil, branch: nil)
  cmd, opts = git_session('merge', opts: opts)
  case flag
  when :commit, :'no-commit'
    op = OptionPartition.new(opts, OPT_GIT[:merge], cmd, project: self, no: OPT_GIT[:no][:merge])
    raise_error 'no branch/commit' if op.empty?
    op << "--#{flag}" << '--'
    if branch
      op << branch
      op.clear(pass: false)
    else
      append_commit(*op.extras)
    end
  else
    return unless VAL_GIT[:merge][:send].include?(command)

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

#populateObject



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

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

  namespace name do
    Git.subtasks 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|
                if (remote = args.remote)
                  args = args.extras
                else
                  remote = choice_remote
                  args = args.to_a.drop(1)
                end
                __send__(action, flag, args, 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.message)
              end
            else
              format_desc(action, flag, 'pathspec+', before: flag == :add ? 'opts*' : nil)
              task flag do |_, args|
                if flag == :fixup
                  ref, squash, pick = choice_commit(accept: [['Auto squash?', true]], reflog: false,
                                                    values: ['Pick [amend|reword]'])
                  pick = case pick&.downcase
                         when 'a', 'amend'
                           'amend'
                         when 'r', 'reword'
                           'reword'
                         end
                  if squash
                    found = false
                    git_spawn(git_output('log --format=%h'), stdout: false).each do |val|
                      if found
                        squash = val.chomp
                        break
                      end
                      found = val.chomp == ref
                    end
                  end
                end
                refs = pick == 'reword' ? [] : param_guard(action, flag, args: args.to_a)
                if flag == :add
                  opts = refs
                  refs = []
                else
                  opts = []
                end
                commit(flag, opts, refs: refs, ref: ref, squash: squash, pick: pick)
              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 = args.to_a
                if refs.empty?
                  refs = choice_refs('Choose a tag', 'tags', multiple: true, accept: 'Delete?', series: true)
                  remote = choice_remote
                end
                tag(flag, refs: refs, remote: remote)
              end
            when :add, :sign
              format_desc action, flag, 'name,message?,commit?,remote?'
              task flag, [:name, :message, :commit, :remote] do |_, args|
                if (name = args.name)
                  message = args.message
                  commit = commithead args.commit
                  remote = args.remote
                else
                  commit, name, message = choice_commit(values: [['Enter tag name', true], 'Enter message'],
                                                        series: true, reflog: false)
                  remote = choice_remote
                end
                ret = tag(flag, refs: [name], message: message, commit: commit, remote: remote)
                print_success if success?(ret) && !remote
              end
            end
          when 'stash'
            format_desc(action, flag, 'opts*', after: case flag
                                                      when :push then 'pathspec*'
                                                      when :list then nil
                                                      else 'commit?' end)
            task flag do |_, args|
              stash flag, args.to_a
            end
          when 'log', 'diff'
            case flag
            when :view, :between, :contain
              view = flag == :view
              if view && action == 'log'
                format_desc action, flag, '(^)commit*|:,opts*,ref?,pathspec*'
                task flag do |_, args|
                  args = args.to_a
                  if args.first == ':'
                    args.shift
                    index = choice_commit(multiple: true)
                  else
                    index = []
                    args.each do |val|
                      if matchhead(val)
                        index << commithead(val)
                      elsif (sha = commithash(val))
                        index << sha
                      elsif val.start_with?('^')
                        index << shell_quote(val)
                      else
                        break
                      end
                    end
                    args = args.drop(index.size)
                  end
                  log!(flag, args, index: index)
                end
              else
                format_desc action, flag, 'commit1,commit2,opts*,pathspec*'
                task flag, [:commit1, :commit2] do |_, args|
                  commit1 = commithead args.commit1
                  if commit1
                    commit2 = commithead param_guard(action, flag, args: args, key: :commit2)
                    args = args.extras.to_a
                    range = [commit1, commit2]
                  else
                    range, opts, refs = choice_commit(multiple: view ? true : 2, values: %w[Options Pathspec])
                    range = range.reverse
                    args = OptionPartition.strip(opts)
                    args.concat(refs.shellsplit) if refs
                  end
                  __send__(action == 'log' ? :log! : :diff, flag, args, range: range)
                end
              end
            when :head
              format_desc action, flag, 'commit*|:,opts*,pathspec*'
              task flag do |_, args|
                args = args.to_a
                if args.first == ':'
                  args.shift
                  index = choice_commit(multiple: true)
                else
                  index = []
                  args.each do |val|
                    if matchhead(val)
                      index << commithead(val)
                    elsif (sha = commithash(val))
                      index << sha
                    else
                      break
                    end
                  end
                  args = args.drop(index.size)
                end
                diff(flag, args, index: index)
              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|
                if (branch = args.name)
                  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 = commithead create
                    create = nil
                    detach = args.commit
                  else
                    detach = args.detach
                    commit = commithead args.commit
                  end
                  param_guard(action, flag, args: { create: create }, key: :create, pat: /\Ab\z/i) if create
                else
                  branch = choice_refs 'Choose a branch to switch'
                end
                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|
                if (origin = args.origin)
                  branch = args.name
                else
                  origin, branch = choice_refs('Choose a remote', 'remotes', values: ['Enter branch name'])
                end
                checkout(flag, branch: branch, origin: origin)
              end
            when :commit
              format_desc action, flag, 'ref,opts*'
              task flag, [:commit] do |_, args|
                commit = commithead args.commit
                if commit
                  args = args.extras
                else
                  commit, opts = choice_commit(values: ['Options'])
                  args = OptionPartition.strip(opts)
                end
                checkout(flag, args, commit: commit)
              end
            when :detach
              format_desc action, flag, 'ref?'
              task flag, [:commit] do |_, args|
                commit = commithead args.commit
                unless commit
                  commit, merge = choice_commit(values: ['Merge? [y|N]'])
                  merge = merge&.upcase == 'Y'
                end
                checkout(flag, commit: commit, merge: merge)
              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)
                ref = commithead args.ref
                ref, remote = choice_refs('Choose a remote', 'remotes', accept: [['Push?', true]]) if ref == ':'
                branch(flag, target: target, ref: ref, remote: remote)
              end
            when :track
              format_desc action, flag, '(^~)upstream?,name?'
              task flag, [:upstream, :name] do |_, args|
                if (ref = args.upstream)
                  target = args.name
                  if ref.start_with?('~')
                    ref = ref[1..-1]
                    remote = true
                  end
                else
                  ref, remote, target = choice_refs('Choose a remote', 'remotes', accept: [['Push?', true]],
                                                                                  values: ['Enter branch name'])
                end
                branch(flag, target: target, ref: ref, remote: remote)
              end
            when :delete
              format_desc action, flag, '(^~)name*,:?'
              task flag do |_, args|
                refs = args.to_a
                if refs.empty? || (r = refs.last == ':')
                  accept = ['Delete?']
                  accept << ['Force?', true] unless r
                  remote = choice_refs('Choose a branch', r ? 'remotes' : 'heads', multiple: true,
                                                                                   accept: accept)
                  if r
                    refs.pop
                  else
                    refs = remote.first
                    refs.map! { |val| "^#{val}" } if remote[1]
                    remote = nil
                  end
                end
                branch(flag, refs: refs, remote: remote)
              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|
                if (branch = args.branch)
                  oldbranch = args.oldbranch
                else
                  oldbranch, branch = choice_refs("Choose a branch to #{flag}",
                                                  values: [['Enter new branch name', true]])
                end
                branch(flag, refs: [oldbranch, branch])
              end
            end
          when 'switch'
            case flag
            when :create
              format_desc action, flag, '(^)name,ref?=HEAD|:'
              task flag, [:name, :commit] do |_, args|
                target = param_guard(action, flag, args: args, key: :name)
                commit = commithead args.commit
                commit, track = choice_commit(values: ['Track? [Y|n]'], force: false) if commit == ':'
                switch(flag, target: target, commit: commit, track: track)
              end
            when :detach
              format_desc action, flag, 'ref?=HEAD'
              task flag, [:commit] do |_, args|
                commit = commithead(args.commit) || choice_commit(force: false)
                switch(flag, commit: commit)
              end
            when :merge
              format_desc action, flag, 'branch?'
              task flag, [:branch] do |_, args|
                commit = args.branch || choice_refs('Choose a branch')
                switch(flag, commit: commit)
              end
            end
          when 'reset'
            case flag
            when :commit
              format_desc action, flag, 'ref|:,opts*'
              task flag, [:commit] do |_, args|
                commit = commithead args.commit
                if commit && commit != ':'
                  args = args.extras
                else
                  commit, mode = choice_commit(values: ['Mode [mixed|soft|hard|N]'])
                  args = args.extras.to_a.concat(case mode&.downcase
                                                 when 'h', 'hard' then ['hard']
                                                 when 's', 'soft' then ['soft']
                                                 when 'n', 'N' then ['mixed', 'N']
                                                 else ['mixed'] end)
                end
                print_success if success?(reset(flag, args, 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)
                ref = commithead args.ref
                ref = choice_commit(reflog: false) if ref == ':'
                reset(flag, mode: mode, ref: ref)
              end
            when :patch
              format_desc action, flag, 'ref?=HEAD|:,pathspec*'
              task flag, [:ref] do |_, args|
                ref = commithead args.ref
                ref = choice_commit(reflog: false) unless ref && 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 flag, args.to_a
              end
            when :format
              format_desc action, flag, 'format?,opts*,object*'
              task flag, [:format] do |_, args|
                show(flag, args.extras, format: args.format)
              end
            when :textconv
              format_desc action, flag, 'files+'
              task flag do |_, args|
                files = param_guard(action, flag, args: args.to_a)
                show(flag, files: files)
              end
            end
          when 'rebase', 'merge'
            case flag
            when :branch
              format_desc action, flag, 'upstream,branch?=HEAD,opts*'
              task flag, [:upstream] do |_, args|
                if (upstream = args.upstream)
                  args = args.extras
                else
                  upstream, opts = choice_refs('Choose upstream branch', values: ['Options'])
                  args = OptionPartition.strip(opts)
                end
                rebase(flag, args, upstream: upstream)
              end
            when :onto
              format_desc action, flag, 'ref,upstream,branch?=HEAD'
              task flag, [:commit, :upstream, :branch] do |_, args|
                commit = commithead args.commit
                if commit
                  upstream = param_guard(action, flag, args: args, key: :upstream)
                  branch = args.branch
                  args = []
                else
                  commit = choice_refs 'Choose "onto" branch'
                  target, opts = choice_commit(multiple: 2, values: ['Options'], reflog: false)
                  branch, upstream = target
                  args = OptionPartition.strip(opts)
                end
                rebase(flag, args, commit: commit, upstream: upstream, branch: branch)
              end
            when :commit, :'no-commit'
              format_desc action, flag, 'refs+,opts*'
              task flag do |_, args|
                args = args.to_a
                if args.empty?
                  accept = "Merge with #{`#{git_output('branch --show-current')}`.chomp}?"
                  branch, opts = choice_refs('Choose a branch', values: ['Options'], accept: accept)
                  args = OptionPartition.strip(opts)
                end
                merge(flag, args, branch: branch)
              end
            when :send
              format_desc(action, flag, VAL_GIT[action.to_sym][:send], arg: nil)
              task flag, [:command] do |_, args|
                command = param_guard(action, flag, args: args, key: :command,
                                                    values: VAL_GIT[action.to_sym][:send])
                __send__(action, 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 = commithead args.ref
                size = args.size
                if !size && ref.to_i.between?(1, 40)
                  size = ref
                  ref = nil
                end
                rev_parse(flag, ref: ref, size: size)
              end
            when :build
              format_desc action, flag, OPT_GIT[:status]
              task flag do |_, args|
                revbuild flag, args.to_a
              end
            when :output
              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|
                ls_remote(flag, args.extras, remote: args.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
          when 'restore'
            case flag
            when :source
              format_desc action, flag, 'ref,opts*,pathspec*'
              task flag, [:commit] do |_, args|
                commit = commithead args.commit
                if commit
                  args = args.extras
                else
                  commit, opts, files = choice_commit(values: ['Options', ['Pathspec', true]])
                  args = OptionPartition.strip(opts)
                  files = files&.shellsplit
                end
                restore(flag, args, commit: commit, files: files)
              end
            when :staged, :worktree
              format_desc action, flag, 'opts*,pathspec*,:?'
              task flag do |_, args|
                args = args.to_a
                if args.empty? || args.last == ':'
                  files = []
                  status_data.each do |line|
                    case (flag == :staged ? line[2] : line[1])
                    when /[AMDRTC]/
                      files << line[0]
                    end
                  end
                  unless files.empty?
                    files = choice_index('Select a file', files, multiple: true, force: false,
                                                                 accept: 'Restore?')
                  end
                  args.pop
                  args, glob = args.partition { |val| val.match?(/^(?:[a-z-]+=|[^*]+$)/) }
                  (files ||= []).concat(glob)
                  next if args.empty? && files.empty?
                end
                restore(flag, args, files: files)
              end
            end
          when 'git'
            before = case flag
                     when :mv then 'source+,destination'
                     when :revert then 'commit+' end
            format_desc(action, flag, 'opts*', before: before, after: case flag
                                                                      when :add then 'pathspec*|:pattern:*'
                                                                      when :clean, :rm then 'pathspec*' end)
            task flag do |_, args|
              git flag, args.to_a
            end
          end
        end
      end
    end
  end
end

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



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

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

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



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

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, opts = git_session('rebase', opts: opts)
  case flag
  when :branch
    return unless upstream

    op = OptionPartition.new(opts, OPT_GIT[:rebase], cmd, project: self, no: OPT_GIT[:no][:rebase])
    cmd << shell_escape(upstream)
    append_head op.shift
    op.clear(pass: false)
  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



351
352
353
# File 'lib/squared/workspace/project/git.rb', line 351

def ref
  Git.ref
end

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



1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
# File 'lib/squared/workspace/project/git.rb', line 1131

def reset(flag, opts = [], refs: nil, ref: nil, mode: nil, commit: nil)
  cmd, opts = git_session('reset', opts: opts)
  case flag
  when :commit, :index
    op = OptionPartition.new(opts, OPT_GIT[:reset] + VAL_GIT[:reset], cmd,
                             project: self, no: OPT_GIT[:no][:reset],
                             first: flag == :index ? matchpathspec : nil)
    if flag == :commit
      op.append(commit)
        .clear(pass: false)
      ref = false
    else
      refs = op.extras
    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'
    append_pathspec(refs, pass: false)
  end
  unless ref == false
    append_commit(ref, head: true)
    append_pathspec(refs, pass: false) if refs
  end
  source
end

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



1520
1521
1522
1523
1524
1525
1526
# File 'lib/squared/workspace/project/git.rb', line 1520

def restore(flag, opts = [], commit: nil, files: nil)
  cmd, opts = git_session('restore', shell_option(flag, commit, escape: false, force: false), opts: opts)
  op = OptionPartition.new(opts, OPT_GIT[:restore], cmd, project: self, no: OPT_GIT[:no][:restore],
                                                         first: matchpathspec)
  append_pathspec(op.extras + (files || []), pass: false)
  source(sync: false, stderr: true)
end

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



1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
# File 'lib/squared/workspace/project/git.rb', line 1560

def rev_parse(flag, opts = [], ref: nil, size: nil)
  cmd, opts = git_session('rev-parse', opts: opts)
  case flag
  when :commit
    cmd << ((n = size.to_i) > 0 ? basic_option('short', [n, 5].max) : '--verify')
    append_commit(ref, head: true)
  when :branch
    cmd << '--abbrev-ref'
    append_commit(ref, head: true)
  when :output
    if opts.delete('sq-quote')
      cmd << '--sq-quote'
      args = true
    end
    op = OptionPartition.new(opts, OPT_GIT[:rev_parse], cmd, project: self, no: OPT_GIT[:no][:rev_parse],
                                                             args: args)
    op.append(escape: args)
  end
  source(banner: verbosetype > 1)
end

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



1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
# File 'lib/squared/workspace/project/git.rb', line 1075

def revbuild(flag = nil, opts = [], sync: invoked_sync?('revbuild', flag), **kwargs)
  statusargs = lambda do
    {
      include: relativepath(as_a(kwargs[:include]), all: true),
      exclude: relativepath(as_a(kwargs[:exclude]), all: true)
    }
  end
  unless workspace.closed
    if @revbuild
      statusargs.call.each { |key, val| @revbuild[key] += val }
    else
      @revbuild = statusargs.call
    end
    return
  end
  sha = git_spawn('rev-parse --verify HEAD').chomp
  return if sha.empty?

  kwargs = kwargs.key?(:include) || kwargs.key?(:exclude) ? statusargs.call : @revbuild || {}
  case flag
  when :build
    op = OptionPartition.new(opts, OPT_GIT[:status], project: self)
    op.clear(append: true)
    args = op.to_a
  else
    args = []
    args << basic_option('untracked-files', flag) if (flag = option('untracked-files', prefix: 'git'))
    args << basic_option('ignore-submodules', flag) if (flag = option('ignore-submodules', prefix: 'git'))
    args << basic_option('ignored', flag) if (flag = option('ignored', prefix: 'git'))
  end
  if (cur = workspace.rev_entry(name)) && cur['revision'] == sha && !env('REVBUILD_FORCE')
    files = status_digest(*args, **kwargs)
    if cur['files'].size == files.size && cur['files'].find { |key, val| files[key] != val }.nil?
      if verbose
        if (since = workspace.rev_timesince(name, 'build'))
          puts log_message(Logger::INFO, name, 'no changes', subject: 'revbuild', hint: "#{since} ago")
        else
          workspace.rev_timeutc(name, 'build')
        end
      end
      return
    end
  end
  start = epochtime
  build(@output, sync: sync, from: :'git:revbuild')
rescue StandardError => e
  warn log_message(Logger::WARN, e, pass: true) if warning?
else
  if verbose
    msg = sub_style('completed', styles: theme[:active])
    puts log_message(Logger::INFO, name, msg, subject: 'revbuild', hint: time_format(epochtime - start))
  end
  workspace.rev_write(name, { 'revision' => sha, 'files' => status_digest(*args, **kwargs) },
                      sync: sync, utc: 'build')
end

#revbuild?Boolean

Returns:

  • (Boolean)


1652
1653
1654
# File 'lib/squared/workspace/project/git.rb', line 1652

def revbuild?
  build? && !!workspace.revfile
end

#show(flag, opts = [], format: nil, files: []) ⇒ Object



1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
# File 'lib/squared/workspace/project/git.rb', line 1528

def show(flag, opts = [], format: nil, files: [])
  cmd, opts = git_session('show', opts: opts)
  case flag
  when :textconv
    cmd << '--textconv'
    append_value(files.flat_map { |val| Dir[val] }
                      .select { |val| projectpath?(val) }
                      .map! { |val| shell_quote("HEAD:#{val}") })
    source(banner: false)
    return
  when :oneline
    format = 'oneline'
  end
  if format
    case (val = format.downcase)
    when 'oneline', 'short', 'medium', 'full', 'fuller', 'reference', 'email', 'raw'
      cmd << basic_option('format', val)
    else
      if format.match?(/^t?format:/) || format.include?('%')
        cmd << quote_option('pretty', format)
      else
        opts << format
      end
    end
  end
  op = OptionPartition.new(opts, OPT_GIT[:show] + OPT_GIT[:diff][:show] + OPT_GIT[:log][:diff], cmd,
                           project: self,
                           no: OPT_GIT[:no][:show] + collect_hash(OPT_GIT[:no][:log], pass: [:base]))
  op.append(delim: true)
  source(exception: false, banner: flag != :oneline)
end

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



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/squared/workspace/project/git.rb', line 1002

def stash(flag = nil, opts = [], sync: invoked_sync?('stash', flag))
  if flag
    cmd, opts = git_session('stash', flag, opts: opts)
    list = OPT_GIT[:stash][:common] + OPT_GIT[:stash].fetch(flag, [])
    if flag == :list
      list += collect_hash(OPT_GIT[:log])
      no = collect_hash(OPT_GIT[:no][:log])
    end
    op = OptionPartition.new(opts, list, cmd, project: self, no: no, first: flag == :push ? matchpathspec : nil)
    case flag
    when :push
      append_pathspec op.extras
    when :pop, :apply, :drop
      unless op.empty?
        op << shell_escape(op.pop)
        op.clear
      end
    when :clear
      if confirm("Remove #{sub_style('all', styles: theme[:active])} stash entries? [y/N] ", 'N')
        source(stdout: true)
      end
      return
    when :list
      op.clear
      out, banner, from = source(io: true)
      print_item banner
      list_result(write_lines(out), 'objects', from: from)
      return
    end
  else
    git_session('stash', 'push', opts: opts)
    append_option(OPT_GIT[:stash][:push].grep_v(/[=|]/), no: true, ignore: false)
    append_message
  end
  source(banner: !quiet?, sync: sync, **threadargs)
end

#statusObject



1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/squared/workspace/project/git.rb', line 1039

def status(*)
  cmd = git_session 'status'
  cmd << (option('long') ? '--long' : '--short')
  cmd << '--branch' if option('branch')
  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
  if verbose
    r = color(:red)
    g = color(:green)
    sub = if session_arg?('short')
            [
              { pat: /^(.)([A-Z?!])(.+)$/, styles: r, index: 2 },
              { pat: /^([A-Z?!])(.+)$/, styles: g },
              { pat: /^(\?\?)(.+)$/, styles: r },
              { pat: /^(## )(.+?)(\.{3})(.+)$/, styles: [nil, g, nil, r], index: -1 }
            ]
          else
            [{ pat: /^(\t+)([a-z]+: +.+)$/, styles: r, index: 2 }]
          end
  end
  out, banner, from = source(io: true)
  ret = write_lines(out, banner: banner, sub: sub)
  list_result(ret, 'files', from: from, action: 'modified')
end

#switch(flag, opts = [], target: nil, commit: nil, track: nil) ⇒ Object



1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
# File 'lib/squared/workspace/project/git.rb', line 1494

def switch(flag, opts = [], target: nil, commit: nil, track: nil)
  cmd = git_session('switch', opts: opts).first
  case flag
  when :create
    c = 'c'
    if target.start_with?('^')
      target = target[1..-1]
      c = c.upcase
    end
    cmd << quote_option(c, target)
    cmd << case (track ||= option('track', ignore: false))
           when 'n', 'N', '0', 'false'
             '--no-track'
           when 'y', 'Y', '1', 'true'
             '--track'
           when 'direct', 'inherit'
             basic_option('track', track)
           end
  when :detach, :merge
    cmd << "--#{flag}"
  end
  cmd << '--force' if option('force')
  append_head commit
  source
end

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



1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
# File 'lib/squared/workspace/project/git.rb', line 1198

def tag(flag, opts = [], refs: [], message: nil, commit: nil, remote: nil)
  cmd, opts = git_session('tag', opts: opts)
  case flag
  when :add, :sign
    if flag == :sign || option('sign')
      cmd << '--sign'
    elsif !session_arg?('s', 'sign', 'u', 'local-user')
      cmd << '--annotate'
    end
    cmd << '--force' if option('force')
    if !commit && message && (sha = commithash(message))
      commit = sha
      message = nil
    end
    append_message message
    append_value refs
    append_head commit
  when :delete
    cmd << '--delete'
    append_value refs
  else
    op = OptionPartition.new(opts, OPT_GIT[:tag], cmd << '--list', project: self, no: OPT_GIT[:no][:tag])
    out, banner, from = source(io: true)
    print_item banner
    ret = write_lines(out, grep: op.extras)
    list_result(ret, 'tags', from: from, grep: op.extras)
    return
  end
  remote ||= option('remote')
  source
  git_spawn('push', flag == :delete ? '-d' : nil, remote, *refs.map { |val| shell_quote(val) }) if remote
end