Class: Rpatch::PatchEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/rpatch/entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old, new, level) ⇒ PatchEntry

Returns a new instance of PatchEntry.



14
15
16
17
18
19
# File 'lib/rpatch/entry.rb', line 14

def initialize(old, new, level)
  @entry_old = old
  @entry_new = new
  @level = level
  @hunks = []
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



12
13
14
# File 'lib/rpatch/entry.rb', line 12

def level
  @level
end

Instance Method Details

#feed_line(line) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/rpatch/entry.rb', line 29

def feed_line(line)
  if line =~ /^@@/
    hunk = PatchHunk.new line, @hunks.size + 1
    @hunks << hunk
  elsif @hunks.any?
    @hunks[-1].feed_line line
  else
    raise PatchFormatError, "Feed lines must start with @@, not: #{line}"
  end
end

#newfileObject



25
26
27
# File 'lib/rpatch/entry.rb', line 25

def newfile
  @newfile ||= path_strip_prefix(@entry_new, @level)
end

#oldfileObject



21
22
23
# File 'lib/rpatch/entry.rb', line 21

def oldfile
  @oldfile ||= path_strip_prefix(@entry_old, @level)
end

#patch_on_directory(inputdir, outputdir = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/rpatch/entry.rb', line 122

def patch_on_directory(inputdir, outputdir=nil)
  outputdir ||= inputdir
  if oldfile == '/dev/null'
    input = newfile.start_with?('/') ? newfile : File.join(inputdir, newfile)
  else
    input = oldfile.start_with?('/') ? oldfile : File.join(inputdir, oldfile)
  end
  output = newfile.start_with?('/') ? newfile : File.join(outputdir, newfile)
  patch_on_file(input, output)
end

#patch_on_file(input, output = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rpatch/entry.rb', line 57

def patch_on_file(input, output=nil)
  lines = []
  output ||= input
  if output.is_a? IO or output.is_a? StringIO
  	filename = "<#{newfile}>"
  else
  	filename = output
    unless File.exist?(File.dirname(output))
      FileUtils.mkdir_p File.dirname(output)
    end
  end

  if input.is_a? IO or input.is_a? StringIO
    lines = input.readlines.map{|line| line.chomp}
  elsif File.file? input
    File.open(input) do |io|
      lines = io.readlines.map{|line| line.chomp}
    end
  end
  lines_dup = lines.dup

  patch_applied = false
  patch_status = true
  @hunks.each do |hunk|
    begin
      hunk.patch(lines)
    rescue AlreadyPatchedError => e
      Tty.info "#{filename}: #{e.message}"
    rescue Exception => e
      Tty.error "#{filename}: #{e.message}"
      patch_status = false
    else
      patch_applied = true
    end
  end

  if output.is_a? IO or output.is_a? StringIO
    output.write lines * "\n" + "\n"
  elsif not patch_applied
    Tty.notice "#{filename}: nothing changed"
    if input != output
      File.open(output, "w") do |io|
        io.write lines * "\n" + "\n"
      end
    end
  else
    unless patch_status
      Tty.warning "saved orignal file as \"#{output}.orig\"."
      File.open("#{output}.orig", "w") do |io|
        io.write lines_dup * "\n" + "\n"
      end
    end
    if lines.size > 0
      File.open(output, "w") do |io|
        io.write lines * "\n" + "\n"
      end
      Tty.notice "Patched \"#{output}\"."
    else
      File.unlink output
      Tty.notice "Remove \"#{output}\"."
    end
  end
  return patch_status
end

#patch_on_message(text) ⇒ Object

Used in test cases.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rpatch/entry.rb', line 41

def patch_on_message(text)
  lines = text.split("\n").map{|line| line.chomp}

  @hunks.each do |hunk|
    hunk.patch(lines)
  end

  if lines.size > 0
    lines * "\n" + "\n"
  else
    ''
  end
rescue Exception => e
  Tty.error e.message
end