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.



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

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.



10
11
12
# File 'lib/rpatch/entry.rb', line 10

def level
  @level
end

Instance Method Details

#feed_line(line) ⇒ Object



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

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



23
24
25
# File 'lib/rpatch/entry.rb', line 23

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

#oldfileObject



19
20
21
# File 'lib/rpatch/entry.rb', line 19

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

#patch_on_directory(inputdir, outputdir = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/rpatch/entry.rb', line 117

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

#patch_on_file(input, output = nil) ⇒ Object



55
56
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
# File 'lib/rpatch/entry.rb', line 55

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
  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
      STDERR.puts "#{filename}: #{e.message}"
    rescue Exception => e
      STDERR.puts "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
    STDERR.puts "#{filename}: nothing changed"
    if input != output
      File.open(output, "w") do |io|
        io.write lines * "\n" + "\n"
      end
    end
  else
    unless patch_status
      STDERR.puts "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
      STDERR.puts "Patched \"#{output}\"."
      File.open(output, "w") do |io|
        io.write lines * "\n" + "\n"
      end
    else
      STDERR.puts "Remove \"#{output}\"."
      File.unlink output
    end
  end
  return patch_status
end

#patch_on_message(text) ⇒ Object

Used in test cases.



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

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
  STDERR.puts "Error: #{e.message}"
end