1pub use super::linked_worktree_error::{LinkedWorktreeError, LinkedWorktreeOperation};
4
5#[cfg(doc)]
6use crate::error::Error;
7use crate::error::{
8 AttrPathspecError, ErrorSummary, FilterError, OverwriteReason, PathOutsideRepoError,
9 PathspecFromFileParseError, SubmoduleError, WorktreeOperation as DirtyWorktreeOperation,
10};
11
12#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum WorktreeError {
18 #[error(transparent)]
20 Linked(#[from] LinkedWorktreeError),
21
22 #[error("{0}")]
24 PathIo(#[from] crate::error::PathIoError),
25
26 #[error("{path} changed while it was being added")]
28 FileChanged {
29 path: String,
31 },
32
33 #[error("path error: {detail}")]
35 Path {
36 detail: String,
38 },
39
40 #[error("path is not valid UTF-8: {path}")]
42 NonUtf8Path {
43 path: String,
45 },
46
47 #[error("invalid path '{path}'")]
49 InvalidPath {
50 path: String,
52 },
53
54 #[error("invalid path '{path}': {reason}")]
56 InvalidPathComponent {
57 path: String,
59 reason: crate::path_validate::PathComponentError,
61 },
62
63 #[error(transparent)]
65 OutsideRepo(#[from] PathOutsideRepoError),
66
67 #[error(transparent)]
69 PathspecFromFile(#[from] PathspecFromFileParseError),
70
71 #[error("invalid attr magic in pathspec '{spec}': {reason}")]
73 InvalidAttrPathspec {
74 spec: String,
76 #[source]
78 reason: AttrPathspecError,
79 },
80
81 #[error("pathspec '{spec}' did not match any file(s) known to git")]
83 PathspecNoMatch {
84 spec: String,
86 },
87
88 #[error("pathspec '{spec}' is beyond a symbolic link")]
90 PathspecBeyondSymlink {
91 spec: String,
93 },
94
95 #[error("the following paths are ignored:\n{}", paths.join("\n"))]
97 PathspecIgnored {
98 paths: Vec<String>,
100 },
101
102 #[error("conflict restore options cannot be used with --staged")]
104 RestoreConflictWithStaged,
105
106 #[error("conflict restore options cannot be used with --source")]
108 RestoreConflictWithSource,
109
110 #[error("path '{path}' does not have their version")]
112 RestoreConflictMissingTheirs {
113 path: String,
115 },
116
117 #[error("path '{path}' does not have our version")]
119 RestoreConflictMissingOurs {
120 path: String,
122 },
123
124 #[error("path '{path}' is not unmerged")]
126 RestoreConflictNotUnmerged {
127 path: String,
129 },
130
131 #[error("{}", crate::error::restore_unmerged_message(paths))]
133 RestorePathUnmerged {
134 paths: Vec<String>,
136 },
137
138 #[error("embedded git repository '{path}' has no commit checked out")]
140 EmbeddedRepoWithoutCommit {
141 path: String,
143 },
144
145 #[error(
147 "refusing to remove paths with unsaved content:\n{}",
148 paths.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n")
149 )]
150 RemoveBlocked {
151 paths: Vec<crate::porcelain::paths::BlockedRemoval>,
153 },
154
155 #[error("not removing '{path}' recursively without -r")]
157 NotRemovingRecursively {
158 path: String,
160 },
161
162 #[error("you must specify path(s) to remove")]
164 RemovePathsRequired,
165
166 #[error("refusing to remove '{path}' because it contains the current directory")]
168 RemoveContainsCurrentDirectory {
169 path: String,
171 },
172
173 #[error("you must specify path(s) to restore")]
175 RestorePathsRequired,
176
177 #[error("cannot combine choosing a conflict side with rebuilding the conflict")]
179 RestoreStageWithRemerge,
180
181 #[error("this operation must be run in a work tree")]
183 RequiresWorkTree,
184
185 #[error("'{src}' is not under version control")]
187 MoveSourceNotTracked {
188 src: String,
190 },
191
192 #[error("destination '{destination}' already exists")]
194 MoveDestinationExists {
195 src: String,
197 destination: String,
199 },
200
201 #[error("destination '{destination}' is not a directory")]
203 MoveDestinationNotDirectory {
204 destination: String,
206 },
207
208 #[error("cannot move '{src}' to a subdirectory of itself, '{destination}'")]
210 MoveIntoSubdirectory {
211 src: String,
213 destination: String,
215 },
216
217 #[error("cannot move '{src}' into itself")]
219 MoveIntoSelf {
220 src: String,
222 destination: String,
224 },
225
226 #[error("nothing to move from '{src}'")]
228 MoveNothingToMove {
229 src: String,
231 },
232
233 #[error("multiple sources would move to '{destination}'")]
235 MoveDuplicateDestination {
236 destination: String,
238 },
239
240 #[error("cannot move '{src}' to '{destination}': bad source path")]
242 MoveBadSource {
243 src: String,
245 destination: String,
247 },
248
249 #[error("internal error: missing index entry for '{path}'")]
251 MoveMissingIndexEntry {
252 path: String,
254 },
255
256 #[error("{message}")]
258 WouldOverwrite {
259 reason: OverwriteReason,
261 paths: Vec<String>,
263 message: String,
265 },
266
267 #[error("you have uncommitted changes — commit them before {operation}")]
269 DirtyWorktree {
270 operation: DirtyWorktreeOperation,
272 paths: Vec<String>,
274 },
275
276 #[error("{source}; {} work-tree path(s) could not be restored", unrestored.len())]
283 CheckoutRollbackIncomplete {
284 source: ErrorSummary,
286 unrestored: Vec<String>,
288 },
289
290 #[error("{}", move_rollback_incomplete_message(source, *unreversed, backup_restore.as_ref()))]
295 MoveRollbackIncomplete {
296 source: ErrorSummary,
298 unreversed: usize,
300 backup_restore: Option<ErrorSummary>,
302 },
303
304 #[error("unmerged paths still have conflict markers")]
306 UnmergedConflictMarkers {
307 paths: Vec<String>,
309 },
310
311 #[error(transparent)]
313 Filter(#[from] FilterError),
314
315 #[error(transparent)]
317 Submodule(#[from] SubmoduleError),
318}
319
320impl WorktreeError {
321 #[must_use]
324 pub fn is_cancelled(&self) -> bool {
325 match self {
326 Self::CheckoutRollbackIncomplete { source, .. }
327 | Self::MoveRollbackIncomplete { source, .. } => source.is_cancelled(),
328 _ => false,
329 }
330 }
331
332 #[must_use]
334 pub fn kind_name(&self) -> &'static str {
335 match self {
336 Self::Linked(_) => "worktree",
337 Self::PathIo(path_io) => match path_io.operation {
338 crate::error::PathIoOperation::Worktree(_) => "worktree_path_io",
339 crate::error::PathIoOperation::GitDir(_) => "git_dir_path_io",
340 },
341 Self::FileChanged { .. } => "worktree_file_changed",
342 Self::Path { .. } => "path_error",
343 Self::NonUtf8Path { .. } => "non_utf8_path",
344 Self::InvalidPath { .. } => "invalid_path",
345 Self::InvalidPathComponent { .. } => "invalid_path_component",
346 Self::OutsideRepo(_) => "path_outside_repo",
347 Self::PathspecFromFile(_) => "pathspec_from_file",
348 Self::InvalidAttrPathspec { .. } => "invalid_attr_pathspec",
349 Self::PathspecNoMatch { .. } => "pathspec_no_match",
350 Self::PathspecBeyondSymlink { .. } => "pathspec_beyond_symlink",
351 Self::PathspecIgnored { .. } => "pathspec_ignored",
352 Self::RestoreConflictWithStaged => "restore_conflict_with_staged",
353 Self::RestoreConflictWithSource => "restore_conflict_with_source",
354 Self::RestoreConflictMissingTheirs { .. } => "restore_conflict_missing_theirs",
355 Self::RestoreConflictMissingOurs { .. } => "restore_conflict_missing_ours",
356 Self::RestoreConflictNotUnmerged { .. } => "restore_conflict_not_unmerged",
357 Self::RestorePathUnmerged { .. } => "restore_path_unmerged",
358 Self::EmbeddedRepoWithoutCommit { .. } => "embedded_repo_without_commit",
359 Self::RemoveBlocked { .. } => "remove_blocked",
360 Self::NotRemovingRecursively { .. } => "not_removing_recursively",
361 Self::RemovePathsRequired => "remove_paths_required",
362 Self::RemoveContainsCurrentDirectory { .. } => "remove_contains_current_directory",
363 Self::RestorePathsRequired => "restore_paths_required",
364 Self::RestoreStageWithRemerge => "restore_stage_with_remerge",
365 Self::MoveRollbackIncomplete { .. } => "move_rollback_incomplete",
366 Self::RequiresWorkTree => "requires_work_tree",
367 Self::MoveSourceNotTracked { .. } => "move_source_not_tracked",
368 Self::MoveDestinationExists { .. } => "move_destination_exists",
369 Self::MoveDestinationNotDirectory { .. } => "move_destination_not_directory",
370 Self::MoveIntoSubdirectory { .. } => "move_into_subdirectory",
371 Self::MoveIntoSelf { .. } => "move_into_self",
372 Self::MoveNothingToMove { .. } => "move_nothing_to_move",
373 Self::MoveDuplicateDestination { .. } => "move_duplicate_destination",
374 Self::MoveBadSource { .. } => "move_bad_source",
375 Self::MoveMissingIndexEntry { .. } => "move_missing_index_entry",
376 Self::WouldOverwrite { .. } => "would_overwrite",
377 Self::DirtyWorktree { .. } => "dirty_worktree",
378 Self::CheckoutRollbackIncomplete { .. } => "checkout_rollback_incomplete",
379 Self::UnmergedConflictMarkers { .. } => "unmerged_conflict_markers",
380 Self::Filter(_) => "filter",
381 Self::Submodule(_) => "submodule",
382 }
383 }
384}
385
386fn move_rollback_incomplete_message(
387 source: &ErrorSummary,
388 unreversed: usize,
389 backup_restore: Option<&ErrorSummary>,
390) -> String {
391 let mut message = source.to_string();
392 if unreversed > 0 {
393 message.push_str(&format!(
394 "; incomplete worktree rollback ({unreversed} rename(s) could not be reversed)"
395 ));
396 }
397 if let Some(err) = backup_restore {
398 message.push_str(&format!(
399 "; restoring forced-destination backups failed: {err}"
400 ));
401 }
402 message
403}