rskit-git 0.2.0-alpha.4

Composable git repository interfaces backed by libgit2
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Git-specific error types.

use std::io;
use std::path::PathBuf;

use rskit_errors::{AppError, ErrorCode};

/// Git domain errors.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum GitError {
    /// Repository not found at path.
    #[error("repository not found at {path}")]
    NotFound {
        /// Filesystem path that did not contain a git repository.
        path: PathBuf,
    },

    /// Git reference not found.
    #[error("ref not found: {refname}")]
    RefNotFound {
        /// Missing reference name.
        refname: String,
    },

    /// Remote not found.
    #[error("remote not found: {name}")]
    RemoteNotFound {
        /// Missing remote name.
        name: String,
    },

    /// Config key not found.
    #[error("config key not found: {key}")]
    ConfigNotFound {
        /// Missing config key.
        key: String,
    },

    /// Ambiguous git reference.
    #[error("ambiguous ref: {refname}")]
    AmbiguousRef {
        /// Reference name that resolved to multiple candidates.
        refname: String,
    },

    /// Existing resource conflict.
    #[error("{kind} already exists: {name}")]
    AlreadyExists {
        /// Existing resource kind.
        kind: &'static str,
        /// Existing resource name.
        name: String,
    },

    /// Attempted to delete the currently checked out branch.
    #[error("branch is currently checked out: {name}")]
    CheckedOutBranch {
        /// Branch name.
        name: String,
    },

    /// Merge conflict in a file.
    #[error("merge conflict in {path}")]
    Conflict {
        /// Path containing merge conflict markers or index conflicts.
        path: PathBuf,
    },

    /// HEAD is detached (not pointing to a branch).
    #[error("detached HEAD")]
    DetachedHead,

    /// Invalid blame line range.
    #[error("invalid line range: {start}..{end}")]
    InvalidLineRange {
        /// One-based starting line.
        start: usize,
        /// One-based ending line.
        end: usize,
    },

    /// Invalid repository path.
    #[error("invalid path: {path}")]
    InvalidPath {
        /// Invalid repository-relative path.
        path: String,
    },

    /// Invalid git config key.
    #[error("invalid config key: {key}")]
    InvalidConfigKey {
        /// Invalid git config key.
        key: String,
    },

    /// No merge base exists between the two commits.
    #[error("no merge base found between {a} and {b}")]
    NoMergeBase {
        /// First commit reference.
        a: String,
        /// Second commit reference.
        b: String,
    },

    /// Commit signing is not supported by the selected backend.
    #[error("commit signing is not supported by the selected backend")]
    SigningNotSupported,

    /// Git author/committer identity is not configured.
    ///
    /// libgit2 cannot build a default signature because `user.name` and/or
    /// `user.email` are unset in the effective git configuration. This is an
    /// actionable configuration problem, not an internal failure.
    #[error("git identity is not configured: {key} is not set")]
    IdentityMissing {
        /// Missing identity config key (for example `user.name` or `user.email`).
        key: String,
    },

    /// Unsupported transport configuration.
    #[error("invalid transport configuration: {kind}")]
    InvalidTransport {
        /// Transport kind that could not be applied.
        kind: String,
    },

    /// Network error during remote operations.
    #[error("network error: {0}")]
    Network(String),

    /// Authentication or authorization failure during a remote operation.
    ///
    /// libgit2 reports the transport-level failure (bad credentials, expired
    /// token, insufficient scope/permission) via an `Auth` code or an
    /// `Http`/`Ssh`/`Callback` class. This is an actionable access problem, not
    /// an internal failure, so its (credential-redacted) message is surfaced to
    /// the user.
    #[error("remote authentication failed: {message}")]
    RemoteAuth {
        /// Credential-redacted description of the authentication failure.
        message: String,
    },

    /// The remote rejected the push of one or more references.
    ///
    /// Covers a non-fast-forward update, a protected-branch/hook rejection, and
    /// any other server-reported ref rejection. The rejection is reported by
    /// the remote (not an internal failure), so the offending ref and the
    /// (credential-redacted) reason are surfaced to the user.
    #[error("remote rejected push to {refname}: {reason}")]
    PushRejected {
        /// Fully-qualified destination reference(s) the remote rejected; a
        /// comma-separated list when several refs were pushed together.
        refname: String,
        /// Credential-redacted rejection reason reported by the remote.
        reason: String,
    },

    /// Git CLI command failure.
    #[error("git CLI command failed: git {args:?}: {stderr}")]
    CommandFailed {
        /// CLI arguments that were attempted.
        args: Vec<String>,
        /// Process exit code, if available.
        exit_code: Option<i32>,
        /// Standard output (may contain conflict diagnostics).
        stdout: String,
        /// Standard error output.
        stderr: String,
        /// Whether stdout exceeded the configured capture limit.
        stdout_truncated: bool,
        /// Whether stderr exceeded the configured capture limit.
        stderr_truncated: bool,
    },

    /// Invalid object ID string.
    #[error("invalid object ID: {value}")]
    InvalidOid {
        /// Invalid hex value.
        value: String,
    },

    /// Operation is intentionally not implemented.
    #[error("git operation not implemented: {operation}")]
    NotImplemented {
        /// Unimplemented operation name.
        operation: &'static str,
    },

    /// Internal git2 error.
    #[error(transparent)]
    Internal(#[from] git2::Error),
}

impl From<GitError> for AppError {
    fn from(error: GitError) -> Self {
        match error {
            GitError::NotFound { path } => {
                let display = path.display().to_string();
                AppError::not_found("repository", Some(&display))
            }
            GitError::RefNotFound { refname } => AppError::not_found("ref", Some(&refname)),
            GitError::RemoteNotFound { name } => AppError::not_found("remote", Some(&name)),
            GitError::ConfigNotFound { key } => AppError::not_found("config", Some(&key)),
            GitError::AmbiguousRef { refname } => {
                AppError::invalid_input("ref", format!("ambiguous ref: {refname}"))
            }
            GitError::AlreadyExists { kind, name } => {
                AppError::already_exists(format!("{kind} '{name}'"))
            }
            GitError::CheckedOutBranch { name } => {
                AppError::conflict(format!("branch is currently checked out: {name}"))
            }
            GitError::Conflict { path } => {
                AppError::conflict(format!("merge conflict in {}", path.display()))
            }
            GitError::DetachedHead => AppError::invalid_input("HEAD", "detached HEAD"),
            GitError::InvalidLineRange { start, end } => {
                AppError::invalid_input("line range", format!("{start}..{end}"))
            }
            GitError::InvalidPath { path } => AppError::invalid_input("path", path),
            GitError::InvalidConfigKey { key } => AppError::invalid_input("key", key),
            GitError::NoMergeBase { a, b } => {
                AppError::not_found("merge base", Some(&format!("{a}..{b}")))
            }
            GitError::SigningNotSupported => AppError::invalid_input(
                "sign",
                "commit signing is not supported by the selected backend",
            ),
            GitError::IdentityMissing { key } => {
                AppError::invalid_input("git identity", format!("{key} is not configured"))
                    .hint(
                        "Set it with `git config user.name \"…\"` and \
                         `git config user.email \"…\"` (add --global to apply it for every repository).",
                    )
            }
            GitError::InvalidTransport { kind } => AppError::invalid_input("transport", kind),
            GitError::Network(message) => {
                AppError::external_service("git", io::Error::other(message))
            }
            GitError::RemoteAuth { message } => {
                AppError::unauthorized(format!("git remote authentication failed: {message}"))
                    .hint(
                        "Check the remote credentials and that the token/key grants push access \
                         to this repository (e.g. on GitHub, a fine-grained token needs the \
                         `contents: write` permission).",
                    )
            }
            GitError::PushRejected { refname, reason } => {
                AppError::conflict(format!("remote rejected push to {refname}: {reason}")).hint(
                    "The remote rejected the update. Integrate remote changes (fetch and rebase) \
                     for a non-fast-forward, or, if the branch is protected, land the commit \
                     through a pull request and push tags only.",
                )
            }
            GitError::CommandFailed {
                args,
                exit_code,
                stdout,
                stderr,
                stdout_truncated,
                stderr_truncated,
            } => {
                let mut detail = format!("git {}: {}", args.join(" "), stderr);
                if let Some(exit_code) = exit_code {
                    detail.push_str(&format!(" (exit code: {exit_code})"));
                }
                if stdout_truncated || stderr_truncated {
                    detail.push_str(&format!(
                        " (stdout_truncated: {stdout_truncated}, stderr_truncated: {stderr_truncated})"
                    ));
                }
                if !stdout.is_empty() {
                    detail.push_str("\nstdout: ");
                    detail.push_str(&stdout);
                }
                AppError::external_service("git", io::Error::other(detail))
            }
            GitError::InvalidOid { value } => AppError::invalid_input("oid", value),
            GitError::NotImplemented { operation } => AppError::new(
                ErrorCode::InvalidInput,
                format!("git operation not supported: {operation}"),
            ),
            GitError::Internal(inner) => AppError::internal(inner),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn git_errors_map_to_actionable_app_error_codes() {
        let cases = [
            (
                GitError::NotFound {
                    path: PathBuf::from("repo"),
                },
                ErrorCode::NotFound,
            ),
            (
                GitError::RefNotFound {
                    refname: "main".to_string(),
                },
                ErrorCode::NotFound,
            ),
            (
                GitError::RemoteNotFound {
                    name: "origin".to_string(),
                },
                ErrorCode::NotFound,
            ),
            (
                GitError::ConfigNotFound {
                    key: "user.name".to_string(),
                },
                ErrorCode::NotFound,
            ),
            (
                GitError::AmbiguousRef {
                    refname: "feature".to_string(),
                },
                ErrorCode::InvalidInput,
            ),
            (
                GitError::AlreadyExists {
                    kind: "branch",
                    name: "main".to_string(),
                },
                ErrorCode::AlreadyExists,
            ),
            (
                GitError::CheckedOutBranch {
                    name: "main".to_string(),
                },
                ErrorCode::Conflict,
            ),
            (
                GitError::Conflict {
                    path: PathBuf::from("src/lib.rs"),
                },
                ErrorCode::Conflict,
            ),
            (GitError::DetachedHead, ErrorCode::InvalidInput),
            (
                GitError::InvalidLineRange { start: 5, end: 3 },
                ErrorCode::InvalidInput,
            ),
            (
                GitError::InvalidPath {
                    path: "../outside".to_string(),
                },
                ErrorCode::InvalidInput,
            ),
            (
                GitError::InvalidConfigKey {
                    key: "bad key".to_string(),
                },
                ErrorCode::InvalidInput,
            ),
            (
                GitError::NoMergeBase {
                    a: "a".to_string(),
                    b: "b".to_string(),
                },
                ErrorCode::NotFound,
            ),
            (GitError::SigningNotSupported, ErrorCode::InvalidInput),
            (
                GitError::IdentityMissing {
                    key: "user.name".to_string(),
                },
                ErrorCode::InvalidInput,
            ),
            (
                GitError::InvalidTransport {
                    kind: "ssh".to_string(),
                },
                ErrorCode::InvalidInput,
            ),
            (
                GitError::Network("offline".to_string()),
                ErrorCode::ExternalService,
            ),
            (
                GitError::RemoteAuth {
                    message: "401 Unauthorized".to_string(),
                },
                ErrorCode::Unauthorized,
            ),
            (
                GitError::PushRejected {
                    refname: "refs/heads/main".to_string(),
                    reason: "protected branch".to_string(),
                },
                ErrorCode::Conflict,
            ),
            (
                GitError::CommandFailed {
                    args: vec!["status".to_string()],
                    exit_code: Some(128),
                    stdout: "partial stdout".to_string(),
                    stderr: "fatal".to_string(),
                    stdout_truncated: true,
                    stderr_truncated: false,
                },
                ErrorCode::ExternalService,
            ),
            (
                GitError::InvalidOid {
                    value: "not-a-sha".to_string(),
                },
                ErrorCode::InvalidInput,
            ),
            (
                GitError::NotImplemented { operation: "sign" },
                ErrorCode::InvalidInput,
            ),
            (
                GitError::Internal(git2::Error::from_str("git2 failed")),
                ErrorCode::Internal,
            ),
        ];

        for (git_error, expected) in cases {
            let app_error = AppError::from(git_error);
            assert_eq!(app_error.code(), expected);
        }
    }

    #[test]
    fn command_failure_message_includes_diagnostics() {
        let app_error = AppError::from(GitError::CommandFailed {
            args: vec!["push".to_string(), "origin".to_string()],
            exit_code: Some(1),
            stdout: "hint".to_string(),
            stderr: "denied".to_string(),
            stdout_truncated: false,
            stderr_truncated: true,
        });

        let detail = app_error
            .cause()
            .as_ref()
            .map(ToString::to_string)
            .unwrap_or_else(|| app_error.message().to_string());
        assert!(detail.contains("push origin"));
        assert!(detail.contains("denied"));
        assert!(detail.contains("exit code: 1"));
        assert!(detail.contains("stderr_truncated: true"));
        assert!(detail.contains("stdout: hint"));
    }

    #[test]
    fn identity_missing_message_is_actionable() {
        let app_error = AppError::from(GitError::IdentityMissing {
            key: "user.email".to_string(),
        });

        assert_eq!(app_error.code(), ErrorCode::InvalidInput);
        let message = app_error.message();
        assert!(message.contains("user.email"));
        assert!(message.contains("git config user.name"));
        assert!(message.contains("git config user.email"));
    }

    #[test]
    fn remote_auth_message_is_actionable() {
        let app_error = AppError::from(GitError::RemoteAuth {
            message: "403 Forbidden".to_string(),
        });

        assert_eq!(app_error.code(), ErrorCode::Unauthorized);
        assert!(app_error.message().contains("403 Forbidden"));
    }

    #[test]
    fn push_rejected_message_names_ref_and_reason() {
        let app_error = AppError::from(GitError::PushRejected {
            refname: "refs/heads/main".to_string(),
            reason: "protected branch hook declined".to_string(),
        });

        assert_eq!(app_error.code(), ErrorCode::Conflict);
        let message = app_error.message();
        assert!(message.contains("refs/heads/main"));
        assert!(message.contains("protected branch hook declined"));
    }
}