Created
August 18, 2011 12:18
-
-
Save schacon/1153943 to your computer and use it in GitHub Desktop.
simple rebase
This file contains 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 | |
# take a branch and rebase the current one onto it | |
branch_target = branch_onto = ARGV[0] | |
raise 'no target' unless branch_target | |
branch_onto = ARGV[1] if ARGV[1] | |
# record where we are and what branch we're on | |
branch_current = `git symbolic-ref HEAD`.chomp | |
sha_current = `git rev-parse #{branch_current}`.chomp | |
# ensure clean wd | |
raise 'not clean' unless `git status -s`.chomp == '' | |
# figure out the patch series | |
commits = `git rev-list #{branch_current} ^#{branch_target}`.chomp | |
shas = commits.split("\n").reverse | |
raise 'no commits' unless shas.size > 0 | |
# extract all the patches | |
shas.each do |sha| | |
puts "Extracting patch for #{sha}" | |
`git show --format=email #{sha} > /tmp/#{sha}.p` | |
end | |
# reset to the new branch | |
`git reset --hard #{branch_onto}` | |
# apply all the patches | |
cool = true | |
shas.each do |sha| | |
puts "Applying patch for #{sha}" | |
`git am /tmp/#{sha}.p` | |
if $?.exitstatus != 0 | |
cool = false | |
break | |
end | |
end | |
# reset if anything goes wrong | |
if !cool | |
puts "Rebase failed, rolling back..." | |
`git am --abort` | |
`git reset --hard #{sha_current}` | |
else | |
puts "Rebase succeeded! (if you need to revert, old sha was #{sha_current})" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment