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

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

Direct Known Subclasses

Node, Python, Ruby

Instance Attribute Summary

Attributes inherited from Base

#group, #name, #path, #project, #workspace

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Format

#check_style, #emphasize, #log_message, #log_title, #sub_style

Methods inherited from Base

#base_path, #build, #build?, #clean, #clean?, #copy, #copy?, #depend, #depend?, #doc, #doc?, #enabled?, #has?, #initialize, #refresh, #refresh?, #styles, to_s, #to_s, #to_sym, #verbose?

Methods included from Common

#as_a, #get, #message, #set

Constructor Details

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

Class Method Details

.is_a?(path) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/squared/repo/project/git.rb', line 18

def is_a?(path)
  if path.is_a?(::String) || path.is_a?(::Pathname)
    Pathname.new(path).join('.git').directory?
  else
    super
  end
end

.tasksObject



10
11
12
# File 'lib/squared/repo/project/git.rb', line 10

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

.to_symObject



14
15
16
# File 'lib/squared/repo/project/git.rb', line 14

def to_sym
  :git
end

Instance Method Details

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/squared/repo/project/git.rb', line 256

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}"
    if option('ours')
      cmd << '--ours'
    elsif option('theirs')
      cmd << '--theirs'
    end
    if (val = option('tree-ish'))
      cmd << val
    end
    append_pathspec files
  end
  source
end

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

Raises:

  • (ArgumentError)


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

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



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/squared/repo/project/git.rb', line 314

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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/squared/repo/project/git.rb', line 192

def fetch(flag = nil, opts: [])
  cmd = git_session 'fetch'
  cmd << '--all' if flag == :all || option('all')
  cmd << '--recurse-submodules' if flag == :submodules || option('recurse-submodules')
  opts.each do |val|
    case val
    when 'tags', 'prune', 'prune-tags', 'dry-run'
      cmd << "--#{val}"
    else
      cmd << "--depth=#{$1}" if /^depth=(\d+)$/.match(val)
    end
  end
  source(sync: invoked_sync?('fetch', flag), stderr: true, exception: !@workspace.multitask?)
end

#files(flag, grep: nil) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/squared/repo/project/git.rb', line 306

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/squared/repo/project/git.rb', line 40

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

  namespace @name do
    @@tasks[:git].each do |action, flags|
      namespace action do
        flags.each do |flag|
          case action
          when :pull
            desc format_desc(action, flag, %w[all tags prune ff-only autostash dry-run])
            task flag, [:opts] do |_, args|
              opts = collect_args(args, :opts)
              pull(flag, opts: opts)
            end
          when :fetch
            desc format_desc(action, flag, %w[tags prune prune-tags depth=n dry-run])
            task flag, [:opts] do |_, args|
              opts = collect_args(args, :opts)
              fetch(flag, opts: opts)
            end
          when :commit
            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)
                commit(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
            desc format_desc(action, flag, 'commit')
            task flag, [:commit] do |_, args|
              guard_params(action, flag, args: args, key: :commit)
              reset(flag, commit: args.commit)
            end
          end
        end
      end
    end
  end
end

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/squared/repo/project/git.rb', line 166

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
  cmd << '--recurse-submodules' if flag == :submodules || option('recurse-submodules')
  opts.each do |val|
    case val
    when 'all', 'tags', 'prune', 'ff-only', 'autostash', 'dry-run'
      cmd << "--#{val}"
    end
  end
  source(sync: sync, stderr: true, exception: !@workspace.multitask?)
end

#rebaseObject



188
189
190
# File 'lib/squared/repo/project/git.rb', line 188

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

#refs(flag, grep: nil) ⇒ Object



298
299
300
301
302
303
304
# File 'lib/squared/repo/project/git.rb', line 298

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, commit: nil) ⇒ Object



252
253
254
# File 'lib/squared/repo/project/git.rb', line 252

def reset(flag, commit: nil)
  source "git reset --#{flag} #{commit}"
end

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



288
289
290
291
292
293
294
295
296
# File 'lib/squared/repo/project/git.rb', line 288

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, ref || 'HEAD'
  source
end

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



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/squared/repo/project/git.rb', line 207

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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/squared/repo/project/git.rb', line 221

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