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[
85 def self.find_url_and_path(file_path)
86 # Search file_path from the bottom up, at each level checking whether
87 # we've found a directory we know exists in the source tree.
89 dirname, basename = File.split(file_path)
90 dirname.split(/\//).reverse.inject(basename) do |path, directory|
91 path = directory + "/" + path
93 return [OPENSOURCE_TRAC_URL, path] if OPENSOURCE_DIRS.include?(directory)
101 def self.linkifyFilename(filename)
102 url, pathBeneathTrunk = find_url_and_path(filename)
104 url.nil? ? filename : "<a href='#{url}browser/trunk/#{pathBeneathTrunk}'>#{filename}</a>"
111 text-decoration: none;
112 border-bottom: 1px dotted;
120 background-color: #f8f8f8;
121 border: 1px solid #ddd;
122 font-family: monospace;
128 font-family: sans-serif;
133 h1 :link, h1 :visited {
139 background-color: #eee;
143 background-color: white;
145 border-width: 1px 0px;
148 .lineNumber, .expansionLineNumber {
149 border-bottom: 1px solid #998;
150 border-right: 1px solid #ddd;
152 display: inline-block;
153 padding: 1px 5px 0px 0px;
155 vertical-align: bottom;
160 background-color: #eed;
163 .expansionLineNumber {
164 background-color: #eee;
170 white-space: pre-wrap;
174 border: 2px solid black;
177 .context, .context .lineNumber {
179 background-color: #fef;
183 background-color: #dfd;
187 background-color: #9e9;
188 text-decoration: none;
192 background-color: #fdd;
196 background-color: #e99;
197 text-decoration: none;
200 /* Support for inline comments */
210 .comment textarea, .overallComments textarea {
216 .overallComments .open {
217 -webkit-transition: height .2s;
221 #statusBubbleContainer.wrap {
230 display: -webkit-box;
237 border-top: 1px solid #ddd;
238 background-color: #eee;
239 font-family: sans-serif;
253 background-color: black;
275 .commentContext .lineNumber {
276 background-color: yellow;
279 .selected .lineNumber {
280 background-color: #69F;
281 border-bottom-color: #69F;
282 border-right-color: #69F;
293 .ExpandLinkContainer a {
297 .ExpandLinkContainer a:after {
302 .ExpandLinkContainer a:last-of-type:after {
313 font-family: sans-serif;
320 .comment, .previousComment, .frozenComment {
321 background-color: #ffd;
330 .previousComment, .frozenComment {
333 white-space: pre-wrap;
341 border: 1px solid blue;
346 /* FIXME: Size the statusBubble via postMessage so it sizes to it's content. */
350 vertical-align: middle;
353 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
354 <script src="code-review.js?version=14"></script>
357 def self.revisionOrDescription(string)
359 when /\(revision \d+\)/
360 /\(revision (\d+)\)/.match(string)[1]
362 /\((.*)\)/.match(string)[1]
366 def self.has_image_suffix(filename)
367 filename =~ /\.(png|jpg|gif)$/
371 def initialize(lines)
372 @filename = PrettyPatch.filename_from_diff_header(lines[0].chomp)
374 for i in 0...lines.length
377 @from = PrettyPatch.revisionOrDescription(lines[i])
379 @filename = PrettyPatch.filename_from_diff_header(lines[i].chomp) if @filename.nil?
380 @to = PrettyPatch.revisionOrDescription(lines[i])
381 startOfSections = i + 1
383 when BINARY_FILE_MARKER_FORMAT
385 if (IMAGE_FILE_MARKER_FORMAT.match(lines[i + 1]) or PrettyPatch.has_image_suffix(@filename)) then
387 startOfSections = i + 2
388 for x in startOfSections...lines.length
389 # Binary diffs often have property changes listed before the actual binary data. Skip them.
390 if START_OF_BINARY_DATA_FORMAT.match(lines[x]) then
397 when GIT_INDEX_MARKER_FORMAT
398 @git_indexes = [$1, $2]
399 when GIT_BINARY_FILE_MARKER_FORMAT
401 if (GIT_LITERAL_FORMAT.match(lines[i + 1]) and PrettyPatch.has_image_suffix(@filename)) then
403 startOfSections = i + 1
408 lines_with_contents = lines[startOfSections...lines.length]
409 @sections = DiffSection.parse(lines_with_contents) unless @binary
411 @image_url = "data:image/png;base64," + lines_with_contents.join
414 raise "index line is missing" unless @git_indexes
417 for i in 0...lines_with_contents.length
418 if lines_with_contents[i] =~ /^$/
419 chunks = [lines_with_contents[i + 1 .. -1], lines_with_contents[0 .. i]]
424 raise "no binary chunks" unless chunks
426 @image_urls = chunks.zip(@git_indexes).collect do |chunk, git_index|
427 FileDiff.extract_contents_from_git_binary_chunk(chunk, git_index)
430 @image_error = "Exception raised during decoding git binary patch:<pre>#{CGI.escapeHTML($!.to_s + "\n" + $!.backtrace.join("\n"))}</pre>"
437 str = "<div class='FileDiff'>\n"
438 str += "<h1>#{PrettyPatch.linkifyFilename(@filename)}</h1>\n"
440 str += "<img class='image' src='" + @image_url + "' />"
441 elsif @git_image then
446 image_url = @image_urls[i]
447 style = ["remove", "add"][i]
448 str += "<p class=\"#{style}\">"
450 str += "<img class='image' src='" + image_url + "' />"
452 str += ["Added", "Removed"][i]
457 str += "<span class='text'>Binary file, nothing to see here</span>"
459 str += @sections.collect{ |section| section.to_html }.join("<br>\n") unless @sections.nil?
464 def self.parse(string)
465 haveSeenDiffHeader = false
467 string.each_line do |line|
468 if (PrettyPatch.diff_header?(line))
470 haveSeenDiffHeader = true
471 elsif (!haveSeenDiffHeader && line =~ /^--- /)
473 haveSeenDiffHeader = false
475 linesForDiffs.last << line unless linesForDiffs.last.nil?
478 linesForDiffs.collect { |lines| FileDiff.new(lines) }
481 def self.git_new_file_binary_patch(filename, encoded_chunk, git_index)
483 diff --git a/#{filename} b/#{filename}
485 index 0000000000000000000000000000000000000000..#{git_index}
487 #{encoded_chunk.join("")}literal 0
493 def self.extract_contents_from_git_binary_chunk(encoded_chunk, git_index)
494 # We use Tempfile we need a unique file among processes.
495 tempfile = Tempfile.new("PrettyPatch")
496 # We need a filename which doesn't exist to apply a patch
497 # which creates a new file. Append a suffix so filename
499 filepath = tempfile.path + '.bin'
500 filename = File.basename(filepath)
502 patch = FileDiff.git_new_file_binary_patch(filename, encoded_chunk, git_index)
504 # Apply the git binary patch using git-apply.
505 cmd = GIT_PATH + " apply --directory=" + File.dirname(filepath)
506 stdin, stdout, stderr = *Open3.popen3(cmd)
512 raise error if error != ""
514 contents = File.read(filepath)
516 stdin.close unless stdin.closed?
519 File.unlink(filename) if File.exists?(filename)
522 return nil if contents.empty?
523 return "data:image/png;base64," + [contents].pack("m")
528 def initialize(lines)
529 lines.length >= 1 or raise "DiffSection.parse only received %d lines" % lines.length
531 matches = START_OF_SECTION_FORMAT.match(lines[0])
532 from, to = [matches[1].to_i, matches[2].to_i] unless matches.nil?
534 @lines = lines[1...lines.length].collect do |line|
535 startOfLine = line =~ /^[-\+ ]/ ? 1 : 0
536 text = line[startOfLine...line.length].chomp
539 result = CodeLine.new(from, nil, text)
540 from += 1 unless from.nil?
543 result = CodeLine.new(nil, to, text)
544 to += 1 unless to.nil?
547 result = CodeLine.new(from, to, text)
548 from += 1 unless from.nil?
549 to += 1 unless to.nil?
554 @lines.unshift(ContextLine.new(matches[3])) unless matches.nil? || matches[3].empty?
556 changes = [ [ [], [] ] ]
558 if (!line.fromLineNumber.nil? and !line.toLineNumber.nil?) then
559 changes << [ [], [] ]
562 changes.last.first << line if line.toLineNumber.nil?
563 changes.last.last << line if line.fromLineNumber.nil?
566 for change in changes
567 next unless change.first.length == change.last.length
568 for i in (0...change.first.length)
569 raw_operations = HTMLDiff::DiffBuilder.new(change.first[i].text, change.last[i].text).operations
572 raw_operations.each_with_index do |operation, j|
573 if operation.action == :equal and j < raw_operations.length - 1
574 length = operation.end_in_new - operation.start_in_new
575 if length < SMALLEST_EQUAL_OPERATION
580 operation.start_in_old -= back
581 operation.start_in_new -= back
583 operations << operation
585 change.first[i].operations = operations
586 change.last[i].operations = operations
592 str = "<div class='DiffSection'>\n"
593 str += @lines.collect{ |line| line.to_html }.join
597 def self.parse(lines)
598 linesForSections = lines.inject([[]]) do |sections, line|
599 sections << [] if line =~ /^@@/
600 sections.last << line
604 linesForSections.delete_if { |lines| lines.nil? or lines.empty? }
605 linesForSections.collect { |lines| DiffSection.new(lines) }
610 attr_reader :fromLineNumber
611 attr_reader :toLineNumber
614 def initialize(from, to, text)
615 @fromLineNumber = from
625 lineClasses = ["Line"]
626 lineClasses << ["add"] unless @toLineNumber.nil? or !@fromLineNumber.nil?
627 lineClasses << ["remove"] unless @fromLineNumber.nil? or !@toLineNumber.nil?
632 markedUpText = self.text_as_html
633 str = "<div class='%s'>\n" % self.classes.join(' ')
634 str += "<span class='from lineNumber'>%s</span><span class='to lineNumber'>%s</span>\n" %
635 [@fromLineNumber.nil? ? ' ' : @fromLineNumber,
636 @toLineNumber.nil? ? ' ' : @toLineNumber] unless @fromLineNumber.nil? and @toLineNumber.nil?
637 str += "<span class='text'>%s</span>\n" % markedUpText
642 class CodeLine < Line
643 attr :operations, true
647 tag = @fromLineNumber.nil? ? "ins" : "del"
648 if @operations.nil? or @operations.empty?
649 return CGI.escapeHTML(@text)
651 @operations.each do |operation|
652 start = @fromLineNumber.nil? ? operation.start_in_new : operation.start_in_old
653 eend = @fromLineNumber.nil? ? operation.end_in_new : operation.end_in_old
654 escaped_text = CGI.escapeHTML(@text[start...eend])
655 if eend - start === 0 or operation.action === :equal
658 html << "<#{tag}>#{escaped_text}</#{tag}>"
665 class ContextLine < Line
666 def initialize(context)
667 super("@", "@", context)