File: viewdiff.rb

package info (click to toggle)
docdiff 0.5.0%2Bgit20160313-1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 896 kB
  • ctags: 722
  • sloc: ruby: 14,138; makefile: 98; lisp: 33; sh: 26
file content (378 lines) | stat: -rw-r--r-- 10,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/ruby
# 2005-08-29..xx-xx-xx Hisashi Morita

require 'docdiff/difference'
require 'docdiff/document'
require 'docdiff/view'
require 'docdiff/charstring'

require "tempfile"

# $KCODE="e"

class String
  def to_lines
    scan(Regexp.new("(?:.*(?:\r\n|\r|\n|\z))", Regexp::MULTILINE))
  end
end

class DocDiff
def scan_text_for_diffs(src)
  eol = "(?:\r\n|\n|\r)"
  pats = {
    :classic => "(?:[0-9]+(?:,[0-9]+)?[dac][0-9]+(?:,[0-9]+)?#{eol}.+?(?=^[^-<>0-9 ]))",
    :context => "(?:^\\*{3} .+?#{eol}--- .+?#{eol}.+?(?=^[^-+! *]|\\z))",
    :unified => "(?:^--- .+?#{eol}^\\+{3} .+?#{eol}.+?(?=^[^-+ @]|\\z))"
  }
  src.scan(/(?:#{pats.values.join("|")})|(?:.*?#{eol}+)/m)
end

class DiffFile < Array

  def initialize(src)
    src.extend(CharString)
    src.encoding = CharString.guess_encoding(src)
    src.eol = CharString.guess_eol(src)
    @src = src
  end
  attr_accessor :src

  def guess_diff_type(text)
    case
    when (/^[<>] /m).match(text)  then return "classic"
    when (/^[-+!] /m).match(text) then return "context"
    when (/^[-+]/m).match(text)   then return "unified"
    else                               return "unknown"
    end
  end

  def anatomize
    case guess_diff_type(@src)
    when "classic" then return anatomize_classic(@src)
    when "context" then return anatomize_context(@src)
    when "unified" then return anatomize_unified(@src)
    else
      raise "unsupported diff format: \n#{src.inspect}"
    end
  end

end

module ClassicDiff
  def eol
    "(?:\r\n|\n|\r)"
  end
  def noeol
    "(?:[^\r\n])"
  end
  def hunk_header
    "(?:[0-9]+(?:,[0-9]+)?[dac][0-9]+(?:,[0-9]+)?#{eol})"
  end
  def del
    "(?:^< ?#{noeol}*?#{eol})"
  end
  def sep
    "(?:^---#{eol})"
  end
  def add
    "(?:^> ?#{noeol}*?#{eol})"
  end
  def change
    "(?:#{del}+#{sep}#{add}+)"
  end
  def misc
    "(?:.*?(?:#{eol}|\z))"
  end
  def hunk
    "(?:#{hunk_header}(?:#{change}|#{del}+|#{add}+))"
  end
  def elements
    "(?:#{hunk}|#{misc})"
  end
end

def anatomize_classic(src)
  self.extend ClassicDiff
  diffed = []
  src_encoding = CharString.guess_encoding(src)
  src_eol = CharString.guess_eol(src)
  src.scan(Regexp.new(elements, Regexp::MULTILINE)){|m|
    case
    when /\A[0-9]/.match(m) then # hunk
      diffed.concat(anatomize_classic_hunk(m, src_encoding, src_eol))
    else # not hunk
      diffed.concat(Difference.new(m.split(/^/), m.split(/^/)))
    end
  }
  return diffed
end

def anatomize_classic_hunk(a_hunk, src_encoding, src_eol)
  self.extend ClassicDiff
  diffed = []
  a_hunk.scan(/(#{hunk_header})(#{change}|#{del}+|#{add}+)/){|n|
    head, body = [$1, $2].collect{|e|
      e.extend(CharString)
      e.encoding, e.eol = src_encoding, src_eol
      e
    }
    diffed.concat(Difference.new(head.to_words, head.to_words))
    case
    when /d/.match(head) # del
      diffed.concat(Difference.new(body.to_words, []))
    when /a/.match(head) # add
      diffed.concat(Difference.new([], body.to_words))
    when /c/.match(head) # change (need tweak)
      former, latter = body.split(/#{sep}/).collect{|e|
        e.extend(CharString)
        e.encoding, e.eol = src_encoding, src_eol
        e
      }
      d = Difference.new(former.to_words, latter.to_words)
      diffed_former = d.former_only
      diffed_latter = d.latter_only
      sepstr = /#{sep}/.match(body).to_s.extend(CharString)
      sepstr.encoding, sepstr.eol = src_encoding, src_eol
      sepelm = Difference.new(sepstr.to_words, sepstr.to_words)
      diffed.concat(diffed_former + sepelm + diffed_latter)
    else
      raise "invalid hunk header: #{head}"
    end
  }
  return diffed
end

module ContextDiff
  def eol
    "(?:\r\n|\n|\r|\\z)"
  end
  def noneol
    "(?:[^\r\n])"
  end
  def hunk_header
    "(?:\\*+#{eol})"
  end
  def hunk_subheader_former
    "(?:^\\*+ [0-9]+,[0-9]+ \\*+#{eol})"
  end
  def hunk_subheader_latter
    "(?:^-+ [0-9]+,[0-9]+ -+#{eol})"
  end
  def del
    "(?:^- #{noneol}*?#{eol})"
  end
  def add
    "(?:^\\+ #{noneol}*?#{eol})"
  end
  def change
    "(?:^! #{noneol}*?#{eol})"
  end
  def misc
    "(?:^[^-+!*]+?#{eol}+?)"
  end
  def any
    "(?:#{del}+|#{add}+|#{change}+|#{misc}+)"
  end
  def file_header
    "(?:[-\\*]{3} #{noneol}+?#{eol})"
  end
  def elements
    "(?:#{file_header}|#{hunk_header}#{hunk_subheader_former}#{any}*?#{hunk_subheader_latter}#{any}+|#{misc}|#{noneol}+#{eol})"
  end
end

def anatomize_context(src)
  self.extend ContextDiff
  diffed = []
  src_encoding = CharString.guess_encoding(src)
  src_eol = CharString.guess_eol(src)
  src.scan(/#{elements}/m){|m|
    case
    when /\A\*{10,}#{eol}^\*{3} /.match(m) then # hunk
      diffed.concat(anatomize_context_hunk(m, src_encoding, src_eol))
    else # not hunk
      m.extend(CharString)
      m.encoding, m.eol = src_encoding, src_eol
      diffed.concat(Difference.new(m.to_words, m.to_words))
    end
  }
  return diffed
end

def anatomize_context_hunk(a_hunk, src_encoding, src_eol)
  self.extend ContextDiff
  diffed = []
  h, sh_f, body_f, sh_l, body_l = nil
  a_hunk.scan(/(#{hunk_header})(#{hunk_subheader_former})(.*?)(#{hunk_subheader_latter})(.*?)\z/m){|m|
    h, sh_f, body_f, sh_l, body_l = m[0..4].collect{|he|
      if he
        he.extend(CharString)
        he.encoding, he.eol = src_encoding, src_eol
      end
      he
    }
  }
  diffed_former, diffed_latter = anatomize_context_hunk_scanbodies(body_f, body_l, src_encoding, src_eol)
  diffed.concat(Difference.new(h.to_words, h.to_words) +
                Difference.new(sh_f.to_words, sh_f.to_words) +
                diffed_former +
                Difference.new(sh_l.to_words, sh_l.to_words) +
                diffed_latter)
  return diffed
end

def anatomize_context_hunk_scanbodies(body_f, body_l, src_encoding, src_eol)
  body_f = '' if body_f.nil?
  body_l = '' if body_l.nil?
  self.extend ContextDiff
  changes_org = [[], []]
  changes_org[0], changes_org[1] = [body_f, body_l].collect{|b|
    b.scan(/#{change}+/).collect{|ch|
      if ch
        ch.extend(CharString)
        ch.encoding, ch.eol = src_encoding, src_eol
      end
      ch
    }
  }
  changes = changes_org.dup
  diffed = [[], []]
  [body_f, body_l].each_with_index{|half, i|
    changes[0], changes[1] = changes_org[0].dup, changes_org[1].dup
    half.scan(/(#{del}+)|(#{add}+)|(#{change}+)|(#{misc}+)/m){|elm|
      elm_d, elm_a, elm_c, elm_cmn = elm[0..3]
      [elm_d, elm_a, elm_c, elm_cmn].collect{|e|
        if e
          e.extend(CharString)
          e.encoding, e.eol = src_encoding, src_eol
        end
        e
      }
      case
      when elm_d then d = Difference.new(elm_d.to_words, [])
      when elm_a then d = Difference.new([], elm_a.to_words)
      when elm_c then d = Difference.new(changes[0].shift.to_words, changes[1].shift.to_words)
        case i
        when 0 then d = d.former_only
        when 1 then d = d.latter_only
        else raise
        end
      when elm_cmn then d = Difference.new(elm_cmn.to_words, elm_cmn.to_words)
      else
        raise "bummers!"
      end
      diffed[i].concat(d)
    } # end half.scan
  } # end each_with_index
  return diffed
end

module UnifiedDiff
  def eol
    "(?:\r\n|\n|\r|\z)"
  end
  def noneol
    "(?:[^\r\n])"
  end
  def hunk_header
    "(?:@@ #{noneol}+#{eol})"
  end
  def del
    "(?:^-#{noneol}*?#{eol})"
  end
  def add
    "(?:^\\+#{noneol}*?#{eol})"
  end
  def change
    "(?:#{del}+#{add}+)"
  end
  def common
    "(?:^ #{noneol}*?#{eol})"
  end
  def misc
    "(?:^[^-+]+?#{eol}+?)"
  end
  def any
    "(?:#{del}+|#{add}+|#{change}+|#{common}+|#{misc}+)"
  end
  def file_header
    "(?:^[^-+@ ]#{noneol}+#{eol}(?:^[-+]{3} #{noneol}+#{eol}){2})"
  end
  def elements
    "(?:#{file_header}|#{hunk_header}#{any}+?|#{misc}|#{noneol}+#{eol})"
  end
end

def anatomize_unified(src)
  self.extend UnifiedDiff
  diffed = []
  src_encoding = CharString.guess_encoding(src)
  src_eol = CharString.guess_eol(src)
  src.scan(/#{elements}/m){|m|
    case
    when /\A@@ /.match(m) then # hunk
      diffed.concat(anatomize_unified_hunk(m.to_s, src_encoding, src_eol))
    else # not hunk
      m.extend(CharString)
      m.encoding, m.eol = src_encoding, src_eol
      diffed.concat(Difference.new(m.to_words, m.to_words))
    end
  }
  return diffed
end

def anatomize_unified_hunk(a_hunk, src_encoding, src_eol)
  self.extend UnifiedDiff
  diffed = []
  a_hunk.scan(/(#{hunk_header})(#{any}+#{eol}?)/m){|m|
    head, body = m[0], m[1]
    [head, body].collect{|e|
      e.extend(CharString)
      e.encoding, e.eol = src_encoding, src_eol
    }
    diffed.concat(Difference.new(head.to_words, head.to_words))
    body.scan(/(#{del}+)(#{add}+)|(#{del}+#{eol}?)|(#{add}+)|(#{common}+#{eol}?)|(.*#{eol}?)/m){|m|
      cf, cl, d, a, cmn, msc = m[0..5]
      [cf, cl, d, a, cmn, msc].collect{|e|
        e.extend(CharString)
        e.encoding, e.eol = src_encoding, src_eol
      }
      case
      when (cf and cl) then
        Difference.new(cf.to_words, cl.to_words).each{|e|
          case e.first
          when :change_elt     then diffed << [:change_elt, e[1], nil]
                                    diffed << [:change_elt, nil, e[2]]
          when :del_elt        then diffed << [:change_elt, e[1], nil]
          when :add_elt        then diffed << [:change_elt, nil, e[2]]
          when :common_elt_elt then diffed << e
          else raise "bummers! (#{e.inspect})"
          end
        }
      when d           then diffed.concat(Difference.new(d.to_words, []))
      when a           then diffed.concat(Difference.new([], a.to_words))
      when cmn         then diffed.concat(Difference.new(cmn.to_words, cmn.to_words))
      when msc         then diffed.concat(Difference.new(msc.to_words, msc.to_words))
      else raise "bummers! (#{m.inspect})"
      end
    }
  }
  return diffed
end
end  # class DocDiff

if $0 == __FILE__

  src = ARGF.read
  enc, eol = DocDiff::CharString.guess_encoding(src),
             DocDiff::CharString.guess_eol(src)
  DocDiff.new.scan_text_for_diffs(src).each{|fragment|
    if DocDiff::DiffFile.new('').guess_diff_type(fragment) == "unknown"
      print fragment
    else
      diff = DocDiff::DiffFile.new(fragment).anatomize
      print DocDiff::View.new(diff, enc, eol).to_tty
    end
  }

end