2011-01-19 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         WebKit
73         WebKitLibraries
74         Websites
75     ]
76
77     def self.find_url_and_path(file_path)
78         # Search file_path from the bottom up, at each level checking whether
79         # we've found a directory we know exists in the source tree.
80
81         dirname, basename = File.split(file_path)
82         dirname.split(/\//).reverse.inject(basename) do |path, directory|
83             path = directory + "/" + path
84
85             return [OPENSOURCE_TRAC_URL, path] if OPENSOURCE_DIRS.include?(directory)
86
87             path
88         end
89
90         [nil, file_path]
91     end
92
93     def self.linkifyFilename(filename)
94         url, pathBeneathTrunk = find_url_and_path(filename)
95
96         url.nil? ? filename : "<a href='#{url}browser/trunk/#{pathBeneathTrunk}'>#{filename}</a>"
97     end
98
99
100     HEADER =<<EOF
101 <style>
102 :link, :visited {
103     text-decoration: none;
104     border-bottom: 1px dotted;
105 }
106
107 :link {
108     color: #039;
109 }
110
111 .FileDiff {
112     background-color: #f8f8f8;
113     border: 1px solid #ddd;
114     font-family: monospace;
115     margin: 1em 0;
116 }
117
118 h1 {
119     color: #333;
120     font-family: sans-serif;
121     font-size: 1em;
122     margin-left: 0.5em;
123     display: table-cell;
124     width: 100%;
125     padding: 0.5em;
126 }
127
128 h1 :link, h1 :visited {
129     color: inherit;
130 }
131
132 h1 :hover {
133     color: #555;
134     background-color: #eee;
135 }
136
137 .DiffLinks {
138     float: right;
139 }
140
141 .FileDiffLinkContainer {
142     opacity: 0;
143     display: table-cell;
144     padding-right: 0.5em;
145     white-space: nowrap;
146 }
147
148 .DiffSection {
149     background-color: white;
150     border: solid #ddd;
151     border-width: 1px 0px;
152 }
153
154 .ExpansionLine, .LineContainer {
155     white-space: nowrap;
156 }
157
158 .sidebyside .DiffBlockPart.add:first-child {
159     float: right;
160 }
161
162 .LineSide,
163 .sidebyside .DiffBlockPart.remove,
164 .sidebyside .DiffBlockPart.add {
165     display:inline-block;
166     width:50%;
167     vertical-align: top;
168 }
169
170 .sidebyside .DiffBlockPart.remove .to,
171 .sidebyside .DiffBlockPart.add .from {
172     display: none;
173 }
174
175 .lineNumber, .expansionLineNumber {
176     border-bottom: 1px solid #998;
177     border-right: 1px solid #ddd;
178     color: #444;
179     display: inline-block;
180     padding: 1px 5px 0px 0px;
181     text-align: right;
182     vertical-align: bottom;
183     width: 3em;
184 }
185
186 .lineNumber {
187   background-color: #eed;
188 }
189
190 .expansionLineNumber {
191   background-color: #eee;
192 }
193
194 .text {
195     padding-left: 5px;
196     white-space: pre-wrap;
197     word-wrap: break-word;
198 }
199
200 .image {
201     border: 2px solid black;
202 }
203
204 .context, .context .lineNumber {
205     color: #849;
206     background-color: #fef;
207 }
208
209 .Line.add {
210     background-color: #dfd;
211 }
212
213 .Line.add ins {
214     background-color: #9e9;
215     text-decoration: none;
216 }
217
218 .Line.remove {
219     background-color: #fdd;
220 }
221
222 .Line.remove del {
223     background-color: #e99;
224     text-decoration: none;
225 }
226
227 /* Support for inline comments */
228
229 .author {
230   font-style: italic;
231 }
232
233 .comment {
234   position: relative;
235 }
236
237 .comment textarea {
238   height: 6em;
239 }
240
241 .overallComments textarea {
242   height: 2em;
243 }
244
245 .comment textarea, .overallComments textarea {
246   display: block;
247   width: 100%;
248 }
249
250 .overallComments .open {
251   -webkit-transition: height .2s;
252   height: 4em;
253 }
254
255 #statusBubbleContainer.wrap {
256   display: block;
257 }
258
259 body {
260   margin-bottom: 40px;
261 }
262
263 #toolbar {
264   display: -webkit-box;
265   display: -moz-box;
266   padding: 3px;
267   bottom: 0;
268   left: 0;
269   right: 0;
270   border: 1px solid #ddd;
271   background-color: #eee;
272   font-family: sans-serif;
273 }
274
275 #toolbar.anchored {
276   position: fixed;
277 }
278
279 #toolbar .actions {
280   float: right;
281 }
282
283 .winter {
284   position: fixed;
285   z-index: 5;
286   left: 0;
287   right: 0;
288   top: 0;
289   bottom: 0;
290   background-color: black;
291   opacity: 0.8;
292 }
293
294 .inactive {
295   display: none;
296 }
297
298 .lightbox {
299   position: fixed;
300   z-index: 6;
301   left: 10%;
302   right: 10%;
303   top: 10%;
304   bottom: 10%;
305 }
306
307 .lightbox iframe {
308   width: 100%;
309   height: 100%;
310 }
311
312 .commentContext .lineNumber {
313   background-color: yellow;
314 }
315
316 .selected .lineNumber {
317   background-color: #69F;
318   border-bottom-color: #69F;
319   border-right-color: #69F;
320 }
321
322 .ExpandLinkContainer {
323   opacity: 0;
324   border-top: 1px solid #ddd;
325   border-bottom: 1px solid #ddd;
326 }
327
328 .ExpandArea {
329   margin: 0;
330 }
331
332 .ExpandText {
333   margin-left: 0.67em;
334 }
335
336 .LinkContainer {
337   font-family: sans-serif;
338   font-size: small;
339   font-style: normal;
340   -webkit-transition: opacity 0.5s;
341 }
342
343 .LinkContainer a {
344   border: 0;
345 }
346
347 .LinkContainer a:after {
348   content: " | ";
349   color: black;
350 }
351
352 .LinkContainer a:last-of-type:after {
353   content: "";
354 }
355
356 .help {
357  color: gray;
358  font-style: italic;
359 }
360
361 #message {
362   font-size: small;
363   font-family: sans-serif;
364 }
365
366 .commentStatus {
367   font-style: italic;
368 }
369
370 .comment, .previousComment, .frozenComment {
371   background-color: #ffd;
372 }
373
374 .overallComments {
375   -webkit-box-flex: 1;
376   -moz-box-flex: 1;
377   margin-right: 3px;
378 }
379
380 .previousComment, .frozenComment {
381   border: inset 1px;
382   padding: 5px;
383   white-space: pre-wrap;
384 }
385
386 .comment button {
387   width: 6em;
388 }
389
390 .focused {
391   outline: 1px solid blue;
392   outline-offset: -1px;
393 }
394
395 .statusBubble {
396   /* The width/height get set to the bubble contents via postMessage on browsers that support it. */
397   width: 450px;
398   height: 20px;
399   margin: 2px 2px 0 0;
400   border: none;
401   vertical-align: middle;
402 }
403
404 .pseudo_resize_event_iframe {
405   height: 10%;
406   width: 10%;
407   position: absolute;
408   top: -11%;
409 }
410
411 .clear_float {
412     clear: both;
413 }
414 </style>
415 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
416 <script src="code-review.js?version=25"></script>
417 EOF
418
419     def self.revisionOrDescription(string)
420         case string
421         when /\(revision \d+\)/
422             /\(revision (\d+)\)/.match(string)[1]
423         when /\(.*\)/
424             /\((.*)\)/.match(string)[1]
425         end
426     end
427
428     def self.has_image_suffix(filename)
429         filename =~ /\.(png|jpg|gif)$/
430     end
431
432     class FileDiff
433         def initialize(lines)
434             @filename = PrettyPatch.filename_from_diff_header(lines[0].chomp)
435             startOfSections = 1
436             for i in 0...lines.length
437                 case lines[i]
438                 when /^--- /
439                     @from = PrettyPatch.revisionOrDescription(lines[i])
440                 when /^\+\+\+ /
441                     @filename = PrettyPatch.filename_from_diff_header(lines[i].chomp) if @filename.nil?
442                     @to = PrettyPatch.revisionOrDescription(lines[i])
443                     startOfSections = i + 1
444                     break
445                 when BINARY_FILE_MARKER_FORMAT
446                     @binary = true
447                     if (IMAGE_FILE_MARKER_FORMAT.match(lines[i + 1]) or PrettyPatch.has_image_suffix(@filename)) then
448                         @image = true
449                         startOfSections = i + 2
450                         for x in startOfSections...lines.length
451                             # Binary diffs often have property changes listed before the actual binary data.  Skip them.
452                             if START_OF_BINARY_DATA_FORMAT.match(lines[x]) then
453                                 startOfSections = x
454                                 break
455                             end
456                         end
457                     end
458                     break
459                 when GIT_INDEX_MARKER_FORMAT
460                     @git_indexes = [$1, $2]
461                 when GIT_BINARY_FILE_MARKER_FORMAT
462                     @binary = true
463                     if (GIT_LITERAL_FORMAT.match(lines[i + 1]) and PrettyPatch.has_image_suffix(@filename)) then
464                         @git_image = true
465                         startOfSections = i + 1
466                     end
467                     break
468                 end
469             end
470             lines_with_contents = lines[startOfSections...lines.length]
471             @sections = DiffSection.parse(lines_with_contents) unless @binary
472             if @image
473                 @image_url = "data:image/png;base64," + lines_with_contents.join
474             elsif @git_image
475                 begin
476                     raise "index line is missing" unless @git_indexes
477
478                     chunks = nil
479                     for i in 0...lines_with_contents.length
480                         if lines_with_contents[i] =~ /^$/
481                             chunks = [lines_with_contents[i + 1 .. -1], lines_with_contents[0 .. i]]
482                             break
483                         end
484                     end
485
486                     raise "no binary chunks" unless chunks
487
488                     @image_urls = chunks.zip(@git_indexes).collect do |chunk, git_index|
489                         FileDiff.extract_contents_from_git_binary_chunk(chunk, git_index)
490                     end
491                 rescue
492                     @image_error = "Exception raised during decoding git binary patch:<pre>#{CGI.escapeHTML($!.to_s + "\n" + $!.backtrace.join("\n"))}</pre>"
493                 end
494             end
495             nil
496         end
497
498         def to_html
499             str = "<div class='FileDiff'>\n"
500             str += "<h1>#{PrettyPatch.linkifyFilename(@filename)}</h1>\n"
501             if @image then
502                 str += "<img class='image' src='" + @image_url + "' />"
503             elsif @git_image then
504                 if @image_error
505                     str += @image_error
506                 else
507                     for i in (0...2)
508                         image_url = @image_urls[i]
509                         style = ["remove", "add"][i]
510                         str += "<p class=\"#{style}\">"
511                         if image_url
512                             str += "<img class='image' src='" + image_url + "' />"
513                         else
514                             str += ["Added", "Removed"][i]
515                         end
516                     end
517                 end
518             elsif @binary then
519                 str += "<span class='text'>Binary file, nothing to see here</span>"
520             else
521                 str += @sections.collect{ |section| section.to_html }.join("<br>\n") unless @sections.nil?
522             end
523
524             if @from then
525                 str += "<span class='revision'>" + @from + "</span>"
526             end
527
528             str += "</div>\n"
529         end
530
531         def self.parse(string)
532             haveSeenDiffHeader = false
533             linesForDiffs = []
534             string.each_line do |line|
535                 if (PrettyPatch.diff_header?(line))
536                     linesForDiffs << []
537                     haveSeenDiffHeader = true
538                 elsif (!haveSeenDiffHeader && line =~ /^--- /)
539                     linesForDiffs << []
540                     haveSeenDiffHeader = false
541                 end
542                 linesForDiffs.last << line unless linesForDiffs.last.nil?
543             end
544
545             linesForDiffs.collect { |lines| FileDiff.new(lines) }
546         end
547
548         def self.git_new_file_binary_patch(filename, encoded_chunk, git_index)
549             return <<END
550 diff --git a/#{filename} b/#{filename}
551 new file mode 100644
552 index 0000000000000000000000000000000000000000..#{git_index}
553 GIT binary patch
554 #{encoded_chunk.join("")}literal 0
555 HcmV?d00001
556
557 END
558         end
559
560         def self.extract_contents_from_git_binary_chunk(encoded_chunk, git_index)
561             # We use Tempfile we need a unique file among processes.
562             tempfile = Tempfile.new("PrettyPatch")
563             # We need a filename which doesn't exist to apply a patch
564             # which creates a new file. Append a suffix so filename
565             # doesn't exist.
566             filepath = tempfile.path + '.bin'
567             filename = File.basename(filepath)
568
569             patch = FileDiff.git_new_file_binary_patch(filename, encoded_chunk, git_index)
570
571             # Apply the git binary patch using git-apply.
572             cmd = GIT_PATH + " apply --directory=" + File.dirname(filepath)
573             stdin, stdout, stderr = *Open3.popen3(cmd)
574             begin
575                 stdin.puts(patch)
576                 stdin.close
577
578                 error = stderr.read
579                 raise error if error != ""
580
581                 contents = File.read(filepath)
582             ensure
583                 stdin.close unless stdin.closed?
584                 stdout.close
585                 stderr.close
586                 File.unlink(filename) if File.exists?(filename)
587             end
588
589             return nil if contents.empty?
590             return "data:image/png;base64," + [contents].pack("m")
591         end
592     end
593
594     class DiffBlock
595         attr_accessor :parts
596
597         def initialize(container)
598             @parts = []
599             container << self
600         end
601
602         def to_html
603             str = "<div class='DiffBlock'>\n"
604             str += @parts.collect{ |part| part.to_html }.join
605             str += "<div class='clear_float'></div></div>\n"
606         end
607     end
608
609     class DiffBlockPart
610         attr_reader :className
611         attr :lines
612
613         def initialize(className, container)
614             @className = className
615             @lines = []
616             container.parts << self
617         end
618
619         def to_html
620             str = "<div class='DiffBlockPart %s'>\n" % @className
621             str += @lines.collect{ |line| line.to_html }.join
622             # Don't put white-space after this so adjacent inline-block DiffBlockParts will not wrap.
623             str += "</div>"
624         end
625     end
626
627     class DiffSection
628         def initialize(lines)
629             lines.length >= 1 or raise "DiffSection.parse only received %d lines" % lines.length
630
631             matches = START_OF_SECTION_FORMAT.match(lines[0])
632             from, to = [matches[1].to_i, matches[2].to_i] unless matches.nil?
633
634             @blocks = []
635             diff_block = nil
636             diff_block_part = nil
637
638             for line in lines[1...lines.length]
639                 startOfLine = line =~ /^[-\+ ]/ ? 1 : 0
640                 text = line[startOfLine...line.length].chomp
641                 case line[0]
642                 when ?-
643                     if (diff_block_part.nil? or diff_block_part.className != 'remove')
644                         diff_block = DiffBlock.new(@blocks)
645                         diff_block_part = DiffBlockPart.new('remove', diff_block)
646                     end
647
648                     diff_block_part.lines << CodeLine.new(from, nil, text)
649                     from += 1 unless from.nil?
650                 when ?+
651                     if (diff_block_part.nil? or diff_block_part.className != 'add')
652                         # Put add lines that immediately follow remove lines into the same DiffBlock.
653                         if (diff_block.nil? or diff_block_part.className != 'remove')
654                             diff_block = DiffBlock.new(@blocks)
655                         end
656
657                         diff_block_part = DiffBlockPart.new('add', diff_block)
658                     end
659
660                     diff_block_part.lines << CodeLine.new(nil, to, text)
661                     to += 1 unless to.nil?
662                 else
663                     if (diff_block_part.nil? or diff_block_part.className != 'shared')
664                         diff_block = DiffBlock.new(@blocks)
665                         diff_block_part = DiffBlockPart.new('shared', diff_block)
666                     end
667
668                     diff_block_part.lines << CodeLine.new(from, to, text)
669                     from += 1 unless from.nil?
670                     to += 1 unless to.nil?
671                 end
672             end
673
674             changes = [ [ [], [] ] ]
675             for block in @blocks
676                 for block_part in block.parts
677                     for line in block_part.lines
678                         if (!line.fromLineNumber.nil? and !line.toLineNumber.nil?) then
679                             changes << [ [], [] ]
680                             next
681                         end
682                         changes.last.first << line if line.toLineNumber.nil?
683                         changes.last.last << line if line.fromLineNumber.nil?
684                     end
685                 end
686             end
687
688             for change in changes
689                 next unless change.first.length == change.last.length
690                 for i in (0...change.first.length)
691                     raw_operations = HTMLDiff::DiffBuilder.new(change.first[i].text, change.last[i].text).operations
692                     operations = []
693                     back = 0
694                     raw_operations.each_with_index do |operation, j|
695                         if operation.action == :equal and j < raw_operations.length - 1
696                            length = operation.end_in_new - operation.start_in_new
697                            if length < SMALLEST_EQUAL_OPERATION
698                                back = length
699                                next
700                            end
701                         end
702                         operation.start_in_old -= back
703                         operation.start_in_new -= back
704                         back = 0
705                         operations << operation
706                     end
707                     change.first[i].operations = operations
708                     change.last[i].operations = operations
709                 end
710             end
711
712             @blocks.unshift(ContextLine.new(matches[3])) unless matches.nil? || matches[3].empty?
713         end
714
715         def to_html
716             str = "<div class='DiffSection'>\n"
717             str += @blocks.collect{ |block| block.to_html }.join
718             str += "</div>\n"
719         end
720         
721         def self.parse(lines)
722             linesForSections = lines.inject([[]]) do |sections, line|
723                 sections << [] if line =~ /^@@/
724                 sections.last << line
725                 sections
726             end
727
728             linesForSections.delete_if { |lines| lines.nil? or lines.empty? }
729             linesForSections.collect { |lines| DiffSection.new(lines) }
730         end
731     end
732
733     class Line
734         attr_reader :fromLineNumber
735         attr_reader :toLineNumber
736         attr_reader :text
737
738         def initialize(from, to, text)
739             @fromLineNumber = from
740             @toLineNumber = to
741             @text = text
742         end
743
744         def text_as_html
745             CGI.escapeHTML(text)
746         end
747
748         def classes
749             lineClasses = ["Line", "LineContainer"]
750             lineClasses << ["add"] unless @toLineNumber.nil? or !@fromLineNumber.nil?
751             lineClasses << ["remove"] unless @fromLineNumber.nil? or !@toLineNumber.nil?
752             lineClasses
753         end
754
755         def to_html
756             markedUpText = self.text_as_html
757             str = "<div class='%s'>\n" % self.classes.join(' ')
758             str += "<span class='from lineNumber'>%s</span><span class='to lineNumber'>%s</span>\n" %
759                    [@fromLineNumber.nil? ? '&nbsp;' : @fromLineNumber,
760                     @toLineNumber.nil? ? '&nbsp;' : @toLineNumber] unless @fromLineNumber.nil? and @toLineNumber.nil?
761             str += "<span class='text'>%s</span>\n" % markedUpText
762             str += "</div>\n"
763         end
764     end
765
766     class CodeLine < Line
767         attr :operations, true
768
769         def text_as_html
770             html = []
771             tag = @fromLineNumber.nil? ? "ins" : "del"
772             if @operations.nil? or @operations.empty?
773                 return CGI.escapeHTML(@text)
774             end
775             @operations.each do |operation|
776                 start = @fromLineNumber.nil? ? operation.start_in_new : operation.start_in_old
777                 eend = @fromLineNumber.nil? ? operation.end_in_new : operation.end_in_old
778                 escaped_text = CGI.escapeHTML(@text[start...eend])
779                 if eend - start === 0 or operation.action === :equal
780                     html << escaped_text
781                 else
782                     html << "<#{tag}>#{escaped_text}</#{tag}>"
783                 end
784             end
785             html.join
786         end
787     end
788
789     class ContextLine < Line
790         def initialize(context)
791             super("@", "@", context)
792         end
793
794         def classes
795             super << "context"
796         end
797     end
798 end