squared 0.0.12 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,37 +4,36 @@ module Squared
4
4
  module Workspace
5
5
  module Project
6
6
  class Git < Base
7
- OPT_PULL = %w[all tags prune ff-only autostash dry-run].freeze
8
- OPT_FETCH = %w[tags prune prune-tags depth=n dry-run].freeze
7
+ OPT_PULL = %w[all ff-only autostash prune tags depth=n since=d jobs=n dry-run].freeze
8
+ OPT_FETCH = %w[prune-tags].concat(OPT_PULL[3..-1]).freeze
9
9
  private_constant :OPT_PULL, :OPT_FETCH
10
10
 
11
11
  class << self
12
- include ::Rake::DSL
12
+ include Rake::DSL
13
13
 
14
14
  def populate(ws, **)
15
15
  return if ws.series[:pull].empty?
16
16
 
17
- name = ws.task_name('all')
18
- desc ws.task_name('all[git?=rebase|stash]', desc: true)
19
- task name, [:git] do |_, args|
20
- sync = lambda do |key|
21
- key = ws.task_name(key)
22
- ws.task_defined?(ret = ws.task_join(key, 'sync')) ? ret : key
17
+ namespace(name = ws.task_name('git')) do
18
+ all = ws.task_join(name, 'all')
19
+
20
+ desc ws.task_name("#{all}[git?=rebase|stash,depend?]", desc: true)
21
+ task 'all', [:git, :depend] do |_, args|
22
+ cmd = case args.git
23
+ when 'rebase'
24
+ [ws.task_sync('rebase')]
25
+ when 'stash'
26
+ [ws.task_sync('stash'), ws.task_sync('pull')]
27
+ else
28
+ [ws.task_sync('pull')]
29
+ end
30
+ cmd << ws.task_sync('depend') if args.depend && !ws.series[:depend].empty?
31
+ cmd << ws.task_sync('build')
32
+ Common::Utils.task_invoke(*cmd, **ws.invokeargs)
23
33
  end
24
- cmd = [case args.git
25
- when 'rebase'
26
- sync.('rebase')
27
- when 'stash'
28
- invoke(sync.('stash'), **ws.invokeargs)
29
- sync.('pull')
30
- else
31
- sync.('pull')
32
- end]
33
- cmd << ws.task_name('build')
34
- Common::Utils.task_invoke(*cmd, **ws.invokeargs)
34
+ ws.series.sync << all
35
+ ws.series.multiple << all
35
36
  end
36
- ws.series.sync << name
37
- ws.series.multiple << name
38
37
  end
39
38
 
40
39
  def tasks
@@ -63,7 +62,8 @@ module Squared
63
62
  refs: %i[heads tags].freeze,
64
63
  reset: %i[head soft mixed hard merge keep submodules].freeze,
65
64
  restore: %i[worktree staged overlay].freeze,
66
- rev: %i[commit branch].freeze
65
+ rev: %i[commit branch].freeze,
66
+ show: %i[oneline pretty format].freeze
67
67
  }.freeze
68
68
 
69
69
  def initialize(*, **)
@@ -200,6 +200,21 @@ module Squared
200
200
  reset(flag, ref: args.ref)
201
201
  end
202
202
  end
203
+ when :show
204
+ if flag == :oneline
205
+ desc format_desc(action, flag, 'object*')
206
+ task flag, [:object] do |_, args|
207
+ objs = args.to_a
208
+ show(objs, pretty: 'oneline', abbrev: true)
209
+ end
210
+ else
211
+ desc format_desc(action, flag, 'format,object*')
212
+ task flag, [:format, :object] do |_, args|
213
+ format = guard_params(action, flag, args: args, key: :format)
214
+ objs = args.to_a[1..-1] || []
215
+ show(objs, "#{flag}": format)
216
+ end
217
+ end
203
218
  end
204
219
  end
205
220
  end
@@ -220,7 +235,13 @@ module Squared
220
235
  cmd << '--commit'
221
236
  end
222
237
  append_pull opts, OPT_PULL, flag
223
- source(sync: sync, **threadargs)
238
+ sub = if verbose
239
+ [
240
+ { pat: /^(.+)(\|\s+\d+\s+)([^-]*)(-+)(.*)$/, styles: :red, index: 4 },
241
+ { pat: /^(.+)(\|\s+\d+\s+)(\++)(-*)(.*)$/, styles: :green, index: 3 }
242
+ ]
243
+ end
244
+ source(sync: sync, sub: sub, **threadargs)
224
245
  end
225
246
 
226
247
  def rebase
@@ -399,17 +420,34 @@ module Squared
399
420
  raise_error('commit', 'pathspec', hint: 'missing') if (files = projectmap(files)).empty?
400
421
  "-- #{files.join(' ')}"
401
422
  end
402
- unless push?
403
- source('git branch -vv --list', io: true, banner: false).first.each do |val|
404
- origin = %r{^\* [^\[]+(?<= )\[([\w.-/]+?)/([\w.-]+)\] }.match(val)
405
- next unless origin
406
-
407
- @origin = origin[1]
408
- @branch = origin[2]
409
- break
423
+ origin = nil
424
+ branch = nil
425
+ upstream = false
426
+ source('git fetch --no-tags --quiet', io: true, banner: false)
427
+ source('git branch -vv --list', io: true, banner: false).first.each do |val|
428
+ next unless (data = /^\*\s(\S+)\s+(\h+)(?:\s\[(.+?)(?=\]\s)\])?\s/.match(val))
429
+
430
+ branch = data[1]
431
+ sha = data[2]
432
+ if !data[3]
433
+ unless (origin = option('repository', prefix: 'git', ignore: false))
434
+ out = source('git log -n1 --format=%h%d', io: true, stdout: true, banner: false).first
435
+ if (data = /^#{sha} \(HEAD -> #{Regexp.escape(branch)}, (.+?)\)$/m.match(out))
436
+ split_escape(data[1]).each do |val|
437
+ next unless val.end_with?("/#{branch}")
438
+
439
+ origin = val[0..val.size - branch.size - 2]
440
+ break
441
+ end
442
+ end
443
+ end
444
+ upstream = !origin.nil?
445
+ elsif (data = Regexp.new("^(.+)/#{Regexp.escape(branch)}$").match(data[3]))
446
+ origin = data[1]
410
447
  end
448
+ break
411
449
  end
412
- raise_error('commit', 'work tree is not usable') unless push?
450
+ raise_error('commit', 'work tree is not usable') unless origin && branch
413
451
  cmd = git_session 'commit'
414
452
  cmd << '--dry-run' if option('dry-run')
415
453
  if amend
@@ -424,6 +462,7 @@ module Squared
424
462
  end
425
463
  a = ['git add --verbose']
426
464
  b = ['git push']
465
+ b << '--set-upstream' if upstream
427
466
  if dry_run?
428
467
  a << '--dry-run'
429
468
  b << '--dry-run'
@@ -433,7 +472,7 @@ module Squared
433
472
  b << @origin << @branch
434
473
  puts if pass
435
474
  source a.join(' ')
436
- source
475
+ source cmd
437
476
  source b.join(' ')
438
477
  end
439
478
 
@@ -444,16 +483,38 @@ module Squared
444
483
  source(stdout: true)
445
484
  end
446
485
 
486
+ def show(objs, pretty: nil, format: nil, abbrev: nil)
487
+ cmd = git_session 'show'
488
+ case (flag = pretty&.downcase)
489
+ when 'oneline', 'short', 'medium', 'full', 'fuller', 'reference', 'email', 'raw'
490
+ cmd << "--pretty=#{flag}"
491
+ else
492
+ if pretty
493
+ cmd << "--pretty=#{shell_escape("tformat:#{pretty}", quote: true)}"
494
+ elsif format
495
+ cmd << "--format=#{shell_escape(format, quote: true)}"
496
+ end
497
+ end
498
+ if (abbrev ||= option('abbrev')) == true
499
+ cmd << '--abbrev-commit'
500
+ elsif abbrev.to_i > 0
501
+ cmd << "--abbrev=#{abbrev}"
502
+ end
503
+ append_value objs
504
+ source(exception: false)
505
+ end
506
+
447
507
  private
448
508
 
449
- def source(cmd = @session, exception: true, io: false, sync: true, stdout: false, stderr: false, banner: true)
509
+ def source(cmd = @session, exception: true, io: false, sync: true, stdout: false, stderr: false, banner: true,
510
+ sub: nil)
450
511
  cmd = session_done(cmd)
451
512
  log.info cmd
452
513
  banner = format_banner(cmd.gsub(File.join(path, ''), ''), banner: banner)
453
- cmd = cmd.sub(/^git\b/, "git --work-tree #{shell_quote(path)} --git-dir #{shell_quote(gitpath)}")
514
+ cmd = cmd.sub(/^git\b/, "git --work-tree=#{shell_quote(path)} --git-dir=#{shell_quote(gitpath)}")
454
515
  begin
455
516
  if io
456
- [IO.popen(cmd), banner]
517
+ [stdout ? `#{cmd}` : IO.popen(cmd), banner]
457
518
  elsif stdin? ? sync : stdout
458
519
  print_item banner
459
520
  ret = `#{cmd}`
@@ -469,11 +530,12 @@ module Squared
469
530
  require 'open3'
470
531
  if stderr
471
532
  Open3.popen3(cmd) do |_, out, err|
472
- ret = write_lines(out, banner: banner, pass: true)
533
+ ret = write_lines(out, banner: banner, sub: sub, pass: true)
473
534
  if ret == 0
474
- write_lines(err, banner: banner)
535
+ ret = write_lines(err, banner: banner)
536
+ puts 'Success' if ret == 0 && banner && !stdin?
475
537
  else
476
- write_lines(err, loglevel: ::Logger::DEBUG)
538
+ write_lines(err, loglevel: Logger::DEBUG)
477
539
  end
478
540
  end
479
541
  else
@@ -484,15 +546,15 @@ module Squared
484
546
  log.error e
485
547
  raise if exception
486
548
 
487
- warn log_message(::Logger::WARN, e) if warning?
549
+ warn log_message(Logger::WARN, e) if warning?
488
550
  end
489
551
  end
490
552
 
491
553
  def write_lines(data, banner: nil, loglevel: nil, grep: nil, sub: nil, pass: false)
492
- grep = Regexp.new(grep == '*' ? '.+' : grep) if grep.is_a?(::String)
554
+ grep = Regexp.new(grep == '*' ? '.+' : grep) if grep.is_a?(String)
493
555
  sub = nil if stdin?
494
556
  ret = 0
495
- lines = []
557
+ out = []
496
558
  data.each do |line|
497
559
  next if grep && !line.match?(grep)
498
560
 
@@ -501,14 +563,14 @@ module Squared
501
563
  else
502
564
  sub&.each { |h| line = sub_style(line, **h) }
503
565
  if banner
504
- lines << line
566
+ out << line
505
567
  else
506
568
  puts line
507
569
  end
508
570
  end
509
571
  ret += 1
510
572
  end
511
- print_item banner, lines if banner && (ret > 0 || !pass)
573
+ print_item banner, out if banner && (ret > 0 || !pass)
512
574
  ret
513
575
  end
514
576
 
@@ -528,10 +590,10 @@ module Squared
528
590
  def append_pull(opts, list, flag = nil)
529
591
  append_submodules flag
530
592
  opts.each do |opt|
531
- if list.include?(opt)
593
+ if list.include?(opt) || opt.match(/^(?:depth|jobs)=\d+$/)
532
594
  @session << "--#{opt}"
533
- elsif opt.match(/^depth=(\d+)$/)
534
- @session << "--depth=#{$1}"
595
+ elsif opt.match(/^since=(.+)$/) && (val = Date.parse($1))
596
+ @session << "--shallow-since=\"#{val.strftime('%F %T')}\""
535
597
  end
536
598
  end
537
599
  end
@@ -554,7 +616,7 @@ module Squared
554
616
  end
555
617
 
556
618
  def append_message(val)
557
- @session << "--message \"#{double_quote(val)}\"" if val
619
+ @session << "--message=\"#{double_quote(val)}\"" if val
558
620
  end
559
621
 
560
622
  def append_head
@@ -577,26 +639,18 @@ module Squared
577
639
  end
578
640
  end
579
641
 
580
- def append_option(list)
581
- list.each { |val| @session << "--#{val}" if option(val) }
582
- end
583
-
584
642
  def git_session(*cmd)
585
643
  session('git', *cmd)
586
644
  end
587
645
 
588
- def gitpath
589
- basepath('.git')
590
- end
591
-
592
- def push?
593
- !@origin.nil? && !@branch.nil?
594
- end
595
-
596
646
  def dry_run?
597
647
  @session.include?('--dry-run')
598
648
  end
599
649
 
650
+ def gitpath
651
+ basepath('.git')
652
+ end
653
+
600
654
  def threadargs
601
655
  { stderr: true, exception: exception || !workspace.series.multiple? }
602
656
  end