Class: CloudRCS::Move

Inherits:
PrimitivePatch show all
Defined in:
lib/cloud_rcs/patch_types/move.rb

Overview

A primitive patch type that represents that a file has been moved or renamed.

Constant Summary

Constants inherited from PrimitivePatch

PrimitivePatch::PATH_PREFIX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PrimitivePatch

#apply!, escape_path, merge, #named_patch?, #new_path, #primitive_patch?, priority, #to_a, unescape_path

Class Method Details

.generate(orig_file, changed_file) ⇒ Object



69
70
71
72
73
74
# File 'lib/cloud_rcs/patch_types/move.rb', line 69

def generate(orig_file, changed_file)
  return if orig_file.nil? or changed_file.nil?
  if orig_file.path != changed_file.path
    return Move.new(:original_path => orig_file.path, :path => changed_file.path)
  end
end

.parse(contents) ⇒ Object



76
77
78
79
80
81
# File 'lib/cloud_rcs/patch_types/move.rb', line 76

def parse(contents)
  unless contents =~ /^move\s+(\S+)\s+(\S+)\s*$/
    raise "Failed to parse move patch: \"#{contents}\""
  end
  Move.new(:original_path => unescape_path($1), :path => unescape_path($2))
end

Instance Method Details

#after_initializeObject



8
9
10
11
# File 'lib/cloud_rcs/patch_types/move.rb', line 8

def after_initialize
  verify_path_prefix
  verify_original_path_prefix
end

#apply_to(file) ⇒ Object



60
61
62
63
64
65
# File 'lib/cloud_rcs/patch_types/move.rb', line 60

def apply_to(file)
  if file.path == original_path
    file.path = new_path
  end
  return file
end

#commute(patch) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cloud_rcs/patch_types/move.rb', line 22

def commute(patch)
  if patch.is_a? Move
    if patch.original_path == self.new_path
      raise CommuteException(true, "Conflict: cannot commute move patches that affect the same file.")
    elsif patch.new_path == self.original_path
      raise CommuteException(true, "Conflict: commuting these move patches would result in two files with the same name.")
      
    elsif patch.new_path == self.new_path
      raise CommuteException(true, "Conflict: cannot commute move patches that affect the same files.")

    else
      patch1 = Move.new(:path => patch.path, :original_path => patch.original_path)
      patch2 = Move.new(:path => self.path, :original_path => self.original_path)
    end

  elsif patch.is_a? Addfile and patch.path == self.original_path
    raise CommuteException(true, "Conflict: move and addfile are order-dependent in this case.")

  elsif patch.is_a? Rmfile and patch.path == self.new_path
    raise CommuteException(true, "Conflict: move and rmfile are order-dependent in this case.")

  # If the other patch is something like a Hunk or a Binary, and
  # it operates on the file path that this patch moves a file to,
  # then the commuted version of that patch should have a file
  # path that matches the original_path of this patch.
  elsif patch.path == self.new_path
    patch1 = patch.clone
    patch1.path = self.original_path
    patch2 = self.clone

  else
    patch1 = patch.clone
    patch2 = self.clone
  end

  return patch1, patch2
end

#inverseObject

The inverse patch moves the file back to its original location.



18
19
20
# File 'lib/cloud_rcs/patch_types/move.rb', line 18

def inverse
  Move.new(:original_path => path, :path => original_path)
end

#to_sObject



13
14
15
# File 'lib/cloud_rcs/patch_types/move.rb', line 13

def to_s
  "move #{self.class.escape_path(original_path)} #{self.class.escape_path(path)}"
end