Last active
April 27, 2017 17:25
-
-
Save graphaelli/99d88f2dc9ff886e2c2e346341872c4a to your computer and use it in GitHub Desktop.
rebase all branches with 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 python | |
import argparse | |
import os | |
import subprocess | |
import sys | |
def rebase(base, branch, abort=True): | |
try: | |
subprocess.check_call([ | |
'git', | |
'rebase', | |
base, | |
branch.strip(), | |
]) | |
except subprocess.CalledProcessError: | |
if abort: | |
subprocess.check_call([ | |
'git', | |
'rebase', | |
'--abort', | |
]) | |
return False | |
else: | |
raise | |
return True | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-n', '--no-abort', dest='abort', action='store_false', default=True, | |
help='do not rebase --abort if rebase requires intervention ') | |
parser.add_argument('--base', type=str, default='master') | |
args = parser.parse_args() | |
results = {True: [], False: []} | |
branches = subprocess.check_output(['git', 'branch']) | |
for branch in branches.splitlines(): | |
br = branch[2:] # trim first two spaces or space+asterisk | |
success = rebase(args.base, br, args.abort) | |
results[success].append(br.decode('utf-8')) | |
print("successfully rebased {:d} branches".format(len(results[True]))) | |
failed = len(results[False]) | |
print("failed to rebase {:d} branch{}: {}".format(failed, '' if failed == 1 else 'es', ', '.join(results[False]))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment