Class: Olelo::PatchParser

Inherits:
Object show all
Includes:
Util
Defined in:
lib/olelo/patch.rb

Defined Under Namespace

Classes: Adapter, ChangeHandler, Debugger, Handler

Class Method Summary collapse

Methods included from Util

#check, #decode64, #deep_copy, #encode64, #escape, #escape_html, #escape_javascript, included, #md5, #no_cache?, #sha256, #titlecase, #truncate, #unescape, #unescape_backslash, #unescape_html, #valid_xml_chars?

Class Method Details

.parse(patch, *h) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/olelo/patch.rb', line 115

def self.parse(patch, *h)
  h = h.size == 1 ? h[0] : Adapter.new(h)
  src = dst = nil
  state = :start
  h.initialize!
  patch.split("\n").each do |line|
    case state
    when :start
      case line
      when %r{^diff.* ("?a/.*"?) ("?b/.*"?)$}
        src, dst = unescape_path($1), unescape_path($2)
        state = :header
      when /^\+\+\+ (.*)/
        dst = unescape_path($1)
        state = :header
      when /^\-\-\- (.*)/
        src = unescape_path($1)
        state = :header
      end
    when :header
      case line
      when %r{^diff.* ("?a/.*"?) ("?b/.*"?)$}
        a, b = $1, $2
        h.begin!(src, dst)
        dst ? h.no_changes! : h.deleted!
        h.end!
        src, dst = unescape_path(a), unescape_path(b)
      when /^\+\+\+ (.*)/
        dst = unescape_path($1)
      when /^\-\-\- (.*)/
        src = unescape_path($1)
      when /^deleted file/
        dst = nil
      when /^Binary files (.*) and (.*) differ/
        src, dst = unescape_path($1), unescape_path($2)
        h.begin!(src, dst)
        h.binary!
        h.end!
        state = :start
      when /^@/
        state = :body
        h.begin!(src, dst)
      end
    when :body
      case line
      when %r{^diff.* ("?a/.*"?) ("?b/.*"?)$}
        src, dst = unescape_path($1), unescape_path($2)
        h.end!
        state = :header
      else
        h.line!(line)
      end
    end
  end
  case state
  when :header
    h.begin!(src, dst)
    dst ? h.no_changes! : h.deleted!
    h.end!
  when :body
    h.end!
  end
  h.finalize!
  h
end

.unescape_path(path) ⇒ Object



181
182
183
184
# File 'lib/olelo/patch.rb', line 181

def self.unescape_path(path)
  path = unescape_backslash(path[1..-2]) if path.starts_with? '"'
  path == '/dev/null' ? nil : path[2..-1]
end