Class: Release::Gem::Action::VcsAction

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/release/gem/vcs_action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, opts = { }) ⇒ VcsAction

Returns a new instance of VcsAction.

Raises:



15
16
17
18
19
20
21
22
23
# File 'lib/release/gem/vcs_action.rb', line 15

def initialize(root, opts = {  })
  raise VcsActionError, "root cannot be null" if is_empty?(root)
  @ws = GitCli::Workspace.new(root) 

  oopts = opts || {}
  @ui = oopts[:ui]
  @engine = oopts[:engine]
  path = @ws.workspace_root
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object (private)



466
467
468
469
470
471
472
473
474
475
476
# File 'lib/release/gem/vcs_action.rb', line 466

def method_missing(mtd, *args, &block)
  if @ws.respond_to?(mtd)
    @ws.send(mtd, *args, &block)
  else
    if not @engine.nil? and @engine.respond_to?(mtd)
      @engine.send(mtd, *args, &block)
    else
      super
    end
  end
end

Instance Attribute Details

#uiObject

Returns the value of attribute ui.



13
14
15
# File 'lib/release/gem/vcs_action.rb', line 13

def ui
  @ui
end

Instance Method Details

#add(&block) ⇒ Object



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
# File 'lib/release/gem/vcs_action.rb', line 70

def add(&block)
  
  if block

    stgDir, stgFiles = @ws.staged_files
    modDir, modFiles = @ws.modified_files
    newDir, newFiles = @ws.new_files
    delDir, delFiles = @ws.deleted_files

    modFiles.delete_if { |f| stgFiles.include?(f) }
    modDir.delete_if { |f| stgDir.include?(f) }

    newFiles.delete_if { |f| stgFiles.include?(f) }
    newDir.delete_if { |f| stgDir.include?(f) }

    delFiles.delete_if { |f| stgFiles.include?(f) }
    delDir.delete_if { |f| stgDir.include?(f) }

    res = block.call(:select_files_to_add, { modified: { files: modFiles, dirs: modDir }, new: { files: newFiles, dirs: newDir }, deleted: { files: delFiles, dirs: delDir }, staged: { files: stgFiles, dirs: stgDir }, vcs: self } )

    sel = res.clone

    if not_empty?(sel)
      st, rres = @ws.add_to_staging(*sel)
      if st
        block.call(:files_added_successfully, { count: sel.length, output: rres })
      else
        block.call(:files_failed_to_be_added, { output: rres } )
      end
    else
      block.call(:no_files_given)
    end


  end

end

#add_to_staging(*files) ⇒ Object



436
437
438
# File 'lib/release/gem/vcs_action.rb', line 436

def add_to_staging(*files)
  @ws.add_to_staging(*files) 
end

#add_to_staging_if_commit_before(*files) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/release/gem/vcs_action.rb', line 440

def add_to_staging_if_commit_before(*files)

  stgDir, stgFiles = @ws.staged_files
  modDir, modFiles = @ws.modified_files
  #newDir, newFiles = @ws.new_files
  delDir, delFiles = @ws.deleted_files

  mFiles = modFiles.map { |e| e.path }
  sFiles = stgFiles.map { |e| e.path }
  dFiles = delFiles.map { |e| e.path }

  res = []
  files.each do |f|
    if (mFiles.include?(f) or dFiles.include?(f)) and not sFiles.include?(f)
      res << f
    end
  end

  if not_empty?(res)
    @ws.add_to_staging(*res)
  end

end

#commit(msg = nil, &block) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/release/gem/vcs_action.rb', line 290

def commit(msg = nil, &block)

  res = :value
  if block

    counter = 0

    stgDir, stgFiles = @ws.staged_files
    modDir, modFiles = @ws.modified_files
    newDir, newFiles = @ws.new_files
    delDir, delFiles = @ws.deleted_files

    modFiles.delete_if { |f| stgFiles.include?(f) }
    modDir.delete_if { |f| stgDir.include?(f) }

    newFiles.delete_if { |f| stgFiles.include?(f) }
    newDir.delete_if { |f| stgDir.include?(f) }

    delFiles.delete_if { |f| stgFiles.include?(f) }
    delDir.delete_if { |f| stgDir.include?(f) }

    # block should call vcs for add, remove, ignore and other operations
    res = block.call(:select_files_to_commit, { modified: { files: modFiles, dirs: modDir }, new: { files: newFiles, dirs: newDir }, deleted: { files: delFiles, dirs: delDir }, staged: { files: stgFiles, dirs: stgDir }, vcs: self, counter: counter } )

    if not_empty?(res)
      st, cres = add_to_staging(*res)
      if st
        @prmt.puts Gem.pastel.green(pmsg("\n Files added successfully"))
      else
        @prmt.puts Gem.pastel.red(pmsg("\n Files failed to be added. Message was : #{cres}"))
        proceed = @prmt.yes?(" There are files failed to be added. Proceed with commit without those files?")

      end

      stgDir, stgFiles = @ws.staged_files
      block.call(:staged_elements_of_commit, { files: stgFiles, dirs: stgDir })

      msg = block.call(:commit_message) if is_empty?(msg) 
      raise VcsActionError, "Commit message is empty" if is_empty?(msg)

      cp "Commit with user message : #{msg}"
      st, res = @ws.commit(msg)
      if st
        block.call(:commit_successful, res) if block
      else
        block.call(:commit_failed, res) if block
      end
      [st, res]

    else
      @prmt.puts Gem.pastel.yellow("\n No files selected to be committed. Skipping commit.")

    end

  else

    msg = "Auto commit all ('-am' flag) by release-gem" if is_empty?(msg)
    cp msg
    # changed files only without new files
    @ws.commit_all(msg)
  end

end

#commit_all(msg = nil, &block) ⇒ Object



253
254
255
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
287
288
# File 'lib/release/gem/vcs_action.rb', line 253

def commit_all(msg = nil, &block)
  
  if block
    
    stgDir, stgFiles = @ws.staged_files
    modDir, modFiles = @ws.modified_files
    delDir, delFiles = @ws.deleted_files

    modFiles.delete_if { |f| stgFiles.include?(f) }
    modDir.delete_if { |f| stgDir.include?(f) }

    delFiles.delete_if { |f| stgFiles.include?(f) }
    delDir.delete_if { |f| stgDir.include?(f) }

    msg = block.call(:commit_message, { modified: { files: modFiles, dirs: modDir }, deleted: { files: delFiles, dirs: delDir }, staged: { files: stgFiles, dirs: stgDir } }) if is_empty?(msg) 
    raise VcsActionError, "Commit message is empty" if is_empty?(msg)

    cp "Commit with user message : #{msg}"
    st, res = @ws.commit_all(msg)
    if st
      block.call(:commit_successful, res) if block
    else
      block.call(:commit_failed, res) if block
    end
    [st, res]

  else

    msg = "Auto commit all ('-am' flag) by release-gem" if is_empty?(msg)
    cp msg
    # changed files only without new files
    @ws.commit_all(msg)

  end

end

#delete_file(*files, &block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
251
# File 'lib/release/gem/vcs_action.rb', line 200

def delete_file(*files, &block)

  if block

    stgDir, stgFiles = @ws.staged_files
    modDir, modFiles = @ws.modified_files
    newDir, newFiles = @ws.new_files

    modFiles.delete_if { |f| stgFiles.include?(f) }
    modDir.delete_if { |f| stgDir.include?(f) }

    newFiles.delete_if { |f| stgFiles.include?(f) }
    newDir.delete_if { |f| stgDir.include?(f) }

    res = block.call(:select_files_to_delete, { modified: { files: modFiles, dirs: modDir }, new: { files: newFiles, dirs: newDir }, staged: { files: stgFiles, dirs: stgDir } } )

    sel = res.clone

    if not_empty?(sel)
      staged = []
      nonTrack = []
      sel.each do |s|
        if s.is_a?(GitCli::Delta::NewFile)
          confirm = block.call(:confirm_nontrack_delete, s.path)
          if confirm
            FileUtils.rm(s.path)
            block.call(:nontrack_file_deleted, s.path)
          end
        elsif s.is_a?(GitCli::Delta::ModifiedFile)
          # not staged
          confirm = block.call(:confirm_vcs_delete, s.path)
          puts "vcs confirm : #{confirm}"
          if confirm
            @ws.remove_from_vcs(s.path)
            block.call(:vcs_file_deleted, s.path)
          end
        elsif s.is_a?(GitCli::Delta::StagedFile)
          confirm = block.call(:confirm_staged_delete, s.path)
          if confirm
            @ws.remove_from_staging(s.path)
            block.call(:staged_file_deleted, s.path)
          end
        end
      end

    else
      block.call(:no_files_given)
    end


  end
end

#diff(&block) ⇒ Object



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
# File 'lib/release/gem/vcs_action.rb', line 108

def diff(&block)
  
  if block
    

    stgDir, stgFiles = @ws.staged_files
    modDir, modFiles = @ws.modified_files

    modFiles.delete_if { |f| stgFiles.include?(f) }
    modDir.delete_if { |f| stgDir.include?(f) }

    res = block.call(:select_files_to_diff, { modified: { files: modFiles, dirs: modDir },  staged: { files: stgFiles, dirs: stgDir }, vcs: self } )

    sel = res.clone

    if not_empty?(sel)
      sel.each do |s|
        st, rres = @ws.diff_file(s)
        if st
          block.call(:diff_file_result, { file: s, output: rres })
        else
          block.call(:diff_file_error, { file: s, output: rres } )
        end
      end
    else
      block.call(:no_files_given)
    end


  end

end

#exec(&block) ⇒ Object



25
26
27
# File 'lib/release/gem/vcs_action.rb', line 25

def exec(&block)
  instance_eval(&block) if block
end

#has_deleted_files?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/release/gem/vcs_action.rb', line 44

def has_deleted_files?
  delDir, delFiles = @ws.deleted_files
  not_empty?(delDir) or not_empty?(delFiles)
end

#has_modified_files?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/release/gem/vcs_action.rb', line 39

def has_modified_files?
  modDir, modFiles = @ws.modified_files
  not_empty?(modDir) or not_empty?(modFiles)
end

#has_new_files?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/release/gem/vcs_action.rb', line 34

def has_new_files?
  newDir, newFiles = @ws.new_files
  not_empty?(newDir) or not_empty?(newFiles)
end

#has_staged_file?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/release/gem/vcs_action.rb', line 29

def has_staged_file?
  stgDir, stgFiles = @ws.staged_files
  not_empty?(stgDir) or not_empty?(stgFiles)
end

#ignore(*files, &block) ⇒ Object



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
# File 'lib/release/gem/vcs_action.rb', line 141

def ignore(*files, &block)
  
  if block
    
    newDir, newFiles = @ws.new_files

    res = block.call(:select_files_to_ignore, { files: newFiles, dirs: newDir } )

    sel = res.clone

    if not_empty?(sel)
      st, rres = @ws.ignore(*sel)
      if st
        block.call(:files_ignored_successfully, { count: sel.length, output: rres })
      else
        block.call(:files_failed_to_be_ignored, { output: rres } )
      end
    else
      block.call(:no_files_given)
    end

  else

    @ws.ignore(*files) if not_empty?(files)

  end

end

#overview(&block) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/release/gem/vcs_action.rb', line 49

def overview(&block)
  raise VcsActionError, "block is required" if not block
  
  stgDir, stgFiles = @ws.staged_files
  modDir, modFiles = @ws.modified_files
  newDir, newFiles = @ws.new_files
  delDir, delFiles = @ws.deleted_files

  modFiles.delete_if { |f| stgFiles.include?(f) }
  modDir.delete_if { |f| stgDir.include?(f) }

  newFiles.delete_if { |f| stgFiles.include?(f) }
  newDir.delete_if { |f| stgDir.include?(f) }

  delFiles.delete_if { |f| stgFiles.include?(f) }
  delDir.delete_if { |f| stgDir.include?(f) }

  block.call(:workspace_overview, { modified: { files: modFiles, dirs: modDir }, new: { files: newFiles, dirs: newDir }, deleted: { files: delFiles, dirs: delDir }, staged: { files: stgFiles, dirs: stgDir } } )

end

#push(remote = nil, branch = nil, &block) ⇒ Object



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
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/release/gem/vcs_action.rb', line 392

def push(remote = nil, branch= nil, &block)
 
  remoteConf = @ws.remote_config
  if is_empty?(remoteConf)
    if block
      remote = block.call(:no_remote_repos_defined)
    end
  else
    if is_empty?(remote)
      if block
        remote = block.call(:select_remote, remoteConf)
      else
        remote = remoteConf.keys.first
      end
    end
  end

  #raise VcsActionError, "Push repository remote cannot be empty" if is_empty?(remote)

  if not_empty?(remote) and remote != :skip

    branch = @ws.current_branch if is_empty?(branch)


    if is_local_ahead_of_remote?("#{remote}/#{branch}", branch)

      cp "pushing to #{remote}/#{branch}"
      st, res = @ws.push_changes_with_tags(remote, branch)

      if st
        block.call(:push_successful, res) if block
      else
        block.call(:push_failed, res) if block
      end

      [st, res]

    else
      block.call(:no_changes_to_push, { remote: remote, branch: branch }) if block
    end
  end

end

#remove_from_staging(*files, &block) ⇒ Object



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
# File 'lib/release/gem/vcs_action.rb', line 170

def remove_from_staging(*files, &block)
  
  if block
    
    stgDir, stgFiles = @ws.staged_files

    res = block.call(:select_files_to_remove, { files: stgFiles, dirs: stgDir } )

    sel = res.clone

    if not_empty?(sel)
      st, rres = @ws.remove_from_staging(*sel)
      if st
        block.call(:files_removed_successfully, { count: sel.length, output: rres })
      else
        block.call(:files_removed_to_be_ignored, { output: rres } )
      end
    else
      block.call(:no_files_given)
    end


  else

    @ws.removed_from_staging(*files) if not_empty?(files)

  end

end

#tag(*args, &block) ⇒ Object



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
# File 'lib/release/gem/vcs_action.rb', line 354

def tag(*args, &block)

  opts = args.first || {  }
  tag = opts[:tag]
  msg = opts[:message]

  if not @ws.tag_points_at?("HEAD")

    if is_empty?(tag)
      raise VcsActionError, "tag name cannot be empty" if not block
      tag = block.call(:tag_name)
      raise VcsActionError, "tag name cannot be empty" if is_empty?(tag)
    end

    if is_empty?(msg)
      if block
        msg = block.call(:tag_message, { tag: tag })
      end
    end

    cp "tagged with name : #{tag} and message : #{msg}"
    st, res = @ws.create_tag(tag, msg)
    if st
      block.call(:tagging_success, { tag: tag, output: res }) if block
    else
      block.call(:tagging_failed, { tag: tag, output: res }) if block
    end

    [st, res]

  else

    block.call(:no_tagging_required) if block
    [true, "No tagging required"]
  end

end