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

Instance Attribute Summary

Attributes inherited from Base

#dependfile, #exception, #group, #name, #parent, #path, #pipe, #project, #theme, #verbose, #workspace

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add, aliasargs, #allref, as_path, bannerargs, #basepath, #build, #build?, #clean, #clean?, #copy, #copy?, #depend, #depend?, #dependtype, #dev?, #doc, #doc?, #enabled?, #graph, #graph?, #has?, #initialize_build, #initialize_env, #initialize_logger, #initialize_ref, #inspect, #log, #prod?, ref, #ref?, #script?, #test, #test?, to_s, #to_s, #to_sym, #variable_set, #version, #with

Methods included from Common::Format

#enable_aixterm

Constructor Details

#initializeGit

Returns a new instance of Git.



69
70
71
72
# File 'lib/squared/workspace/project/git.rb', line 69

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

Class Method Details

.batchargsObject



43
44
45
# File 'lib/squared/workspace/project/git.rb', line 43

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

.config?(val) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/squared/workspace/project/git.rb', line 47

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

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

.populate(ws) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/squared/workspace/project/git.rb', line 14

def populate(ws, **)
  return if ws.series[:pull].empty?

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

    desc ws.task_name("#{all}[git?=rebase|stash,depend?]", desc: true)
    task 'all', [:git, :depend] do |_, args|
      cmd = case args.git
            when 'rebase'
              [ws.task_sync('rebase')]
            when 'stash'
              [ws.task_sync('stash'), ws.task_sync('pull')]
            else
              [ws.task_sync('pull')]
            end
      cmd << ws.task_sync('depend') if args.depend && !ws.series[:depend].empty?
      cmd << ws.task_sync('build')
      Common::Utils.task_invoke(*cmd, **ws.invokeargs)
    end
    ws.series.sync << all
    ws.series.multiple << all
  end
end

.tasksObject



39
40
41
# File 'lib/squared/workspace/project/git.rb', line 39

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

Instance Method Details

#checkout(flag, files = [], branch: nil, create: nil, commit: nil, detach: nil) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/squared/workspace/project/git.rb', line 324

def checkout(flag, files = [], branch: nil, create: nil, commit: nil, detach: nil)
  cmd = git_session 'checkout'
  case flag
  when :branch
    cmd << '--detach' if detach == 'd' || option('detach')
    if create
      cmd << "-#{create}" << branch
      if (val = option('start-point'))
        cmd << val
      end
    else
      cmd << branch
    end
    cmd << commit
  when :detach
    cmd << "--#{flag}" << commit
  else
    cmd << "--#{flag}"
    append_ours
    append_head
    append_pathspec files
  end
  source
end

#commit(flag, files = [], message: nil, pass: false) ⇒ Object



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

def commit(flag, files = [], message: nil, pass: false)
  message ||= option('message', 'm', prefix: 'git', ignore: false)
  amend = flag.to_s.start_with?('amend')
  if !message && !amend
    return if pass

    raise_error('commit', 'GIT_MESSAGE="description"', hint: 'missing')
  end
  pathspec = if flag == :all || (amend && files.size == 1 && files.first == '*')
               '--all'
             else
               raise_error('commit', 'pathspec', hint: 'missing') if (files = projectmap(files)).empty?
               "-- #{files.join(' ')}"
             end
  origin = nil
  branch = nil
  source('git fetch --no-tags --quiet', io: true, banner: false)
  source('git branch -vv --list', io: true, banner: false).first.each do |val|
    next unless (data = %r{^\* [^\[]+(?<= )\[([\w.-/]+?)/([\w.-]+)\] }.match(val))

    origin = data[1]
    branch = data[2]
    break
  end
  raise_error('commit', 'work tree is not usable') unless origin && branch
  cmd = git_session 'commit'
  cmd << '--dry-run' if option('dry-run')
  if amend
    cmd << '--amend'
  else
    cmd.delete('--amend')
  end
  if message
    append_message message
  elsif flag == :'amend-orig' || option('no-edit')
    cmd << '--no-edit'
  end
  a = ['git add --verbose']
  b = ['git push']
  if dry_run?
    a << '--dry-run'
    b << '--dry-run'
  end
  a << pathspec
  b << '--force' if amend
  b << @origin << @branch
  puts if pass
  source a.join(' ')
  source cmd
  source b.join(' ')
end

#diff(flag, files = [], branch: nil, index: 0) ⇒ Object



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

def diff(flag, files = [], branch: nil, index: 0)
  cmd = git_session 'diff'
  unless flag == :files
    if /^#[0-9a-f]{5,40}\#$/.match?(sha = files.first)
      sha = sha[1..-2]
      files.shift
    else
      sha = nil
    end
  end
  if (val = option('unified')).to_i > 0
    cmd << "--unified=#{val}"
  end
  append_nocolor
  case flag
  when :cached
    cmd << '--cached'
    cmd << '--merge-base' if option('merge-base')
  when :branch
    cmd << branch
  when :files
    cmd << '--no-index'
  else
    if (val = option('index')) || index > 0
      cmd << "HEAD~#{val || index}"
    elsif sha && option('merge-base')
      cmd << '--merge-base'
    end
  end
  cmd << sha
  append_pathspec(files, parent: flag == :files)
  source(exception: cmd.include?('--exit-code'))
end

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



251
252
253
254
255
256
# File 'lib/squared/workspace/project/git.rb', line 251

def fetch(flag = nil, opts: [])
  cmd = git_session 'fetch'
  cmd << '--all' if flag == :all || option('all')
  append_pull opts, OPT_FETCH, flag
  source(sync: invoked_sync?('fetch', flag), **threadargs)
end

#files(flag, grep: nil) ⇒ Object



367
368
369
370
371
372
373
# File 'lib/squared/workspace/project/git.rb', line 367

def files(flag, grep: nil)
  git_session 'ls-files', " --#{flag}"
  out, banner = source(io: true)
  print_item banner
  ret = write_lines(out, grep: grep)
  list_result(ret, 'files', grep: grep)
end

#populateObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/squared/workspace/project/git.rb', line 78

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

  namespace name do
    @@tasks[Git.ref].each do |action, flags|
      namespace action do
        flags.each do |flag|
          case action
          when :pull, :fetch
            desc format_desc(action, flag, flag == :pull ? OPT_PULL : OPT_FETCH)
            task flag, [:opts] do |_, args|
              __send__(action, flag, opts: args.to_a)
            end
          when :commit, :restore
            if flag == :all
              desc format_desc(action, flag, 'message?')
              task flag, [:message] do |_, args|
                commit(flag, message: args.fetch(:message, nil))
              end
            else
              desc format_desc(action, flag, 'pathspec+')
              task flag, [:pathspec] do |_, args|
                files = guard_params(action, flag, args: args.to_a)
                __send__(action, flag, files)
              end
            end
          when :stash
            if flag == :push
              desc format_desc(action, flag, 'pathspec*')
              task flag, [:pathspec] do |_, args|
                stash(flag, args.to_a)
              end
            else
              desc format_desc(action, flag, 'commit?')
              task flag, [:commit] do |_, args|
                stash(flag, commit: args.commit)
              end
            end
          when :rev
            desc format_desc(action, flag, "ref?=HEAD#{flag == :commit ? ',size?' : ''}")
            task flag, [:ref, :size] do |_, args|
              rev(flag, ref: args.ref, size: args.size)
            end
          when :refs, :files
            desc format_desc(action, flag, 'grep?=pattern')
            task flag, [:grep] do |_, args|
              __send__(action, flag, grep: args.fetch(:grep, nil))
            end
          when :diff
            case flag
            when :head
              desc format_desc(action, flag, 'index?=0,pathspec*')
              task flag, [:pathspec] do |_, args|
                files = args.to_a
                diff(flag, files, index: /^\d+$/.match?(files[0]) && !option('index') ? files.shift.to_i : 0)
              end
            when :cached
              desc format_desc(action, flag, 'pathspec*')
              task flag, [:pathspec] do |_, args|
                diff(flag, args.to_a)
              end
            when :branch
              desc format_desc(action, flag, 'name,pathspec*')
              task flag, [:name, :pathspec] do |_, args|
                branch = guard_params(action, flag, args: args, key: :name)
                diff(flag, args.to_a[1..-1] || [], branch: branch)
              end
            when :files
              desc format_desc(action, flag, 'path1,path2')
              task flag, [:path1, :path2] do |_, args|
                path1 = guard_params(action, flag, args: args, key: :path1)
                path2 = guard_params(action, flag, args: args, key: :path2)
                diff(flag, [path1, path2])
              end
            end
          when :checkout
            case flag
            when :branch
              desc format_desc(action, flag, 'name,create?=b|B,commit?,detach?=d')
              task flag, [:name, :create, :commit, :detach] do |_, args|
                branch = guard_params(action, flag, args: args, key: :name)
                create = args.create
                if args.commit == 'd'
                  detach = 'd'
                  commit = nil
                elsif create == 'd'
                  create = nil
                  commit = nil
                  detach = 'd'
                elsif create && create.size > 1
                  commit = create
                  create = nil
                  detach = args.commit
                else
                  detach = args.detach
                  commit = args.commit
                end
                guard_params(action, flag, args: { create: create }, key: :create, pat: /^b$/i) if create
                checkout(flag, branch: branch, create: create, commit: commit, detach: detach)
              end
            when :detach
              desc format_desc(action, flag, 'branch/commit?')
              task flag, [:commit] do |_, args|
                checkout(flag, commit: args.commit)
              end
            else
              desc format_desc(action, flag, 'pathspec*')
              task flag, [:pathspec] do |_, args|
                checkout(flag, args.to_a)
              end
            end
          when :reset
            if flag == :head
              desc format_desc(action, flag, 'ref,pathspec+')
              task flag, [:ref, :pathspec] do |_, args|
                files = guard_params(action, flag, args: args.to_a[1..-1] || [])
                reset(flag, files, ref: args.ref)
              end
            else
              desc format_desc(action, flag, 'ref?=HEAD')
              task flag, [:ref] do |_, args|
                reset(flag, ref: args.ref)
              end
            end
          when :show
            if flag == :oneline
              desc format_desc(action, flag, 'object+')
              task flag, [:object] do |_, args|
                objs = guard_params(action, flag, args: args.to_a)
                show(objs, pretty: 'oneline', abbrev: true)
              end
            else
              desc format_desc(action, flag, 'format,object+')
              task flag, [:format, :object] do |_, args|
                format = guard_params(action, flag, args: args, key: :format)
                objs = guard_params(action, flag, args: args.to_a[1..-1] || [])
                show(objs, "#{flag}": format)
              end
            end
          end
        end
      end
    end
  end
end

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



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/squared/workspace/project/git.rb', line 225

def pull(flag = nil, sync: invoked_sync?('pull', flag), opts: [])
  cmd = git_session 'pull'
  if flag == :'no-rebase' || option('rebase', equals: '0')
    cmd << '--no-rebase'
  elsif flag == :rebase || option('rebase')
    cmd << '--rebase'
  end
  if flag == :'no-commit' || option('commit', equals: '0')
    cmd << '--no-commit'
  elsif flag == :commit || option('commit')
    cmd << '--commit'
  end
  append_pull opts, OPT_PULL, flag
  sub = if verbose
          [
            { pat: /^(.+)(\|\s+\d+\s+)([^-]*)(-+)(.*)$/, styles: :red, index: 4 },
            { pat: /^(.+)(\|\s+\d+\s+)(\++)(-*)(.*)$/, styles: :green, index: 3 }
          ]
        end
  source(sync: sync, sub: sub, **threadargs)
end

#rebaseObject



247
248
249
# File 'lib/squared/workspace/project/git.rb', line 247

def rebase
  pull(:rebase, sync: invoked_sync?('rebase'))
end

#refObject



74
75
76
# File 'lib/squared/workspace/project/git.rb', line 74

def ref
  Git.ref
end

#refs(flag, grep: nil) ⇒ Object



359
360
361
362
363
364
365
# File 'lib/squared/workspace/project/git.rb', line 359

def refs(flag, grep: nil)
  git_session 'ls-remote', " --#{flag}"
  out, banner = source(io: true)
  print_item banner
  ret = write_lines(out, grep: grep)
  list_result(ret, flag.to_s, grep: grep)
end

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



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/squared/workspace/project/git.rb', line 304

def reset(flag, files = [], ref: nil)
  cmd = git_session 'reset'
  if flag == :head
    append_commit ref
    append_pathspec files
  else
    case flag
    when :hard, :soft, :merge, :keep
      cmd << "--#{flag}"
    when :submodules
      append_submodules flag
    else
      cmd << '--mixed'
      cmd << '--no-refresh' if option('refresh', equals: '0')
    end
    append_commit ref
  end
  source
end

#restore(flag, files) ⇒ Object



461
462
463
464
465
466
# File 'lib/squared/workspace/project/git.rb', line 461

def restore(flag, files)
  git_session 'restore', "--#{flag}"
  append_ours
  append_pathspec(files, expect: true)
  source(stdout: true)
end

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



349
350
351
352
353
354
355
356
357
# File 'lib/squared/workspace/project/git.rb', line 349

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

#show(objs, pretty: nil, format: nil, abbrev: nil) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/squared/workspace/project/git.rb', line 468

def show(objs, pretty: nil, format: nil, abbrev: nil)
  cmd = git_session 'show'
  case (flag = pretty&.downcase)
  when 'oneline', 'short', 'medium', 'full', 'fuller', 'reference', 'email', 'raw'
    cmd << "--pretty=#{flag}"
  else
    if pretty
      cmd << "--pretty=#{shell_escape("tformat:#{pretty}", quote: true)}"
    elsif format
      cmd << "--format=#{shell_escape(format, quote: true)}"
    end
  end
  if (abbrev ||= option('abbrev')) == true
    cmd << '--abbrev-commit'
  elsif abbrev.to_i > 0
    cmd << "--abbrev=#{abbrev}"
  end
  append_value objs
  source(exception: false)
end

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



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/squared/workspace/project/git.rb', line 258

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

#statusObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/squared/workspace/project/git.rb', line 272

def status
  cmd = git_session 'status', option('long') ? '--long' : '--short'
  if (val = option('ignore-submodules', ignore: false))
    cmd << "--ignore-submodules=#{case val
                                  when '0', 'none'
                                    'none'
                                  when '1', 'untracked'
                                    'untracked'
                                  when '2', 'dirty'
                                    'dirty'
                                  else
                                    'all'
                                  end}"
  end
  append_pathspec
  out, banner = source(io: true)
  if invoked_sync?('status')
    print_item banner
    banner = nil
  end
  ret = write_lines(out, banner: banner, sub: if verbose
                                                [
                                                  { pat: /^(.)([A-Z])(.+)$/, styles: :red, index: 2 },
                                                  { pat: /^([A-Z])(.+)$/, styles: :green },
                                                  { pat: /^(\?\?)(.+)$/, styles: :red },
                                                  { pat: /^(## )(.+)(\.{3})(.+)$/,
                                                    styles: [nil, :green, nil, :red], index: -1 }
                                                ]
                                              end)
  list_result(ret, 'files', action: 'modified')
end