Class: CloudRCS::Addfile

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

Overview

A primitive patch type that represents a new or an undeleted file.

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?, #to_a, unescape_path

Class Method Details

.generate(orig_file, changed_file) ⇒ Object



55
56
57
58
59
# File 'lib/cloud_rcs/patch_types/addfile.rb', line 55

def generate(orig_file, changed_file)
  if orig_file.nil? and not changed_file.nil?
    return Addfile.new(:path => changed_file.path, :contents => changed_file.content_type)
  end
end

.parse(contents) ⇒ Object



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

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

.priorityObject

Addfile has a low priority so that it will appear before patches that are likely to depend on it - such as Hunk patches.



51
52
53
# File 'lib/cloud_rcs/patch_types/addfile.rb', line 51

def priority
  10
end

Instance Method Details

#after_initializeObject



7
8
9
# File 'lib/cloud_rcs/patch_types/addfile.rb', line 7

def after_initialize
  verify_path_prefix
end

#apply_to(file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cloud_rcs/patch_types/addfile.rb', line 33

def apply_to(file)
  return file unless file.nil?
  if patch.respond_to? :owner
    new_file = self.class.file_class.new(:owner => patch.owner,
                                         :contents => "",
                                         :content_type => "text/plain")
  else
    new_file = self.class.file_class.new(:contents => "",
                                         :content_type => "text/plain")
  end
  new_file.path = path
  return new_file
end

#commute(patch) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cloud_rcs/patch_types/addfile.rb', line 19

def commute(patch)
  if patch.is_a? Addfile and patch.path == self.path
    raise CommuteException(true, "Conflict: cannot create two files with the same path.")
  elsif patch.is_a? Rmfile and patch.path == self.path
    raise CommuteException(true, "Conflict: commuting addfile with rmfile in this case would cause file to be removed before it is created.")
  elsif patch.is_a? Move and patch.original_path == self.path
    raise CommuteException(true, "Conflict: commuting addfile with move in this case would cause file to be moved before it is created.")
  else
    patch1 = patch.clone
    patch2 = self.clone
  end
  return patch1, patch2
end

#inverseObject



15
16
17
# File 'lib/cloud_rcs/patch_types/addfile.rb', line 15

def inverse
  Rmfile.new(:path => path)
end

#to_sObject



11
12
13
# File 'lib/cloud_rcs/patch_types/addfile.rb', line 11

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