Elffuss BraidHub

utopia-ia/elffuss-braid public

2dd40509e7e2 · 8.1 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Why a workspace operation failed.
5
6use std::io;
7use std::path::{Path, PathBuf};
8
9use braid_format::ObjectId;
10
11use crate::blocker::Blocker;
12use 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]
21pub 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 ({count} paths, starting with {:?}); commit them first", show(.first))]
107 Uncommitted {
108 /// How many paths changed.
109 count: usize,
110 /// The first of them.
111 first: Vec<u8>,
112 },
113
114 /// The command needs the rebase in progress to be finished first.
115 #[error("a rebase is in progress: resolve the conflicts and run `braid commit` first")]
116 RebaseInProgress,
117
118 /// `commit` was given paths while a rebase is in progress: the
119 /// resolution is recorded whole.
120 #[error("while a rebase is in progress, commit records the whole working copy: give no paths")]
121 PathsDuringRebase,
122
123 /// The workspace has changesets that its branch does not have.
124 #[error(
125 "the workspace has {count} changesets that {branch} does not have; land them, or pass --force"
126 )]
127 Unlanded {
128 /// How many.
129 count: usize,
130 /// The branch.
131 branch: String,
132 },
133
134 /// `resolve` was used with no rebase in progress.
135 #[error("no rebase is in progress")]
136 NoRebase,
137
138 /// `resolve` named a path without a conflict, or a content conflict.
139 #[error("{:?}: {reason}", show(.path))]
140 NotResolvable {
141 /// The path.
142 path: Vec<u8>,
143 /// Why it cannot be marked resolved.
144 reason: &'static str,
145 },
146
147 /// The working copy cannot be committed as it is: conflict markers
148 /// remain, conflicts are not marked resolved, or new files wait for
149 /// `braid track`. Every reason is listed, as `status` lists them.
150 #[error("cannot commit yet:\n{}\n{}", reasons(.0), advice(.0))]
151 Blocked(Vec<Blocker>),
152
153 /// There is nothing to commit.
154 #[error("nothing to commit")]
155 NothingToCommit,
156
157 /// A commit needs a message.
158 #[error("a commit needs a message: pass -m")]
159 NoMessage,
160
161 /// No author is configured.
162 #[error(
163 "no author: pass --author \"Name <email>\", set BRAID_AUTHOR, or set name and email in .braid/config"
164 )]
165 NoAuthor,
166
167 /// An author that cannot be used.
168 #[error("invalid author {text:?}: {reason}")]
169 InvalidAuthor {
170 /// What was given.
171 text: String,
172 /// Why it was refused.
173 reason: &'static str,
174 },
175
176 /// A path given on the command line is outside the workspace.
177 #[error("{} is outside the workspace", .0.display())]
178 OutsideWorkspace(PathBuf),
179
180 /// A path in the repository that is not valid.
181 #[error("invalid path {:?}: {error}", show(.path))]
182 InvalidPath {
183 /// The path.
184 path: Vec<u8>,
185 /// What is wrong with it.
186 error: braid_format::PathError,
187 },
188
189 /// `change create --expect` did not match the base.
190 #[error("expected {:?} to be {expected} in the base, it is {found}", show(.path))]
191 ExpectationFailed {
192 /// The path.
193 path: Vec<u8>,
194 /// What was expected: a blob id or `absent`.
195 expected: String,
196 /// What the base has.
197 found: String,
198 },
199
200 /// The workspace's base is not on the branch, so its changes cannot be
201 /// landed as they are.
202 #[error("the workspace's changes are not based on {branch}; run `braid update` first")]
203 NotOnBranch {
204 /// The branch.
205 branch: String,
206 },
207
208 /// An object of the wrong kind where a changeset was expected.
209 #[error("{0} is not a changeset")]
210 NotAChangeset(ObjectId),
211}
212
213impl Error {
214 /// Wraps an I/O error with the action and the path it happened on.
215 pub(crate) fn io(
216 action: &'static str,
217 path: impl AsRef<Path>,
218 ) -> impl FnOnce(io::Error) -> Self {
219 let path = path.as_ref().to_path_buf();
220 move |source| Error::Io {
221 action,
222 path,
223 source,
224 }
225 }
226}
227
228/// How many reasons a refusal lists, at most.
229const REASONS_SHOWN: usize = 20;
230
231/// What to do about a refused commit. `--force` commits past conflicts as
232/// they are, markers and all, so it is offered as a way out only when the
233/// guard alone holds files back.
234fn advice(blockers: &[Blocker]) -> &'static str {
235 if blockers
236 .iter()
237 .any(|blocker| !matches!(blocker, Blocker::NeedsTrack(_)))
238 {
239 "Settle these first. --force would commit the conflicts as they are, markers and all, and leave out the files waiting for `braid track`."
240 } else {
241 "Move a secret out of the workspace, `braid track` a file that is not one, or commit without them with --force."
242 }
243}
244
245fn reasons(blockers: &[Blocker]) -> String {
246 let mut shown: Vec<String> = blockers
247 .iter()
248 .take(REASONS_SHOWN)
249 .map(|blocker| format!(" - {blocker}"))
250 .collect();
251 if blockers.len() > REASONS_SHOWN {
252 shown.push(format!(
253 " - and {} more: see `braid status`",
254 blockers.len() - REASONS_SHOWN
255 ));
256 }
257 shown.join("\n")
258}