Changeset 51826 in webkit


Ignore:
Timestamp:
Dec 7, 2009 6:50:35 PM (14 years ago)
Author:
hamaji@chromium.org
Message:

2009-12-07 Shinichiro Hamaji <hamaji@chromium.org>

Reviewed by David Kilzer.

Bugzilla should show images in git patches
https://bugs.webkit.org/show_bug.cgi?id=31395

Attempt to go r51748 again using --directory option of git-apply.

  • PrettyPatch/PrettyPatch.rb:
Location:
trunk/BugsSite
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/BugsSite/ChangeLog

    r51751 r51826  
     12009-12-07  Shinichiro Hamaji  <hamaji@chromium.org>
     2
     3        Reviewed by David Kilzer.
     4
     5        Bugzilla should show images in git patches
     6        https://bugs.webkit.org/show_bug.cgi?id=31395
     7
     8        Attempt to go r51748 again using --directory option of git-apply.
     9
     10        * PrettyPatch/PrettyPatch.rb:
     11
    1122009-12-06  Shinichiro Hamaji  <hamaji@chromium.org>
    213
  • trunk/BugsSite/PrettyPatch/PrettyPatch.rb

    r51751 r51826  
    11require 'cgi'
    22require 'diff'
     3require 'open3'
    34require 'pp'
    45require 'set'
     6require 'tempfile'
    57
    68module PrettyPatch
    79
    810public
     11
     12    GIT_PATH = "/opt/local/bin/git"
    913
    1014    def self.prettify(string)
     
    3943    ]
    4044
    41     BINARY_FILE_MARKER_FORMAT = /^(?:Cannot display: file marked as a binary type.)|(?:GIT binary patch)$/
     45    BINARY_FILE_MARKER_FORMAT = /^Cannot display: file marked as a binary type.$/
    4246
    4347    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+$/
    4454
    4555    START_OF_BINARY_DATA_FORMAT = /^[0-9a-zA-Z\+\/=]{20,}/ # Assume 20 chars without a space is base64 binary data.
     
    216226                    end
    217227                    break
     228                when GIT_INDEX_MARKER_FORMAT
     229                    @git_indexes = [$1, $2]
     230                when GIT_BINARY_FILE_MARKER_FORMAT
     231                    @binary = true
     232                    if (GIT_LITERAL_FORMAT.match(lines[i + 1]) and PrettyPatch.has_image_suffix(@filename)) then
     233                        @git_image = true
     234                        startOfSections = i + 1
     235                    end
     236                    break
    218237                end
    219238            end
    220239            lines_with_contents = lines[startOfSections...lines.length]
    221240            @sections = DiffSection.parse(lines_with_contents) unless @binary
    222             @image_url = "data:image/png;base64," + lines_with_contents.join if @image
     241            if @image
     242                @image_url = "data:image/png;base64," + lines_with_contents.join
     243            elsif @git_image
     244                begin
     245                    raise "index line is missing" unless @git_indexes
     246
     247                    chunks = nil
     248                    for i in 0...lines_with_contents.length
     249                        if lines_with_contents[i] =~ /^$/
     250                            chunks = [lines_with_contents[i + 1 .. -1], lines_with_contents[0 .. i]]
     251                            break
     252                        end
     253                    end
     254
     255                    raise "no binary chunks" unless chunks
     256
     257                    @image_urls = chunks.zip(@git_indexes).collect do |chunk, git_index|
     258                        FileDiff.extract_contents_from_git_binary_chunk(chunk, git_index)
     259                    end
     260                rescue
     261                    @image_error = "Exception raised during decoding git binary patch:<pre>#{CGI.escapeHTML($!.to_s + "\n" + $!.backtrace.join("\n"))}</pre>"
     262                end
     263            end
    223264            nil
    224265        end
     
    229270            if @image then
    230271                str += "<img class='image' src='" + @image_url + "' />"
     272            elsif @git_image then
     273                if @image_error
     274                    str += @image_error
     275                else
     276                    for i in (0...2)
     277                        image_url = @image_urls[i]
     278                        style = ["remove", "add"][i]
     279                        str += "<p class=\"#{style}\">"
     280                        if image_url
     281                            str += "<img class='image' src='" + image_url + "' />"
     282                        else
     283                            str += ["Added", "Removed"][i]
     284                        end
     285                    end
     286                end
    231287            elsif @binary then
    232288                str += "<span class='text'>Binary file, nothing to see here</span>"
     
    252308
    253309            linesForDiffs.collect { |lines| FileDiff.new(lines) }
     310        end
     311
     312        def self.git_new_file_binary_patch(filename, encoded_chunk, git_index)
     313            return <<END
     314diff --git a/#{filename} b/#{filename}
     315new file mode 100644
     316index 0000000000000000000000000000000000000000..#{git_index}
     317GIT binary patch
     318#{encoded_chunk.join("")}literal 0
     319HcmV?d00001
     320
     321END
     322        end
     323
     324        def self.extract_contents_from_git_binary_chunk(encoded_chunk, git_index)
     325            # We use Tempfile we need a unique file among processes.
     326            tempfile = Tempfile.new("PrettyPatch")
     327            # We need a filename which doesn't exist to apply a patch
     328            # which creates a new file. Append a suffix so filename
     329            # doesn't exist.
     330            filepath = tempfile.path + '.bin'
     331            filename = File.basename(filepath)
     332
     333            patch = FileDiff.git_new_file_binary_patch(filename, encoded_chunk, git_index)
     334
     335            # Apply the git binary patch using git-apply.
     336            cmd = GIT_PATH + " apply --directory=" + File.dirname(filepath)
     337            stdin, stdout, stderr = *Open3.popen3(cmd)
     338            begin
     339                stdin.puts(patch)
     340                stdin.close
     341
     342                error = stderr.read
     343                raise error if error != ""
     344
     345                contents = File.read(filepath)
     346            ensure
     347                stdin.close unless stdin.closed?
     348                stdout.close
     349                stderr.close
     350                File.unlink(filename) if File.exists?(filename)
     351            end
     352
     353            return nil if contents.empty?
     354            return "data:image/png;base64," + [contents].pack("m")
    254355        end
    255356    end
Note: See TracChangeset for help on using the changeset viewer.