Created
September 25, 2012 00:42
-
-
Save dpwright/3779324 to your computer and use it in GitHub Desktop.
Run a git command, and then rebase any branches which were built on top of the current one
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/ruby | |
#GistID: 3779324 | |
require 'escape' | |
exit if ARGV.empty? | |
current_branch = `git symbolic-ref -q HEAD`.sub(/^refs\/heads\//, "").strip | |
exit if current_branch.empty? | |
def branch_output_to_array(output) | |
output.gsub(/^[ *]*/, "").split("\n").collect{ |e| e.strip } | |
end | |
IGNORED_BRANCHES = branch_output_to_array(`git branch --no-color -r`) << "HEAD" | |
def branches_on(commit) | |
ignored = IGNORED_BRANCHES << commit | |
log = `git log --pretty=%d --simplify-by-decoration #{commit} | head -n 1` | |
branches = log.sub(/^ \(([^)]+)\).*$/, '\1').split(", ") | |
branches.collect{ |e| e.strip }.reject{ |b| ignored.include? b } | |
end | |
def children_of(branch) | |
c = branch_output_to_array(`git branch --no-color --contains #{branch}`) | |
c.reject!{ |b| b == branch } | |
grandchildren = c.collect{|c| children_of c}.flatten | |
c.reject{ |b| grandchildren.include? b } | |
end | |
def branch_tree_from(branch) | |
siblings = branches_on branch | |
children = children_of(branch).reject{|c| siblings.include? c} | |
tail = siblings.collect{|s| [s]} + children.collect{|c| branch_tree_from(c)} | |
tail.empty? ? [branch] : [branch, tail] | |
end | |
def rebase_all_children(tree) | |
parent = tree.shift | |
children = tree.shift | |
children.map do |e| | |
system "git rebase #{parent} #{e.first}" | |
if e.size > 1 | |
rebase_all_children e | |
end | |
end | |
end | |
initial_tree = branch_tree_from current_branch | |
if system "git #{Escape.shell_command(ARGV)}" | |
rebase_all_children initial_tree | |
system "git checkout #{current_branch}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment