Class: Squared::Repo::Project::Git

Inherits:
Base
  • Object
show all
Includes:
Format
Defined in:
lib/squared/repo/project/git.rb

Direct Known Subclasses

Node, Python, Ruby

Instance Attribute Summary

Attributes inherited from Base

#group, #log, #name, #path, #project, #theme, #warning, #workspace

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

as_path, #base_path, #build, #build?, #clean, #clean?, #copy, #copy?, #depend, #depend?, #doc, #doc?, #enabled?, #has?, #initialize, #initialize_build, #initialize_script, #inspect, #refresh, #refresh?, #test, #test?, to_s, #to_s, to_sym, #to_sym

Methods included from Common::Task

#collect_args, #invoke, #invoked?

Methods included from Common

#__get__, #__set__, #as_a, #finalize!, #message

Constructor Details

This class inherits a constructor from Squared::Repo::Project::Base

Class Method Details

.is_a?(val) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/squared/repo/project/git.rb', line 39

def is_a?(val)
  if (val = as_path(val))
    val.join('.git').directory?
  else
    super
  end
end

.populate(workspace, parallel: []) ⇒ Object



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

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

  desc 'all[git?=rebase|stash]'
  task :all, [:git] do |_, args|
    sync = ->(key) { parallel.include?(key) ? :"#{key}:sync" : key }
    pull = case args.git
           when 'rebase'
             sync.(:rebase)
           when 'stash'
             invoke sync.(:stash)
             sync.(:pull)
           else
             sync.(:pull)
           end
    invoke(pull, exception: workspace.exception)
    invoke(workspace.dev? ? :refresh : :build, exception: workspace.exception)
  end
end

.tasksObject



35
36
37
# File 'lib/squared/repo/project/git.rb', line 35

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



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/repo/project/git.rb', line 287

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 if commit
  when :detach
    cmd << "--#{flag}"
    cmd << commit if commit
  else
    cmd << "--#{flag}"
    append_ours
    append_head
    append_pathspec files
  end
  source
end

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

Raises:

  • (ArgumentError)


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

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

    raise ArgumentError, message('commit', 'GIT_MESSAGE="description"', hint: 'missing')
  end
  pathspec = if flag == :all || (amend && files.size == 1 && files.first == '*')
               '--all'
             else
               files = source_path(as_a(files))
               raise ArgumentError, message('commit', 'pathspec', hint: 'missing') if files.empty?

               "-- #{files.join(' ')}"
             end
  if !push?
    source('git branch -vv --list', io: true, banner: false)[0].each do |val|
      origin = %r{^\* [^\[]+(?<= )\[([\w.-/]+?)/([\w.-]+)\] }.match(val)
      next unless origin

      @origin = origin[1]
      @branch = origin[2]
      break
    end
  end
  raise ArgumentError, message('commit', 'work tree is not usable') unless push?

  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
  source b.join(' ')
end

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



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/squared/repo/project/git.rb', line 340

def diff(flag, files = [], branch: nil, index: 0)
  cmd = git_session 'diff'
  if /^#[0-9a-f]{5,40}\#$/.match?(sha = files.first)
    files.shift
  else
    sha = nil
  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
  else
    if (val = option('index')) || index > 0
      cmd << "HEAD~#{val || index}"
    elsif sha && option('merge-base')
      cmd << '--merge-base'
    end
  end
  cmd << sha if sha
  append_pathspec files
  source
end

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



215
216
217
218
219
220
# File 'lib/squared/repo/project/git.rb', line 215

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), stderr: true, exception: !workspace.multiple?)
end

#files(flag, grep: nil) ⇒ Object



332
333
334
335
336
337
338
# File 'lib/squared/repo/project/git.rb', line 332

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
# File 'lib/squared/repo/project/git.rb', line 62

def populate(*)
  super
  return unless gitdir.exist?

  namespace name do
    @@tasks[REF].each do |action, flags|
      namespace action do
        flags.each do |flag|
          case action
          when :pull
            desc format_desc(action, flag, OPT_PULL)
            task flag, [:opts] do |_, args|
              opts = collect_args(args, :opts)
              pull(flag, opts: opts)
            end
          when :fetch
            desc format_desc(action, flag, OPT_FETCH)
            task flag, [:opts] do |_, args|
              opts = collect_args(args, :opts)
              fetch(flag, opts: opts)
            end
          when :commit, :restore
            if flag == :all
              desc format_desc(action, flag, 'message?')
              task flag, [:message] do |_, args|
                commit(:all, message: args.fetch(:message, nil))
              end
            else
              desc format_desc(action, flag, 'pathspec+')
              task flag, [:pathspec] do |_, args|
                guard_params(action, flag, args: args, key: :pathspec)
                __send__(action, flag, collect_args(args, :pathspec))
              end
            end
          when :stash
            if flag == :push
              desc format_desc(action, flag, 'pathspec*')
              task :push, [:pathspec] do |_, args|
                stash(:push, collect_args(args, :pathspec))
              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 = collect_args(args, :pathspec)
                index = /^\d+$/.match?(files.first) && !option('index') ? files.shift.to_i : 0
                diff(:head, files, index: index)
              end
            when :cached
              desc format_desc(action, flag, 'pathspec*')
              task flag, [:pathspec] do |_, args|
                diff(:cached, collect_args(args, :pathspec))
              end
            when :branch
              desc format_desc(action, flag, 'name,pathspec*')
              task flag, [:name, :pathspec] do |_, args|
                guard_params(action, flag, args: args, key: :name)
                diff(:branch, collect_args(args, :pathspec), branch: args.name)
              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|
                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('checkout', :branch, args: args, key: :create, pat: /^[Bb]$/) if create
                checkout(:branch, branch: args.name, create: create, commit: commit, detach: detach)
              end
            when :detach
              desc format_desc(action, flag, 'branch/commit?')
              task flag, [:commit] do |_, args|
                checkout(:detach, commit: args.commit)
              end
            else
              desc format_desc(action, flag, 'pathspec*')
              task flag, [:pathspec] do |_, args|
                checkout(flag, collect_args(args, :pathspec))
              end
            end
          when :reset
            if flag == :head
              desc format_desc(action, flag, 'ref?=HEAD,pathspec+')
              task flag, [:ref, :pathspec] do |_, args|
                guard_params(action, flag, args: args, key: :pathspec)
                reset(:head, collect_args(args, :pathspec), ref: args.ref)
              end
            else
              desc format_desc(action, flag, 'ref?=HEAD')
              task flag, [:ref] do |_, args|
                reset(flag, ref: args.ref)
              end
            end
          end
        end
      end
    end
  end
end

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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/squared/repo/project/git.rb', line 195

def pull(flag = nil, sync: invoked_sync?('pull', flag), opts: [])
  cmd = git_session 'pull'
  if flag == :'no-rebase'
    cmd << '--no-rebase'
  elsif flag == :rebase || option('rebase')
    cmd << '--rebase'
  end
  if flag == :'no-commit'
    cmd << '--no-commit'
  elsif flag == :commit || option('commit')
    cmd << '--commit'
  end
  append_pull opts, OPT_PULL, flag
  source(sync: sync, stderr: true, exception: !workspace.multiple?)
end

#rebaseObject



211
212
213
# File 'lib/squared/repo/project/git.rb', line 211

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

#refs(flag, grep: nil) ⇒ Object



324
325
326
327
328
329
330
# File 'lib/squared/repo/project/git.rb', line 324

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



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/squared/repo/project/git.rb', line 267

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



424
425
426
427
428
429
430
431
432
433
# File 'lib/squared/repo/project/git.rb', line 424

def restore(flag, files)
  cmd = git_session 'restore'
  case flag
  when :worktree, :staged, :overlay
    cmd << "--#{flag}"
  end
  append_ours
  append_pathspec(files, expect: true)
  source(stdout: true)
end

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



313
314
315
316
317
318
319
320
321
322
# File 'lib/squared/repo/project/git.rb', line 313

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

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



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/squared/repo/project/git.rb', line 222

def stash(flag = :push, files = [], commit: nil)
  cmd = git_session 'stash', flag.to_s
  case flag
  when :apply, :pop
    cmd << '--index' if option('index')
    cmd << commit if commit
  else
    %w[all staged include-untracked].each { |val| cmd << "--#{val}" if option(val) }
    append_message option('message')
    append_pathspec files
  end
  source(sync: invoked_sync?('stash', flag), exception: workspace.exception)
end

#statusObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/squared/repo/project/git.rb', line 236

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