|
| 1 | +"""add a new pr branch or convert aan existing branch to one""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import typing |
| 6 | + |
| 7 | +import rich.text |
| 8 | + |
| 9 | +from git_pr_helper import styles |
| 10 | +from git_pr_helper.utils import PrBranchInfo |
| 11 | +from git_pr_helper.utils import git |
| 12 | +from git_pr_helper.utils import write_pr_branch_info |
| 13 | + |
| 14 | +if typing.TYPE_CHECKING: |
| 15 | + import argparse |
| 16 | + |
| 17 | + import rich.console |
| 18 | + |
| 19 | + |
| 20 | +def configure_parser(parser: argparse.ArgumentParser): |
| 21 | + parser.add_argument( |
| 22 | + "--prune", |
| 23 | + action="store_true", |
| 24 | + help="remove branches that have the same HEAD as the PR", |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + "pr", |
| 28 | + help="the pr to add, in the format <remote>#<number>", |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def run(console: rich.console.Console, args: argparse.Namespace): |
| 33 | + pr_remote, _, pr_number = args.pr.rpartition("#") |
| 34 | + if not pr_remote: |
| 35 | + pr_remote = "*" |
| 36 | + |
| 37 | + try: |
| 38 | + pr_number = str(int(pr_number)) |
| 39 | + except ValueError: |
| 40 | + console.print( |
| 41 | + rich.text.Text.assemble( |
| 42 | + ("error", styles.ERROR), |
| 43 | + ": ", |
| 44 | + (pr_number, styles.ACCENT), |
| 45 | + " is not a number", |
| 46 | + ) |
| 47 | + ) |
| 48 | + return 1 |
| 49 | + |
| 50 | + head_hashes = [ |
| 51 | + line.partition(" ")[::2] |
| 52 | + for line in git( |
| 53 | + "for-each-ref", |
| 54 | + "--format=%(objectname) %(refname:strip=2)", |
| 55 | + f"refs/remotes/{pr_remote}/pr/{pr_number}", |
| 56 | + ) |
| 57 | + ] |
| 58 | + if not head_hashes: |
| 59 | + console.print( |
| 60 | + rich.text.Text.assemble( |
| 61 | + ("error", styles.ERROR), |
| 62 | + ": did not find a matching pr", |
| 63 | + ) |
| 64 | + ) |
| 65 | + return 1 |
| 66 | + elif len(head_hashes) > 1: |
| 67 | + console.print( |
| 68 | + rich.text.Text.assemble( |
| 69 | + ("error", styles.ERROR), |
| 70 | + ": found more than one PR: ", |
| 71 | + rich.text.Text(", ").join( |
| 72 | + rich.text.Text(pr_remote.replace("/pr/", "#"), style=styles.ACCENT) |
| 73 | + for _, pr_remote in head_hashes |
| 74 | + ), |
| 75 | + ) |
| 76 | + ) |
| 77 | + return 1 |
| 78 | + head_hash, pr_remote = head_hashes[0] |
| 79 | + pr_remote = pr_remote.partition("/")[0] |
| 80 | + |
| 81 | + branches = git( |
| 82 | + "for-each-ref", |
| 83 | + "--points-at", |
| 84 | + head_hash, |
| 85 | + "--exclude", |
| 86 | + "refs/remotes/**/pr/*", |
| 87 | + "--format", |
| 88 | + "%(refname:strip=2)", |
| 89 | + "refs/remotes/**", |
| 90 | + ) |
| 91 | + if len(branches) > 1: |
| 92 | + console.print( |
| 93 | + rich.text.Text.assemble( |
| 94 | + ("More than one branch found", styles.ERROR), |
| 95 | + ": ", |
| 96 | + rich.text.Text(", ").join( |
| 97 | + rich.text.Text(branch, style=styles.ACCENT) for branch in branches |
| 98 | + ), |
| 99 | + ) |
| 100 | + ) |
| 101 | + branches = [] |
| 102 | + |
| 103 | + if branches: |
| 104 | + remote, _, branch = branches[0].partition("/") |
| 105 | + |
| 106 | + else: |
| 107 | + console.show_cursor() |
| 108 | + info = console.input( |
| 109 | + rich.text.Text.assemble( |
| 110 | + "Enter branch info (", |
| 111 | + ("<user>/<repo>", styles.ACCENT), |
| 112 | + ":", |
| 113 | + ("<branch>", styles.ACCENT), |
| 114 | + "): ", |
| 115 | + ) |
| 116 | + ) |
| 117 | + console.show_cursor(False) |
| 118 | + remote, _, branch = info.partition(":") |
| 119 | + if "/" in remote: |
| 120 | + remote = f"git@github.com:{remote}.git" |
| 121 | + |
| 122 | + name = f"pr/{pr_number}" |
| 123 | + git("switch", "-c", name, "--track", f"{pr_remote}/{name}") |
| 124 | + write_pr_branch_info(name, PrBranchInfo(remote, branch, [])) |
| 125 | + |
| 126 | + if args.prune: |
| 127 | + branches = git( |
| 128 | + "for-each-ref", |
| 129 | + "--points-at", |
| 130 | + head_hash, |
| 131 | + "--exclude", |
| 132 | + "refs/heads/**/pr/*", |
| 133 | + "--format", |
| 134 | + "%(refname:strip=2)", |
| 135 | + "refs/heads/**", |
| 136 | + ) |
| 137 | + if branches: |
| 138 | + git("branch", "-D", branch) |
| 139 | + console.print( |
| 140 | + rich.text.Text.assemble( |
| 141 | + "Pruned branches: ", |
| 142 | + rich.text.Text(", ").join( |
| 143 | + rich.text.Text(branch, style=styles.ACCENT) |
| 144 | + for branch in branches |
| 145 | + ), |
| 146 | + ) |
| 147 | + ) |
| 148 | + |
| 149 | + return 0 |
0 commit comments