2010-12-15 Ojan Vafai <ojan@chromium.org>
[WebKit-https.git] / Websites / bugs.webkit.org / PrettyPatch / PrettyPatch.rb
1 require 'cgi'
2 require 'diff'
3 require 'open3'
4 require 'pp'
5 require 'set'
6 require 'tempfile'
7
8 module PrettyPatch
9
10 public
11
12     GIT_PATH = "git"
13
14     def self.prettify(string)
15         fileDiffs = FileDiff.parse(string)
16
17         str = HEADER + "\n"
18         str += fileDiffs.collect{ |diff| diff.to_html }.join
19     end
20
21     def self.filename_from_diff_header(line)
22         DIFF_HEADER_FORMATS.each do |format|
23             match = format.match(line)
24             return match[1] unless match.nil?
25         end
26         nil
27     end
28
29     def self.diff_header?(line)
30         RELAXED_DIFF_HEADER_FORMATS.any? { |format| line =~ format }
31     end
32
33 private
34     DIFF_HEADER_FORMATS = [
35         /^Index: (.*)\r?$/,
36         /^diff --git "?a\/.+"? "?b\/(.+)"?\r?$/,
37         /^\+\+\+ ([^\t]+)(\t.*)?\r?$/
38     ]
39
40     RELAXED_DIFF_HEADER_FORMATS = [
41         /^Index:/,
42         /^diff/
43     ]
44
45     BINARY_FILE_MARKER_FORMAT = /^Cannot display: file marked as a binary type.$/
46
47     IMAGE_FILE_MARKER_FORMAT = /^svn:mime-type = image\/png$/
48
49     GIT_INDEX_MARKER_FORMAT = /^index ([0-9a-f]{40})\.\.([0-9a-f]{40})/
50
51     GIT_BINARY_FILE_MARKER_FORMAT = /^GIT binary patch$/
52
53     GIT_LITERAL_FORMAT = /^literal \d+$/
54
55     START_OF_BINARY_DATA_FORMAT = /^[0-9a-zA-Z\+\/=]{20,}/ # Assume 20 chars without a space is base64 binary data.
56
57     START_OF_SECTION_FORMAT = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@\s*(.*)/
58
59     START_OF_EXTENT_STRING = "%c" % 0
60     END_OF_EXTENT_STRING = "%c" % 1
61
62     SMALLEST_EQUAL_OPERATION = 3
63
64     OPENSOURCE_TRAC_URL = "http://trac.webkit.org/"
65
66     OPENSOURCE_DIRS = Set.new %w[
67         Examples
68         LayoutTests
69         PerformanceTests
70         Source
71         Tools
72         WebCore
73         WebKit
74         WebKit2
75         WebKitLibraries
76         Websites
77     ]
78
79     def self.find_url_and_path(file_path)
80         # Search file_path from the bottom up, at each level checking whether
81         # we've found a directory we know exists in the source tree.
82
83         dirname, basename = File.split(file_path)
84         dirname.split(/\//).reverse.inject(basename) do |path, directory|
85             path = directory + "/" + path
86
87             return [OPENSOURCE_TRAC_URL, path] if OPENSOURCE_DIRS.include?(directory)
88
89             path
90         end
91
92         [nil, file_path]
93     end
94
95     def self.linkifyFilename(filename)
96         url, pathBeneathTrunk = find_url_and_path(filename)
97
98         url.nil? ? filename : "<a href='#{url}browser/trunk/#{pathBeneathTrunk}'>#{filename}</a>"
99     end
100
101
102     HEADER =<<EOF
103 <style>
104 :link, :visited {
105     text-decoration: none;
106     border-bottom: 1px dotted;
107 }
108
109 :link {
110     color: #039;
111 }
112
113 .FileDiff {
114     background-color: #f8f8f8;
115     border: 1px solid #ddd;
116     font-family: monospace;
117     margin: 1em 0;
118 }
119
120 h1 {
121     color: #333;
122     font-family: sans-serif;
123     font-size: 1em;
124     margin-left: 0.5em;
125 }
126
127 h1 :link, h1 :visited {
128     color: inherit;
129 }
130
131 h1 :hover {
132     color: #555;
133     background-color: #eee;
134 }
135
136 .DiffSection {
137     background-color: white;
138     border: solid #ddd;
139     border-width: 1px 0px;
140 }
141
142 .lineNumber, .expansionLineNumber {
143     border-bottom: 1px solid #998;
144     border-right: 1px solid #ddd;
145     color: #444;
146     display: inline-block;
147     padding: 1px 5px 0px 0px;
148     text-align: right;
149     vertical-align: bottom;
150     width: 3em;
151 }
152
153 .lineNumber {
154   background-color: #eed;
155 }
156
157 .expansionLineNumber {
158   background-color: #eee;
159 }
160
161 .text {
162     padding-left: 5px;
163     white-space: pre;
164     white-space: pre-wrap;
165 }
166
167 .image {
168     border: 2px solid black;
169 }
170
171 .context, .context .lineNumber {
172     color: #849;
173     background-color: #fef;
174 }
175
176 .add {
177     background-color: #dfd;
178 }
179
180 .add ins {
181     background-color: #9e9;
182     text-decoration: none;
183 }
184
185 .remove {
186     background-color: #fdd;
187 }
188
189 .remove del {
190     background-color: #e99;
191     text-decoration: none;
192 }
193
194 /* Support for inline comments */
195
196 .author {
197   font-style: italic;
198 }
199
200 .comment {
201   position: relative;
202 }
203
204 .comment textarea {
205   height: 6em;
206 }
207
208 .overallComments textarea {
209   height: 2em;
210 }
211
212 .comment textarea, .overallComments textarea {
213   display: block;
214   width: 100%;
215 }
216
217 .overallComments .open {
218   -webkit-transition: height .2s;
219   height: 4em;
220 }
221
222 #statusBubbleContainer.wrap {
223   display: block;
224 }
225
226 body {
227   margin-bottom: 40px;
228 }
229
230 #toolbar {
231   display: -webkit-box;
232   display: -moz-box;
233   padding: 3px;
234   bottom: 0;
235   left: 0;
236   right: 0;
237   border: 1px solid #ddd;
238   background-color: #eee;
239   font-family: sans-serif;
240 }
241
242 #toolbar.anchored {
243   position: fixed;
244 }
245
246 #toolbar .actions {
247   float: right;
248 }
249
250 .winter {
251   position: fixed;
252   z-index: 5;
253   left: 0;
254   right: 0;
255   top: 0;
256   bottom: 0;
257   background-color: black;
258   opacity: 0.8;
259 }
260
261 .inactive {
262   display: none;
263 }
264
265 .lightbox {
266   position: fixed;
267   z-index: 6;
268   left: 10%;
269   right: 10%;
270   top: 10%;
271   bottom: 10%;
272 }
273
274 .lightbox iframe {
275   width: 100%;
276   height: 100%;
277 }
278
279 .commentContext .lineNumber {
280   background-color: yellow;
281 }
282
283 .selected .lineNumber {
284   background-color: #69F;
285   border-bottom-color: #69F;
286   border-right-color: #69F;
287 }
288
289 .ExpandArea {
290   margin: 0;
291 }
292
293 .ExpandText {
294   margin-left: 0.67em;
295 }
296
297 .ExpandLinkContainer a {
298   border: 0;
299 }
300
301 .ExpandLinkContainer a:after {
302   content: " | ";
303   color: black;
304 }
305
306 .ExpandLinkContainer a:last-of-type:after {
307   content: "";
308 }
309
310 .help {
311  color: gray;
312  font-style: italic;
313 }
314
315 #message {
316   font-size: small;
317   font-family: sans-serif;
318 }
319
320 .commentStatus {
321   font-style: italic;
322 }
323
324 .comment, .previousComment, .frozenComment {
325   background-color: #ffd;
326 }
327
328 .overallComments {
329   -webkit-box-flex: 1;
330   -moz-box-flex: 1;
331   margin-right: 3px;
332 }
333
334 .previousComment, .frozenComment {
335   border: inset 1px;
336   padding: 5px;
337   white-space: pre-wrap;
338 }
339
340 .comment button {
341   width: 6em;
342 }
343
344 .focused {
345   border: 1px solid blue;
346 }
347
348 .statusBubble {
349   /* The width/height get set to the bubble contents via postMessage on browsers that support it. */
350   width: 450px;
351   height: 20px;
352   margin: 2px 2px 0 0;
353   border: none;
354   vertical-align: middle;
355 }
356
357 .pseudo_resize_event_iframe {
358   height: 10%;
359   width: 10%;
360   position: absolute;
361   top: -11%;
362 }
363 </style>
364 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
365 <script src="code-review.js?version=17"></script>
366 EOF
367
368     def self.revisionOrDescription(string)
369         case string
370         when /\(revision \d+\)/
371             /\(revision (\d+)\)/.match(string)[1]
372         when /\(.*\)/
373             /\((.*)\)/.match(string)[1]
374         end
375     end
376
377     def self.has_image_suffix(filename)
378         filename =~ /\.(png|jpg|gif)$/
379     end
380
381     class FileDiff
382         def initialize(lines)
383             @filename = PrettyPatch.filename_from_diff_header(lines[0].chomp)
384             startOfSections = 1
385             for i in 0...lines.length
386                 case lines[i]
387                 when /^--- /
388                     @from = PrettyPatch.revisionOrDescription(lines[i])
389                 when /^\+\+\+ /
390                     @filename = PrettyPatch.filename_from_diff_header(lines[i].chomp) if @filename.nil?
391                     @to = PrettyPatch.revisionOrDescription(lines[i])
392                     startOfSections = i + 1
393                     break
394                 when BINARY_FILE_MARKER_FORMAT
395                     @binary = true
396                     if (IMAGE_FILE_MARKER_FORMAT.match(lines[i + 1]) or PrettyPatch.has_image_suffix(@filename)) then
397                         @image = true
398                         startOfSections = i + 2
399                         for x in startOfSections...lines.length
400                             # Binary diffs often have property changes listed before the actual binary data.  Skip them.
401                             if START_OF_BINARY_DATA_FORMAT.match(lines[x]) then
402                                 startOfSections = x
403                                 break
404                             end
405                         end
406                     end
407                     break
408                 when GIT_INDEX_MARKER_FORMAT
409                     @git_indexes = [$1, $2]
410                 when GIT_BINARY_FILE_MARKER_FORMAT
411                     @binary = true
412                     if (GIT_LITERAL_FORMAT.match(lines[i + 1]) and PrettyPatch.has_image_suffix(@filename)) then
413                         @git_image = true
414                         startOfSections = i + 1
415                     end
416                     break
417                 end
418             end
419             lines_with_contents = lines[startOfSections...lines.length]
420             @sections = DiffSection.parse(lines_with_contents) unless @binary
421             if @image
422                 @image_url = "data:image/png;base64," + lines_with_contents.join
423             elsif @git_image
424                 begin
425                     raise "index line is missing" unless @git_indexes
426
427                     chunks = nil
428                     for i in 0...lines_with_contents.length
429                         if lines_with_contents[i] =~ /^$/
430                             chunks = [lines_with_contents[i + 1 .. -1], lines_with_contents[0 .. i]]
431                             break
432                         end
433                     end
434
435                     raise "no binary chunks" unless chunks
436
437                     @image_urls = chunks.zip(@git_indexes).collect do |chunk, git_index|
438                         FileDiff.extract_contents_from_git_binary_chunk(chunk, git_index)
439                     end
440                 rescue
441                     @image_error = "Exception raised during decoding git binary patch:<pre>#{CGI.escapeHTML($!.to_s + "\n" + $!.backtrace.join("\n"))}</pre>"
442                 end
443             end
444             nil
445         end
446
447         def to_html
448             str = "<div class='FileDiff'>\n"
449             str += "<h1>#{PrettyPatch.linkifyFilename(@filename)}</h1>\n"
450             if @image then
451                 str += "<img class='image' src='" + @image_url + "' />"
452             elsif @git_image then
453                 if @image_error
454                     str += @image_error
455                 else
456                     for i in (0...2)
457                         image_url = @image_urls[i]
458                         style = ["remove", "add"][i]
459                         str += "<p class=\"#{style}\">"
460                         if image_url
461                             str += "<img class='image' src='" + image_url + "' />"
462                         else
463                             str += ["Added", "Removed"][i]
464                         end
465                     end
466                 end
467             elsif @binary then
468                 str += "<span class='text'>Binary file, nothing to see here</span>"
469             else
470                 str += @sections.collect{ |section| section.to_html }.join("<br>\n") unless @sections.nil?
471             end
472             str += "</div>\n"
473         end
474
475         def self.parse(string)
476             haveSeenDiffHeader = false
477             linesForDiffs = []
478             string.each_line do |line|
479                 if (PrettyPatch.diff_header?(line))
480                     linesForDiffs << []
481                     haveSeenDiffHeader = true
482                 elsif (!haveSeenDiffHeader && line =~ /^--- /)
483                     linesForDiffs << []
484                     haveSeenDiffHeader = false
485                 end
486                 linesForDiffs.last << line unless linesForDiffs.last.nil?
487             end
488
489             linesForDiffs.collect { |lines| FileDiff.new(lines) }
490         end
491
492         def self.git_new_file_binary_patch(filename, encoded_chunk, git_index)
493             return <<END
494 diff --git a/#{filename} b/#{filename}
495 new file mode 100644
496 index 0000000000000000000000000000000000000000..#{git_index}
497 GIT binary patch
498 #{encoded_chunk.join("")}literal 0
499 HcmV?d00001
500
501 END
502         end
503
504         def self.extract_contents_from_git_binary_chunk(encoded_chunk, git_index)
505             # We use Tempfile we need a unique file among processes.
506             tempfile = Tempfile.new("PrettyPatch")
507             # We need a filename which doesn't exist to apply a patch
508             # which creates a new file. Append a suffix so filename
509             # doesn't exist.
510             filepath = tempfile.path + '.bin'
511             filename = File.basename(filepath)
512
513             patch = FileDiff.git_new_file_binary_patch(filename, encoded_chunk, git_index)
514
515             # Apply the git binary patch using git-apply.
516             cmd = GIT_PATH + " apply --directory=" + File.dirname(filepath)
517             stdin, stdout, stderr = *Open3.popen3(cmd)
518             begin
519                 stdin.puts(patch)
520                 stdin.close
521
522                 error = stderr.read
523                 raise error if error != ""
524
525                 contents = File.read(filepath)
526             ensure
527                 stdin.close unless stdin.closed?
528                 stdout.close
529                 stderr.close
530                 File.unlink(filename) if File.exists?(filename)
531             end
532
533             return nil if contents.empty?
534             return "data:image/png;base64," + [contents].pack("m")
535         end
536     end
537
538     class DiffSection
539         def initialize(lines)
540             lines.length >= 1 or raise "DiffSection.parse only received %d lines" % lines.length
541
542             matches = START_OF_SECTION_FORMAT.match(lines[0])
543             from, to = [matches[1].to_i, matches[2].to_i] unless matches.nil?
544
545             @lines = lines[1...lines.length].collect do |line|
546                 startOfLine = line =~ /^[-\+ ]/ ? 1 : 0
547                 text = line[startOfLine...line.length].chomp
548                 case line[0]
549                 when ?-
550                     result = CodeLine.new(from, nil, text)
551                     from += 1 unless from.nil?
552                     result
553                 when ?+
554                     result = CodeLine.new(nil, to, text)
555                     to += 1 unless to.nil?
556                     result
557                 else
558                     result = CodeLine.new(from, to, text)
559                     from += 1 unless from.nil?
560                     to += 1 unless to.nil?
561                     result
562                 end
563             end
564
565             @lines.unshift(ContextLine.new(matches[3])) unless matches.nil? || matches[3].empty?
566
567             changes = [ [ [], [] ] ]
568             for line in @lines
569                 if (!line.fromLineNumber.nil? and !line.toLineNumber.nil?) then
570                     changes << [ [], [] ]
571                     next
572                 end
573                 changes.last.first << line if line.toLineNumber.nil?
574                 changes.last.last << line if line.fromLineNumber.nil?
575             end
576
577             for change in changes
578                 next unless change.first.length == change.last.length
579                 for i in (0...change.first.length)
580                     raw_operations = HTMLDiff::DiffBuilder.new(change.first[i].text, change.last[i].text).operations
581                     operations = []
582                     back = 0
583                     raw_operations.each_with_index do |operation, j|
584                         if operation.action == :equal and j < raw_operations.length - 1
585                            length = operation.end_in_new - operation.start_in_new
586                            if length < SMALLEST_EQUAL_OPERATION
587                                back = length
588                                next
589                            end
590                         end
591                         operation.start_in_old -= back
592                         operation.start_in_new -= back
593                         back = 0
594                         operations << operation
595                     end
596                     change.first[i].operations = operations
597                     change.last[i].operations = operations
598                 end
599             end
600         end
601
602         def to_html
603             str = "<div class='DiffSection'>\n"
604             str += @lines.collect{ |line| line.to_html }.join
605             str += "</div>\n"
606         end
607         
608         def self.parse(lines)
609             linesForSections = lines.inject([[]]) do |sections, line|
610                 sections << [] if line =~ /^@@/
611                 sections.last << line
612                 sections
613             end
614
615             linesForSections.delete_if { |lines| lines.nil? or lines.empty? }
616             linesForSections.collect { |lines| DiffSection.new(lines) }
617         end
618     end
619
620     class Line
621         attr_reader :fromLineNumber
622         attr_reader :toLineNumber
623         attr_reader :text
624
625         def initialize(from, to, text)
626             @fromLineNumber = from
627             @toLineNumber = to
628             @text = text
629         end
630
631         def text_as_html
632             CGI.escapeHTML(text)
633         end
634
635         def classes
636             lineClasses = ["Line"]
637             lineClasses << ["add"] unless @toLineNumber.nil? or !@fromLineNumber.nil?
638             lineClasses << ["remove"] unless @fromLineNumber.nil? or !@toLineNumber.nil?
639             lineClasses
640         end
641
642         def to_html
643             markedUpText = self.text_as_html
644             str = "<div class='%s'>\n" % self.classes.join(' ')
645             str += "<span class='from lineNumber'>%s</span><span class='to lineNumber'>%s</span>\n" %
646                    [@fromLineNumber.nil? ? '&nbsp;' : @fromLineNumber,
647                     @toLineNumber.nil? ? '&nbsp;' : @toLineNumber] unless @fromLineNumber.nil? and @toLineNumber.nil?
648             str += "<span class='text'>%s</span>\n" % markedUpText
649             str += "</div>\n"
650         end
651     end
652
653     class CodeLine < Line
654         attr :operations, true
655
656         def text_as_html
657             html = []
658             tag = @fromLineNumber.nil? ? "ins" : "del"
659             if @operations.nil? or @operations.empty?
660                 return CGI.escapeHTML(@text)
661             end
662             @operations.each do |operation|
663                 start = @fromLineNumber.nil? ? operation.start_in_new : operation.start_in_old
664                 eend = @fromLineNumber.nil? ? operation.end_in_new : operation.end_in_old
665                 escaped_text = CGI.escapeHTML(@text[start...eend])
666                 if eend - start === 0 or operation.action === :equal
667                     html << escaped_text
668                 else
669                     html << "<#{tag}>#{escaped_text}</#{tag}>"
670                 end
671             end
672             html.join
673         end
674     end
675
676     class ContextLine < Line
677         def initialize(context)
678             super("@", "@", context)
679         end
680
681         def classes
682             super << "context"
683         end
684     end
685 end