14 def self.prettify(string)
15 fileDiffs = FileDiff.parse(string)
18 str += fileDiffs.collect{ |diff| diff.to_html }.join
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?
29 def self.diff_header?(line)
30 RELAXED_DIFF_HEADER_FORMATS.any? { |format| line =~ format }
34 DIFF_HEADER_FORMATS = [
36 /^diff --git "?a\/.+"? "?b\/(.+)"?\r?$/,
37 /^\+\+\+ ([^\t]+)(\t.*)?\r?$/
40 RELAXED_DIFF_HEADER_FORMATS = [
45 BINARY_FILE_MARKER_FORMAT = /^Cannot display: file marked as a binary type.$/
47 IMAGE_FILE_MARKER_FORMAT = /^svn:mime-type = image\/png$/
49 GIT_INDEX_MARKER_FORMAT = /^index ([0-9a-f]{40})\.\.([0-9a-f]{40})/
51 GIT_BINARY_FILE_MARKER_FORMAT = /^GIT binary patch$/
53 GIT_LITERAL_FORMAT = /^literal \d+$/
55 START_OF_BINARY_DATA_FORMAT = /^[0-9a-zA-Z\+\/=]{20,}/ # Assume 20 chars without a space is base64 binary data.
57 START_OF_SECTION_FORMAT = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@\s*(.*)/
59 START_OF_EXTENT_STRING = "%c" % 0
60 END_OF_EXTENT_STRING = "%c" % 1
62 SMALLEST_EQUAL_OPERATION = 3
64 OPENSOURCE_TRAC_URL = "http://trac.webkit.org/"
66 OPENSOURCE_DIRS = Set.new %w[
78 def self.find_url_and_path(file_path)
79 # Search file_path from the bottom up, at each level checking whether
80 # we've found a directory we know exists in the source tree.
82 dirname, basename = File.split(file_path)
83 dirname.split(/\//).reverse.inject(basename) do |path, directory|
84 path = directory + "/" + path
86 return [OPENSOURCE_TRAC_URL, path] if OPENSOURCE_DIRS.include?(directory)
94 def self.linkifyFilename(filename)
95 url, pathBeneathTrunk = find_url_and_path(filename)
97 url.nil? ? filename : "<a href='#{url}browser/trunk/#{pathBeneathTrunk}'>#{filename}</a>"
104 text-decoration: none;
105 border-bottom: 1px dotted;
113 background-color: #f8f8f8;
114 border: 1px solid #ddd;
115 font-family: monospace;
121 font-family: sans-serif;
129 h1 :link, h1 :visited {
135 background-color: #eee;
144 font-family: sans-serif;
149 .FileDiff .DiffLinks {
155 .FileDiffLinkContainer {
157 padding-right: 0.5em;
162 background-color: white;
164 border-width: 1px 0px;
168 display:inline-block;
173 .lineNumber, .expansionLineNumber {
174 border-bottom: 1px solid #998;
175 border-right: 1px solid #ddd;
177 display: inline-block;
178 padding: 1px 5px 0px 0px;
180 vertical-align: bottom;
185 background-color: #eed;
188 .expansionLineNumber {
189 background-color: #eee;
195 white-space: pre-wrap;
199 border: 2px solid black;
202 .context, .context .lineNumber {
204 background-color: #fef;
208 background-color: #dfd;
212 background-color: #9e9;
213 text-decoration: none;
217 background-color: #fdd;
221 background-color: #e99;
222 text-decoration: none;
225 /* Support for inline comments */
239 .overallComments textarea {
243 .comment textarea, .overallComments textarea {
248 .overallComments .open {
249 -webkit-transition: height .2s;
253 #statusBubbleContainer.wrap {
262 display: -webkit-box;
268 border: 1px solid #ddd;
269 background-color: #eee;
270 font-family: sans-serif;
288 background-color: black;
310 .commentContext .lineNumber {
311 background-color: yellow;
314 .selected .lineNumber {
315 background-color: #69F;
316 border-bottom-color: #69F;
317 border-right-color: #69F;
320 .ExpandArea, .ExpandLinkContainer {
322 white-space: pre-wrap;
333 .LinkContainer a:after {
338 .LinkContainer a:last-of-type:after {
349 font-family: sans-serif;
356 .comment, .previousComment, .frozenComment {
357 background-color: #ffd;
366 .previousComment, .frozenComment {
369 white-space: pre-wrap;
377 outline: 1px solid blue;
378 outline-offset: -1px;
382 /* The width/height get set to the bubble contents via postMessage on browsers that support it. */
387 vertical-align: middle;
390 .pseudo_resize_event_iframe {
397 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
398 <script src="code-review.js?version=22"></script>
401 def self.revisionOrDescription(string)
403 when /\(revision \d+\)/
404 /\(revision (\d+)\)/.match(string)[1]
406 /\((.*)\)/.match(string)[1]
410 def self.has_image_suffix(filename)
411 filename =~ /\.(png|jpg|gif)$/
415 def initialize(lines)
416 @filename = PrettyPatch.filename_from_diff_header(lines[0].chomp)
418 for i in 0...lines.length
421 @from = PrettyPatch.revisionOrDescription(lines[i])
423 @filename = PrettyPatch.filename_from_diff_header(lines[i].chomp) if @filename.nil?
424 @to = PrettyPatch.revisionOrDescription(lines[i])
425 startOfSections = i + 1
427 when BINARY_FILE_MARKER_FORMAT
429 if (IMAGE_FILE_MARKER_FORMAT.match(lines[i + 1]) or PrettyPatch.has_image_suffix(@filename)) then
431 startOfSections = i + 2
432 for x in startOfSections...lines.length
433 # Binary diffs often have property changes listed before the actual binary data. Skip them.
434 if START_OF_BINARY_DATA_FORMAT.match(lines[x]) then
441 when GIT_INDEX_MARKER_FORMAT
442 @git_indexes = [$1, $2]
443 when GIT_BINARY_FILE_MARKER_FORMAT
445 if (GIT_LITERAL_FORMAT.match(lines[i + 1]) and PrettyPatch.has_image_suffix(@filename)) then
447 startOfSections = i + 1
452 lines_with_contents = lines[startOfSections...lines.length]
453 @sections = DiffSection.parse(lines_with_contents) unless @binary
455 @image_url = "data:image/png;base64," + lines_with_contents.join
458 raise "index line is missing" unless @git_indexes
461 for i in 0...lines_with_contents.length
462 if lines_with_contents[i] =~ /^$/
463 chunks = [lines_with_contents[i + 1 .. -1], lines_with_contents[0 .. i]]
468 raise "no binary chunks" unless chunks
470 @image_urls = chunks.zip(@git_indexes).collect do |chunk, git_index|
471 FileDiff.extract_contents_from_git_binary_chunk(chunk, git_index)
474 @image_error = "Exception raised during decoding git binary patch:<pre>#{CGI.escapeHTML($!.to_s + "\n" + $!.backtrace.join("\n"))}</pre>"
481 str = "<div class='FileDiff'>\n"
482 str += "<h1>#{PrettyPatch.linkifyFilename(@filename)}</h1>\n"
484 str += "<img class='image' src='" + @image_url + "' />"
485 elsif @git_image then
490 image_url = @image_urls[i]
491 style = ["remove", "add"][i]
492 str += "<p class=\"#{style}\">"
494 str += "<img class='image' src='" + image_url + "' />"
496 str += ["Added", "Removed"][i]
501 str += "<span class='text'>Binary file, nothing to see here</span>"
503 str += @sections.collect{ |section| section.to_html }.join("<br>\n") unless @sections.nil?
508 def self.parse(string)
509 haveSeenDiffHeader = false
511 string.each_line do |line|
512 if (PrettyPatch.diff_header?(line))
514 haveSeenDiffHeader = true
515 elsif (!haveSeenDiffHeader && line =~ /^--- /)
517 haveSeenDiffHeader = false
519 linesForDiffs.last << line unless linesForDiffs.last.nil?
522 linesForDiffs.collect { |lines| FileDiff.new(lines) }
525 def self.git_new_file_binary_patch(filename, encoded_chunk, git_index)
527 diff --git a/#{filename} b/#{filename}
529 index 0000000000000000000000000000000000000000..#{git_index}
531 #{encoded_chunk.join("")}literal 0
537 def self.extract_contents_from_git_binary_chunk(encoded_chunk, git_index)
538 # We use Tempfile we need a unique file among processes.
539 tempfile = Tempfile.new("PrettyPatch")
540 # We need a filename which doesn't exist to apply a patch
541 # which creates a new file. Append a suffix so filename
543 filepath = tempfile.path + '.bin'
544 filename = File.basename(filepath)
546 patch = FileDiff.git_new_file_binary_patch(filename, encoded_chunk, git_index)
548 # Apply the git binary patch using git-apply.
549 cmd = GIT_PATH + " apply --directory=" + File.dirname(filepath)
550 stdin, stdout, stderr = *Open3.popen3(cmd)
556 raise error if error != ""
558 contents = File.read(filepath)
560 stdin.close unless stdin.closed?
563 File.unlink(filename) if File.exists?(filename)
566 return nil if contents.empty?
567 return "data:image/png;base64," + [contents].pack("m")
574 def initialize(container)
580 str = "<div class='DiffBlock'>\n"
581 str += @parts.collect{ |part| part.to_html }.join
587 attr_reader :className
590 def initialize(className, container)
591 @className = className
593 container.parts << self
597 str = "<div class='DiffBlockPart %s'>\n" % @className
598 str += @lines.collect{ |line| line.to_html }.join
604 def initialize(lines)
605 lines.length >= 1 or raise "DiffSection.parse only received %d lines" % lines.length
607 matches = START_OF_SECTION_FORMAT.match(lines[0])
608 from, to = [matches[1].to_i, matches[2].to_i] unless matches.nil?
612 diff_block_part = nil
614 for line in lines[1...lines.length]
615 startOfLine = line =~ /^[-\+ ]/ ? 1 : 0
616 text = line[startOfLine...line.length].chomp
619 if (diff_block_part.nil? or diff_block_part.className != 'remove')
620 diff_block = DiffBlock.new(@blocks)
621 diff_block_part = DiffBlockPart.new('remove', diff_block)
624 diff_block_part.lines << CodeLine.new(from, nil, text)
625 from += 1 unless from.nil?
627 if (diff_block_part.nil? or diff_block_part.className != 'add')
628 # Put add lines that immediately follow remove lines into the same DiffBlock.
629 if (diff_block.nil? or diff_block_part.className != 'remove')
630 diff_block = DiffBlock.new(@blocks)
633 diff_block_part = DiffBlockPart.new('add', diff_block)
636 diff_block_part.lines << CodeLine.new(nil, to, text)
637 to += 1 unless to.nil?
639 if (diff_block_part.nil? or diff_block_part.className != 'shared')
640 diff_block = DiffBlock.new(@blocks)
641 diff_block_part = DiffBlockPart.new('shared', diff_block)
644 diff_block_part.lines << CodeLine.new(from, to, text)
645 from += 1 unless from.nil?
646 to += 1 unless to.nil?
650 changes = [ [ [], [] ] ]
652 for block_part in block.parts
653 for line in block_part.lines
654 if (!line.fromLineNumber.nil? and !line.toLineNumber.nil?) then
655 changes << [ [], [] ]
658 changes.last.first << line if line.toLineNumber.nil?
659 changes.last.last << line if line.fromLineNumber.nil?
664 for change in changes
665 next unless change.first.length == change.last.length
666 for i in (0...change.first.length)
667 raw_operations = HTMLDiff::DiffBuilder.new(change.first[i].text, change.last[i].text).operations
670 raw_operations.each_with_index do |operation, j|
671 if operation.action == :equal and j < raw_operations.length - 1
672 length = operation.end_in_new - operation.start_in_new
673 if length < SMALLEST_EQUAL_OPERATION
678 operation.start_in_old -= back
679 operation.start_in_new -= back
681 operations << operation
683 change.first[i].operations = operations
684 change.last[i].operations = operations
688 @blocks.unshift(ContextLine.new(matches[3])) unless matches.nil? || matches[3].empty?
692 str = "<div class='DiffSection'>\n"
693 str += @blocks.collect{ |block| block.to_html }.join
697 def self.parse(lines)
698 linesForSections = lines.inject([[]]) do |sections, line|
699 sections << [] if line =~ /^@@/
700 sections.last << line
704 linesForSections.delete_if { |lines| lines.nil? or lines.empty? }
705 linesForSections.collect { |lines| DiffSection.new(lines) }
710 attr_reader :fromLineNumber
711 attr_reader :toLineNumber
714 def initialize(from, to, text)
715 @fromLineNumber = from
725 lineClasses = ["Line", "LineContainer"]
726 lineClasses << ["add"] unless @toLineNumber.nil? or !@fromLineNumber.nil?
727 lineClasses << ["remove"] unless @fromLineNumber.nil? or !@toLineNumber.nil?
732 markedUpText = self.text_as_html
733 str = "<div class='%s'>\n" % self.classes.join(' ')
734 str += "<span class='from lineNumber'>%s</span><span class='to lineNumber'>%s</span>\n" %
735 [@fromLineNumber.nil? ? ' ' : @fromLineNumber,
736 @toLineNumber.nil? ? ' ' : @toLineNumber] unless @fromLineNumber.nil? and @toLineNumber.nil?
737 str += "<span class='text'>%s</span>\n" % markedUpText
742 class CodeLine < Line
743 attr :operations, true
747 tag = @fromLineNumber.nil? ? "ins" : "del"
748 if @operations.nil? or @operations.empty?
749 return CGI.escapeHTML(@text)
751 @operations.each do |operation|
752 start = @fromLineNumber.nil? ? operation.start_in_new : operation.start_in_old
753 eend = @fromLineNumber.nil? ? operation.end_in_new : operation.end_in_old
754 escaped_text = CGI.escapeHTML(@text[start...eend])
755 if eend - start === 0 or operation.action === :equal
758 html << "<#{tag}>#{escaped_text}</#{tag}>"
765 class ContextLine < Line
766 def initialize(context)
767 super("@", "@", context)