Created
August 22, 2016 20:14
-
-
Save gmalmquist/fdb17e780336fb94313d1f1af4199847 to your computer and use it in GitHub Desktop.
Quick utility to create a backup of a branch.
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/env python2.7 | |
from __future__ import print_function, with_statement | |
import os | |
import sys | |
from subprocess import Popen, PIPE | |
def call(cmd_str, *more): | |
args = cmd_str.split(' ') | |
if more: | |
args.extend(more) | |
args = filter(None, args) | |
print('>', ' '.join(args)) | |
p = Popen(args, stdout=PIPE, stderr=PIPE) | |
out, err = p.communicate() | |
return p.returncode, out, err | |
def call_or_die(*cmd): | |
code, out, err = call(*cmd) | |
if code != 0: | |
sys.stderr.write(out) | |
sys.stderr.write('\nError: {}'.format(err)) | |
sys.stderr.write('\n') | |
sys.stderr.flush() | |
sys.exit(1) | |
return out.strip() | |
branch = call_or_die('git rev-parse --abbrev-ref HEAD') | |
branches = [s.strip().rsplit(' ', 1)[-1] for s in call_or_die('git branch').split('\n')] | |
def posterities(branch): | |
i = 1 | |
while True: | |
yield '{}-posterity{}'.format(branch, i if i > 1 else '') | |
i += 1 | |
for posterity in posterities(branch): | |
if posterity not in branches: | |
call_or_die('git checkout -b', posterity) | |
call_or_die('git checkout', branch) | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment