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 "'graphite' and 'gh-stack' are implemented in this version. \
225 Support for branchless and sapling is planned. 'ghstack' (Meta's \
226 Phabricator-style stacker) is a different tool and is not planned."
227 )
228 )]
229 UnsupportedModel { model: String },
230
231 #[error("Unknown stack model '{value}'")]
232 #[diagnostic(
233 code(workon::stack::unknown_model),
234 help("Valid values: graphite, gh-stack, git, none, auto")
235 )]
236 UnknownModel { value: String },
237
238 #[error("Worktree granularity 'diff' is not yet implemented")]
239 #[diagnostic(
240 code(workon::stack::unsupported_granularity),
241 help(
242 "Only 'stack' (one worktree per stack) is supported in this version. \
243 'diff' (one worktree per branch) is planned."
244 )
245 )]
246 UnsupportedGranularity,
247
248 #[error("Unknown worktree granularity '{value}'")]
249 #[diagnostic(code(workon::stack::unknown_granularity), help("Valid values: stack"))]
250 UnknownGranularity { value: String },
251
252 #[error("Graphite CLI ('gt') is not installed or not in PATH")]
253 #[diagnostic(
254 code(workon::stack::gt_not_installed),
255 help(
256 "Install Graphite: https://graphite.dev/cli \
257 Or set workon.stackModel = none to disable stack support."
258 )
259 )]
260 GtNotInstalled,
261
262 #[error("Graphite command failed: {stderr}")]
263 #[diagnostic(code(workon::stack::gt_command_failed))]
264 GtCommandFailed { stderr: String },
265
266 #[error("Failed to parse Graphite metadata: {message}")]
267 #[diagnostic(code(workon::stack::gt_parse_failed))]
268 GtParseFailed { message: String },
269
270 #[error("Repository is not Graphite-managed (no .graphite_repo_config)")]
271 #[diagnostic(
272 code(workon::stack::not_a_graphite_repo),
273 help("Run 'gt init' in this repository, or unset workon.stackModel.")
274 )]
275 NotAGraphiteRepo,
276
277 #[error("Branch '{branch}' exists in stack metadata but its local ref was deleted")]
278 #[diagnostic(
279 code(workon::stack::deleted_branch_node),
280 help(
281 "The branch was tracked by Graphite but its local ref no longer exists. \
282 Run 'gt branch checkout {branch}' to restore it, or \
283 'gt branch delete {branch}' to remove it from the stack."
284 )
285 )]
286 DeletedBranchNode { branch: String },
287
288 #[error("gh-stack file '{}' uses schema version {version}, which this version of git-workon does not support", path.display())]
289 #[diagnostic(
290 code(workon::stack::gh_stack_schema_unsupported),
291 help("Upgrade git-workon, or downgrade the gh-stack extension that wrote this file.")
292 )]
293 GhStackSchemaUnsupported { path: PathBuf, version: u64 },
294
295 #[error("Failed to parse gh-stack file '{}': {message}", path.display())]
296 #[diagnostic(code(workon::stack::gh_stack_parse_failed))]
297 GhStackParseFailed { path: PathBuf, message: String },
298
299 #[error("Timed out waiting for gh-stack lock '{}'", path.display())]
300 #[diagnostic(
301 code(workon::stack::gh_stack_locked),
302 help("Another `gh stack` or `git-workon` process may be mid-write; try again shortly.")
303 )]
304 GhStackLocked { path: PathBuf },
305
306 #[error("Failed to write gh-stack file '{}': {message}", path.display())]
307 #[diagnostic(code(workon::stack::gh_stack_write_failed))]
308 GhStackWriteFailed { path: PathBuf, message: String },
309
310 #[error("Failed to link gh-stack file '{}': {message}", path.display())]
311 #[diagnostic(code(workon::stack::gh_stack_link_failed))]
312 GhStackLinkFailed { path: PathBuf, message: String },
313
314 #[error("No gh-stack stack ends at branch '{base}'")]
315 #[diagnostic(
316 code(workon::stack::gh_stack_no_stack_for_base),
317 help(
318 "Run `gh stack init` or `gh stack add` for '{base}' first, or check workon.stackModel."
319 )
320 )]
321 GhStackNoStackForBase { base: String },
322
323 #[error("No gh-stack stack ends at branch '{base}' in the canonical store")]
324 #[diagnostic(
325 code(workon::stack::gh_stack_stack_in_unlinked_worktree),
326 help(
327 "'{base}' may be tracked in an unlinked worktree's gh-stack file. \
328 Run `workon doctor --fix` to migrate it into the canonical store, then retry."
329 )
330 )]
331 GhStackStackInUnlinkedWorktree { base: String },
332
333 #[error("Branch '{branch}' exists in stack metadata but its local ref was deleted")]
334 #[diagnostic(
335 code(workon::stack::deleted_stack_node),
336 help(
337 "The branch was tracked by your stack tool but its local ref no longer exists. \
338 Recreate the branch, or remove it from the stack metadata."
339 )
340 )]
341 DeletedStackNode { branch: String },
342}
343
344#[derive(Error, Diagnostic, Debug)]
346pub enum ChangesetError {
347 #[error("Branch '{branch}' has no resolvable local ref")]
348 #[diagnostic(
349 code(workon::changeset::unresolvable_branch),
350 help("The branch may have been deleted while stack metadata lingered; sync your stack tool (e.g. `gt sync`) or re-create the branch")
351 )]
352 UnresolvableBranch { branch: String },
353
354 #[error(
355 "Recorded parent revision '{revision}' for branch '{branch}' does not resolve to a commit"
356 )]
357 #[diagnostic(
358 code(workon::changeset::invalid_parent_revision),
359 help("Stack metadata may be corrupt or copied from another clone; resync your stack tool (e.g. `gt restack`) to rewrite it")
360 )]
361 InvalidParentRevision { branch: String, revision: String },
362
363 #[error("Branch '{branch}' has no upstream to infer changesets from")]
364 #[diagnostic(
365 code(workon::changeset::no_upstream),
366 help(
367 "Set an upstream (git branch --set-upstream-to=<remote>/<branch>) or use a stack tool"
368 )
369 )]
370 NoUpstream { branch: String },
371}
372
373#[derive(Error, Diagnostic, Debug)]
375pub enum PrError {
376 #[error("Invalid PR reference: {input}")]
377 #[diagnostic(
378 code(workon::pr::invalid_reference),
379 help("Use formats like #123, pr-123, or https://github.com/owner/repo/pull/123")
380 )]
381 InvalidReference { input: String },
382
383 #[error("PR #{number} not found on remote {remote}")]
384 #[diagnostic(
385 code(workon::pr::not_found),
386 help("Verify the PR number exists and you have access to the repository")
387 )]
388 PrNotFound { number: u32, remote: String },
389
390 #[error("No git remote configured")]
391 #[diagnostic(
392 code(workon::pr::no_remote),
393 help("Add a remote with: git remote add origin <url>")
394 )]
395 NoRemoteConfigured,
396
397 #[error("Failed to fetch PR refs from {remote}: {message}")]
398 #[diagnostic(
399 code(workon::pr::fetch_failed),
400 help("Check your network connection and repository access")
401 )]
402 FetchFailed { remote: String, message: String },
403
404 #[error("gh CLI is not installed or not in PATH")]
405 #[diagnostic(
406 code(workon::pr::gh_not_installed),
407 help("Install gh CLI: https://cli.github.com/")
408 )]
409 GhNotInstalled,
410
411 #[error("Failed to fetch PR metadata from gh: {message}")]
412 #[diagnostic(
413 code(workon::pr::gh_fetch_failed),
414 help("Check your network connection and GitHub authentication (gh auth status)")
415 )]
416 GhFetchFailed { message: String },
417
418 #[error("Invalid JSON output from gh CLI: {message}")]
419 #[diagnostic(
420 code(workon::pr::gh_json_parse_failed),
421 help("This may indicate a gh CLI version incompatibility")
422 )]
423 GhJsonParseFailed { message: String },
424
425 #[error("Fork repository missing owner information")]
426 #[diagnostic(
427 code(workon::pr::missing_fork_owner),
428 help("This PR may be from a deleted fork")
429 )]
430 MissingForkOwner,
431}
432
433#[derive(Error, Diagnostic, Debug)]
435pub enum CheckoutError {
436 #[error(transparent)]
438 #[diagnostic(code(workon::checkout::git_error))]
439 Git(#[from] git2::Error),
440
441 #[error("Branch '{branch}' not found in the worktree")]
443 #[diagnostic(
444 code(workon::checkout::branch_not_found),
445 help("Ensure the branch exists locally before checking it out in place")
446 )]
447 BranchNotFound { branch: String },
448
449 #[error("Checkout of '{branch}' conflicts with uncommitted changes in {path}")]
451 #[diagnostic(
452 code(workon::checkout::conflict),
453 help("Stash or commit changes first, or use the interactive prompt to shelve them")
454 )]
455 Conflict { branch: String, path: String },
456
457 #[error("Checkout aborted")]
459 #[diagnostic(code(workon::checkout::aborted))]
460 Aborted,
461}
462
463#[derive(Error, Diagnostic, Debug)]
465pub enum PruneError {
466 #[error("worktree(s) not found: {}", .names.join(", "))]
467 #[diagnostic(
468 code(workon::prune::names_not_found),
469 help("Use 'git workon list' to see available worktrees")
470 )]
471 NamesNotFound { names: Vec<String> },
472}
473
474#[derive(Error, Diagnostic, Debug)]
476pub enum CopyError {
477 #[error("Invalid glob pattern '{pattern}'")]
478 #[diagnostic(
479 code(workon::copy::invalid_glob_pattern),
480 help("Check glob pattern syntax: *, **, ?, [...]")
481 )]
482 InvalidGlobPattern {
483 pattern: String,
484 #[source]
485 source: glob::PatternError,
486 },
487
488 #[error("Path is not valid UTF-8: {}", path.display())]
489 #[diagnostic(code(workon::copy::invalid_path))]
490 InvalidPath { path: PathBuf },
491
492 #[error("Failed to read glob entry")]
493 #[diagnostic(code(workon::copy::glob_error))]
494 GlobEntry(#[from] glob::GlobError),
495
496 #[error("Failed to copy '{}' to '{}'", src.display(), dest.display())]
497 #[diagnostic(code(workon::copy::copy_failed))]
498 CopyFailed {
499 src: PathBuf,
500 dest: PathBuf,
501 #[source]
502 source: std::io::Error,
503 },
504
505 #[error("Failed to open repository at '{}'", path.display())]
506 #[diagnostic(
507 code(workon::copy::repo_open_error),
508 help("Ensure the path is a valid git repository")
509 )]
510 RepoOpen {
511 path: PathBuf,
512 #[source]
513 source: git2::Error,
514 },
515}