Last active
August 29, 2015 14:05
-
-
Save midwire/fccc1904696511f185da to your computer and use it in GitHub Desktop.
Cherry Pick commits from an external repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'pry' | |
| require 'colored' | |
| require 'trollop' | |
| class CherryPickExternal | |
| attr_accessor :external_repo, :sha, :elapsed, :color_output, :test_run | |
| def self.run(*args) | |
| start_time = Time.now | |
| opts = Trollop::options do | |
| opt(:external_repo, "Path to external repository's '.git' directory.", type: :string, short: 'r', required: true) | |
| opt(:sha, 'The commit SHA of the external repo', type: :string, short: 's', required: true) | |
| opt(:color, 'Print colored output', default: true, short: 'c') | |
| opt(:test_run, 'Look at the patch output but do not apply it', default: false, short: 't') | |
| end | |
| instance = CherryPickExternal.new(opts) | |
| instance.process | |
| instance.elapsed = Time.now - start_time | |
| instance.report_summary | |
| end | |
| def initialize(opts) | |
| self.external_repo = opts[:external_repo] | |
| self.sha = opts[:sha] | |
| self.color_output = opts[:color] | |
| self.test_run = opts[:test_run] | |
| end | |
| def color(string, clr) | |
| if self.color_output | |
| string.send(clr) | |
| else | |
| string | |
| end | |
| end | |
| def report_summary | |
| puts(color(">>> Processed in [#{elapsed}] seconds", :red)) | |
| end | |
| def normalized_repo_path | |
| self.external_repo += '.git' unless self.external_repo.match(/\.git$/) | |
| self.external_repo = File.expand_path(external_repo, Dir.getwd) | |
| end | |
| def process | |
| cmd = `which git`.chomp | |
| cmd << " --git-dir=#{normalized_repo_path}" | |
| cmd << " format-patch -k -1 --stdout" | |
| cmd << " #{sha} | git am -3 -k" unless self.test_run | |
| out = `#{cmd}` | |
| puts(">>> #{out}") | |
| end | |
| end | |
| CherryPickExternal.run(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment