|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 | + |
| 4 | +""" |
| 5 | +The resolve command: walks the conflicts apply left behind, one branch at a time |
| 6 | +Takes its list of branches from the last local run, or from the report the bot left |
| 7 | +on a pull request, which is the only thing a reviewer has when CI did the analysis |
| 8 | +""" |
| 9 | + |
| 10 | +from commands.apply import backport_branch_name, branches_to_backport, load_run |
| 11 | +from commands.publish import branch_state, offer_publish, UNFINISHED |
| 12 | +from util.config import BackportError |
| 13 | +from util.git import ( |
| 14 | + WORKTREE_ROOT, |
| 15 | + continue_cherry_pick, |
| 16 | + files_with_conflict_markers, |
| 17 | + staged_files, |
| 18 | + unmerged_files, |
| 19 | +) |
| 20 | +from util.github import base_repo, read_plan |
| 21 | +from util.render import ask_yn |
| 22 | + |
| 23 | +from typing import Dict, Tuple |
| 24 | + |
| 25 | +# What happened to one branch |
| 26 | +FINISHED = "finished" |
| 27 | +LEFT = "left alone" |
| 28 | +STILL_STUCK = "still stuck" |
| 29 | +NOTHING_TO_DO = "nothing to do" |
| 30 | + |
| 31 | + |
| 32 | +def run_from_pr(number: str) -> Dict: |
| 33 | + """ |
| 34 | + The run the bot recorded on a pull request, as a run-shaped dict |
| 35 | + Returns {"fix": sha, "verdicts": {...}}. Raises when the pull request carries no |
| 36 | + plan, since guessing which branches were flagged would be worse than stopping |
| 37 | + """ |
| 38 | + plan = read_plan(base_repo("upstream"), number) |
| 39 | + if plan is None: |
| 40 | + raise BackportError( |
| 41 | + f"no backport plan found on #{number}.\n" |
| 42 | + " Only a pull request the bot has reported on carries one." |
| 43 | + ) |
| 44 | + return { |
| 45 | + "fix": plan["fix"], |
| 46 | + # Every branch the bot touched is worth offering, and branch_state below is |
| 47 | + # what decides which of them actually still need work |
| 48 | + "verdicts": {b: "affected" for b in plan.get("branches", {})}, |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +def resolve_branch(release: str, local_branch: str) -> Tuple[str, str]: |
| 53 | + """ |
| 54 | + Walks one conflicted branch: shows the files, waits, then finishes the pick |
| 55 | + Returns (outcome, detail) |
| 56 | + """ |
| 57 | + worktree = WORKTREE_ROOT / local_branch |
| 58 | + stuck = unmerged_files(worktree) |
| 59 | + print() |
| 60 | + print(f"{release}") |
| 61 | + print(f" worktree: {worktree}") |
| 62 | + if stuck: |
| 63 | + print(f" {len(stuck)} file(s) still conflicting:") |
| 64 | + for name in stuck: |
| 65 | + print(f" {name}") |
| 66 | + print(" Fix them there and 'git add' each one. Nothing is committed until you") |
| 67 | + print(" say so, and the pick is only finished once git sees nothing unmerged.") |
| 68 | + |
| 69 | + while True: |
| 70 | + if not ask_yn(" Resolved?"): |
| 71 | + return LEFT, "come back to it with 'backport resolve'" |
| 72 | + # git's own view first. A delete or rename conflict carries no markers, so a |
| 73 | + # marker check alone would wave it through and quietly keep one side |
| 74 | + still = unmerged_files(worktree) |
| 75 | + if still: |
| 76 | + print(f" git still calls {len(still)} file(s) unmerged:") |
| 77 | + for name in still: |
| 78 | + print(f" {name}") |
| 79 | + print(" Choose a side, then 'git add' each of them.") |
| 80 | + continue |
| 81 | + markers = files_with_conflict_markers( |
| 82 | + worktree, sorted(set(stuck) | set(staged_files(worktree))) |
| 83 | + ) |
| 84 | + if markers: |
| 85 | + # git will happily commit a file with the markers still in it |
| 86 | + print(f" These still have conflict markers: {', '.join(markers)}") |
| 87 | + continue |
| 88 | + failure = continue_cherry_pick(worktree) |
| 89 | + if failure: |
| 90 | + print(f" git could not finish it: {failure}") |
| 91 | + continue |
| 92 | + return FINISHED, f"{local_branch} is ready" |
| 93 | + |
| 94 | + |
| 95 | +def cmd_resolve(args) -> int: |
| 96 | + """ |
| 97 | + Finishes the conflicted backports, then offers to open their pull requests |
| 98 | + Returns 0 when nothing is left conflicting, 1 when a branch was left alone |
| 99 | + """ |
| 100 | + run = run_from_pr(args.pr) if args.pr else load_run() |
| 101 | + branches = branches_to_backport(run["verdicts"], args.branch) |
| 102 | + fix = run["fix"] |
| 103 | + |
| 104 | + stuck = [ |
| 105 | + b |
| 106 | + for b in branches |
| 107 | + if branch_state(b, backport_branch_name(fix, b)) == UNFINISHED |
| 108 | + ] |
| 109 | + if not stuck: |
| 110 | + print("Nothing is waiting on a conflict.") |
| 111 | + if args.pr: |
| 112 | + print("Run 'backport apply' first if the branches are not here yet.") |
| 113 | + return 0 |
| 114 | + |
| 115 | + print(f"Fix {fix[:10]}, {len(stuck)} branch(es) to resolve: {', '.join(stuck)}") |
| 116 | + outcomes = {} |
| 117 | + for release in stuck: |
| 118 | + outcomes[release] = resolve_branch(release, backport_branch_name(fix, release)) |
| 119 | + |
| 120 | + finished = [b for b, (o, _) in outcomes.items() if o == FINISHED] |
| 121 | + left = [b for b, (o, _) in outcomes.items() if o != FINISHED] |
| 122 | + print() |
| 123 | + print(f"{len(finished)} of {len(stuck)} resolved") |
| 124 | + if left: |
| 125 | + print(f"Still conflicting: {', '.join(left)}") |
| 126 | + |
| 127 | + if finished: |
| 128 | + offer_publish(run, finished, args.remote) |
| 129 | + return 1 if left else 0 |
0 commit comments