diff options
author | Jesse Luehrs <doy@tozt.net> | 2011-08-10 00:36:43 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2011-08-10 00:39:41 -0500 |
commit | 823238dc6666d249cd91c4a65e25a0e1c5e2b9ee (patch) | |
tree | 81eef052077dbbe9aef669eb147d0b3aaafa131f /bin/git | |
parent | ccd7b72f4ae12a3237b9dd4669286d5997a06016 (diff) | |
download | conf-823238dc6666d249cd91c4a65e25a0e1c5e2b9ee.tar.gz conf-823238dc6666d249cd91c4a65e25a0e1c5e2b9ee.zip |
add script for rebasing local commits on a branch underneath the branch
Diffstat (limited to 'bin/git')
-rwxr-xr-x | bin/git/git-rebase-under | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/bin/git/git-rebase-under b/bin/git/git-rebase-under new file mode 100755 index 0000000..df665b9 --- /dev/null +++ b/bin/git/git-rebase-under @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.010; + +use Path::Class 'dir'; + +sub git { + if (!defined wantarray) { + system('git', @_); + } + elsif (wantarray) { + chomp(my @ret = qx{git @_}); + return @ret; + } + else { + chomp(my $ret = qx{git @_}); + return $ret; + } +} + +my $git_dir = dir(scalar(git qw(rev-parse --show-toplevel)))->subdir('.git'); +my $state_file = $git_dir->file('rebase-under-state'); + +my ($onto, $branch, @commits); +if ($ARGV[0] eq '--continue') { + die "can't continue: no state file" unless -r $state_file; + ($onto, $branch, @commits) = split("\n", $state_file->slurp); +} +else { + $onto = $ARGV[0] // 'master'; + $branch = git qw(symbolic-ref -q HEAD); + $branch =~ s+^refs/heads/++; + + my $remote_branch = "origin/$branch"; + @commits = git 'rev-list', '--reverse', "$remote_branch..$branch"; +} + +$state_file->openw->print(join("\n", $onto, $branch, @commits)); + +git 'checkout', $onto; +while (@commits) { + my $commit = shift @commits; + $state_file->openw->print(join("\n", $onto, $branch, @commits)); + git 'cherry-pick', $commit; + if ($?) { + die <<DIE; +Conflict detected. Fix the conflict, and run `git rebase-under --continue`. +DIE + } +} +git 'checkout', $branch; +git 'rebase', $onto; |