| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Why a workspace operation failed. |
| 5 | |
| 6 | use std::io; |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | |
| 9 | use braid_format::ObjectId; |
| 10 | |
| 11 | use crate::blocker::Blocker; |
| 12 | use crate::paths::show; |
| 13 | |
| 14 | /// Why a workspace operation failed. |
| 15 | /// |
| 16 | /// Conflicts are not errors: landing, updating and committing report them as |
| 17 | /// outcomes. These are the failures, after which the branch has not moved |
| 18 | /// and the workspace is as it was, except where a variant says otherwise. |
| 19 | #[derive(Debug, thiserror::Error)] |
| 20 | #[non_exhaustive] |
| 21 | pub enum Error { |
| 22 | /// A file system call failed. |
| 23 | #[error("cannot {action} {}: {source}", .path.display())] |
| 24 | Io { |
| 25 | /// What was being done, as a verb. |
| 26 | action: &'static str, |
| 27 | /// The file or directory involved. |
| 28 | path: PathBuf, |
| 29 | /// The error from the operating system. |
| 30 | #[source] |
| 31 | source: io::Error, |
| 32 | }, |
| 33 | |
| 34 | /// The object store failed. |
| 35 | #[error(transparent)] |
| 36 | Store(#[from] braid_store::Error), |
| 37 | |
| 38 | /// The repository could not be opened or read. |
| 39 | #[error(transparent)] |
| 40 | Repo(#[from] braid_repo::Error), |
| 41 | |
| 42 | /// The engine refused an operation or found invalid data. |
| 43 | #[error(transparent)] |
| 44 | Engine(#[from] braid_engine::Error), |
| 45 | |
| 46 | /// A changeset could not be assembled. |
| 47 | #[error("cannot assemble a changeset: {0}")] |
| 48 | Assemble(#[from] braid_format::ChangesetError), |
| 49 | |
| 50 | /// The current directory is not inside a workspace. |
| 51 | #[error( |
| 52 | "not inside a workspace: run this in your workspace's directory, or pass --workspace <name> (`braid workspace list` shows them; `braid workspace add <name>` makes one in .braid/work/<name>)" |
| 53 | )] |
| 54 | NotAWorkspace, |
| 55 | |
| 56 | /// A workspace name that is not allowed. |
| 57 | #[error( |
| 58 | "invalid workspace name {0:?}: use 1 to 64 letters, digits, '.', '_' or '-', starting with a letter or digit" |
| 59 | )] |
| 60 | InvalidName(String), |
| 61 | |
| 62 | /// A workspace with this name exists already. |
| 63 | #[error("workspace {0:?} exists already")] |
| 64 | WorkspaceExists(String), |
| 65 | |
| 66 | /// No workspace has this name. |
| 67 | #[error("no workspace named {0:?}")] |
| 68 | NoSuchWorkspace(String), |
| 69 | |
| 70 | /// The directory for a new workspace exists and is not empty. |
| 71 | #[error("{} exists and is not empty", .0.display())] |
| 72 | DirectoryNotEmpty(PathBuf), |
| 73 | |
| 74 | /// A file of the workspace's state is damaged. |
| 75 | #[error("{}: {detail}", .path.display())] |
| 76 | BadState { |
| 77 | /// The file. |
| 78 | path: PathBuf, |
| 79 | /// What is wrong with it. |
| 80 | detail: String, |
| 81 | }, |
| 82 | |
| 83 | /// A checkout would be unsafe or impossible: a name this platform or |
| 84 | /// file system cannot hold, a symlink in the way, untracked data that |
| 85 | /// would be overwritten. Nothing was written. |
| 86 | #[error("cannot write {:?}: {reason}", show(.path))] |
| 87 | Unsafe { |
| 88 | /// The path in the repository. |
| 89 | path: Vec<u8>, |
| 90 | /// Why. |
| 91 | reason: String, |
| 92 | }, |
| 93 | |
| 94 | /// Two paths of a tree would be the same file on this file system, |
| 95 | /// which ignores letter case or Unicode normalisation. Neither was |
| 96 | /// written. |
| 97 | #[error("{:?} and {:?} would be the same file on this file system", show(.first), show(.second))] |
| 98 | Collision { |
| 99 | /// One path. |
| 100 | first: Vec<u8>, |
| 101 | /// The other. |
| 102 | second: Vec<u8>, |
| 103 | }, |
| 104 | |
| 105 | /// The workspace has changes that are not committed. |
| 106 | #[error("the workspace has uncommitted changes ({}); commit them first", some_paths(*.count, .first))] |
| 107 | Uncommitted { |
| 108 | /// How many paths changed. |
| 109 | count: usize, |
| 110 | /// The first of them. |
| 111 | first: Vec<u8>, |
| 112 | }, |
| 113 | |
| 114 | /// The workspace holds new files that the secrets guard holds back, |
| 115 | /// which removing it would delete. They are not uncommitted changes to |
| 116 | /// commit: any of them may be a secret. |
| 117 | #[error("{}", held_back(*.count, .first))] |
| 118 | HeldBack { |
| 119 | /// How many there are. |
| 120 | count: usize, |
| 121 | /// The first of them. |
| 122 | first: Vec<u8>, |
| 123 | }, |
| 124 | |
| 125 | /// The command needs the rebase in progress to be finished first. |
| 126 | #[error("a rebase is in progress: resolve the conflicts and run `braid commit` first")] |
| 127 | RebaseInProgress, |
| 128 | |
| 129 | /// `commit` was given paths while a rebase is in progress: the |
| 130 | /// resolution is recorded whole. |
| 131 | #[error("while a rebase is in progress, commit records the whole working copy: give no paths")] |
| 132 | PathsDuringRebase, |
| 133 | |
| 134 | /// The workspace has changesets that its branch does not have. |
| 135 | #[error( |
| 136 | "the workspace has {count} changesets that {branch} does not have; land them, or pass --force" |
| 137 | )] |
| 138 | Unlanded { |
| 139 | /// How many. |
| 140 | count: usize, |
| 141 | /// The branch. |
| 142 | branch: String, |
| 143 | }, |
| 144 | |
| 145 | /// `resolve` was used with no rebase in progress. |
| 146 | #[error("no rebase is in progress")] |
| 147 | NoRebase, |
| 148 | |
| 149 | /// `resolve` named a path without a conflict, or a content conflict. |
| 150 | #[error("{:?}: {reason}", show(.path))] |
| 151 | NotResolvable { |
| 152 | /// The path. |
| 153 | path: Vec<u8>, |
| 154 | /// Why it cannot be marked resolved. |
| 155 | reason: &'static str, |
| 156 | }, |
| 157 | |
| 158 | /// The working copy cannot be committed as it is: conflict markers |
| 159 | /// remain, conflicts are not marked resolved, or new files wait for |
| 160 | /// `braid track`. Every reason is listed, as `status` lists them. |
| 161 | #[error("cannot commit yet:\n{}\n{}", reasons(.0), advice(.0))] |
| 162 | Blocked(Vec<Blocker>), |
| 163 | |
| 164 | /// There is nothing to commit. |
| 165 | #[error("nothing to commit")] |
| 166 | NothingToCommit, |
| 167 | |
| 168 | /// A commit needs a message. |
| 169 | #[error("a commit needs a message: pass -m")] |
| 170 | NoMessage, |
| 171 | |
| 172 | /// No author is configured. |
| 173 | #[error( |
| 174 | "no author: pass --author \"Name <email>\", set BRAID_AUTHOR, or set name and email in .braid/config" |
| 175 | )] |
| 176 | NoAuthor, |
| 177 | |
| 178 | /// An author that cannot be used. |
| 179 | #[error("invalid author {text:?}: {reason}")] |
| 180 | InvalidAuthor { |
| 181 | /// What was given. |
| 182 | text: String, |
| 183 | /// Why it was refused. |
| 184 | reason: &'static str, |
| 185 | }, |
| 186 | |
| 187 | /// A path given on the command line is outside the workspace. |
| 188 | #[error("{} is outside the workspace", .0.display())] |
| 189 | OutsideWorkspace(PathBuf), |
| 190 | |
| 191 | /// A path in the repository that is not valid. |
| 192 | #[error("invalid path {:?}: {error}", show(.path))] |
| 193 | InvalidPath { |
| 194 | /// The path. |
| 195 | path: Vec<u8>, |
| 196 | /// What is wrong with it. |
| 197 | error: braid_format::PathError, |
| 198 | }, |
| 199 | |
| 200 | /// `change create --expect` did not match the base. |
| 201 | #[error("expected {:?} to be {expected} in the base, it is {found}", show(.path))] |
| 202 | ExpectationFailed { |
| 203 | /// The path. |
| 204 | path: Vec<u8>, |
| 205 | /// What was expected: a blob id or `absent`. |
| 206 | expected: String, |
| 207 | /// What the base has. |
| 208 | found: String, |
| 209 | }, |
| 210 | |
| 211 | /// The workspace's base is not on the branch, so its changes cannot be |
| 212 | /// landed as they are. |
| 213 | #[error("the workspace's changes are not based on {branch}; run `braid update` first")] |
| 214 | NotOnBranch { |
| 215 | /// The branch. |
| 216 | branch: String, |
| 217 | }, |
| 218 | |
| 219 | /// An object of the wrong kind where a changeset was expected. |
| 220 | #[error("{0} is not a changeset")] |
| 221 | NotAChangeset(ObjectId), |
| 222 | |
| 223 | /// A stack to land holds no changeset. |
| 224 | #[error("there is nothing to land: the stack is empty")] |
| 225 | EmptyStack, |
| 226 | |
| 227 | /// A stack to land whose changesets do not follow each other. |
| 228 | #[error( |
| 229 | "not a stack: changeset {index} of it does not have the one before it as its first parent" |
| 230 | )] |
| 231 | NotAStack { |
| 232 | /// The position of the first changeset that does not follow. |
| 233 | index: usize, |
| 234 | }, |
| 235 | |
| 236 | /// A changeset without parents cannot be reverted: there is nothing to |
| 237 | /// go back to. |
| 238 | #[error("changeset {} has no parent, so there is nothing to revert it to", .0.short())] |
| 239 | RevertRoot(ObjectId), |
| 240 | |
| 241 | /// A merge is reverted only on request, against its first parent. |
| 242 | #[error( |
| 243 | "changeset {} is a merge of {parents} parents: pass --mainline 1 to undo what it brought in against its first parent", |
| 244 | .changeset.short() |
| 245 | )] |
| 246 | RevertMerge { |
| 247 | /// The merge. |
| 248 | changeset: ObjectId, |
| 249 | /// How many parents it has. |
| 250 | parents: usize, |
| 251 | }, |
| 252 | |
| 253 | /// `--mainline` was given for a changeset that is not a merge. |
| 254 | #[error("changeset {} is not a merge: --mainline is only for reverting a merge", .0.short())] |
| 255 | NotAMerge(ObjectId), |
| 256 | |
| 257 | /// The changeset changes no file, so its revert would change none. |
| 258 | #[error("changeset {} changes no file against its first parent: there is nothing to revert", .0.short())] |
| 259 | NothingToRevert(ObjectId), |
| 260 | |
| 261 | /// The workspace's base changed while its changesets were landing |
| 262 | /// somewhere else. |
| 263 | #[error( |
| 264 | "the workspace moved while its changes were landing (its base was {}, it is {}); run `braid update`", |
| 265 | short(.expected), |
| 266 | short(.found) |
| 267 | )] |
| 268 | BaseMoved { |
| 269 | /// The base when the stack was taken. |
| 270 | expected: Option<ObjectId>, |
| 271 | /// The base now. |
| 272 | found: Option<ObjectId>, |
| 273 | }, |
| 274 | } |
| 275 | |
| 276 | fn short(id: &Option<ObjectId>) -> String { |
| 277 | match id { |
| 278 | Some(id) => id.short(), |
| 279 | None => "nothing".to_owned(), |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | impl Error { |
| 284 | /// Wraps an I/O error with the action and the path it happened on. |
| 285 | pub(crate) fn io( |
| 286 | action: &'static str, |
| 287 | path: impl AsRef<Path>, |
| 288 | ) -> impl FnOnce(io::Error) -> Self { |
| 289 | let path = path.as_ref().to_path_buf(); |
| 290 | move |source| Error::Io { |
| 291 | action, |
| 292 | path, |
| 293 | source, |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /// `1 path, "a"`, or `3 paths, starting with "a"`. |
| 299 | fn some_paths(count: usize, first: &[u8]) -> String { |
| 300 | match count { |
| 301 | 1 => format!("1 path, {:?}", show(first)), |
| 302 | n => format!("{n} paths, starting with {:?}", show(first)), |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /// Why a workspace that holds files the secrets guard holds back is not |
| 307 | /// removed, and what to do: never to commit them as they are, since any of |
| 308 | /// them may be a secret. |
| 309 | fn held_back(count: usize, first: &[u8]) -> String { |
| 310 | let first = show(first); |
| 311 | if count == 1 { |
| 312 | format!( |
| 313 | "the workspace holds a new file the secrets guard holds back, {first:?}, which removing the workspace would delete: move it out of the workspace if it is a secret, or `braid track` and commit it if it is not; --force removes the workspace with it" |
| 314 | ) |
| 315 | } else { |
| 316 | format!( |
| 317 | "the workspace holds {count} new files the secrets guard holds back, starting with {first:?}, which removing the workspace would delete: move the secrets among them out of the workspace, and `braid track` and commit the files that are not secrets; --force removes the workspace with them" |
| 318 | ) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /// How many reasons a refusal lists, at most. |
| 323 | const REASONS_SHOWN: usize = 20; |
| 324 | |
| 325 | /// What to do about a refused commit. `--force` commits past conflicts as |
| 326 | /// they are, markers and all, so it is offered as a way out only when the |
| 327 | /// guard alone holds files back. |
| 328 | fn advice(blockers: &[Blocker]) -> &'static str { |
| 329 | if blockers |
| 330 | .iter() |
| 331 | .any(|blocker| !matches!(blocker, Blocker::NeedsTrack(_))) |
| 332 | { |
| 333 | "Settle these first. --force would commit the conflicts as they are, markers and all, and leave out the files waiting for `braid track`." |
| 334 | } else { |
| 335 | "Move a secret out of the workspace, `braid track` a file that is not one, or commit without them with --force." |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | fn reasons(blockers: &[Blocker]) -> String { |
| 340 | let mut shown: Vec<String> = blockers |
| 341 | .iter() |
| 342 | .take(REASONS_SHOWN) |
| 343 | .map(|blocker| format!(" - {blocker}")) |
| 344 | .collect(); |
| 345 | if blockers.len() > REASONS_SHOWN { |
| 346 | shown.push(format!( |
| 347 | " - and {} more: see `braid status`", |
| 348 | blockers.len() - REASONS_SHOWN |
| 349 | )); |
| 350 | } |
| 351 | shown.join("\n") |
| 352 | } |