14 def self.prettify(string)
15 fileDiffs = FileDiff.parse(string)
19 # Just look at the first line to see if it is an SVN revision number as added
20 # by webkit-patch for git checkouts.
21 string.each_line do |line|
22 match = /^Subversion\ Revision: (\d*)$/.match(line)
24 str += "<span class='revision'>" + match[1] + "</span>\n"
29 str += fileDiffs.collect{ |diff| diff.to_html }.join
32 def self.filename_from_diff_header(line)
33 DIFF_HEADER_FORMATS.each do |format|
34 match = format.match(line)
35 return match[1] unless match.nil?
40 def self.diff_header?(line)
41 RELAXED_DIFF_HEADER_FORMATS.any? { |format| line =~ format }
45 DIFF_HEADER_FORMATS = [
47 /^diff --git "?a\/.+"? "?b\/(.+)"?\r?$/,
48 /^\+\+\+ ([^\t]+)(\t.*)?\r?$/
51 RELAXED_DIFF_HEADER_FORMATS = [
56 BINARY_FILE_MARKER_FORMAT = /^Cannot display: file marked as a binary type.$/
58 IMAGE_FILE_MARKER_FORMAT = /^svn:mime-type = image\/png$/
60 GIT_INDEX_MARKER_FORMAT = /^index ([0-9a-f]{40})\.\.([0-9a-f]{40})/
62 GIT_BINARY_FILE_MARKER_FORMAT = /^GIT binary patch$/
64 GIT_LITERAL_FORMAT = /^literal \d+$/
66 START_OF_BINARY_DATA_FORMAT = /^[0-9a-zA-Z\+\/=]{20,}/ # Assume 20 chars without a space is base64 binary data.
68 START_OF_SECTION_FORMAT = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@\s*(.*)/
70 START_OF_EXTENT_STRING = "%c" % 0
71 END_OF_EXTENT_STRING = "%c" % 1
73 SMALLEST_EQUAL_OPERATION = 3
75 OPENSOURCE_TRAC_URL = "http://trac.webkit.org/"
77 OPENSOURCE_DIRS = Set.new %w[
88 def self.find_url_and_path(file_path)
89 # Search file_path from the bottom up, at each level checking whether
90 # we've found a directory we know exists in the source tree.
92 dirname, basename = File.split(file_path)
93 dirname.split(/\//).reverse.inject(basename) do |path, directory|
94 path = directory + "/" + path
96 return [OPENSOURCE_TRAC_URL, path] if OPENSOURCE_DIRS.include?(directory)
104 def self.linkifyFilename(filename)
105 url, pathBeneathTrunk = find_url_and_path(filename)
107 url.nil? ? filename : "<a href='#{url}browser/trunk/#{pathBeneathTrunk}'>#{filename}</a>"
114 text-decoration: none;
115 border-bottom: 1px dotted;
123 background-color: #f8f8f8;
124 border: 1px solid #ddd;
125 font-family: monospace;
131 font-family: sans-serif;
139 h1 :link, h1 :visited {
145 background-color: #eee;
152 .FileDiffLinkContainer {
155 padding-right: 0.5em;
160 background-color: white;
162 border-width: 1px 0px;
165 .ExpansionLine, .LineContainer {
169 .sidebyside .DiffBlockPart.add:first-child {
173 .LineSide:last-child {
178 .sidebyside .DiffBlockPart.remove,
179 .sidebyside .DiffBlockPart.add {
180 display:inline-block;
185 .sidebyside .DiffBlockPart.remove .to,
186 .sidebyside .DiffBlockPart.add .from {
190 .lineNumber, .expansionLineNumber {
191 border-bottom: 1px solid #998;
192 border-right: 1px solid #ddd;
194 display: inline-block;
195 padding: 1px 5px 0px 0px;
197 vertical-align: bottom;
202 background-color: #eed;
205 .expansionLineNumber {
206 background-color: #eee;
211 white-space: pre-wrap;
212 word-wrap: break-word;
216 border: 2px solid black;
219 .context, .context .lineNumber {
221 background-color: #fef;
225 background-color: #dfd;
229 background-color: #9e9;
230 text-decoration: none;
234 background-color: #fdd;
238 background-color: #e99;
239 text-decoration: none;
242 /* Support for inline comments */
256 .overallComments textarea {
260 .comment textarea, .overallComments textarea {
265 .overallComments .open {
266 -webkit-transition: height .2s;
270 #statusBubbleContainer.wrap {
279 display: -webkit-box;
284 border: 1px solid #ddd;
285 background-color: #eee;
286 font-family: sans-serif;
305 background-color: black;
328 .commentContext .lineNumber {
329 background-color: yellow;
332 .selected .lineNumber {
333 background-color: #69F;
334 border-bottom-color: #69F;
335 border-right-color: #69F;
338 .ExpandLinkContainer {
340 border-top: 1px solid #ddd;
341 border-bottom: 1px solid #ddd;
353 font-family: sans-serif;
356 -webkit-transition: opacity 0.5s;
363 .LinkContainer a:after {
368 .LinkContainer a:last-of-type:after {
379 font-family: sans-serif;
386 .comment, .previousComment, .frozenComment {
387 background-color: #ffd;
396 .previousComment, .frozenComment {
399 white-space: pre-wrap;
407 outline: 1px solid blue;
408 outline-offset: -1px;
412 /* The width/height get set to the bubble contents via postMessage on browsers that support it. */
417 vertical-align: middle;
420 .pseudo_resize_event_iframe {
436 outline: 1px solid #DDD;
439 background-color: #EEE;
442 .autosave-state:empty {
445 .autosave-state.saving {
453 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
454 <script src="code-review.js?version=36"></script>
457 def self.revisionOrDescription(string)
459 when /\(revision \d+\)/
460 /\(revision (\d+)\)/.match(string)[1]
462 /\((.*)\)/.match(string)[1]
466 def self.has_image_suffix(filename)
467 filename =~ /\.(png|jpg|gif)$/
471 def initialize(lines)
472 @filename = PrettyPatch.filename_from_diff_header(lines[0].chomp)
474 for i in 0...lines.length
477 @from = PrettyPatch.revisionOrDescription(lines[i])
479 @filename = PrettyPatch.filename_from_diff_header(lines[i].chomp) if @filename.nil?
480 @to = PrettyPatch.revisionOrDescription(lines[i])
481 startOfSections = i + 1
483 when BINARY_FILE_MARKER_FORMAT
485 if (IMAGE_FILE_MARKER_FORMAT.match(lines[i + 1]) or PrettyPatch.has_image_suffix(@filename)) then
487 startOfSections = i + 2
488 for x in startOfSections...lines.length
489 # Binary diffs often have property changes listed before the actual binary data. Skip them.
490 if START_OF_BINARY_DATA_FORMAT.match(lines[x]) then
497 when GIT_INDEX_MARKER_FORMAT
498 @git_indexes = [$1, $2]
499 when GIT_BINARY_FILE_MARKER_FORMAT
501 if (GIT_LITERAL_FORMAT.match(lines[i + 1]) and PrettyPatch.has_image_suffix(@filename)) then
503 startOfSections = i + 1
508 lines_with_contents = lines[startOfSections...lines.length]
509 @sections = DiffSection.parse(lines_with_contents) unless @binary
511 @image_url = "data:image/png;base64," + lines_with_contents.join
514 raise "index line is missing" unless @git_indexes
517 for i in 0...lines_with_contents.length
518 if lines_with_contents[i] =~ /^$/
519 chunks = [lines_with_contents[i + 1 .. -1], lines_with_contents[0 .. i]]
524 raise "no binary chunks" unless chunks
526 @image_urls = chunks.zip(@git_indexes).collect do |chunk, git_index|
527 FileDiff.extract_contents_from_git_binary_chunk(chunk, git_index)
530 @image_error = "Exception raised during decoding git binary patch:<pre>#{CGI.escapeHTML($!.to_s + "\n" + $!.backtrace.join("\n"))}</pre>"
537 str = "<div class='FileDiff'>\n"
538 str += "<h1>#{PrettyPatch.linkifyFilename(@filename)}</h1>\n"
540 str += "<img class='image' src='" + @image_url + "' />"
541 elsif @git_image then
546 image_url = @image_urls[i]
547 style = ["remove", "add"][i]
548 str += "<p class=\"#{style}\">"
550 str += "<img class='image' src='" + image_url + "' />"
552 str += ["Added", "Removed"][i]
557 str += "<span class='text'>Binary file, nothing to see here</span>"
559 str += @sections.collect{ |section| section.to_html }.join("<br>\n") unless @sections.nil?
563 str += "<span class='revision'>" + @from + "</span>"
569 def self.parse(string)
570 haveSeenDiffHeader = false
572 string.each_line do |line|
573 if (PrettyPatch.diff_header?(line))
575 haveSeenDiffHeader = true
576 elsif (!haveSeenDiffHeader && line =~ /^--- /)
578 haveSeenDiffHeader = false
580 linesForDiffs.last << line unless linesForDiffs.last.nil?
583 linesForDiffs.collect { |lines| FileDiff.new(lines) }
586 def self.git_new_file_binary_patch(filename, encoded_chunk, git_index)
588 diff --git a/#{filename} b/#{filename}
590 index 0000000000000000000000000000000000000000..#{git_index}
592 #{encoded_chunk.join("")}literal 0
598 def self.extract_contents_from_git_binary_chunk(encoded_chunk, git_index)
599 # We use Tempfile we need a unique file among processes.
600 tempfile = Tempfile.new("PrettyPatch")
601 # We need a filename which doesn't exist to apply a patch
602 # which creates a new file. Append a suffix so filename
604 filepath = tempfile.path + '.bin'
605 filename = File.basename(filepath)
607 patch = FileDiff.git_new_file_binary_patch(filename, encoded_chunk, git_index)
609 # Apply the git binary patch using git-apply.
610 cmd = GIT_PATH + " apply --directory=" + File.dirname(filepath)
611 stdin, stdout, stderr = *Open3.popen3(cmd)
617 raise error if error != ""
619 contents = File.read(filepath)
621 stdin.close unless stdin.closed?
624 File.unlink(filename) if File.exists?(filename)
627 return nil if contents.empty?
628 return "data:image/png;base64," + [contents].pack("m")
635 def initialize(container)
641 str = "<div class='DiffBlock'>\n"
642 str += @parts.collect{ |part| part.to_html }.join
643 str += "<div class='clear_float'></div></div>\n"
648 attr_reader :className
651 def initialize(className, container)
652 @className = className
654 container.parts << self
658 str = "<div class='DiffBlockPart %s'>\n" % @className
659 str += @lines.collect{ |line| line.to_html }.join
660 # Don't put white-space after this so adjacent inline-block DiffBlockParts will not wrap.
666 def initialize(lines)
667 lines.length >= 1 or raise "DiffSection.parse only received %d lines" % lines.length
669 matches = START_OF_SECTION_FORMAT.match(lines[0])
670 from, to = [matches[1].to_i, matches[2].to_i] unless matches.nil?
674 diff_block_part = nil
676 for line in lines[1...lines.length]
677 startOfLine = line =~ /^[-\+ ]/ ? 1 : 0
678 text = line[startOfLine...line.length].chomp
681 if (diff_block_part.nil? or diff_block_part.className != 'remove')
682 diff_block = DiffBlock.new(@blocks)
683 diff_block_part = DiffBlockPart.new('remove', diff_block)
686 diff_block_part.lines << CodeLine.new(from, nil, text)
687 from += 1 unless from.nil?
689 if (diff_block_part.nil? or diff_block_part.className != 'add')
690 # Put add lines that immediately follow remove lines into the same DiffBlock.
691 if (diff_block.nil? or diff_block_part.className != 'remove')
692 diff_block = DiffBlock.new(@blocks)
695 diff_block_part = DiffBlockPart.new('add', diff_block)
698 diff_block_part.lines << CodeLine.new(nil, to, text)
699 to += 1 unless to.nil?
701 if (diff_block_part.nil? or diff_block_part.className != 'shared')
702 diff_block = DiffBlock.new(@blocks)
703 diff_block_part = DiffBlockPart.new('shared', diff_block)
706 diff_block_part.lines << CodeLine.new(from, to, text)
707 from += 1 unless from.nil?
708 to += 1 unless to.nil?
712 changes = [ [ [], [] ] ]
714 for block_part in block.parts
715 for line in block_part.lines
716 if (!line.fromLineNumber.nil? and !line.toLineNumber.nil?) then
717 changes << [ [], [] ]
720 changes.last.first << line if line.toLineNumber.nil?
721 changes.last.last << line if line.fromLineNumber.nil?
726 for change in changes
727 next unless change.first.length == change.last.length
728 for i in (0...change.first.length)
729 raw_operations = HTMLDiff::DiffBuilder.new(change.first[i].text, change.last[i].text).operations
732 raw_operations.each_with_index do |operation, j|
733 if operation.action == :equal and j < raw_operations.length - 1
734 length = operation.end_in_new - operation.start_in_new
735 if length < SMALLEST_EQUAL_OPERATION
740 operation.start_in_old -= back
741 operation.start_in_new -= back
743 operations << operation
745 change.first[i].operations = operations
746 change.last[i].operations = operations
750 @blocks.unshift(ContextLine.new(matches[3])) unless matches.nil? || matches[3].empty?
754 str = "<div class='DiffSection'>\n"
755 str += @blocks.collect{ |block| block.to_html }.join
759 def self.parse(lines)
760 linesForSections = lines.inject([[]]) do |sections, line|
761 sections << [] if line =~ /^@@/
762 sections.last << line
766 linesForSections.delete_if { |lines| lines.nil? or lines.empty? }
767 linesForSections.collect { |lines| DiffSection.new(lines) }
772 attr_reader :fromLineNumber
773 attr_reader :toLineNumber
776 def initialize(from, to, text)
777 @fromLineNumber = from
787 lineClasses = ["Line", "LineContainer"]
788 lineClasses << ["add"] unless @toLineNumber.nil? or !@fromLineNumber.nil?
789 lineClasses << ["remove"] unless @fromLineNumber.nil? or !@toLineNumber.nil?
794 markedUpText = self.text_as_html
795 str = "<div class='%s'>\n" % self.classes.join(' ')
796 str += "<span class='from lineNumber'>%s</span><span class='to lineNumber'>%s</span>" %
797 [@fromLineNumber.nil? ? ' ' : @fromLineNumber,
798 @toLineNumber.nil? ? ' ' : @toLineNumber] unless @fromLineNumber.nil? and @toLineNumber.nil?
799 str += "<span class='text'>%s</span>\n" % markedUpText
804 class CodeLine < Line
805 attr :operations, true
809 tag = @fromLineNumber.nil? ? "ins" : "del"
810 if @operations.nil? or @operations.empty?
811 return CGI.escapeHTML(@text)
813 @operations.each do |operation|
814 start = @fromLineNumber.nil? ? operation.start_in_new : operation.start_in_old
815 eend = @fromLineNumber.nil? ? operation.end_in_new : operation.end_in_old
816 escaped_text = CGI.escapeHTML(@text[start...eend])
817 if eend - start === 0 or operation.action === :equal
820 html << "<#{tag}>#{escaped_text}</#{tag}>"
827 class ContextLine < Line
828 def initialize(context)
829 super("@", "@", context)