Class: XMigra::GitSpecifics::VersionComparator

Inherits:
Object
  • Object
show all
Defined in:
lib/xmigra/vcs_support/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(vcs_object, options = {}) ⇒ VersionComparator

vcs_object.kind_of?(GitSpecifics)



327
328
329
330
331
332
333
334
335
# File 'lib/xmigra/vcs_support/git.rb', line 327

def initialize(vcs_object, options={})
  @object = vcs_object
  @expected_content_method = options[:expected_content_method]
  @path_statuses = Hash.new do |h, file_path|
    file_path = Pathname(file_path).expand_path
    next h[file_path] if h.has_key?(file_path)
    h[file_path] = @object.git_retrieve_status(file_path)
  end
end

Instance Method Details

#latest_commit(file_path) ⇒ Object



376
377
378
379
380
381
382
383
384
# File 'lib/xmigra/vcs_support/git.rb', line 376

def latest_commit(file_path)
  @object.git(
    :log,
    '--pretty=format:%H',
    '-1',
    '--',
    file_path
  )
end

#relative_version(file_path) ⇒ Object



337
338
339
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
368
369
370
371
372
373
374
# File 'lib/xmigra/vcs_support/git.rb', line 337

def relative_version(file_path)
  # Comparing @object.file_path (a) to file_path (b)
  #
  # returns: :newer, :equal, :older, or :missing
  
  b_status = @path_statuses[file_path]
  
  return :missing if b_status.nil? || b_status.include?('D')
  
  a_status = @path_statuses[@object.file_path]
  
  if a_status == '??' || a_status[0] == 'A'
    if b_status == '??' || b_status[0] == 'A' || b_status.include?('M')
      return relative_version_by_content(file_path)
    end
    
    return :older
  elsif a_status == '  '
    return :newer unless b_status == '  '
    
    return begin
      a_commit = latest_commit(@object.file_path)
      b_commit = latest_commit(file_path)
      
      if @object.git_commits_in? a_commit..b_commit, file_path
        :newer
      elsif @object.git_commits_in? b_commit..a_commit, @object.file_path
        :older
      else
        :equal
      end
    end
  elsif b_status == '  '
    return :older
  else
    return relative_version_by_content(file_path)
  end
end

#relative_version_by_content(file_path) ⇒ Object



386
387
388
389
390
391
392
393
# File 'lib/xmigra/vcs_support/git.rb', line 386

def relative_version_by_content(file_path)
  ec_method = @expected_content_method
  if !ec_method || @object.send(ec_method, file_path)
    return :equal
  else
    return :newer
  end
end