git-workon-lib 0.7.4

API for managing worktrees
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::path::PathBuf;

use miette::Diagnostic;
use thiserror::Error;

/// Result type alias using WorkonError
pub type Result<T> = std::result::Result<T, WorkonError>;

/// Main error type for the workon library
#[derive(Error, Diagnostic, Debug)]
pub enum WorkonError {
    /// Git operation failed
    #[error(transparent)]
    #[diagnostic(code(workon::git_error))]
    Git(#[from] git2::Error),

    /// I/O operation failed
    #[error(transparent)]
    #[diagnostic(code(workon::io_error))]
    Io(#[from] std::io::Error),

    /// Repository-related errors
    #[error(transparent)]
    #[diagnostic(forward(0))]
    Repo(#[from] RepoError),

    /// Worktree-related errors
    #[error(transparent)]
    #[diagnostic(forward(0))]
    Worktree(#[from] WorktreeError),

    /// Configuration-related errors
    #[error(transparent)]
    #[diagnostic(forward(0))]
    Config(#[from] ConfigError),

    /// Default branch detection errors
    #[error(transparent)]
    #[diagnostic(forward(0))]
    DefaultBranch(#[from] DefaultBranchError),

    /// Pull request-related errors
    #[error(transparent)]
    #[diagnostic(forward(0))]
    Pr(#[from] PrError),

    /// File copy errors
    #[error(transparent)]
    #[diagnostic(forward(0))]
    Copy(#[from] CopyError),

    /// Stacked diff workflow errors
    #[error(transparent)]
    #[diagnostic(forward(0))]
    Stack(#[from] StackError),

    /// In-place checkout errors
    #[error(transparent)]
    #[diagnostic(forward(0))]
    Checkout(#[from] CheckoutError),
}

/// Repository-specific errors
#[derive(Error, Diagnostic, Debug)]
pub enum RepoError {
    #[error("Not a bare repository at {0}")]
    #[diagnostic(
        code(workon::repo::not_bare),
        help("Workon commands must be run in bare repositories")
    )]
    NotBare(String),
}

/// Worktree-specific errors
#[derive(Error, Diagnostic, Debug)]
pub enum WorktreeError {
    #[error("Invalid .git file format")]
    #[diagnostic(
        code(workon::worktree::invalid_git_file),
        help("The .git file should contain 'gitdir: <path>' pointing to the git directory")
    )]
    InvalidGitFile,

    #[error("Could not find worktree '{0}'")]
    #[diagnostic(
        code(workon::worktree::not_found),
        help("Use 'git workon list' to see available worktrees")
    )]
    NotFound(String),

    #[error("Not in a worktree directory")]
    #[diagnostic(
        code(workon::worktree::not_in_worktree),
        help("Run this command from within a worktree directory")
    )]
    NotInWorktree,

    #[error("Could not determine branch target")]
    #[diagnostic(
        code(workon::worktree::no_branch_target),
        help("The branch may be in an invalid state")
    )]
    NoBranchTarget,

    #[error("Could not get current branch target")]
    #[diagnostic(code(workon::worktree::no_current_branch_target))]
    NoCurrentBranchTarget,

    #[error("Could not get local branch target")]
    #[diagnostic(code(workon::worktree::no_local_branch_target))]
    NoLocalBranchTarget,

    #[error("Worktree path has no parent directory")]
    #[diagnostic(
        code(workon::worktree::no_parent),
        help("Cannot create parent directories for worktree path")
    )]
    NoParent,

    #[error("Invalid worktree name: contains invalid UTF-8")]
    #[diagnostic(
        code(workon::worktree::invalid_name),
        help("Worktree names must be valid UTF-8 strings")
    )]
    InvalidName,

    #[error("Expected an empty index!")]
    #[diagnostic(code(workon::worktree::non_empty_index))]
    NonEmptyIndex,

    #[error("Worktree '{to}' already exists")]
    #[diagnostic(
        code(workon::worktree::target_exists),
        help("Choose a different name or remove the existing worktree first")
    )]
    TargetExists { to: String },

    #[error("Cannot move detached HEAD worktree")]
    #[diagnostic(
        code(workon::worktree::move_detached),
        help("Detached HEAD worktrees have no branch to rename")
    )]
    CannotMoveDetached,

    #[error("Branch '{0}' is protected and cannot be renamed")]
    #[diagnostic(
        code(workon::worktree::protected_branch_move),
        help("Protected branches are configured in workon.pruneProtectedBranches. Use --force to override.")
    )]
    ProtectedBranchMove(String),

    #[error("Worktree is dirty (uncommitted changes)")]
    #[diagnostic(
        code(workon::worktree::dirty_worktree),
        help("Commit or stash changes, or use --force to override")
    )]
    DirtyWorktree,

    #[error("Worktree has unpushed commits")]
    #[diagnostic(
        code(workon::worktree::unpushed_commits),
        help("Push commits first, or use --force to override")
    )]
    UnpushedCommits,
}

/// Configuration-related errors
#[derive(Error, Diagnostic, Debug)]
pub enum ConfigError {
    #[error("Invalid PR format: '{format}' - {reason}")]
    #[diagnostic(
        code(workon::config::invalid_pr_format),
        help("Valid placeholders: {{number}}, {{title}}, {{author}}, {{branch}}")
    )]
    InvalidPrFormat { format: String, reason: String },

    #[error("Config entry has no value")]
    #[diagnostic(code(workon::config::no_value))]
    NoValue,
}

/// Default branch detection errors
#[derive(Error, Diagnostic, Debug)]
pub enum DefaultBranchError {
    #[error("Could not determine default branch for remote {remote:?}")]
    #[diagnostic(
        code(workon::default_branch::no_remote_default),
        help("The remote may not have a default branch configured")
    )]
    NoRemoteDefault { remote: Option<String> },

    #[error("Remote is not connected")]
    #[diagnostic(
        code(workon::default_branch::not_connected),
        help("Failed to establish connection to remote repository")
    )]
    NotConnected,

    #[error("Could not determine default branch: neither 'main' nor 'master' exist, and init.defaultBranch is not configured")]
    #[diagnostic(
        code(workon::default_branch::no_default_branch),
        help("Set init.defaultBranch in your git config, or create a 'main' or 'master' branch")
    )]
    NoDefaultBranch,
}

/// Stacked diff workflow errors
#[derive(Error, Diagnostic, Debug)]
pub enum StackError {
    #[error("Stack model '{model}' is not yet supported")]
    #[diagnostic(
        code(workon::stack::unsupported_model),
        help(
            "Only 'graphite' is implemented in this version. \
             Support for branchless, sapling, and spr is planned."
        )
    )]
    UnsupportedModel { model: String },

    #[error("Unknown stack model '{value}'")]
    #[diagnostic(
        code(workon::stack::unknown_model),
        help("Valid values: graphite, none, auto")
    )]
    UnknownModel { value: String },

    #[error("Worktree granularity 'diff' is not yet implemented")]
    #[diagnostic(
        code(workon::stack::unsupported_granularity),
        help(
            "Only 'stack' (one worktree per stack) is supported in this version. \
             'diff' (one worktree per branch) is planned."
        )
    )]
    UnsupportedGranularity,

    #[error("Unknown worktree granularity '{value}'")]
    #[diagnostic(code(workon::stack::unknown_granularity), help("Valid values: stack"))]
    UnknownGranularity { value: String },

    #[error("Graphite CLI ('gt') is not installed or not in PATH")]
    #[diagnostic(
        code(workon::stack::gt_not_installed),
        help(
            "Install Graphite: https://graphite.dev/cli \
             Or set workon.stackModel = none to disable stack support."
        )
    )]
    GtNotInstalled,

    #[error("Graphite command failed: {stderr}")]
    #[diagnostic(code(workon::stack::gt_command_failed))]
    GtCommandFailed { stderr: String },

    #[error("Failed to parse Graphite metadata: {message}")]
    #[diagnostic(code(workon::stack::gt_parse_failed))]
    GtParseFailed { message: String },

    #[error("Repository is not Graphite-managed (no .graphite_repo_config)")]
    #[diagnostic(
        code(workon::stack::not_a_graphite_repo),
        help("Run 'gt init' in this repository, or unset workon.stackModel.")
    )]
    NotAGraphiteRepo,

    #[error("Branch '{branch}' exists in stack metadata but its local ref was deleted")]
    #[diagnostic(
        code(workon::stack::deleted_branch_node),
        help(
            "The branch was tracked by Graphite but its local ref no longer exists. \
             Run 'gt branch checkout {branch}' to restore it, or \
             'gt branch delete {branch}' to remove it from the stack."
        )
    )]
    DeletedBranchNode { branch: String },
}

/// Pull request-related errors
#[derive(Error, Diagnostic, Debug)]
pub enum PrError {
    #[error("Invalid PR reference: {input}")]
    #[diagnostic(
        code(workon::pr::invalid_reference),
        help("Use formats like #123, pr-123, or https://github.com/owner/repo/pull/123")
    )]
    InvalidReference { input: String },

    #[error("PR #{number} not found on remote {remote}")]
    #[diagnostic(
        code(workon::pr::not_found),
        help("Verify the PR number exists and you have access to the repository")
    )]
    PrNotFound { number: u32, remote: String },

    #[error("No git remote configured")]
    #[diagnostic(
        code(workon::pr::no_remote),
        help("Add a remote with: git remote add origin <url>")
    )]
    NoRemoteConfigured,

    #[error("Failed to fetch PR refs from {remote}: {message}")]
    #[diagnostic(
        code(workon::pr::fetch_failed),
        help("Check your network connection and repository access")
    )]
    FetchFailed { remote: String, message: String },

    #[error("gh CLI is not installed or not in PATH")]
    #[diagnostic(
        code(workon::pr::gh_not_installed),
        help("Install gh CLI: https://cli.github.com/")
    )]
    GhNotInstalled,

    #[error("Failed to fetch PR metadata from gh: {message}")]
    #[diagnostic(
        code(workon::pr::gh_fetch_failed),
        help("Check your network connection and GitHub authentication (gh auth status)")
    )]
    GhFetchFailed { message: String },

    #[error("Invalid JSON output from gh CLI: {message}")]
    #[diagnostic(
        code(workon::pr::gh_json_parse_failed),
        help("This may indicate a gh CLI version incompatibility")
    )]
    GhJsonParseFailed { message: String },

    #[error("Fork repository missing owner information")]
    #[diagnostic(
        code(workon::pr::missing_fork_owner),
        help("This PR may be from a deleted fork")
    )]
    MissingForkOwner,
}

/// In-place checkout errors
#[derive(Error, Diagnostic, Debug)]
pub enum CheckoutError {
    /// A git2 error during checkout
    #[error(transparent)]
    #[diagnostic(code(workon::checkout::git_error))]
    Git(#[from] git2::Error),

    /// Branch not found in the host worktree
    #[error("Branch '{branch}' not found in the worktree")]
    #[diagnostic(
        code(workon::checkout::branch_not_found),
        help("Ensure the branch exists locally before checking it out in place")
    )]
    BranchNotFound { branch: String },

    /// Checkout conflicts with uncommitted changes in the working tree
    #[error("Checkout of '{branch}' conflicts with uncommitted changes in {path}")]
    #[diagnostic(
        code(workon::checkout::conflict),
        help("Stash or commit changes first, or use the interactive prompt to shelve them")
    )]
    Conflict { branch: String, path: String },

    /// User aborted an interactive checkout
    #[error("Checkout aborted")]
    #[diagnostic(code(workon::checkout::aborted))]
    Aborted,
}

/// File copy errors
#[derive(Error, Diagnostic, Debug)]
pub enum CopyError {
    #[error("Invalid glob pattern '{pattern}'")]
    #[diagnostic(
        code(workon::copy::invalid_glob_pattern),
        help("Check glob pattern syntax: *, **, ?, [...]")
    )]
    InvalidGlobPattern {
        pattern: String,
        #[source]
        source: glob::PatternError,
    },

    #[error("Path is not valid UTF-8: {}", path.display())]
    #[diagnostic(code(workon::copy::invalid_path))]
    InvalidPath { path: PathBuf },

    #[error("Failed to read glob entry")]
    #[diagnostic(code(workon::copy::glob_error))]
    GlobEntry(#[from] glob::GlobError),

    #[error("Failed to copy '{}' to '{}'", src.display(), dest.display())]
    #[diagnostic(code(workon::copy::copy_failed))]
    CopyFailed {
        src: PathBuf,
        dest: PathBuf,
        #[source]
        source: std::io::Error,
    },

    #[error("Failed to open repository at '{}'", path.display())]
    #[diagnostic(
        code(workon::copy::repo_open_error),
        help("Ensure the path is a valid git repository")
    )]
    RepoOpen {
        path: PathBuf,
        #[source]
        source: git2::Error,
    },
}