squared 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,31 +4,33 @@ module Squared
4
4
  module Workspace
5
5
  module Project
6
6
  class Git < Base
7
- include Format
8
-
9
- REF = :git
10
7
  OPT_PULL = %w[all tags prune ff-only autostash dry-run].freeze
11
8
  OPT_FETCH = %w[tags prune prune-tags depth=n dry-run].freeze
12
- private_constant :REF, :OPT_PULL, :OPT_FETCH
9
+ private_constant :OPT_PULL, :OPT_FETCH
13
10
 
14
11
  class << self
15
- def populate(workspace, parallel: [], **)
12
+ include ::Rake::DSL
13
+
14
+ def populate(workspace, **)
16
15
  return if workspace.series.pull.empty?
17
16
 
18
17
  desc 'all[git?=rebase|stash]'
19
18
  task 'all', [:git] do |_, args|
20
- sync = ->(key) { parallel.include?(key) ? :"#{key}:sync" : key }
19
+ exception = workspace.exception
20
+ warning = workspace.warning
21
+ sync = ->(key) { workspace.task_defined?(s = "#{key}:sync") ? s : key }
21
22
  pull = case args.git
22
23
  when 'rebase'
23
- sync.(:rebase)
24
+ sync.('rebase')
24
25
  when 'stash'
25
- invoke sync.(:stash)
26
- sync.(:pull)
26
+ invoke(sync.('stash'), exception: exception, warning: warning)
27
+ sync.('pull')
27
28
  else
28
- sync.(:pull)
29
+ sync.('pull')
29
30
  end
30
- invoke(pull, exception: workspace.exception)
31
- invoke(workspace.dev? ? :refresh : :build, exception: workspace.exception)
31
+ Common::Task.invoke(pull, exception: exception, warning: warning)
32
+ Common::Task.invoke(workspace.dev? && workspace.series.has?(:refresh) ? :refresh : :build,
33
+ exception: exception, warning: warning)
32
34
  end
33
35
  end
34
36
 
@@ -45,7 +47,7 @@ module Squared
45
47
  end
46
48
  end
47
49
 
48
- @@tasks[REF] = {
50
+ @@tasks[ref] = {
49
51
  checkout: %i[branch detach force merge],
50
52
  commit: %i[add amend amend-orig all no-all],
51
53
  diff: %i[head cached branch files],
@@ -59,26 +61,29 @@ module Squared
59
61
  rev: %i[commit branch]
60
62
  }.freeze
61
63
 
64
+ def initialize(*, **)
65
+ super
66
+ initialize_ref(Git.ref)
67
+ end
68
+
62
69
  def populate(*)
63
70
  super
64
- return unless gitdir.exist?
71
+ return unless gitdir.exist? && ref?(Git.ref)
65
72
 
66
73
  namespace name do
67
- @@tasks[REF].each do |action, flags|
74
+ @@tasks[Git.ref].each do |action, flags|
68
75
  namespace action do
69
76
  flags.each do |flag|
70
77
  case action
71
78
  when :pull
72
79
  desc format_desc(action, flag, OPT_PULL)
73
80
  task flag, [:opts] do |_, args|
74
- opts = collect_args(args, :opts)
75
- pull(flag, opts: opts)
81
+ pull(flag, opts: args.to_a)
76
82
  end
77
83
  when :fetch
78
84
  desc format_desc(action, flag, OPT_FETCH)
79
85
  task flag, [:opts] do |_, args|
80
- opts = collect_args(args, :opts)
81
- fetch(flag, opts: opts)
86
+ fetch(flag, opts: args.to_a)
82
87
  end
83
88
  when :commit, :restore
84
89
  if flag == :all
@@ -89,15 +94,16 @@ module Squared
89
94
  else
90
95
  desc format_desc(action, flag, 'pathspec+')
91
96
  task flag, [:pathspec] do |_, args|
92
- guard_params(action, flag, args: args, key: :pathspec)
93
- __send__(action, flag, collect_args(args, :pathspec))
97
+ files = args.to_a
98
+ guard_params(action, flag, args: files)
99
+ __send__(action, flag, files)
94
100
  end
95
101
  end
96
102
  when :stash
97
103
  if flag == :push
98
104
  desc format_desc(action, flag, 'pathspec*')
99
105
  task flag, [:pathspec] do |_, args|
100
- stash(flag, collect_args(args, :pathspec))
106
+ stash(flag, args.to_a)
101
107
  end
102
108
  else
103
109
  desc format_desc(action, flag, 'commit?')
@@ -120,20 +126,20 @@ module Squared
120
126
  when :head
121
127
  desc format_desc(action, flag, 'index?=0,pathspec*')
122
128
  task flag, [:pathspec] do |_, args|
123
- files = collect_args(args, :pathspec)
124
- index = /^\d+$/.match?(files.first) && !option('index') ? files.shift.to_i : 0
129
+ files = args.to_a
130
+ index = /^\d+$/.match?(files.first) && !git_option('index') ? files.shift.to_i : 0
125
131
  diff(flag, files, index: index)
126
132
  end
127
133
  when :cached
128
134
  desc format_desc(action, flag, 'pathspec*')
129
135
  task flag, [:pathspec] do |_, args|
130
- diff(flag, collect_args(args, :pathspec))
136
+ diff(flag, args.to_a)
131
137
  end
132
138
  when :branch
133
139
  desc format_desc(action, flag, 'name,pathspec*')
134
140
  task flag, [:name, :pathspec] do |_, args|
135
141
  guard_params(action, flag, args: args, key: :name)
136
- diff(flag, collect_args(args, :pathspec), branch: args.name)
142
+ diff(flag, args.to_a[1..-1], branch: args.name)
137
143
  end
138
144
  when :files
139
145
  desc format_desc(action, flag, 'path1,path2')
@@ -165,7 +171,7 @@ module Squared
165
171
  detach = args.detach
166
172
  commit = args.commit
167
173
  end
168
- guard_params('checkout', :branch, args: args, key: :create, pat: /^[Bb]$/) if create
174
+ guard_params(action, flag, args: { create: create }, key: :create, pat: /^b$/i) if create
169
175
  checkout(flag, branch: args.name, create: create, commit: commit, detach: detach)
170
176
  end
171
177
  when :detach
@@ -176,15 +182,14 @@ module Squared
176
182
  else
177
183
  desc format_desc(action, flag, 'pathspec*')
178
184
  task flag, [:pathspec] do |_, args|
179
- checkout(flag, collect_args(args, :pathspec))
185
+ checkout(flag, args.to_a)
180
186
  end
181
187
  end
182
188
  when :reset
183
189
  if flag == :head
184
190
  desc format_desc(action, flag, 'ref?=HEAD,pathspec+')
185
191
  task flag, [:ref, :pathspec] do |_, args|
186
- guard_params(action, flag, args: args, key: :pathspec)
187
- reset(flag, collect_args(args, :pathspec), ref: args.ref)
192
+ reset(flag, args.to_a[1..-1], ref: args.ref)
188
193
  end
189
194
  else
190
195
  desc format_desc(action, flag, 'ref?=HEAD')
@@ -203,12 +208,12 @@ module Squared
203
208
  cmd = git_session 'pull'
204
209
  if flag == :'no-rebase'
205
210
  cmd << '--no-rebase'
206
- elsif flag == :rebase || option('rebase')
211
+ elsif flag == :rebase || git_option('rebase')
207
212
  cmd << '--rebase'
208
213
  end
209
214
  if flag == :'no-commit'
210
215
  cmd << '--no-commit'
211
- elsif flag == :commit || option('commit')
216
+ elsif flag == :commit || git_option('commit')
212
217
  cmd << '--commit'
213
218
  end
214
219
  append_pull opts, OPT_PULL, flag
@@ -221,28 +226,28 @@ module Squared
221
226
 
222
227
  def fetch(flag = nil, opts: [])
223
228
  cmd = git_session 'fetch'
224
- cmd << '--all' if flag == :all || option('all')
229
+ cmd << '--all' if flag == :all || git_option('all')
225
230
  append_pull opts, OPT_FETCH, flag
226
231
  source(sync: invoked_sync?('fetch', flag), stderr: true, exception: !workspace.series.multiple?)
227
232
  end
228
233
 
229
- def stash(flag = :push, files = [], commit: nil)
230
- cmd = git_session 'stash', flag.to_s
234
+ def stash(flag = nil, files = [], commit: nil)
235
+ cmd = git_session 'stash', (flag || 'push').to_s
231
236
  case flag
232
237
  when :apply, :pop
233
- cmd << '--index' if option('index')
238
+ cmd << '--index' if git_option('index')
234
239
  cmd << commit
235
240
  else
236
241
  append_option %w[all staged include-untracked]
237
- append_message option('message', 'm', zero: false)
242
+ append_message git_option('message', 'm', zero: false)
238
243
  append_pathspec files
239
244
  end
240
245
  source(sync: invoked_sync?('stash', flag), exception: workspace.exception)
241
246
  end
242
247
 
243
248
  def status
244
- cmd = git_session 'status', option('long') ? '--long' : '--short'
245
- if (val = option('ignore-submodules'))
249
+ cmd = git_session 'status', git_option('long') ? '--long' : '--short'
250
+ if (val = git_option('ignore-submodules'))
246
251
  cmd << "--ignore-submodules=#{case val
247
252
  when '0', 'none'
248
253
  'none'
@@ -272,6 +277,7 @@ module Squared
272
277
  def reset(flag, files = [], ref: nil)
273
278
  cmd = git_session 'reset'
274
279
  if flag == :head
280
+ guard_params('reset', flag, args: files)
275
281
  append_commit ref
276
282
  append_pathspec files
277
283
  else
@@ -282,7 +288,7 @@ module Squared
282
288
  append_submodules flag
283
289
  else
284
290
  cmd << '--mixed'
285
- cmd << '--no-refresh' if option('refresh', equals: '0')
291
+ cmd << '--no-refresh' if git_option('refresh', equals: '0')
286
292
  end
287
293
  append_commit ref
288
294
  end
@@ -293,10 +299,10 @@ module Squared
293
299
  cmd = git_session 'checkout'
294
300
  case flag
295
301
  when :branch
296
- cmd << '--detach' if detach == 'd' || option('detach')
302
+ cmd << '--detach' if detach == 'd' || git_option('detach')
297
303
  if create
298
304
  cmd << "-#{create}" << branch
299
- if (val = option('start-point'))
305
+ if (val = git_option('start-point'))
300
306
  cmd << val
301
307
  end
302
308
  else
@@ -351,22 +357,22 @@ module Squared
351
357
  sha = nil
352
358
  end
353
359
  end
354
- if (val = option('unified')).to_i > 0
360
+ if (val = git_option('unified')).to_i > 0
355
361
  cmd << "--unified=#{val}"
356
362
  end
357
363
  append_nocolor
358
364
  case flag
359
365
  when :cached
360
366
  cmd << '--cached'
361
- cmd << '--merge-base' if option('merge-base')
367
+ cmd << '--merge-base' if git_option('merge-base')
362
368
  when :branch
363
369
  cmd << branch
364
370
  when :files
365
371
  cmd << '--no-index'
366
372
  else
367
- if (val = option('index')) || index > 0
373
+ if (val = git_option('index')) || index > 0
368
374
  cmd << "HEAD~#{val || index}"
369
- elsif sha && option('merge-base')
375
+ elsif sha && git_option('merge-base')
370
376
  cmd << '--merge-base'
371
377
  end
372
378
  end
@@ -376,7 +382,7 @@ module Squared
376
382
  end
377
383
 
378
384
  def commit(flag, files = [], message: nil, pass: false)
379
- message ||= option('message', 'm', zero: false)
385
+ message ||= git_option('message', 'm', zero: false)
380
386
  amend = flag.to_s.start_with?('amend')
381
387
  if !message && !amend
382
388
  return if pass
@@ -391,7 +397,7 @@ module Squared
391
397
  "-- #{files.join(' ')}"
392
398
  end
393
399
  if !push?
394
- source('git branch -vv --list', io: true, banner: false)[0].each do |val|
400
+ source('git branch -vv --list', io: true, banner: false).first.each do |val|
395
401
  origin = %r{^\* [^\[]+(?<= )\[([\w.-/]+?)/([\w.-]+)\] }.match(val)
396
402
  next unless origin
397
403
 
@@ -403,7 +409,7 @@ module Squared
403
409
  raise_error('commit', 'work tree is not usable') unless push?
404
410
 
405
411
  cmd = git_session 'commit'
406
- cmd << '--dry-run' if option('dry-run')
412
+ cmd << '--dry-run' if git_option('dry-run')
407
413
  if amend
408
414
  cmd << '--amend'
409
415
  else
@@ -411,7 +417,7 @@ module Squared
411
417
  end
412
418
  if message
413
419
  append_message message
414
- elsif flag == :'amend-orig' || option('no-edit')
420
+ elsif flag == :'amend-orig' || git_option('no-edit')
415
421
  cmd << '--no-edit'
416
422
  end
417
423
  a = ['git add --verbose']
@@ -440,14 +446,14 @@ module Squared
440
446
  source(stdout: true)
441
447
  end
442
448
 
443
- protected
449
+ private
444
450
 
445
451
  def source(cmd = @session, exception: true, banner: true, io: false, sync: true, stdout: false, stderr: false)
446
452
  cmd = close_session(cmd)
447
453
  log.info cmd
454
+ banner = format_banner(cmd.gsub(File.join(path, ''), ''), banner: banner, multiple: true)
455
+ cmd = cmd.sub(/^git\b/, "git --work-tree #{shell_quote(path)} --git-dir #{shell_quote(gitdir)}")
448
456
  begin
449
- banner = format_banner(cmd.gsub(File.join(path, ''), ''), banner: banner, multiple: true)
450
- cmd = cmd.sub(/^git\b/, "git --work-tree #{shell_quote(path)} --git-dir #{shell_quote(gitdir)}")
451
457
  if io
452
458
  [IO.popen(cmd), banner]
453
459
  elsif pipe? ? sync : stdout
@@ -480,7 +486,7 @@ module Squared
480
486
  log.error e
481
487
  raise if exception
482
488
 
483
- warn e
489
+ warn e if workspace.warning
484
490
  end
485
491
  end
486
492
 
@@ -526,14 +532,6 @@ module Squared
526
532
  files.map { |val| val == '.' ? '.' : shell_quote(base_path(val.strip)) }
527
533
  end
528
534
 
529
- def source_path?(val)
530
- return val.to_s.start_with?(File.join(path, '').to_s) if Pathname.new(val).absolute?
531
-
532
- !val.match?(%r{^\.\.[/\\]})
533
- end
534
-
535
- private
536
-
537
535
  def append_pull(opts, list, flag = nil)
538
536
  append_submodules flag
539
537
  opts.each do |opt|
@@ -551,7 +549,7 @@ module Squared
551
549
  end
552
550
 
553
551
  def append_pathspec(files = [], expect: false, pass: false)
554
- if files.empty? && (val = option('pathspec'))
552
+ if files.empty? && (val = git_option('pathspec'))
555
553
  files = split_escape(val)
556
554
  end
557
555
  files = source_path(files, pass: pass)
@@ -567,30 +565,30 @@ module Squared
567
565
  end
568
566
 
569
567
  def append_head
570
- @session << (option('head') || option('tree-ish'))
568
+ @session << (git_option('head') || git_option('tree-ish'))
571
569
  end
572
570
 
573
571
  def append_ours
574
- if option('ours')
572
+ if git_option('ours')
575
573
  @session << '--ours'
576
- elsif option('theirs')
574
+ elsif git_option('theirs')
577
575
  @session << '--theirs'
578
576
  end
579
577
  end
580
578
 
581
579
  def append_submodules(flag = nil)
582
- if option('recurse-submodules', equals: '0')
580
+ if git_option('recurse-submodules', equals: '0')
583
581
  @session << '--no-recurse-submodules'
584
- elsif flag == :submodules || option('recurse-submodules')
582
+ elsif flag == :submodules || git_option('recurse-submodules')
585
583
  @session << '--recurse-submodules'
586
584
  end
587
585
  end
588
586
 
589
587
  def append_option(list)
590
- list.each { |val| @session << "--#{val}" if option(val) }
588
+ list.each { |val| @session << "--#{val}" if git_option(val) }
591
589
  end
592
590
 
593
- def option(*args, equals: nil, zero: true)
591
+ def git_option(*args, equals: nil, zero: true)
594
592
  for val in args
595
593
  break if (ret = ENV["GIT_#{val.gsub(/\W/, '_').upcase}"])
596
594
  end
@@ -606,11 +604,13 @@ module Squared
606
604
  end
607
605
 
608
606
  def gitdir
609
- @gitdir ||= base_path('.git')
607
+ base_path('.git')
610
608
  end
611
609
 
612
- def invoked_sync?(action, flag = nil)
613
- !flag.nil? || super(action)
610
+ def source_path?(val)
611
+ return val.to_s.start_with?(File.join(path, '').to_s) if Pathname.new(val).absolute?
612
+
613
+ !val.match?(%r{^\.\.[/\\]})
614
614
  end
615
615
 
616
616
  def push?