def attach(branches): if len(branches) < 1: raise ornithopter.UsageError("need a branch") elif len(branches) > 1: raise ornithopter.UsageError("too many branches") branch, = branches if not is_branch(branch): raise CommandError("%s: branch not found" % branch) subprocess.check_call(["git", "freeze"]) subprocess.check_call(["git", "checkout", branch]) subprocess.check_call(["git", "thaw"])
def main(): COMMANDS = { "-A": all_overview, "-a": attach, "-c": create, "-n": no_attach, "-m": move, "-d": delete, "-D": delete_hard, } try: progname, args = ornithopter.parse() cmd, val = None, None branches = [] for opt, value in args: if opt in COMMANDS: if cmd: raise ornithopter.UsageError("got both %s and %s" % (cmd, opt)) cmd = opt if opt in {"-c", "-n", "-m"}: val = value() elif opt: raise ornithopter.InvalidOption(opt) else: branches.append(value()) if cmd: if val is None: COMMANDS[cmd](branches) else: COMMANDS[cmd](val, branches) elif branches: attach(branches) else: overview() except CommandError as e: sys.stderr.write("%s: %s\n" % (progname, e)) sys.exit(1) except ornithopter.UsageError as e: sys.stderr.write("%s: %s\n" % (progname, e)) sys.stderr.write("".join([ "usage: {progname} [-A]\n", " {progname} [-a] BRANCH\n", " {progname} -c NEW [OLD]\n", " {progname} -n NEW [OLD]\n", " {progname} -m NEW [OLD]\n", " {progname} -d BRANCH…\n", " {progname} -D BRANCH…\n", ]).format(progname=progname)) sys.exit(64) except subprocess.CalledProcessError as e: sys.exit(e.returncode)
def move(name, branches): if len(branches) > 1: raise ornithopter.UsageError("too many branches") command = ["git", "branch", "-m", "--"] + branches + [name] subprocess.check_call(command)
def no_attach(name, branches): if len(branches) > 1: raise ornithopter.UsageError("too many branches") command = ["git", "branch", "--", name] + branches subprocess.check_call(command)
def create(name, branches): if len(branches) > 1: raise ornithopter.UsageError("too many branches") command = ["git", "checkout", "-b", name] + branches subprocess.check_call(command)
def all_overview(branches): if branches: raise ornithopter.UsageError("-A takes no branches") overview("refs")