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
170#[derive(Error, Diagnostic, Debug)]
172pub enum ConfigError {
173 #[error("Invalid PR format: '{format}' - {reason}")]
174 #[diagnostic(
175 code(workon::config::invalid_pr_format),
176 help("Valid placeholders: {{number}}, {{title}}, {{author}}, {{branch}}")
177 )]
178 InvalidPrFormat { format: String, reason: String },
179
180 #[error("Config entry has no value")]
181 #[diagnostic(code(workon::config::no_value))]
182 NoValue,
183}
184
185#[derive(Error, Diagnostic, Debug)]
187pub enum DefaultBranchError {
188 #[error("Could not determine default branch for remote {remote:?}")]
189 #[diagnostic(
190 code(workon::default_branch::no_remote_default),
191 help("The remote may not have a default branch configured")
192 )]
193 NoRemoteDefault { remote: Option<String> },
194
195 #[error("Remote is not connected")]
196 #[diagnostic(
197 code(workon::default_branch::not_connected),
198 help("Failed to establish connection to remote repository")
199 )]
200 NotConnected,
201
202 #[error("Could not determine default branch: neither 'main' nor 'master' exist, and init.defaultBranch is not configured")]
203 #[diagnostic(
204 code(workon::default_branch::no_default_branch),
205 help("Set init.defaultBranch in your git config, or create a 'main' or 'master' branch")
206 )]
207 NoDefaultBranch,
208}
209
210#[derive(Error, Diagnostic, Debug)]
212pub enum StackError {
213 #[error("Stack model '{model}' is not yet supported")]
214 #[diagnostic(
215 code(workon::stack::unsupported_model),
216 help(
217 "Only 'graphite' is implemented in this version. \
218 Support for branchless, sapling, and spr is planned."
219 )
220 )]
221 UnsupportedModel { model: String },
222
223 #[error("Unknown stack model '{value}'")]
224 #[diagnostic(
225 code(workon::stack::unknown_model),
226 help("Valid values: graphite, git, none, auto")
227 )]
228 UnknownModel { value: String },
229
230 #[error("Worktree granularity 'diff' is not yet implemented")]
231 #[diagnostic(
232 code(workon::stack::unsupported_granularity),
233 help(
234 "Only 'stack' (one worktree per stack) is supported in this version. \
235 'diff' (one worktree per branch) is planned."
236 )
237 )]
238 UnsupportedGranularity,
239
240 #[error("Unknown worktree granularity '{value}'")]
241 #[diagnostic(code(workon::stack::unknown_granularity), help("Valid values: stack"))]
242 UnknownGranularity { value: String },
243
244 #[error("Graphite CLI ('gt') is not installed or not in PATH")]
245 #[diagnostic(
246 code(workon::stack::gt_not_installed),
247 help(
248 "Install Graphite: https://graphite.dev/cli \
249 Or set workon.stackModel = none to disable stack support."
250 )
251 )]
252 GtNotInstalled,
253
254 #[error("Graphite command failed: {stderr}")]
255 #[diagnostic(code(workon::stack::gt_command_failed))]
256 GtCommandFailed { stderr: String },
257
258 #[error("Failed to parse Graphite metadata: {message}")]
259 #[diagnostic(code(workon::stack::gt_parse_failed))]
260 GtParseFailed { message: String },
261
262 #[error("Repository is not Graphite-managed (no .graphite_repo_config)")]
263 #[diagnostic(
264 code(workon::stack::not_a_graphite_repo),
265 help("Run 'gt init' in this repository, or unset workon.stackModel.")
266 )]
267 NotAGraphiteRepo,
268
269 #[error("Branch '{branch}' exists in stack metadata but its local ref was deleted")]
270 #[diagnostic(
271 code(workon::stack::deleted_branch_node),
272 help(
273 "The branch was tracked by Graphite but its local ref no longer exists. \
274 Run 'gt branch checkout {branch}' to restore it, or \
275 'gt branch delete {branch}' to remove it from the stack."
276 )
277 )]
278 DeletedBranchNode { branch: String },
279}
280
281#[derive(Error, Diagnostic, Debug)]
283pub enum ChangesetError {
284 #[error("Branch '{branch}' has no resolvable local ref")]
285 #[diagnostic(
286 code(workon::changeset::unresolvable_branch),
287 help("The branch may have been deleted while stack metadata lingered; run 'gt sync' or re-create the branch")
288 )]
289 UnresolvableBranch { branch: String },
290
291 #[error(
292 "Recorded parent revision '{revision}' for branch '{branch}' does not resolve to a commit"
293 )]
294 #[diagnostic(
295 code(workon::changeset::invalid_parent_revision),
296 help("Stack metadata may be corrupt or copied from another clone; run 'gt restack' to rewrite it")
297 )]
298 InvalidParentRevision { branch: String, revision: String },
299
300 #[error("Branch '{branch}' has no upstream to infer changesets from")]
301 #[diagnostic(
302 code(workon::changeset::no_upstream),
303 help(
304 "Set an upstream (git branch --set-upstream-to=<remote>/<branch>) or use a stack tool"
305 )
306 )]
307 NoUpstream { branch: String },
308}
309
310#[derive(Error, Diagnostic, Debug)]
312pub enum PrError {
313 #[error("Invalid PR reference: {input}")]
314 #[diagnostic(
315 code(workon::pr::invalid_reference),
316 help("Use formats like #123, pr-123, or https://github.com/owner/repo/pull/123")
317 )]
318 InvalidReference { input: String },
319
320 #[error("PR #{number} not found on remote {remote}")]
321 #[diagnostic(
322 code(workon::pr::not_found),
323 help("Verify the PR number exists and you have access to the repository")
324 )]
325 PrNotFound { number: u32, remote: String },
326
327 #[error("No git remote configured")]
328 #[diagnostic(
329 code(workon::pr::no_remote),
330 help("Add a remote with: git remote add origin <url>")
331 )]
332 NoRemoteConfigured,
333
334 #[error("Failed to fetch PR refs from {remote}: {message}")]
335 #[diagnostic(
336 code(workon::pr::fetch_failed),
337 help("Check your network connection and repository access")
338 )]
339 FetchFailed { remote: String, message: String },
340
341 #[error("gh CLI is not installed or not in PATH")]
342 #[diagnostic(
343 code(workon::pr::gh_not_installed),
344 help("Install gh CLI: https://cli.github.com/")
345 )]
346 GhNotInstalled,
347
348 #[error("Failed to fetch PR metadata from gh: {message}")]
349 #[diagnostic(
350 code(workon::pr::gh_fetch_failed),
351 help("Check your network connection and GitHub authentication (gh auth status)")
352 )]
353 GhFetchFailed { message: String },
354
355 #[error("Invalid JSON output from gh CLI: {message}")]
356 #[diagnostic(
357 code(workon::pr::gh_json_parse_failed),
358 help("This may indicate a gh CLI version incompatibility")
359 )]
360 GhJsonParseFailed { message: String },
361
362 #[error("Fork repository missing owner information")]
363 #[diagnostic(
364 code(workon::pr::missing_fork_owner),
365 help("This PR may be from a deleted fork")
366 )]
367 MissingForkOwner,
368}
369
370#[derive(Error, Diagnostic, Debug)]
372pub enum CheckoutError {
373 #[error(transparent)]
375 #[diagnostic(code(workon::checkout::git_error))]
376 Git(#[from] git2::Error),
377
378 #[error("Branch '{branch}' not found in the worktree")]
380 #[diagnostic(
381 code(workon::checkout::branch_not_found),
382 help("Ensure the branch exists locally before checking it out in place")
383 )]
384 BranchNotFound { branch: String },
385
386 #[error("Checkout of '{branch}' conflicts with uncommitted changes in {path}")]
388 #[diagnostic(
389 code(workon::checkout::conflict),
390 help("Stash or commit changes first, or use the interactive prompt to shelve them")
391 )]
392 Conflict { branch: String, path: String },
393
394 #[error("Checkout aborted")]
396 #[diagnostic(code(workon::checkout::aborted))]
397 Aborted,
398}
399
400#[derive(Error, Diagnostic, Debug)]
402pub enum PruneError {
403 #[error("worktree(s) not found: {}", .names.join(", "))]
404 #[diagnostic(
405 code(workon::prune::names_not_found),
406 help("Use 'git workon list' to see available worktrees")
407 )]
408 NamesNotFound { names: Vec<String> },
409}
410
411#[derive(Error, Diagnostic, Debug)]
413pub enum CopyError {
414 #[error("Invalid glob pattern '{pattern}'")]
415 #[diagnostic(
416 code(workon::copy::invalid_glob_pattern),
417 help("Check glob pattern syntax: *, **, ?, [...]")
418 )]
419 InvalidGlobPattern {
420 pattern: String,
421 #[source]
422 source: glob::PatternError,
423 },
424
425 #[error("Path is not valid UTF-8: {}", path.display())]
426 #[diagnostic(code(workon::copy::invalid_path))]
427 InvalidPath { path: PathBuf },
428
429 #[error("Failed to read glob entry")]
430 #[diagnostic(code(workon::copy::glob_error))]
431 GlobEntry(#[from] glob::GlobError),
432
433 #[error("Failed to copy '{}' to '{}'", src.display(), dest.display())]
434 #[diagnostic(code(workon::copy::copy_failed))]
435 CopyFailed {
436 src: PathBuf,
437 dest: PathBuf,
438 #[source]
439 source: std::io::Error,
440 },
441
442 #[error("Failed to open repository at '{}'", path.display())]
443 #[diagnostic(
444 code(workon::copy::repo_open_error),
445 help("Ensure the path is a valid git repository")
446 )]
447 RepoOpen {
448 path: PathBuf,
449 #[source]
450 source: git2::Error,
451 },
452}