| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! `revert`: undo a changeset that landed with a new changeset, and land |
| 5 | //! that. |
| 6 | |
| 7 | use std::process::ExitCode; |
| 8 | |
| 9 | use anyhow::{Result, bail}; |
| 10 | use braid_format::ObjectId; |
| 11 | use braid_repo::Repo; |
| 12 | use braid_wc::{Identity, RevertRequest}; |
| 13 | use clap::Args; |
| 14 | use serde_json::json; |
| 15 | |
| 16 | use crate::hub::{self, link::Link}; |
| 17 | use crate::work::{IdentityArgs, Report, branches, identity, print_conflicts}; |
| 18 | use crate::{Cli, open_repo, print_json, resolve_changeset}; |
| 19 | |
| 20 | /// The long help of `revert`. |
| 21 | pub(crate) const REVERT_HELP: &str = |
| 22 | "Undo a changeset that landed on a branch: make a new changeset on it |
| 23 | whose changes put every file back as its first parent had it, modes, |
| 24 | symlinks and submodules included, and land that on the branch. The message |
| 25 | is `Revert \"<subject>\"`, and the changeset has a `reverts` field naming |
| 26 | the one it undoes. |
| 27 | |
| 28 | Landing works as for any change: when the branch has moved on since, the |
| 29 | revert is rebased onto its head path by path, so later changes to other |
| 30 | lines and files stay. When a later change touched the same lines, the |
| 31 | landing stops with conflicts, the branch does not move and the exit status |
| 32 | is 2: undo the change by hand in a workspace instead. |
| 33 | |
| 34 | A changeset without parents cannot be reverted, nor a merge unless |
| 35 | --mainline 1 says to undo what it brought in against its first parent; |
| 36 | --mainline is refused for a changeset that is not a merge. --branch must |
| 37 | name a branch, not a ref in one of braid's own namespaces. |
| 38 | --no-land only makes the changeset and prints its id. In a repository |
| 39 | linked to a hub, the revert lands on the hub, then fetches; --local, or |
| 40 | BRAID_HUB=off, lands here. |
| 41 | |
| 42 | With --json it prints revert, the new changeset's id; reverts, the one it |
| 43 | undoes; branch; and land, the report of the landing as `braid land --help` |
| 44 | describes it, or null with --no-land."; |
| 45 | |
| 46 | #[derive(Debug, Args)] |
| 47 | pub(crate) struct RevertArgs { |
| 48 | /// The changeset to undo: a ref, an id, or a short id (6 hex digits or |
| 49 | /// more). |
| 50 | revision: String, |
| 51 | /// The branch to land the revert on (default: main, master, or the |
| 52 | /// repository's only branch). |
| 53 | #[arg(long, value_name = "BRANCH")] |
| 54 | branch: Option<String>, |
| 55 | /// Only make the revert and print its id; do not land it. |
| 56 | #[arg(long)] |
| 57 | no_land: bool, |
| 58 | /// Revert a merge against this parent: only 1, the first, is supported. |
| 59 | #[arg(long, value_name = "PARENT", value_parser = clap::value_parser!(u8).range(1..=1))] |
| 60 | mainline: Option<u8>, |
| 61 | #[command(flatten)] |
| 62 | identity: IdentityArgs, |
| 63 | /// Land in this repository even if it is linked to a hub. |
| 64 | #[arg(long)] |
| 65 | local: bool, |
| 66 | } |
| 67 | |
| 68 | /// The branch a command acts on: the one given, or else the repository's |
| 69 | /// main branch: `main`, `master`, or its only branch. A name in one of |
| 70 | /// braid's own namespaces, such as a green ref, is refused. |
| 71 | pub(crate) fn branch_or_main(repo: &Repo, given: Option<&str>) -> Result<String> { |
| 72 | let branch = match given { |
| 73 | Some(branch) => branch.to_owned(), |
| 74 | None => crate::agents::main_branch(&branches(&repo.store().refs()?))?, |
| 75 | }; |
| 76 | if !braid_repo::is_branch(&branch) { |
| 77 | bail!( |
| 78 | "{branch:?} is in one of braid's own namespaces (braid/, git/, remote/, tags/), not a branch" |
| 79 | ); |
| 80 | } |
| 81 | Ok(branch) |
| 82 | } |
| 83 | |
| 84 | /// Refuses to revert `culprit` on `branch` unless it is on the branch's |
| 85 | /// first-parent chain, as landing requires: anything else would be stored |
| 86 | /// first and refused after. |
| 87 | pub(crate) fn check_on_branch(repo: &Repo, branch: &str, culprit: ObjectId) -> Result<()> { |
| 88 | let Some(head) = repo.store().refs()?.get(branch) else { |
| 89 | bail!("there is no branch {branch:?} to land the revert on"); |
| 90 | }; |
| 91 | if !braid_engine::first_parent_chain_contains(repo.store(), head, culprit, usize::MAX)? { |
| 92 | bail!( |
| 93 | "changeset {} is not on {branch}'s first-parent chain: only what landed on {branch} can be reverted there", |
| 94 | culprit.short() |
| 95 | ); |
| 96 | } |
| 97 | Ok(()) |
| 98 | } |
| 99 | |
| 100 | /// Lands `change`, a revert made with [`braid_wc::create_revert`], on |
| 101 | /// `branch`: on the hub of `link`, then fetching, or in this repository. |
| 102 | pub(crate) fn land_revert( |
| 103 | repo: &Repo, |
| 104 | link: Option<&Link>, |
| 105 | branch: &str, |
| 106 | change: ObjectId, |
| 107 | agent: &str, |
| 108 | committer: &Identity, |
| 109 | ) -> Result<Report> { |
| 110 | Ok(match link { |
| 111 | Some(link) => { |
| 112 | let hub = hub::connect(link)?; |
| 113 | let remote = hub.repo(link.repo.clone()); |
| 114 | let response = |
| 115 | hub::land::land_change(repo, &remote, link, branch, change, agent, "braid revert")?; |
| 116 | Report::hub(&response) |
| 117 | } |
| 118 | None => Report::landing(&braid_wc::land_change( |
| 119 | repo.store(), |
| 120 | branch, |
| 121 | change, |
| 122 | agent, |
| 123 | committer, |
| 124 | )?), |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | pub(crate) fn revert(cli: &Cli, args: &RevertArgs) -> Result<ExitCode> { |
| 129 | let repo = open_repo(cli)?; |
| 130 | let culprit = resolve_changeset(&repo, &args.revision)?; |
| 131 | let branch = branch_or_main(&repo, args.branch.as_deref())?; |
| 132 | let author = identity(&args.identity, &repo)?; |
| 133 | let link = if args.no_land { |
| 134 | None |
| 135 | } else { |
| 136 | hub::linked(&repo, args.local)? |
| 137 | }; |
| 138 | if !args.no_land && link.is_none() { |
| 139 | check_on_branch(&repo, &branch, culprit)?; |
| 140 | } |
| 141 | let change = braid_wc::create_revert( |
| 142 | repo.store(), |
| 143 | &RevertRequest { |
| 144 | culprit, |
| 145 | mainline: args.mainline.is_some(), |
| 146 | author: author.clone(), |
| 147 | agent: cli.agent.clone(), |
| 148 | }, |
| 149 | )?; |
| 150 | let report = if args.no_land { |
| 151 | None |
| 152 | } else { |
| 153 | Some(land_revert( |
| 154 | &repo, |
| 155 | link.as_ref(), |
| 156 | &branch, |
| 157 | change, |
| 158 | &cli.agent, |
| 159 | &author, |
| 160 | )?) |
| 161 | }; |
| 162 | let code = report.as_ref().map_or(ExitCode::SUCCESS, Report::exit_code); |
| 163 | if let Some(report) = &report { |
| 164 | report.note_red(); |
| 165 | } |
| 166 | if cli.json { |
| 167 | print_json(&json!({ |
| 168 | "revert": change.to_hex(), |
| 169 | "reverts": culprit.to_hex(), |
| 170 | "branch": branch, |
| 171 | "land": report.as_ref().map(Report::json), |
| 172 | }))?; |
| 173 | return Ok(code); |
| 174 | } |
| 175 | match &report { |
| 176 | None => println!( |
| 177 | "Made {}, the revert of {}; it has not landed", |
| 178 | change.short(), |
| 179 | culprit.short() |
| 180 | ), |
| 181 | Some(report) if report.conflicted() => { |
| 182 | println!( |
| 183 | "The revert of {} conflicts with what landed on {branch} since; {branch} did not move:", |
| 184 | culprit.short() |
| 185 | ); |
| 186 | print_conflicts(&report.conflicts); |
| 187 | println!("Undo the change by hand in a workspace instead."); |
| 188 | } |
| 189 | Some(report) if report.outcome == "already-applied" => println!( |
| 190 | "{branch} has what the revert of {} would change already: nothing landed", |
| 191 | culprit.short() |
| 192 | ), |
| 193 | Some(report) => { |
| 194 | println!("Reverted {} with {}", culprit.short(), change.short()); |
| 195 | report.print_landing(cli); |
| 196 | } |
| 197 | } |
| 198 | Ok(code) |
| 199 | } |