1use std::path::PathBuf;
2
3use miette::Diagnostic;
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, WorkonError>;
8
9#[derive(Error, Diagnostic, Debug)]
11pub enum WorkonError {
12 #[error(transparent)]
14 #[diagnostic(code(workon::git_error))]
15 Git(#[from] git2::Error),
16
17 #[error(transparent)]
19 #[diagnostic(code(workon::io_error))]
20 Io(#[from] std::io::Error),
21
22 #[error(transparent)]
24 #[diagnostic(forward(0))]
25 Repo(#[from] RepoError),
26
27 #[error(transparent)]
29 #[diagnostic(forward(0))]
30 Worktree(#[from] WorktreeError),
31
32 #[error(transparent)]
34 #[diagnostic(forward(0))]
35 Config(#[from] ConfigError),
36
37 #[error(transparent)]
39 #[diagnostic(forward(0))]
40 DefaultBranch(#[from] DefaultBranchError),
41
42 #[error(transparent)]
44 #[diagnostic(forward(0))]
45 Pr(#[from] PrError),
46
47 #[error(transparent)]
49 #[diagnostic(forward(0))]
50 Copy(#[from] CopyError),
51
52 #[error(transparent)]
54 #[diagnostic(forward(0))]
55 Stack(#[from] StackError),
56
57 #[error(transparent)]
59 #[diagnostic(forward(0))]
60 Checkout(#[from] CheckoutError),
61
62 #[error(transparent)]
64 #[diagnostic(forward(0))]
65 Prune(#[from] PruneError),
66
67 #[error(transparent)]
69 #[diagnostic(forward(0))]
70 Changeset(#[from] ChangesetError),
71}
72
73#[derive(Error, Diagnostic, Debug)]
75pub enum RepoError {
76 #[error("Not a bare repository at {0}")]
77 #[diagnostic(
78 code(workon::repo::not_bare),
79 help("Workon commands must be run in bare repositories")
80 )]
81 NotBare(String),
82}
83
84#[derive(Error, Diagnostic, Debug)]
86pub enum WorktreeError {
87 #[error("Could not find worktree '{0}'")]
88 #[diagnostic(
89 code(workon::worktree::not_found),
90 help("Use 'git workon list' to see available worktrees")
91 )]
92 NotFound(String),
93
94 #[error("Not in a worktree directory")]
95 #[diagnostic(
96 code(workon::worktree::not_in_worktree),
97 help("Run this command from within a worktree directory")
98 )]
99 NotInWorktree,
100
101 #[error("Could not determine branch target")]
102 #[diagnostic(
103 code(workon::worktree::no_branch_target),
104 help("The branch may be in an invalid state")
105 )]
106 NoBranchTarget,
107
108 #[error("Could not get current branch target")]
109 #[diagnostic(code(workon::worktree::no_current_branch_target))]
110 NoCurrentBranchTarget,
111
112 #[error("Could not get local branch target")]
113 #[diagnostic(code(workon::worktree::no_local_branch_target))]
114 NoLocalBranchTarget,
115
116 #[error("Worktree path has no parent directory")]
117 #[diagnostic(
118 code(workon::worktree::no_parent),
119 help("Cannot create parent directories for worktree path")
120 )]
121 NoParent,
122
123 #[error("Invalid worktree name: contains invalid UTF-8")]
124 #[diagnostic(
125 code(workon::worktree::invalid_name),
126 help("Worktree names must be valid UTF-8 strings")
127 )]
128 InvalidName,
129
130 #[error("Expected an empty index!")]
131 #[diagnostic(code(workon::worktree::non_empty_index))]
132 NonEmptyIndex,
133
134 #[error("Worktree '{to}' already exists")]
135 #[diagnostic(
136 code(workon::worktree::target_exists),
137 help("Choose a different name or remove the existing worktree first")
138 )]
139 TargetExists { to: String },
140
141 #[error("Cannot move detached HEAD worktree")]
142 #[diagnostic(
143 code(workon::worktree::move_detached),
144 help("Detached HEAD worktrees have no branch to rename")
145 )]
146 CannotMoveDetached,
147
148 #[error("Branch '{0}' is protected and cannot be renamed")]
149 #[diagnostic(
150 code(workon::worktree::protected_branch_move),
151 help("Protected branches are configured in workon.pruneProtectedBranches. Use --force to override.")
152 )]
153 ProtectedBranchMove(String),
154
155 #[error("Worktree is dirty (uncommitted changes)")]
156 #[diagnostic(
157 code(workon::worktree::dirty_worktree),
158 help("Commit or stash changes, or use --force to override")
159 )]
160 DirtyWorktree,
161
162 #[error("Worktree has unpushed commits")]
163 #[diagnostic(
164 code(workon::worktree::unpushed_commits),
165 help("Push commits first, or use --force to override")
166 )]
167 UnpushedCommits,
168
169 #[error("Worktree admin name '{name}' is already in use by {path}")]
170 #[diagnostic(
171 code(workon::worktree::name_conflict),
172 help("Remove or rename the existing worktree, or run 'git workon doctor' to find a stale admin name")
173 )]
174 WorktreeNameConflict { name: String, path: String },
175}
176
177#[derive(Error, Diagnostic, Debug)]
179pub enum ConfigError {
180 #[error("Invalid PR format: '{format}' - {reason}")]
181 #[diagnostic(
182 code(workon::config::invalid_pr_format),
183 help("Valid placeholders: {{number}}, {{title}}, {{author}}, {{branch}}")
184 )]
185 InvalidPrFormat { format: String, reason: String },
186
187 #[error("Config entry has no value")]
188 #[diagnostic(code(workon::config::no_value))]
189 NoValue,
190}
191
192#[derive(Error, Diagnostic, Debug)]
194pub enum DefaultBranchError {
195 #[error("Could not determine default branch for remote {remote:?}")]
196 #[diagnostic(
197 code(workon::default_branch::no_remote_default),
198 help("The remote may not have a default branch configured")
199 )]
200 NoRemoteDefault { remote: Option<String> },
201
202 #[error("Remote is not connected")]
203 #[diagnostic(
204 code(workon::default_branch::not_connected),
205 help("Failed to establish connection to remote repository")
206 )]
207 NotConnected,
208
209 #[error("Could not determine default branch: neither 'main' nor 'master' exist, and init.defaultBranch is not configured")]
210 #[diagnostic(
211 code(workon::default_branch::no_default_branch),
212 help("Set init.defaultBranch in your git config, or create a 'main' or 'master' branch")
213 )]
214 NoDefaultBranch,
215}
216
217#[derive(Error, Diagnostic, Debug)]
219pub enum StackError {
220 #[error("Stack model '{model}' is not yet supported")]
221 #[diagnostic(
222 code(workon::stack::unsupported_model),
223 help(
224 "Only 'graphite' is implemented in this version. \
225 Support for branchless, sapling, and spr is planned."
226 )
227 )]
228 UnsupportedModel { model: String },
229
230 #[error("Unknown stack model '{value}'")]
231 #[diagnostic(
232 code(workon::stack::unknown_model),
233 help("Valid values: graphite, git, none, auto")
234 )]
235 UnknownModel { value: String },
236
237 #[error("Worktree granularity 'diff' is not yet implemented")]
238 #[diagnostic(
239 code(workon::stack::unsupported_granularity),
240 help(
241 "Only 'stack' (one worktree per stack) is supported in this version. \
242 'diff' (one worktree per branch) is planned."
243 )
244 )]
245 UnsupportedGranularity,
246
247 #[error("Unknown worktree granularity '{value}'")]
248 #[diagnostic(code(workon::stack::unknown_granularity), help("Valid values: stack"))]
249 UnknownGranularity { value: String },
250
251 #[error("Graphite CLI ('gt') is not installed or not in PATH")]
252 #[diagnostic(
253 code(workon::stack::gt_not_installed),
254 help(
255 "Install Graphite: https://graphite.dev/cli \
256 Or set workon.stackModel = none to disable stack support."
257 )
258 )]
259 GtNotInstalled,
260
261 #[error("Graphite command failed: {stderr}")]
262 #[diagnostic(code(workon::stack::gt_command_failed))]
263 GtCommandFailed { stderr: String },
264
265 #[error("Failed to parse Graphite metadata: {message}")]
266 #[diagnostic(code(workon::stack::gt_parse_failed))]
267 GtParseFailed { message: String },
268
269 #[error("Repository is not Graphite-managed (no .graphite_repo_config)")]
270 #[diagnostic(
271 code(workon::stack::not_a_graphite_repo),
272 help("Run 'gt init' in this repository, or unset workon.stackModel.")
273 )]
274 NotAGraphiteRepo,
275
276 #[error("Branch '{branch}' exists in stack metadata but its local ref was deleted")]
277 #[diagnostic(
278 code(workon::stack::deleted_branch_node),
279 help(
280 "The branch was tracked by Graphite but its local ref no longer exists. \
281 Run 'gt branch checkout {branch}' to restore it, or \
282 'gt branch delete {branch}' to remove it from the stack."
283 )
284 )]
285 DeletedBranchNode { branch: String },
286}
287
288#[derive(Error, Diagnostic, Debug)]
290pub enum ChangesetError {
291 #[error("Branch '{branch}' has no resolvable local ref")]
292 #[diagnostic(
293 code(workon::changeset::unresolvable_branch),
294 help("The branch may have been deleted while stack metadata lingered; run 'gt sync' or re-create the branch")
295 )]
296 UnresolvableBranch { branch: String },
297
298 #[error(
299 "Recorded parent revision '{revision}' for branch '{branch}' does not resolve to a commit"
300 )]
301 #[diagnostic(
302 code(workon::changeset::invalid_parent_revision),
303 help("Stack metadata may be corrupt or copied from another clone; run 'gt restack' to rewrite it")
304 )]
305 InvalidParentRevision { branch: String, revision: String },
306
307 #[error("Branch '{branch}' has no upstream to infer changesets from")]
308 #[diagnostic(
309 code(workon::changeset::no_upstream),
310 help(
311 "Set an upstream (git branch --set-upstream-to=<remote>/<branch>) or use a stack tool"
312 )
313 )]
314 NoUpstream { branch: String },
315}
316
317#[derive(Error, Diagnostic, Debug)]
319pub enum PrError {
320 #[error("Invalid PR reference: {input}")]
321 #[diagnostic(
322 code(workon::pr::invalid_reference),
323 help("Use formats like #123, pr-123, or https://github.com/owner/repo/pull/123")
324 )]
325 InvalidReference { input: String },
326
327 #[error("PR #{number} not found on remote {remote}")]
328 #[diagnostic(
329 code(workon::pr::not_found),
330 help("Verify the PR number exists and you have access to the repository")
331 )]
332 PrNotFound { number: u32, remote: String },
333
334 #[error("No git remote configured")]
335 #[diagnostic(
336 code(workon::pr::no_remote),
337 help("Add a remote with: git remote add origin <url>")
338 )]
339 NoRemoteConfigured,
340
341 #[error("Failed to fetch PR refs from {remote}: {message}")]
342 #[diagnostic(
343 code(workon::pr::fetch_failed),
344 help("Check your network connection and repository access")
345 )]
346 FetchFailed { remote: String, message: String },
347
348 #[error("gh CLI is not installed or not in PATH")]
349 #[diagnostic(
350 code(workon::pr::gh_not_installed),
351 help("Install gh CLI: https://cli.github.com/")
352 )]
353 GhNotInstalled,
354
355 #[error("Failed to fetch PR metadata from gh: {message}")]
356 #[diagnostic(
357 code(workon::pr::gh_fetch_failed),
358 help("Check your network connection and GitHub authentication (gh auth status)")
359 )]
360 GhFetchFailed { message: String },
361
362 #[error("Invalid JSON output from gh CLI: {message}")]
363 #[diagnostic(
364 code(workon::pr::gh_json_parse_failed),
365 help("This may indicate a gh CLI version incompatibility")
366 )]
367 GhJsonParseFailed { message: String },
368
369 #[error("Fork repository missing owner information")]
370 #[diagnostic(
371 code(workon::pr::missing_fork_owner),
372 help("This PR may be from a deleted fork")
373 )]
374 MissingForkOwner,
375}
376
377#[derive(Error, Diagnostic, Debug)]
379pub enum CheckoutError {
380 #[error(transparent)]
382 #[diagnostic(code(workon::checkout::git_error))]
383 Git(#[from] git2::Error),
384
385 #[error("Branch '{branch}' not found in the worktree")]
387 #[diagnostic(
388 code(workon::checkout::branch_not_found),
389 help("Ensure the branch exists locally before checking it out in place")
390 )]
391 BranchNotFound { branch: String },
392
393 #[error("Checkout of '{branch}' conflicts with uncommitted changes in {path}")]
395 #[diagnostic(
396 code(workon::checkout::conflict),
397 help("Stash or commit changes first, or use the interactive prompt to shelve them")
398 )]
399 Conflict { branch: String, path: String },
400
401 #[error("Checkout aborted")]
403 #[diagnostic(code(workon::checkout::aborted))]
404 Aborted,
405}
406
407#[derive(Error, Diagnostic, Debug)]
409pub enum PruneError {
410 #[error("worktree(s) not found: {}", .names.join(", "))]
411 #[diagnostic(
412 code(workon::prune::names_not_found),
413 help("Use 'git workon list' to see available worktrees")
414 )]
415 NamesNotFound { names: Vec<String> },
416}
417
418#[derive(Error, Diagnostic, Debug)]
420pub enum CopyError {
421 #[error("Invalid glob pattern '{pattern}'")]
422 #[diagnostic(
423 code(workon::copy::invalid_glob_pattern),
424 help("Check glob pattern syntax: *, **, ?, [...]")
425 )]
426 InvalidGlobPattern {
427 pattern: String,
428 #[source]
429 source: glob::PatternError,
430 },
431
432 #[error("Path is not valid UTF-8: {}", path.display())]
433 #[diagnostic(code(workon::copy::invalid_path))]
434 InvalidPath { path: PathBuf },
435
436 #[error("Failed to read glob entry")]
437 #[diagnostic(code(workon::copy::glob_error))]
438 GlobEntry(#[from] glob::GlobError),
439
440 #[error("Failed to copy '{}' to '{}'", src.display(), dest.display())]
441 #[diagnostic(code(workon::copy::copy_failed))]
442 CopyFailed {
443 src: PathBuf,
444 dest: PathBuf,
445 #[source]
446 source: std::io::Error,
447 },
448
449 #[error("Failed to open repository at '{}'", path.display())]
450 #[diagnostic(
451 code(workon::copy::repo_open_error),
452 help("Ensure the path is a valid git repository")
453 )]
454 RepoOpen {
455 path: PathBuf,
456 #[source]
457 source: git2::Error,
458 },
459}