-
-
Save philroche/f722face4a4b463bc5f259747a07b17c to your computer and use it in GitHub Desktop.
This file contains 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 tempfile | |
import subprocess | |
import traceback | |
def u(somebytes): | |
return None if somebytes is None else somebytes.decode('utf-8') | |
def run(cmd): | |
cmd_args = cmd.split() | |
print("$ {}".format(cmd)) | |
return u(subprocess.check_output( | |
cmd_args, | |
stderr=subprocess.STDOUT)) | |
def mergein(branch, revno): | |
print(run("bzr merge -c {} {}".format(revno, branch))) | |
def commit_msg(branch, revno): | |
out = run("bzr log -r {} {}".format(revno, branch)) | |
print(out) | |
msg = "" | |
collect = False | |
for line in out.splitlines(): | |
if not collect and line.startswith('message:'): | |
collect = True | |
elif collect and line.startswith('------'): | |
collect = False | |
elif collect: | |
msg += line.lstrip() | |
return msg | |
def commit(msg): | |
f = tempfile.NamedTemporaryFile(prefix="commit_msg", mode="w") | |
f.write(msg) | |
f.flush() | |
print(run("bzr commit -F {}".format(f.name))) | |
def attach_procedence(msg, branch, revno): | |
return msg + "\n\nFrom {} revno {}".format(branch, revno) | |
def cherrypick(branch, start, end): | |
for revno in range(start, end + 1): | |
print("Cherrypicking revno {} from {}".format(revno, branch)) | |
try: | |
mergein(branch, revno) | |
msg = commit_msg(branch, revno) | |
msg = attach_procedence(msg, branch, revno) | |
print("{} commit msg: {}".format(revno, msg)) | |
commit(msg) | |
except Exception: | |
traceback.print_exc() | |
print("Cherrypicking stopped at revno {} from {}".format( | |
revno, branch)) | |
return | |
def main(): | |
parser = argparse.ArgumentParser(description='Cherrypick bzr commits') | |
parser.add_argument( | |
'branch', type=str, help="Bazaar branch to pick commits from") | |
parser.add_argument( | |
'first', type=int, help="First revno to cherrypick") | |
parser.add_argument( | |
'last', type=int, help="Last revno to cherrypick") | |
args = parser.parse_args() | |
cherrypick(args.branch, args.first, args.last) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment