Skip to content

Instantly share code, notes, and snippets.

@mnieber
Last active February 27, 2018 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnieber/ca0388a73144f5d724d41ee6924c3ed1 to your computer and use it in GitHub Desktop.
Save mnieber/ca0388a73144f5d724d41ee6924c3ed1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Usage: git split [-ri] <sha>
#
# Creates a tag (the split-point) at the sha where your branch begins.
# Allows you to interactively rebase onto this tag.
# Allows you to rebase your branch onto some other sha,
# moving the split-point along.
#
# Note: place this script on the path, so git can find it,
# and make it executable (chmod +x git-fixdown).
#
# Author: Maarten Nieber
# Url: https://gist.github.com/mnieber/ca0388a73144f5d724d41ee6924c3ed1
import argparse
import subprocess
import sys
def git(*args):
pipes = subprocess.Popen(
['git'] + list(args),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
std_out, std_err = pipes.communicate()
if pipes.returncode != 0:
print(std_out)
print(std_err.strip())
sys.exit(pipes.returncode)
return std_out
def create_tag(label, sha=None):
if sha:
git('tag', '-af', label, sha, '-m', branch_split)
else:
git('tag', '-af', label, '-m', branch_split)
def branch_name():
return git('rev-parse', '--abbrev-ref', 'HEAD')[:-1]
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"sha",
default='',
nargs='?',
help=(
"Move split-point tag to this sha"
)
)
parser.add_argument(
"-r", "--rebase",
help=(
"Rebase from the split-point tag onto this sha"
)
)
parser.add_argument(
"-i", "--ri",
action="store_true",
help=(
"Do an interactive rebase onto the split-point tag"
)
)
args = parser.parse_args()
branch_split = "%s_split" % branch_name()
if args.ri:
try:
git('rebase', '--interactive', '--', branch_split)
except:
pass
elif args.rebase:
try:
git('rebase', branch_split, '--onto', args.rebase)
create_tag(branch_split, args.rebase)
except:
pass
elif args.sha:
create_tag(branch_split, args.sha)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment