require 'cgi' require 'diff' require 'open3' require 'open-uri' require 'pp' require 'set' require 'tempfile' module PrettyPatch public GIT_PATH = "git" def self.prettify(string) $last_prettify_file_count = -1 $last_prettify_part_count = { "remove" => 0, "add" => 0, "shared" => 0, "binary" => 0, "extract-error" => 0 } string = normalize_line_ending(string) str = HEADER + "\n" # Just look at the first line to see if it is an SVN revision number as added # by webkit-patch for git checkouts. $svn_revision = 0 string.each_line do |line| match = /^Subversion\ Revision: (\d*)$/.match(line) unless match.nil? str += "" + match[1] + "\n" $svn_revision = match[1].to_i; end break end fileDiffs = FileDiff.parse(string) $last_prettify_file_count = fileDiffs.length str += fileDiffs.collect{ |diff| diff.to_html }.join end def self.filename_from_diff_header(line) DIFF_HEADER_FORMATS.each do |format| match = format.match(line) return match[1] unless match.nil? end nil end def self.diff_header?(line) RELAXED_DIFF_HEADER_FORMATS.any? { |format| line =~ format } end private DIFF_HEADER_FORMATS = [ /^Index: (.*)\r?$/, /^diff --git "?a\/.+"? "?b\/(.+)"?\r?$/, /^\+\+\+ ([^\t]+)(\t.*)?\r?$/ ] RELAXED_DIFF_HEADER_FORMATS = [ /^Index:/, /^diff/ ] BINARY_FILE_MARKER_FORMAT = /^Cannot display: file marked as a binary type.$/ IMAGE_FILE_MARKER_FORMAT = /^svn:mime-type = image\/png$/ GIT_INDEX_MARKER_FORMAT = /^index ([0-9a-f]{40})\.\.([0-9a-f]{40})/ GIT_BINARY_FILE_MARKER_FORMAT = /^GIT binary patch$/ GIT_BINARY_PATCH_FORMAT = /^(literal|delta) \d+$/ GIT_LITERAL_FORMAT = /^literal \d+$/ GIT_DELTA_FORMAT = /^delta \d+$/ START_OF_BINARY_DATA_FORMAT = /^[0-9a-zA-Z\+\/=]{20,}/ # Assume 20 chars without a space is base64 binary data. START_OF_SECTION_FORMAT = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@\s*(.*)/ START_OF_EXTENT_STRING = "%c" % 0 END_OF_EXTENT_STRING = "%c" % 1 # We won't search for intra-line diffs in lines longer than this length, to avoid hangs. See . MAXIMUM_INTRALINE_DIFF_LINE_LENGTH = 10000 SMALLEST_EQUAL_OPERATION = 3 OPENSOURCE_TRAC_URL = "http://trac.webkit.org/" OPENSOURCE_DIRS = Set.new %w[ Examples LayoutTests PerformanceTests Source Tools WebKitLibraries Websites ] def self.normalize_line_ending(s) s.gsub /\r\n?/, "\n" end def self.find_url_and_path(file_path) # Search file_path from the bottom up, at each level checking whether # we've found a directory we know exists in the source tree. dirname, basename = File.split(file_path) dirname.split(/\//).reverse.inject(basename) do |path, directory| path = directory + "/" + path return [OPENSOURCE_TRAC_URL, path] if OPENSOURCE_DIRS.include?(directory) path end [nil, file_path] end def self.linkifyFilename(filename) url, pathBeneathTrunk = find_url_and_path(filename) url.nil? ? filename : "#{filename}" end HEADER =< :link, :visited { text-decoration: none; border-bottom: 1px dotted; } :link { color: #039; } .FileDiff { background-color: #f8f8f8; border: 1px solid #ddd; font-family: monospace; margin: 1em 0; position: relative; } h1 { color: #333; font-family: sans-serif; font-size: 1em; margin-left: 0.5em; display: table-cell; width: 100%; padding: 0.5em; } h1 :link, h1 :visited { color: inherit; } h1 :hover { color: #555; background-color: #eee; } .DiffLinks { float: right; } .FileDiffLinkContainer { opacity: 0; display: table-cell; padding-right: 0.5em; white-space: nowrap; } .DiffSection { background-color: white; border: solid #ddd; border-width: 1px 0px; } .ExpansionLine, .LineContainer { white-space: nowrap; } .sidebyside .DiffBlockPart.add:first-child { float: right; } .LineSide, .sidebyside .DiffBlockPart.remove, .sidebyside .DiffBlockPart.add { display:inline-block; width: 50%; vertical-align: top; } .sidebyside .resizeHandle { width: 5px; height: 100%; cursor: move; position: absolute; top: 0; left: 50%; } .sidebyside .resizeHandle:hover { background-color: grey; opacity: 0.5; } .sidebyside .DiffBlockPart.remove .to, .sidebyside .DiffBlockPart.add .from { display: none; } .lineNumber, .expansionLineNumber { border-bottom: 1px solid #998; border-right: 1px solid #ddd; color: #444; display: inline-block; padding: 1px 5px 0px 0px; text-align: right; vertical-align: bottom; width: 3em; } .lineNumber { background-color: #eed; } .expansionLineNumber { background-color: #eee; } .text { padding-left: 5px; white-space: pre-wrap; word-wrap: break-word; } .image { border: 2px solid black; } .context, .context .lineNumber { color: #849; background-color: #fef; } .Line.add, .FileDiff .add { background-color: #dfd; } .Line.add ins { background-color: #9e9; text-decoration: none; } .Line.remove, .FileDiff .remove { background-color: #fdd; } .Line.remove del { background-color: #e99; text-decoration: none; } /* Support for inline comments */ .author { font-style: italic; } .comment { position: relative; } .comment textarea { height: 6em; } .overallComments textarea { height: 2em; max-width: 100%; } .comment textarea, .overallComments textarea { display: block; width: 100%; } .overallComments .open { -webkit-transition: height .2s; height: 4em; } #statusBubbleContainer.wrap { display: block; } body { margin-bottom: 40px; } #toolbar { display: -webkit-box; display: -moz-box; padding: 3px; left: 0; right: 0; border: 1px solid #ddd; background-color: #eee; font-family: sans-serif; position: fixed; } #toolbar.anchored { bottom: 0; } #toolbar .actions { float: right; } .winter { position: fixed; z-index: 5; left: 0; right: 0; top: 0; bottom: 0; background-color: black; opacity: 0.8; } .inactive { display: none; } .lightbox { position: fixed; z-index: 6; left: 10%; right: 10%; top: 10%; bottom: 10%; background: white; } .lightbox iframe { width: 100%; height: 100%; } .commentContext .lineNumber { background-color: yellow; } .selected .lineNumber { background-color: #69F; border-bottom-color: #69F; border-right-color: #69F; } .ExpandLinkContainer { opacity: 0; border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; } .ExpandArea { margin: 0; } .ExpandText { margin-left: 0.67em; } .LinkContainer { font-family: sans-serif; font-size: small; font-style: normal; -webkit-transition: opacity 0.5s; } .LinkContainer a { border: 0; } .LinkContainer a:after { content: " | "; color: black; } .LinkContainer a:last-of-type:after { content: ""; } .help { color: gray; font-style: italic; } #message { font-size: small; font-family: sans-serif; } .commentStatus { font-style: italic; } .comment, .previousComment, .frozenComment { background-color: #ffd; } .overallComments { -webkit-box-flex: 1; -moz-box-flex: 1; margin-right: 3px; } .previousComment, .frozenComment { border: inset 1px; padding: 5px; white-space: pre-wrap; } .comment button { width: 6em; } div:focus { outline: 1px solid blue; outline-offset: -1px; } .statusBubble { /* The width/height get set to the bubble contents via postMessage on browsers that support it. */ width: 450px; height: 20px; margin: 2px 2px 0 0; border: none; vertical-align: middle; } .pseudo_resize_event_iframe { height: 10%; width: 10%; position: absolute; top: -11%; } .revision { display: none; } .autosave-state { position: absolute; right: 0; top: -1.3em; padding: 0 3px; outline: 1px solid #DDD; color: #8FDF5F; font-size: small; background-color: #EEE; } .autosave-state:empty { outline: 0px; } .autosave-state.saving { color: #E98080; } .clear_float { clear: both; } EOF def self.revisionOrDescription(string) case string when /\(revision \d+\)/ /\(revision (\d+)\)/.match(string)[1] when /\(.*\)/ /\((.*)\)/.match(string)[1] end end def self.has_image_suffix(filename) filename =~ /\.(png|jpg|gif)$/ end class FileDiff def initialize(lines) @filename = PrettyPatch.filename_from_diff_header(lines[0].chomp) startOfSections = 1 for i in 0...lines.length case lines[i] when /^--- / @from = PrettyPatch.revisionOrDescription(lines[i]) when /^\+\+\+ / @filename = PrettyPatch.filename_from_diff_header(lines[i].chomp) if @filename.nil? @to = PrettyPatch.revisionOrDescription(lines[i]) startOfSections = i + 1 break when BINARY_FILE_MARKER_FORMAT @binary = true if (IMAGE_FILE_MARKER_FORMAT.match(lines[i + 1]) or PrettyPatch.has_image_suffix(@filename)) then @image = true startOfSections = i + 2 for x in startOfSections...lines.length # Binary diffs often have property changes listed before the actual binary data. Skip them. if START_OF_BINARY_DATA_FORMAT.match(lines[x]) then startOfSections = x break end end end break when GIT_INDEX_MARKER_FORMAT @git_indexes = [$1, $2] when GIT_BINARY_FILE_MARKER_FORMAT @binary = true if (GIT_BINARY_PATCH_FORMAT.match(lines[i + 1]) and PrettyPatch.has_image_suffix(@filename)) then @git_image = true startOfSections = i + 1 end break end end lines_with_contents = lines[startOfSections...lines.length] @sections = DiffSection.parse(lines_with_contents) unless @binary if @image and not lines_with_contents.empty? @image_url = "data:image/png;base64," + lines_with_contents.join @image_checksum = FileDiff.read_checksum_from_png(lines_with_contents.join.unpack("m").join) elsif @git_image begin raise "index line is missing" unless @git_indexes chunks = nil for i in 0...lines_with_contents.length if lines_with_contents[i] =~ /^$/ chunks = [lines_with_contents[i + 1 .. -1], lines_with_contents[0 .. i]] break end end raise "no binary chunks" unless chunks from_filepath = FileDiff.extract_contents_of_from_revision(@filename, chunks[0], @git_indexes[0]) to_filepath = FileDiff.extract_contents_of_to_revision(@filename, chunks[1], @git_indexes[1], from_filepath, @git_indexes[0]) filepaths = from_filepath, to_filepath binary_contents = filepaths.collect { |filepath| File.exists?(filepath) ? File.read(filepath) : nil } @image_urls = binary_contents.collect { |content| (content and not content.empty?) ? "data:image/png;base64," + [content].pack("m") : nil } @image_checksums = binary_contents.collect { |content| FileDiff.read_checksum_from_png(content) } rescue $last_prettify_part_count["extract-error"] += 1 @image_error = "Exception raised during decoding git binary patch:
#{CGI.escapeHTML($!.to_s + "\n" + $!.backtrace.join("\n"))}
" ensure File.unlink(from_filepath) if (from_filepath and File.exists?(from_filepath)) File.unlink(to_filepath) if (to_filepath and File.exists?(to_filepath)) end end nil end def image_to_html if not @image_url then return "Image file removed" end image_snippet = "" if not @image_checksum then return image_snippet end return "

" + @image_checksum + "

" + image_snippet end def to_html str = "
\n" str += "

#{PrettyPatch.linkifyFilename(@filename)}

\n" if @image then str += self.image_to_html elsif @git_image then if @image_error str += @image_error else for i in (0...2) image_url = @image_urls[i] image_checksum = @image_checksums[i] style = ["remove", "add"][i] str += "

" if image_checksum str += image_checksum + "
" end if image_url str += "" else str += ["

Added", "

Removed"][i] end end end elsif @binary then $last_prettify_part_count["binary"] += 1 str += "Binary file, nothing to see here" else str += @sections.collect{ |section| section.to_html }.join("
\n") unless @sections.nil? end if @from then str += "" + @from + "" end str += "
\n" end def self.parse(string) haveSeenDiffHeader = false linesForDiffs = [] string.each_line do |line| if (PrettyPatch.diff_header?(line)) linesForDiffs << [] haveSeenDiffHeader = true elsif (!haveSeenDiffHeader && line =~ /^--- /) linesForDiffs << [] haveSeenDiffHeader = false end linesForDiffs.last << line unless linesForDiffs.last.nil? end linesForDiffs.collect { |lines| FileDiff.new(lines) } end def self.read_checksum_from_png(png_bytes) match = png_bytes && png_bytes.match(/tEXtchecksum\0([a-fA-F0-9]{32})/) match ? match[1] : nil end def self.git_new_file_binary_patch(filename, encoded_chunk, git_index) return <= 1 or raise "DiffSection.parse only received %d lines" % lines.length matches = START_OF_SECTION_FORMAT.match(lines[0]) if matches from, to = [matches[1].to_i, matches[3].to_i] if matches[2] and matches[4] from_end = from + matches[2].to_i to_end = to + matches[4].to_i end end @blocks = [] diff_block = nil diff_block_part = nil for line in lines[1...lines.length] startOfLine = line =~ /^[-\+ ]/ ? 1 : 0 text = line[startOfLine...line.length].chomp case line[0] when ?- if (diff_block_part.nil? or diff_block_part.className != 'remove') diff_block = DiffBlock.new(@blocks) diff_block_part = DiffBlockPart.new('remove', diff_block) end diff_block_part.lines << CodeLine.new(from, nil, text) from += 1 unless from.nil? when ?+ if (diff_block_part.nil? or diff_block_part.className != 'add') # Put add lines that immediately follow remove lines into the same DiffBlock. if (diff_block.nil? or diff_block_part.className != 'remove') diff_block = DiffBlock.new(@blocks) end diff_block_part = DiffBlockPart.new('add', diff_block) end diff_block_part.lines << CodeLine.new(nil, to, text) to += 1 unless to.nil? else if (diff_block_part.nil? or diff_block_part.className != 'shared') diff_block = DiffBlock.new(@blocks) diff_block_part = DiffBlockPart.new('shared', diff_block) end diff_block_part.lines << CodeLine.new(from, to, text) from += 1 unless from.nil? to += 1 unless to.nil? end break if from_end and to_end and from == from_end and to == to_end end changes = [ [ [], [] ] ] for block in @blocks for block_part in block.parts for line in block_part.lines if (!line.fromLineNumber.nil? and !line.toLineNumber.nil?) then changes << [ [], [] ] next end changes.last.first << line if line.toLineNumber.nil? changes.last.last << line if line.fromLineNumber.nil? end end end for change in changes next unless change.first.length == change.last.length for i in (0...change.first.length) from_text = change.first[i].text to_text = change.last[i].text next if from_text.length > MAXIMUM_INTRALINE_DIFF_LINE_LENGTH or to_text.length > MAXIMUM_INTRALINE_DIFF_LINE_LENGTH raw_operations = HTMLDiff::DiffBuilder.new(from_text, to_text).operations operations = [] back = 0 raw_operations.each_with_index do |operation, j| if operation.action == :equal and j < raw_operations.length - 1 length = operation.end_in_new - operation.start_in_new if length < SMALLEST_EQUAL_OPERATION back = length next end end operation.start_in_old -= back operation.start_in_new -= back back = 0 operations << operation end change.first[i].operations = operations change.last[i].operations = operations end end @blocks.unshift(ContextLine.new(matches[5])) unless matches.nil? || matches[5].empty? end def to_html str = "
\n" str += @blocks.collect{ |block| block.to_html }.join str += "
\n" end def self.parse(lines) linesForSections = lines.inject([[]]) do |sections, line| sections << [] if line =~ /^@@/ sections.last << line sections end linesForSections.delete_if { |lines| lines.nil? or lines.empty? } linesForSections.collect { |lines| DiffSection.new(lines) } end end class Line attr_reader :fromLineNumber attr_reader :toLineNumber attr_reader :text def initialize(from, to, text) @fromLineNumber = from @toLineNumber = to @text = text end def text_as_html CGI.escapeHTML(text) end def classes lineClasses = ["Line", "LineContainer"] lineClasses << ["add"] unless @toLineNumber.nil? or !@fromLineNumber.nil? lineClasses << ["remove"] unless @fromLineNumber.nil? or !@toLineNumber.nil? lineClasses end def to_html markedUpText = self.text_as_html str = "
\n" % self.classes.join(' ') str += "%s%s" % [@fromLineNumber.nil? ? ' ' : @fromLineNumber, @toLineNumber.nil? ? ' ' : @toLineNumber] unless @fromLineNumber.nil? and @toLineNumber.nil? str += "%s\n" % markedUpText str += "
\n" end end class CodeLine < Line attr :operations, true def text_as_html html = [] tag = @fromLineNumber.nil? ? "ins" : "del" if @operations.nil? or @operations.empty? return CGI.escapeHTML(@text) end @operations.each do |operation| start = @fromLineNumber.nil? ? operation.start_in_new : operation.start_in_old eend = @fromLineNumber.nil? ? operation.end_in_new : operation.end_in_old escaped_text = CGI.escapeHTML(@text[start...eend]) if eend - start === 0 or operation.action === :equal html << escaped_text else html << "<#{tag}>#{escaped_text}" end end html.join end end class ContextLine < Line def initialize(context) super("@", "@", context) end def classes super << "context" end end end