| 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("not inside a workspace; create one with `braid workspace add <name>`")] |
| 52 | NotAWorkspace, |
| 53 | |
| 54 | /// A workspace name that is not allowed. |
| 55 | #[error( |
| 56 | "invalid workspace name {0:?}: use 1 to 64 letters, digits, '.', '_' or '-', starting with a letter or digit" |
| 57 | )] |
| 58 | InvalidName(String), |
| 59 | |
| 60 | /// A workspace with this name exists already. |
| 61 | #[error("workspace {0:?} exists already")] |
| 62 | WorkspaceExists(String), |
| 63 | |
| 64 | /// No workspace has this name. |
| 65 | #[error("no workspace named {0:?}")] |
| 66 | NoSuchWorkspace(String), |
| 67 | |
| 68 | /// The directory for a new workspace exists and is not empty. |
| 69 | #[error("{} exists and is not empty", .0.display())] |
| 70 | DirectoryNotEmpty(PathBuf), |
| 71 | |
| 72 | /// A file of the workspace's state is damaged. |
| 73 | #[error("{}: {detail}", .path.display())] |
| 74 | BadState { |
| 75 | /// The file. |
| 76 | path: PathBuf, |
| 77 | /// What is wrong with it. |
| 78 | detail: String, |
| 79 | }, |
| 80 | |
| 81 | /// A checkout would be unsafe or impossible: a name this platform or |
| 82 | /// file system cannot hold, a symlink in the way, untracked data that |
| 83 | /// would be overwritten. Nothing was written. |
| 84 | #[error("cannot write {:?}: {reason}", show(.path))] |
| 85 | Unsafe { |
| 86 | /// The path in the repository. |
| 87 | path: Vec<u8>, |
| 88 | /// Why. |
| 89 | reason: String, |
| 90 | }, |
| 91 | |
| 92 | /// Two paths of a tree would be the same file on this file system, |
| 93 | /// which ignores letter case or Unicode normalisation. Neither was |
| 94 | /// written. |
| 95 | #[error("{:?} and {:?} would be the same file on this file system", show(.first), show(.second))] |
| 96 | Collision { |
| 97 | /// One path. |
| 98 | first: Vec<u8>, |
| 99 | /// The other. |
| 100 | second: Vec<u8>, |
| 101 | }, |
| 102 | |
| 103 | /// The workspace has changes that are not committed. |
| 104 | #[error("the workspace has uncommitted changes ({count} paths, starting with {:?}); commit them first", show(.first))] |
| 105 | Uncommitted { |
| 106 | /// How many paths changed. |
| 107 | count: usize, |
| 108 | /// The first of them. |
| 109 | first: Vec<u8>, |
| 110 | }, |
| 111 | |
| 112 | /// The command needs the rebase in progress to be finished first. |
| 113 | #[error("a rebase is in progress: resolve the conflicts and run `braid commit` first")] |
| 114 | RebaseInProgress, |
| 115 | |
| 116 | /// `commit` was given paths while a rebase is in progress: the |
| 117 | /// resolution is recorded whole. |
| 118 | #[error("while a rebase is in progress, commit records the whole working copy: give no paths")] |
| 119 | PathsDuringRebase, |
| 120 | |
| 121 | /// The workspace has changesets that its branch does not have. |
| 122 | #[error( |
| 123 | "the workspace has {count} changesets that {branch} does not have; land them, or pass --force" |
| 124 | )] |
| 125 | Unlanded { |
| 126 | /// How many. |
| 127 | count: usize, |
| 128 | /// The branch. |
| 129 | branch: String, |
| 130 | }, |
| 131 | |
| 132 | /// `resolve` was used with no rebase in progress. |
| 133 | #[error("no rebase is in progress")] |
| 134 | NoRebase, |
| 135 | |
| 136 | /// `resolve` named a path without a conflict, or a content conflict. |
| 137 | #[error("{:?}: {reason}", show(.path))] |
| 138 | NotResolvable { |
| 139 | /// The path. |
| 140 | path: Vec<u8>, |
| 141 | /// Why it cannot be marked resolved. |
| 142 | reason: &'static str, |
| 143 | }, |
| 144 | |
| 145 | /// The working copy cannot be committed as it is: conflict markers |
| 146 | /// remain, conflicts are not marked resolved, or new files wait for |
| 147 | /// `braid track`. Every reason is listed, as `status` lists them. |
| 148 | #[error("cannot commit yet:\n{}\nSettle these first, or commit anyway with --force: files waiting for `braid track` stay out.", reasons(.0))] |
| 149 | Blocked(Vec<Blocker>), |
| 150 | |
| 151 | /// There is nothing to commit. |
| 152 | #[error("nothing to commit")] |
| 153 | NothingToCommit, |
| 154 | |
| 155 | /// A commit needs a message. |
| 156 | #[error("a commit needs a message: pass -m")] |
| 157 | NoMessage, |
| 158 | |
| 159 | /// No author is configured. |
| 160 | #[error( |
| 161 | "no author: pass --author \"Name <email>\", set BRAID_AUTHOR, or set name and email in .braid/config" |
| 162 | )] |
| 163 | NoAuthor, |
| 164 | |
| 165 | /// An author that cannot be used. |
| 166 | #[error("invalid author {text:?}: {reason}")] |
| 167 | InvalidAuthor { |
| 168 | /// What was given. |
| 169 | text: String, |
| 170 | /// Why it was refused. |
| 171 | reason: &'static str, |
| 172 | }, |
| 173 | |
| 174 | /// A path given on the command line is outside the workspace. |
| 175 | #[error("{} is outside the workspace", .0.display())] |
| 176 | OutsideWorkspace(PathBuf), |
| 177 | |
| 178 | /// A path in the repository that is not valid. |
| 179 | #[error("invalid path {:?}: {error}", show(.path))] |
| 180 | InvalidPath { |
| 181 | /// The path. |
| 182 | path: Vec<u8>, |
| 183 | /// What is wrong with it. |
| 184 | error: braid_format::PathError, |
| 185 | }, |
| 186 | |
| 187 | /// `change create --expect` did not match the base. |
| 188 | #[error("expected {:?} to be {expected} in the base, it is {found}", show(.path))] |
| 189 | ExpectationFailed { |
| 190 | /// The path. |
| 191 | path: Vec<u8>, |
| 192 | /// What was expected: a blob id or `absent`. |
| 193 | expected: String, |
| 194 | /// What the base has. |
| 195 | found: String, |
| 196 | }, |
| 197 | |
| 198 | /// The workspace's base is not on the branch, so its changes cannot be |
| 199 | /// landed as they are. |
| 200 | #[error("the workspace's changes are not based on {branch}; run `braid update` first")] |
| 201 | NotOnBranch { |
| 202 | /// The branch. |
| 203 | branch: String, |
| 204 | }, |
| 205 | |
| 206 | /// An object of the wrong kind where a changeset was expected. |
| 207 | #[error("{0} is not a changeset")] |
| 208 | NotAChangeset(ObjectId), |
| 209 | } |
| 210 | |
| 211 | impl Error { |
| 212 | /// Wraps an I/O error with the action and the path it happened on. |
| 213 | pub(crate) fn io( |
| 214 | action: &'static str, |
| 215 | path: impl AsRef<Path>, |
| 216 | ) -> impl FnOnce(io::Error) -> Self { |
| 217 | let path = path.as_ref().to_path_buf(); |
| 218 | move |source| Error::Io { |
| 219 | action, |
| 220 | path, |
| 221 | source, |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /// How many reasons a refusal lists, at most. |
| 227 | const REASONS_SHOWN: usize = 20; |
| 228 | |
| 229 | fn reasons(blockers: &[Blocker]) -> String { |
| 230 | let mut shown: Vec<String> = blockers |
| 231 | .iter() |
| 232 | .take(REASONS_SHOWN) |
| 233 | .map(|blocker| format!(" - {blocker}")) |
| 234 | .collect(); |
| 235 | if blockers.len() > REASONS_SHOWN { |
| 236 | shown.push(format!( |
| 237 | " - and {} more: see `braid status`", |
| 238 | blockers.len() - REASONS_SHOWN |
| 239 | )); |
| 240 | } |
| 241 | shown.join("\n") |
| 242 | } |