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)



324
325
326
327
328
329
330
331
332
# File 'lib/xmigra/vcs_support/git.rb', line 324

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



373
374
375
376
377
378
379
380
381
# File 'lib/xmigra/vcs_support/git.rb', line 373

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

#relative_version(file_path) ⇒ Object



334
335
336
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
# File 'lib/xmigra/vcs_support/git.rb', line 334

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



383
384
385
386
387
388
389
390
# File 'lib/xmigra/vcs_support/git.rb', line 383

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