Skip to content

Instantly share code, notes, and snippets.

@markjaquith
Last active September 5, 2024 01:34
Show Gist options
  • Select an option

  • Save markjaquith/4219135 to your computer and use it in GitHub Desktop.

Select an option

Save markjaquith/4219135 to your computer and use it in GitHub Desktop.

Revisions

  1. markjaquith revised this gist Jan 10, 2014. 1 changed file with 13 additions and 5 deletions.
    18 changes: 13 additions & 5 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -50,17 +50,24 @@ def apply_patch patch_url
    file.each_line do |line|
    if line.start_with? 'diff --git a/'
    throw :level, 1
    elsif line.start_with? 'Index: src/'
    throw :level, 1
    elsif line.start_with? '+++ src/'
    throw :level, 1
    end
    end
    end
    throw :level, 0
    end
    puts `patch -p#{patch_level} < #{temp_file} && rm #{temp_file}`
    command = "patch -p#{patch_level} < #{temp_file} && rm #{temp_file}"
    command = "cd src; patch -p#{patch_level} < #{temp_file} && rm #{temp_file}" if File.directory? 'src'

    puts `#{command}`
    end

    def patch_from_ticket ticket
    # Craft the ticket URL
    ticket_url = "http://core.trac.wordpress.org/attachment/ticket/#{ticket}/"
    ticket_url = "https://core.trac.wordpress.org/attachment/ticket/#{ticket}/"

    # Set up the regexp
    attachment_matcher = %r{/raw-attachment/ticket/#{ticket}/([^"]+)"\s+class="trac-rawlink"\s+title="Download">​</a>\s+\(<span title="[^"]+">[^<]+</span>\)\s+-\s+added by <em>([^<]+)</em>\s+<a class="timeline"[^>]+>([^<]+) ago</a>\.}
    @@ -78,7 +85,7 @@ def patch_from_ticket ticket
    quit_with "No patches found on that ticket" if patches.count == 0
    patch = choose_from patches
    quit_with "Not a valid patch choice" if patch.nil?
    "http://core.trac.wordpress.org/raw-attachment/ticket/#{ticket}/#{patch[:file]}"
    "https://core.trac.wordpress.org/raw-attachment/ticket/#{ticket}/#{patch[:file]}"
    end

    def choose_from patches
    @@ -92,8 +99,9 @@ def choose_from patches
    puts "#{index.to_s.rjust( patches.count.to_s.length + 2 ).bold}: #{patch[:file].ljust(longest_file).bold} #{patch[:author].ljust(longest_author)} #{patch[:ago]} ago"
    end
    puts "#{'=' * line_length}".bold
    choice = prompt nil, "Patch number to apply: "
    return nil if (choice.nil? || choice.to_i < 1 || patches[choice.to_i - 1].nil? )
    choice = prompt nil, "Patch number to apply [latest by default]: "
    choice = patches.length if choice.nil?
    return nil if (choice.to_i < 1 || patches[choice.to_i - 1].nil? )
    patches[choice.to_i - 1]
    end

  2. markjaquith revised this gist Sep 12, 2013. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,27 @@
    #!/usr/bin/ruby
    #
    # The MIT License (MIT)

    # Copyright (c) 2013 Mark Jaquith

    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to deal
    # in the Software without restriction, including without limitation the rights
    # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    # copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:

    # The above copyright notice and this permission notice shall be included in
    # all copies or substantial portions of the Software.

    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    # THE SOFTWARE.

    require 'open-uri'

    # Extend String so we can do a_string.bold
  3. markjaquith revised this gist Jul 5, 2013. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -22,16 +22,15 @@ def quit_with message
    def apply_patch patch_url
    temp_file = 'wppatch.diff'
    `curl -s '#{patch_url}' > #{temp_file}`
    patch_level = 0
    catch :done do
    patch_level = catch :level do
    File.open temp_file do |file|
    file.each_line do |line|
    if line.start_with? 'diff --git a/'
    patch_level = 1
    throw :done
    throw :level, 1
    end
    end
    end
    throw :level, 0
    end
    puts `patch -p#{patch_level} < #{temp_file} && rm #{temp_file}`
    end
  4. markjaquith revised this gist Jul 5, 2013. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,20 @@ def quit_with message
    end

    def apply_patch patch_url
    puts `curl -s '#{patch_url}' | patch -p0`
    temp_file = 'wppatch.diff'
    `curl -s '#{patch_url}' > #{temp_file}`
    patch_level = 0
    catch :done do
    File.open temp_file do |file|
    file.each_line do |line|
    if line.start_with? 'diff --git a/'
    patch_level = 1
    throw :done
    end
    end
    end
    end
    puts `patch -p#{patch_level} < #{temp_file} && rm #{temp_file}`
    end

    def patch_from_ticket ticket
  5. markjaquith revised this gist Apr 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ def patch_from_ticket ticket
    ticket_url = "http://core.trac.wordpress.org/attachment/ticket/#{ticket}/"

    # Set up the regexp
    attachment_matcher = %r{/raw-attachment/ticket/#{ticket}/([^"]+)"\s+class="trac-rawlink"\s+title="Download">​</a>\s+\(<span title="[^"]+">[^<]+</span>\)\s+-\s+added by <em>([^<]+)</em>\s+<a class="timeline"[^>]+>([^<]+)</a> ago\.}
    attachment_matcher = %r{/raw-attachment/ticket/#{ticket}/([^"]+)"\s+class="trac-rawlink"\s+title="Download">​</a>\s+\(<span title="[^"]+">[^<]+</span>\)\s+-\s+added by <em>([^<]+)</em>\s+<a class="timeline"[^>]+>([^<]+) ago</a>\.}

    patches = []

  6. markjaquith revised this gist Dec 6, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ def quit_with message
    end

    def apply_patch patch_url
    puts `curl '#{patch_url}' | patch -p0`
    puts `curl -s '#{patch_url}' | patch -p0`
    end

    def patch_from_ticket ticket
  7. markjaquith revised this gist Dec 6, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -64,6 +64,9 @@ def choose_from patches

    ticket = ( ARGV[0].nil? ) ? nil : ARGV[0].chomp;

    # No ticket provided? Check the current git branch for the pattern: ticket-12345
    ticket = `git symbolic-ref HEAD 2>/dev/null`[%r{/ticket-([0-9]+)\Z}, 1] if ticket.nil?

    # They must pass a ticket number as the first arg
    quit_with "You must supply a ticket number" if ticket.nil?

  8. markjaquith created this gist Dec 5, 2012.
    85 changes: 85 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    #!/usr/bin/ruby
    require 'open-uri'

    # Extend String so we can do a_string.bold
    class String
    def bold
    "\033[1m#{self}\033[0m"
    end
    end

    def prompt(default, *args)
    print(*args)
    result = STDIN.gets.strip
    return result.empty? ? default : result
    end

    def quit_with message
    puts message
    exit
    end

    def apply_patch patch_url
    puts `curl '#{patch_url}' | patch -p0`
    end

    def patch_from_ticket ticket
    # Craft the ticket URL
    ticket_url = "http://core.trac.wordpress.org/attachment/ticket/#{ticket}/"

    # Set up the regexp
    attachment_matcher = %r{/raw-attachment/ticket/#{ticket}/([^"]+)"\s+class="trac-rawlink"\s+title="Download">​</a>\s+\(<span title="[^"]+">[^<]+</span>\)\s+-\s+added by <em>([^<]+)</em>\s+<a class="timeline"[^>]+>([^<]+)</a> ago\.}

    patches = []

    # Grab the ticket
    open ticket_url do |t|
    html = t.read
    html.scan(attachment_matcher) do |match|
    # zip the match into a hash
    patches << Hash[[:file, :author, :ago].zip(match)]
    end
    end
    quit_with "No patches found on that ticket" if patches.count == 0
    patch = choose_from patches
    quit_with "Not a valid patch choice" if patch.nil?
    "http://core.trac.wordpress.org/raw-attachment/ticket/#{ticket}/#{patch[:file]}"
    end

    def choose_from patches
    return patches.pop if patches.count == 1
    longest_file = patches.max {|a, b| a[:file].length <=> b[:file].length }[:file].length
    longest_author = patches.max {|a, b| a[:author].length <=> b[:author].length }[:author].length
    line_length = 5 + longest_file + 2 + longest_author + 2 + 13
    puts "#{'=' * line_length}".bold
    patches.each_with_index do |patch, index|
    index = index + 1
    puts "#{index.to_s.rjust( patches.count.to_s.length + 2 ).bold}: #{patch[:file].ljust(longest_file).bold} #{patch[:author].ljust(longest_author)} #{patch[:ago]} ago"
    end
    puts "#{'=' * line_length}".bold
    choice = prompt nil, "Patch number to apply: "
    return nil if (choice.nil? || choice.to_i < 1 || patches[choice.to_i - 1].nil? )
    patches[choice.to_i - 1]
    end

    ticket = ( ARGV[0].nil? ) ? nil : ARGV[0].chomp;

    # They must pass a ticket number as the first arg
    quit_with "You must supply a ticket number" if ticket.nil?

    # Ticket must be an integer or one of a series of valid Trac URLs
    if ! ticket.match( %r{\A[0-9]+\Z} ).nil?
    # Ticket number
    patch_url = patch_from_ticket ticket
    elsif ! ticket.match( %r{https?://core.trac.wordpress.org/raw-attachment/} ).nil?
    # Raw patch
    patch_url = ticket
    elsif ! ticket.match( %r{https?://core.trac.wordpress.org/attachment/} ).nil?
    # Link to patch's page
    patch_url = ticket.sub 'wordpress.org/attachment/', 'wordpress.org/raw-attachment/'
    else
    quit_with "Ticket number must be an integer or a Trac patch URL"
    end

    # Still here?
    apply_patch patch_url