squared 0.0.9 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,25 +12,25 @@ module Squared
12
12
  include ::Rake::DSL
13
13
 
14
14
  def populate(ws, **)
15
- return if ws.series.pull.empty?
15
+ return if ws.series[:pull].empty?
16
16
 
17
17
  desc ws.task_name('all[git?=rebase|stash]', desc: true)
18
18
  task ws.task_name('all'), [:git] do |_, args|
19
19
  sync = lambda do |key|
20
20
  key = ws.task_name(key)
21
- ws.task_defined?(ret = "#{key}:sync") ? ret : key
21
+ ws.task_defined?(ret = ws.task_join(key, 'sync')) ? ret : key
22
22
  end
23
23
  cmd = [case args.git
24
24
  when 'rebase'
25
25
  sync.('rebase')
26
26
  when 'stash'
27
- invoke(sync.('stash'), exception: ws.exception, warning: ws.warning)
27
+ invoke(sync.('stash'), **ws.invokeargs)
28
28
  sync.('pull')
29
29
  else
30
30
  sync.('pull')
31
31
  end]
32
- cmd << ws.task_name(ws.dev? && ws.series.some?(:refresh) ? 'refresh' : 'build')
33
- Common::Task.invoke(cmd, exception: ws.exception, warning: ws.warning)
32
+ cmd << ws.task_name('build')
33
+ Common::Task.invoke(cmd, **ws.invokeargs)
34
34
  end
35
35
  end
36
36
 
@@ -38,32 +38,34 @@ module Squared
38
38
  %i[pull rebase fetch stash status].freeze
39
39
  end
40
40
 
41
- def is_a?(val)
42
- if (val = as_path(val))
43
- val.join('.git').directory?
44
- else
45
- super
46
- end
41
+ def batchargs
42
+ [ref, { 'pull-s': %i[stash pull], 'rebase-s': %i[stash rebase] }]
43
+ end
44
+
45
+ def config?(val)
46
+ return false unless (val = as_path(val))
47
+
48
+ val.join('.git').directory?
47
49
  end
48
50
  end
49
51
 
50
52
  @@tasks[ref] = {
51
- checkout: %i[branch detach force merge],
52
- commit: %i[add amend amend-orig all no-all],
53
- diff: %i[head cached branch files],
54
- fetch: %i[all submodules unshallow],
55
- files: %i[cached modified deleted others],
56
- pull: %i[head rebase no-rebase commit no-commit submodules],
57
- stash: %i[push pop apply list clear],
58
- refs: %i[heads tags],
59
- reset: %i[head soft mixed hard merge keep submodules],
60
- restore: %i[worktree staged overlay],
61
- rev: %i[commit branch]
53
+ checkout: %i[branch detach force merge].freeze,
54
+ commit: %i[add amend amend-orig all no-all].freeze,
55
+ diff: %i[head cached branch files].freeze,
56
+ fetch: %i[all submodules unshallow].freeze,
57
+ files: %i[cached modified deleted others].freeze,
58
+ pull: %i[head rebase no-rebase commit no-commit submodules].freeze,
59
+ stash: %i[push pop apply list clear].freeze,
60
+ refs: %i[heads tags].freeze,
61
+ reset: %i[head soft mixed hard merge keep submodules].freeze,
62
+ restore: %i[worktree staged overlay].freeze,
63
+ rev: %i[commit branch].freeze
62
64
  }.freeze
63
65
 
64
66
  def initialize(*, **)
65
67
  super
66
- initialize_ref(Git.ref)
68
+ initialize_ref(Git.ref) if git_path.exist?
67
69
  end
68
70
 
69
71
  def ref
@@ -72,22 +74,17 @@ module Squared
72
74
 
73
75
  def populate(*)
74
76
  super
75
- return unless gitdir.exist? && ref?(Git.ref)
77
+ return unless ref?(Git.ref)
76
78
 
77
79
  namespace name do
78
80
  @@tasks[Git.ref].each do |action, flags|
79
81
  namespace action do
80
82
  flags.each do |flag|
81
83
  case action
82
- when :pull
83
- desc format_desc(action, flag, OPT_PULL)
84
- task flag, [:opts] do |_, args|
85
- pull(flag, opts: args.to_a)
86
- end
87
- when :fetch
88
- desc format_desc(action, flag, OPT_FETCH)
84
+ when :pull, :fetch
85
+ desc format_desc(action, flag, flag == :pull ? OPT_PULL : OPT_FETCH)
89
86
  task flag, [:opts] do |_, args|
90
- fetch(flag, opts: args.to_a)
87
+ __send__(action, flag, opts: args.to_a)
91
88
  end
92
89
  when :commit, :restore
93
90
  if flag == :all
@@ -131,8 +128,7 @@ module Squared
131
128
  desc format_desc(action, flag, 'index?=0,pathspec*')
132
129
  task flag, [:pathspec] do |_, args|
133
130
  files = args.to_a
134
- index = /^\d+$/.match?(files.first) && !git_option('index') ? files.shift.to_i : 0
135
- diff(flag, files, index: index)
131
+ diff(flag, files, index: /^\d+$/.match?(files[0]) && !option('index') ? files.shift.to_i : 0)
136
132
  end
137
133
  when :cached
138
134
  desc format_desc(action, flag, 'pathspec*')
@@ -193,7 +189,9 @@ module Squared
193
189
  if flag == :head
194
190
  desc format_desc(action, flag, 'ref?=HEAD,pathspec+')
195
191
  task flag, [:ref, :pathspec] do |_, args|
196
- reset(flag, args.to_a[1..-1], ref: args.ref)
192
+ files = args.to_a[1..-1]
193
+ guard_params(action, flag, args: files)
194
+ reset(flag, files, ref: args.ref)
197
195
  end
198
196
  else
199
197
  desc format_desc(action, flag, 'ref?=HEAD')
@@ -210,18 +208,18 @@ module Squared
210
208
 
211
209
  def pull(flag = nil, sync: invoked_sync?('pull', flag), opts: [])
212
210
  cmd = git_session 'pull'
213
- if flag == :'no-rebase'
211
+ if flag == :'no-rebase' || option('rebase', equals: '0')
214
212
  cmd << '--no-rebase'
215
- elsif flag == :rebase || git_option('rebase')
213
+ elsif flag == :rebase || option('rebase')
216
214
  cmd << '--rebase'
217
215
  end
218
- if flag == :'no-commit'
216
+ if flag == :'no-commit' || option('commit', equals: '0')
219
217
  cmd << '--no-commit'
220
- elsif flag == :commit || git_option('commit')
218
+ elsif flag == :commit || option('commit')
221
219
  cmd << '--commit'
222
220
  end
223
221
  append_pull opts, OPT_PULL, flag
224
- source(sync: sync, stderr: true, exception: !workspace.series.multiple?)
222
+ source(sync: sync, **threadargs)
225
223
  end
226
224
 
227
225
  def rebase
@@ -230,28 +228,28 @@ module Squared
230
228
 
231
229
  def fetch(flag = nil, opts: [])
232
230
  cmd = git_session 'fetch'
233
- cmd << '--all' if flag == :all || git_option('all')
231
+ cmd << '--all' if flag == :all || option('all')
234
232
  append_pull opts, OPT_FETCH, flag
235
- source(sync: invoked_sync?('fetch', flag), stderr: true, exception: !workspace.series.multiple?)
233
+ source(sync: invoked_sync?('fetch', flag), **threadargs)
236
234
  end
237
235
 
238
236
  def stash(flag = nil, files = [], commit: nil)
239
237
  cmd = git_session 'stash', (flag || 'push').to_s
240
238
  case flag
241
239
  when :apply, :pop
242
- cmd << '--index' if git_option('index')
240
+ cmd << '--index' if option('index')
243
241
  cmd << commit
244
242
  else
245
- append_option %w[all staged include-untracked]
246
- append_message git_option('message', 'm', zero: false)
243
+ append_option %w[all staged include-untracked].freeze
244
+ append_message option('message', 'm', ignore: false)
247
245
  append_pathspec files
248
246
  end
249
- source(sync: invoked_sync?('stash', flag), exception: workspace.exception)
247
+ source(sync: invoked_sync?('stash', flag), **threadargs)
250
248
  end
251
249
 
252
250
  def status
253
- cmd = git_session 'status', git_option('long') ? '--long' : '--short'
254
- if (val = git_option('ignore-submodules'))
251
+ cmd = git_session 'status', option('long') ? '--long' : '--short'
252
+ if (val = option('ignore-submodules', ignore: false))
255
253
  cmd << "--ignore-submodules=#{case val
256
254
  when '0', 'none'
257
255
  'none'
@@ -265,11 +263,11 @@ module Squared
265
263
  end
266
264
  append_pathspec
267
265
  out, banner = source(io: true)
268
- if banner && invoked_sync?('status')
266
+ if invoked_sync?('status')
269
267
  print_item banner
270
268
  banner = nil
271
269
  end
272
- sub = if verbose?
270
+ sub = if verbose
273
271
  [
274
272
  { pat: /^(.)([A-Z])(.+)$/, styles: :red, index: 2 },
275
273
  { pat: /^([A-Z])(.+)$/, styles: :green },
@@ -284,7 +282,6 @@ module Squared
284
282
  def reset(flag, files = [], ref: nil)
285
283
  cmd = git_session 'reset'
286
284
  if flag == :head
287
- guard_params('reset', flag, args: files)
288
285
  append_commit ref
289
286
  append_pathspec files
290
287
  else
@@ -295,7 +292,7 @@ module Squared
295
292
  append_submodules flag
296
293
  else
297
294
  cmd << '--mixed'
298
- cmd << '--no-refresh' if git_option('refresh', equals: '0')
295
+ cmd << '--no-refresh' if option('refresh', equals: '0')
299
296
  end
300
297
  append_commit ref
301
298
  end
@@ -306,10 +303,10 @@ module Squared
306
303
  cmd = git_session 'checkout'
307
304
  case flag
308
305
  when :branch
309
- cmd << '--detach' if detach == 'd' || git_option('detach')
306
+ cmd << '--detach' if detach == 'd' || option('detach')
310
307
  if create
311
308
  cmd << "-#{create}" << branch
312
- if (val = git_option('start-point'))
309
+ if (val = option('start-point'))
313
310
  cmd << val
314
311
  end
315
312
  else
@@ -328,12 +325,11 @@ module Squared
328
325
  end
329
326
 
330
327
  def rev(flag, ref: nil, size: nil)
331
- opt = if flag == :branch
332
- '--abbrev-ref'
333
- else
334
- (n = size.to_i) > 0 ? "--short=#{[n, 5].max}" : '--verify'
335
- end
336
- git_session 'rev-parse', opt
328
+ git_session 'rev-parse', if flag == :branch
329
+ '--abbrev-ref'
330
+ else
331
+ (n = size.to_i) > 0 ? "--short=#{[n, 5].max}" : '--verify'
332
+ end
337
333
  append_commit ref
338
334
  source
339
335
  end
@@ -364,22 +360,22 @@ module Squared
364
360
  sha = nil
365
361
  end
366
362
  end
367
- if (val = git_option('unified')).to_i > 0
363
+ if (val = option('unified')).to_i > 0
368
364
  cmd << "--unified=#{val}"
369
365
  end
370
366
  append_nocolor
371
367
  case flag
372
368
  when :cached
373
369
  cmd << '--cached'
374
- cmd << '--merge-base' if git_option('merge-base')
370
+ cmd << '--merge-base' if option('merge-base')
375
371
  when :branch
376
372
  cmd << branch
377
373
  when :files
378
374
  cmd << '--no-index'
379
375
  else
380
- if (val = git_option('index')) || index > 0
376
+ if (val = option('index')) || index > 0
381
377
  cmd << "HEAD~#{val || index}"
382
- elsif sha && git_option('merge-base')
378
+ elsif sha && option('merge-base')
383
379
  cmd << '--merge-base'
384
380
  end
385
381
  end
@@ -389,7 +385,7 @@ module Squared
389
385
  end
390
386
 
391
387
  def commit(flag, files = [], message: nil, pass: false)
392
- message ||= git_option('message', 'm', zero: false)
388
+ message ||= option('message', 'm', prefix: 'git', ignore: false)
393
389
  amend = flag.to_s.start_with?('amend')
394
390
  if !message && !amend
395
391
  return if pass
@@ -403,7 +399,7 @@ module Squared
403
399
  raise_error('commit', 'pathspec', hint: 'missing') if files.empty?
404
400
  "-- #{files.join(' ')}"
405
401
  end
406
- if !push?
402
+ unless push?
407
403
  source('git branch -vv --list', io: true, banner: false).first.each do |val|
408
404
  origin = %r{^\* [^\[]+(?<= )\[([\w.-/]+?)/([\w.-]+)\] }.match(val)
409
405
  next unless origin
@@ -414,9 +410,8 @@ module Squared
414
410
  end
415
411
  end
416
412
  raise_error('commit', 'work tree is not usable') unless push?
417
-
418
413
  cmd = git_session 'commit'
419
- cmd << '--dry-run' if git_option('dry-run')
414
+ cmd << '--dry-run' if option('dry-run')
420
415
  if amend
421
416
  cmd << '--amend'
422
417
  else
@@ -424,7 +419,7 @@ module Squared
424
419
  end
425
420
  if message
426
421
  append_message message
427
- elsif flag == :'amend-orig' || git_option('no-edit')
422
+ elsif flag == :'amend-orig' || option('no-edit')
428
423
  cmd << '--no-edit'
429
424
  end
430
425
  a = ['git add --verbose']
@@ -443,11 +438,7 @@ module Squared
443
438
  end
444
439
 
445
440
  def restore(flag, files)
446
- cmd = git_session 'restore'
447
- case flag
448
- when :worktree, :staged, :overlay
449
- cmd << "--#{flag}"
450
- end
441
+ git_session 'restore', "--#{flag}"
451
442
  append_ours
452
443
  append_pathspec(files, expect: true)
453
444
  source(stdout: true)
@@ -455,20 +446,21 @@ module Squared
455
446
 
456
447
  private
457
448
 
458
- def source(cmd = @session, exception: true, banner: true, io: false, sync: true, stdout: false, stderr: false)
459
- cmd = close_session(cmd)
449
+ def source(cmd = @session, exception: true, io: false, sync: true, stdout: false, stderr: false,
450
+ banner: ARG[:BANNER])
451
+ cmd = session_done(cmd)
460
452
  log.info cmd
461
453
  banner = format_banner(cmd.gsub(File.join(path, ''), ''), banner: banner, multiple: true)
462
- cmd = cmd.sub(/^git\b/, "git --work-tree #{shell_quote(path)} --git-dir #{shell_quote(gitdir)}")
454
+ cmd = cmd.sub(/^git\b/, "git --work-tree #{shell_quote(path)} --git-dir #{shell_quote(git_path)}")
463
455
  begin
464
456
  if io
465
457
  [IO.popen(cmd), banner]
466
- elsif pipe? ? sync : stdout
458
+ elsif stdin? ? sync : stdout
467
459
  print_item banner
468
460
  ret = `#{cmd}`
469
461
  if !ret.empty?
470
462
  puts ret
471
- elsif banner && stdout && !pipe?
463
+ elsif banner && stdout && !stdin?
472
464
  puts 'Success'
473
465
  end
474
466
  elsif sync || (!exception && !stderr)
@@ -482,7 +474,7 @@ module Squared
482
474
  if ret == 0
483
475
  write_lines(err, banner: banner)
484
476
  else
485
- write_lines(err, loglevel: Logger::DEBUG)
477
+ write_lines(err, loglevel: ::Logger::DEBUG)
486
478
  end
487
479
  end
488
480
  else
@@ -493,16 +485,16 @@ module Squared
493
485
  log.error e
494
486
  raise if exception
495
487
 
496
- warn e if workspace.warning
488
+ warn log_message(::Logger::WARN, e) if warning?
497
489
  end
498
490
  end
499
491
 
500
- def write_lines(iter, banner: nil, loglevel: nil, grep: nil, sub: nil, pass: false)
492
+ def write_lines(data, banner: nil, loglevel: nil, grep: nil, sub: nil, pass: false)
501
493
  grep = Regexp.new(grep == '*' ? '.+' : grep) if grep.is_a?(::String)
502
- sub = nil if pipe?
503
- found = 0
494
+ sub = nil if stdin?
495
+ ret = 0
504
496
  lines = []
505
- iter.each do |line|
497
+ data.each do |line|
506
498
  next if grep && !line.match?(grep)
507
499
 
508
500
  if loglevel
@@ -515,14 +507,14 @@ module Squared
515
507
  puts line
516
508
  end
517
509
  end
518
- found += 1
510
+ ret += 1
519
511
  end
520
- print_item banner, lines if banner && (found > 0 || !pass)
521
- found
512
+ print_item banner, lines if banner && (ret > 0 || !pass)
513
+ ret
522
514
  end
523
515
 
524
516
  def list_result(size, type, action: 'found', grep: nil)
525
- return unless verbose?
517
+ return unless verbose
526
518
 
527
519
  if size > 0
528
520
  styles = theme.fetch(:banner, []).reject { |s| s.to_s.end_with?('!') }
@@ -556,7 +548,7 @@ module Squared
556
548
  end
557
549
 
558
550
  def append_pathspec(files = [], expect: false, pass: false)
559
- if files.empty? && (val = git_option('pathspec'))
551
+ if files.empty? && (val = option('pathspec'))
560
552
  files = split_escape(val)
561
553
  end
562
554
  files = source_path(files, pass: pass)
@@ -572,54 +564,37 @@ module Squared
572
564
  end
573
565
 
574
566
  def append_head
575
- @session << (git_option('head') || git_option('tree-ish'))
567
+ @session << (option('head') || option('tree-ish'))
576
568
  end
577
569
 
578
570
  def append_ours
579
- if git_option('ours')
571
+ if option('ours')
580
572
  @session << '--ours'
581
- elsif git_option('theirs')
573
+ elsif option('theirs')
582
574
  @session << '--theirs'
583
575
  end
584
576
  end
585
577
 
586
578
  def append_submodules(flag = nil)
587
- if git_option('recurse-submodules', equals: '0')
579
+ if option('recurse-submodules', equals: '0')
588
580
  @session << '--no-recurse-submodules'
589
- elsif flag == :submodules || git_option('recurse-submodules')
581
+ elsif flag == :submodules || option('recurse-submodules')
590
582
  @session << '--recurse-submodules'
591
583
  end
592
584
  end
593
585
 
594
586
  def append_option(list)
595
- list.each { |val| @session << "--#{val}" if git_option(val) }
596
- end
597
-
598
- def git_option(*args, equals: nil, zero: true)
599
- for val in args
600
- break if (ret = ENV["GIT_#{val.gsub(/\W/, '_').upcase}"])
601
- end
602
- if !equals.nil?
603
- ret == equals.to_s
604
- elsif !ret.nil? && !ret.empty? && !(ret == '0' && zero)
605
- ret
606
- end
587
+ list.each { |val| @session << "--#{val}" if option(val) }
607
588
  end
608
589
 
609
590
  def git_session(*cmd)
610
591
  session('git', *cmd)
611
592
  end
612
593
 
613
- def gitdir
594
+ def git_path
614
595
  base_path('.git')
615
596
  end
616
597
 
617
- def source_path?(val)
618
- return val.to_s.start_with?(File.join(path, '').to_s) if Pathname.new(val).absolute?
619
-
620
- !val.match?(%r{^\.\.[/\\]})
621
- end
622
-
623
598
  def push?
624
599
  !@origin.nil? && !@branch.nil?
625
600
  end
@@ -627,7 +602,13 @@ module Squared
627
602
  def dry_run?
628
603
  @session.include?('--dry-run')
629
604
  end
605
+
606
+ def threadargs
607
+ { stderr: true, exception: !workspace.series.multiple? }
608
+ end
630
609
  end
610
+
611
+ Application.implement Git
631
612
  end
632
613
  end
633
614
  end