Skip to content

Instantly share code, notes, and snippets.

@cybertk
Created July 22, 2014 16:34
Show Gist options
  • Save cybertk/a4d8a92da23d5ce00a5d to your computer and use it in GitHub Desktop.
Save cybertk/a4d8a92da23d5ce00a5d to your computer and use it in GitHub Desktop.
Generate patches from gerrit
#!/usr/bin/env ruby
# Generate patch from gerrit
require 'optparse'
def generatePatch(base_branch, topic_pattern, output_dir)
`git review -l | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g"`.lines do |line|
_, patch, topic = line.match(/(\d+).*(#{topic_pattern}).*/).to_a
next if not patch or not topic
topic.sub! '/', '_'
# Download patch from gerrit
`git review -d #{patch}`
# Get current branch
branch = `git rev-parse --abbrev-ref HEAD`
# Generate patch
`git format-patch #{base_branch} --stdout > #{output_dir}/#{topic}.patch`
# Cleanup
`git checkout -q #{base_branch}`
`git branch -D #{branch}`
puts "Generated #{topic}.patch"
end
end
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: generate_patch.rb [options]"
options[:pattern] = 'bug\/\d+'
opts.on( '-p', '--pattern PATTERN', 'Match gerrit topic with PATTERN' ) do |pattern|
options[:pattern] = pattern
end
options[:branch] = 'master'
opts.on( '-b', '--branch BRANCH', 'Generate patches aginst BRANCH' ) do |branch|
options[:branch] = branch
end
options[:output] = '.'
opts.on( '-o', '--output DIR', 'Save patches to DIR' ) do |output|
options[:output] = output
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
optparse.parse!
generatePatch(options[:branch], options[:pattern], options[:output])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment