(branch, num, project='ipython/ipython')
| 58 | return branch[1:].strip().decode('utf-8') |
| 59 | |
| 60 | def backport_pr(branch, num, project='ipython/ipython'): |
| 61 | current_branch = get_current_branch() |
| 62 | if branch != current_branch: |
| 63 | check_call(['git', 'checkout', branch]) |
| 64 | check_call(['git', 'pull']) |
| 65 | pr = get_pull_request(project, num, auth=True) |
| 66 | files = get_pull_request_files(project, num, auth=True) |
| 67 | patch_url = pr['patch_url'] |
| 68 | title = pr['title'] |
| 69 | description = pr['body'] |
| 70 | fname = "PR%i.patch" % num |
| 71 | if os.path.exists(fname): |
| 72 | print("using patch from {fname}".format(**locals())) |
| 73 | with open(fname, 'rb') as f: |
| 74 | patch = f.read() |
| 75 | else: |
| 76 | req = urlopen(patch_url) |
| 77 | patch = req.read() |
| 78 | |
| 79 | lines = description.splitlines() |
| 80 | if len(lines) > 5: |
| 81 | lines = lines[:5] + ['...'] |
| 82 | description = '\n'.join(lines) |
| 83 | |
| 84 | msg = "Backport PR #%i: %s" % (num, title) + '\n\n' + description |
| 85 | check = Popen(['git', 'apply', '--check', '--verbose'], stdin=PIPE) |
| 86 | a,b = check.communicate(patch) |
| 87 | |
| 88 | if check.returncode: |
| 89 | print("patch did not apply, saving to {fname}".format(**locals())) |
| 90 | print("edit {fname} until `cat {fname} | git apply --check` succeeds".format(**locals())) |
| 91 | print("then run tools/backport_pr.py {num} again".format(**locals())) |
| 92 | if not os.path.exists(fname): |
| 93 | with open(fname, 'wb') as f: |
| 94 | f.write(patch) |
| 95 | return 1 |
| 96 | |
| 97 | p = Popen(['git', 'apply'], stdin=PIPE) |
| 98 | a,b = p.communicate(patch) |
| 99 | |
| 100 | filenames = [ f['filename'] for f in files ] |
| 101 | |
| 102 | check_call(['git', 'add'] + filenames) |
| 103 | |
| 104 | check_call(['git', 'commit', '-m', msg]) |
| 105 | |
| 106 | print("PR #%i applied, with msg:" % num) |
| 107 | print() |
| 108 | print(msg) |
| 109 | print() |
| 110 | |
| 111 | if branch != current_branch: |
| 112 | check_call(['git', 'checkout', current_branch]) |
| 113 | |
| 114 | return 0 |
| 115 | |
| 116 | backport_re = re.compile(r"(?:[Bb]ackport|[Mm]erge).*#(\d+)") |
| 117 |
no test coverage detected