Last active
March 15, 2017 02:03
-
-
Save Noreaster76/e38d1ecbcccc8b47986e25273e2b2778 to your computer and use it in GitHub Desktop.
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
# DISCLAIMER: THE BELOW IS LESS-THAN-TERRIFIC RUBY. IT IS JUST SOMETHING I WHIPPED UP QUICKLY. | |
# This is a script for iterating over, in order of oldest to newest, all | |
# commits in all GitHub pull requests that were merged into a particular | |
# branch, and cherry-picking them onto the currently checked-out branch. | |
# It's useful for creating a copy of, say, a feature branch that was branched | |
# off a branch you did not want it to be, but that feature branch is too | |
# complex to simply rebase elsewhere. | |
# | |
# Usage: | |
# | |
# 1. You'll need to install the octokit and colorize gems first. | |
# | |
# gem install octokit | |
# gem install colorize | |
# | |
# I suppose you could also just add those gems to a Gemfile and | |
# use Bundler, but that's up to you. | |
# | |
# 2. Change the REPO and BRANCH_TO_CHERRY_PICK constants below. | |
# | |
# 3. Make sure you have the proper destination branch, the branch | |
# onto which you want to cherry-pick commits, checked out. | |
# | |
# 4. `ruby cherry-pick-all-branch-commits.rb` | |
# | |
require 'octokit' | |
require 'colorize' | |
# Change these to the values you need | |
REPO = 'foo/mooplex' | |
BRANCH_TO_CHERRY_PICK = 'my-amazing-feature-branch' | |
def branch_already_has_diff?(sha) | |
system "git log | grep --quiet #{sha}" | |
end | |
def cherry_pick(commit) | |
sha = commit[:sha] | |
if branch_already_has_diff?(sha) | |
puts "#{sha} already cherry-picked.".colorize(:yellow) | |
true | |
else | |
puts "Cherry-picking commit #{sha}..." | |
system "git cherry-pick -x #{sha}" | |
end | |
end | |
def cherry_pick_all_commits_in_pull_request(pull_request) | |
pull_request_number = pull_request[:number] | |
puts "Cherry-picking commits from pull request ##{pull_request_number}, #{pull_request[:title]}...".colorize(:light_white) | |
commits = @client.pull_request_commits(REPO, pull_request_number) | |
all_commits_done = false | |
for commit in commits | |
all_commits_done = false | |
break unless cherry_pick(commit) | |
all_commits_done = true | |
end | |
all_commits_done | |
end | |
@client = Octokit::Client.new(netrc: true) | |
pull_requests = @client.pull_requests(REPO, state: 'closed', base: BRANCH_TO_CHERRY_PICK) | |
for pull_request in pull_requests.reverse | |
break unless cherry_pick_all_commits_in_pull_request(pull_request) | |
puts "Cherry-picking all commits from pull request ##{pull_request[:number]} succeeded.".colorize(:green) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment