outpost-core 0.1.3

Core library for Git Outpost, a clone-backed alternative to git worktree workflows.
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
use std::path::PathBuf;

use thiserror::Error;

pub type OutpostResult<T> = Result<T, OutpostError>;

#[derive(Debug, Error)]
pub enum OutpostError {
    #[error("not inside a Git repository: {}", .0.display())]
    NotARepo(PathBuf),

    #[error("not inside a managed outpost: {}", .0.display())]
    NotAnOutpost(PathBuf),

    #[error("source repository not found at {}", .0.display())]
    SourceMissing(PathBuf),

    #[error("{command} must be run from {expected}; effective cwd is {}", .cwd.display())]
    WrongContext {
        command: &'static str,
        expected: &'static str,
        cwd: PathBuf,
    },

    #[error("{command} requires <outpost> when run from source repository {}", .cwd.display())]
    MissingOutpostPath { command: &'static str, cwd: PathBuf },

    #[error("destination already exists: {}", .0.display())]
    DestinationExists(PathBuf),

    #[error("destination {} is inside an existing Git repository", .0.display())]
    DestinationInsideRepo(PathBuf),

    #[error("working tree is dirty in {}; {hint}", .repo.display())]
    DirtyTree { repo: PathBuf, hint: &'static str },

    #[error("branch {branch} has unpushed commits in {}; {hint}", .repo.display())]
    UnpushedCommits {
        repo: PathBuf,
        branch: String,
        hint: &'static str,
    },

    #[error("history diverges from source repository on branch {branch}")]
    Divergence { branch: String },

    #[error("branch not found: {branch} in {}", .repo.display())]
    BranchNotFound { branch: String, repo: PathBuf },

    #[error("no upstream tracking configured for branch {branch}")]
    NoUpstreamTracking { branch: String },

    #[error(
        "upstream is not a branch ref (got {merge_ref}); cannot synchronize from a non-branch upstream"
    )]
    UpstreamNotABranch { merge_ref: String },

    #[error("invalid ref name: {name}")]
    InvalidRefName { name: String },

    #[error(
        "source repository {} has {branch} checked out; cannot push to a non-bare checked-out branch (configure receive.denyCurrentBranch=updateInstead on the source, or check out a different branch in the source)",
        .r#source.display()
    )]
    PushIntoCheckedOutBranch { r#source: PathBuf, branch: String },

    #[error("branch {branch} does not exist on the source repository")]
    AmbiguousBranchCreation { branch: String },

    #[error("outpost is locked: {}{reason}", .path.display())]
    OutpostLocked { path: PathBuf, reason: String },

    #[error("registry entry path is not a managed outpost of this source: {}", .0.display())]
    RegistryEntryNotManaged(PathBuf),

    #[error("registry entry not found: {}", .0.display())]
    RegistryEntryNotFound(PathBuf),

    #[error("outpost id prefix not found: {0}")]
    OutpostIdPrefixNotFound(String),

    #[error("outpost id prefix is ambiguous: {0}")]
    OutpostIdPrefixAmbiguous(String),

    #[error("outpost selector is ambiguous: {0}")]
    OutpostSelectorAmbiguous(String),

    #[error("invalid registry file at {}: {reason}", .path.display())]
    BadRegistry { path: PathBuf, reason: String },

    #[error("invalid outpost metadata at {}: {reason}", .outpost.display())]
    BadMetadata { outpost: PathBuf, reason: String },

    #[error("git command failed: `git {args}` (exit {code}): {stderr}")]
    GitFailed {
        args: String,
        code: i32,
        stderr: String,
    },

    #[error("git command terminated by signal: `git {args}`{signal_str}")]
    GitTerminatedBySignal { args: String, signal_str: String },

    #[error("io error at {}: {source}", .path.display())]
    IoAt {
        path: PathBuf,
        source: std::io::Error,
    },
}

impl OutpostError {
    pub fn exit_code(&self) -> u8 {
        use OutpostError::*;
        match self {
            NotARepo(_)
            | NotAnOutpost(_)
            | SourceMissing(_)
            | WrongContext { .. }
            | MissingOutpostPath { .. } => 2,
            DestinationExists(_)
            | DestinationInsideRepo(_)
            | DirtyTree { .. }
            | UnpushedCommits { .. }
            | OutpostLocked { .. } => 3,
            Divergence { .. }
            | PushIntoCheckedOutBranch { .. }
            | AmbiguousBranchCreation { .. } => 4,
            BranchNotFound { .. }
            | NoUpstreamTracking { .. }
            | InvalidRefName { .. }
            | UpstreamNotABranch { .. } => 5,
            BadRegistry { .. }
            | BadMetadata { .. }
            | OutpostIdPrefixNotFound(_)
            | OutpostIdPrefixAmbiguous(_)
            | OutpostSelectorAmbiguous(_)
            | RegistryEntryNotManaged(_)
            | RegistryEntryNotFound(_) => 6,
            GitFailed { code, .. } => (*code).clamp(1, 125) as u8,
            GitTerminatedBySignal { .. } => 137,
            IoAt { .. } => 70,
        }
    }
}

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

    fn path(value: &str) -> PathBuf {
        PathBuf::from(value)
    }

    #[test]
    fn display_strings_match_snapshot() {
        let cases = [
            (
                OutpostError::NotARepo(path("/repo")),
                "not inside a Git repository: /repo",
            ),
            (
                OutpostError::NotAnOutpost(path("/outpost")),
                "not inside a managed outpost: /outpost",
            ),
            (
                OutpostError::SourceMissing(path("/source")),
                "source repository not found at /source",
            ),
            (
                OutpostError::WrongContext {
                    command: "pull",
                    expected: "a managed outpost",
                    cwd: path("/source"),
                },
                "pull must be run from a managed outpost; effective cwd is /source",
            ),
            (
                OutpostError::MissingOutpostPath {
                    command: "lock",
                    cwd: path("/source"),
                },
                "lock requires <outpost> when run from source repository /source",
            ),
            (
                OutpostError::DestinationExists(path("/dest")),
                "destination already exists: /dest",
            ),
            (
                OutpostError::DestinationInsideRepo(path("/dest")),
                "destination /dest is inside an existing Git repository",
            ),
            (
                OutpostError::DirtyTree {
                    repo: path("/repo"),
                    hint: "pass --force",
                },
                "working tree is dirty in /repo; pass --force",
            ),
            (
                OutpostError::UnpushedCommits {
                    repo: path("/repo"),
                    branch: "main".to_owned(),
                    hint: "push first",
                },
                "branch main has unpushed commits in /repo; push first",
            ),
            (
                OutpostError::Divergence {
                    branch: "main".to_owned(),
                },
                "history diverges from source repository on branch main",
            ),
            (
                OutpostError::BranchNotFound {
                    branch: "feature".to_owned(),
                    repo: path("/repo"),
                },
                "branch not found: feature in /repo",
            ),
            (
                OutpostError::NoUpstreamTracking {
                    branch: "feature".to_owned(),
                },
                "no upstream tracking configured for branch feature",
            ),
            (
                OutpostError::UpstreamNotABranch {
                    merge_ref: "refs/tags/v1".to_owned(),
                },
                "upstream is not a branch ref (got refs/tags/v1); cannot synchronize from a non-branch upstream",
            ),
            (
                OutpostError::InvalidRefName {
                    name: "-evil".to_owned(),
                },
                "invalid ref name: -evil",
            ),
            (
                OutpostError::PushIntoCheckedOutBranch {
                    source: path("/source"),
                    branch: "main".to_owned(),
                },
                "source repository /source has main checked out; cannot push to a non-bare checked-out branch (configure receive.denyCurrentBranch=updateInstead on the source, or check out a different branch in the source)",
            ),
            (
                OutpostError::AmbiguousBranchCreation {
                    branch: "feature".to_owned(),
                },
                "branch feature does not exist on the source repository",
            ),
            (
                OutpostError::OutpostLocked {
                    path: path("/outpost"),
                    reason: ": release".to_owned(),
                },
                "outpost is locked: /outpost: release",
            ),
            (
                OutpostError::RegistryEntryNotManaged(path("/outpost")),
                "registry entry path is not a managed outpost of this source: /outpost",
            ),
            (
                OutpostError::RegistryEntryNotFound(path("/missing")),
                "registry entry not found: /missing",
            ),
            (
                OutpostError::OutpostIdPrefixNotFound("abcde".to_owned()),
                "outpost id prefix not found: abcde",
            ),
            (
                OutpostError::OutpostIdPrefixAmbiguous("abcde".to_owned()),
                "outpost id prefix is ambiguous: abcde",
            ),
            (
                OutpostError::OutpostSelectorAmbiguous("abcde".to_owned()),
                "outpost selector is ambiguous: abcde",
            ),
            (
                OutpostError::BadRegistry {
                    path: path("/repo/.outpost/registry.json"),
                    reason: "invalid json".to_owned(),
                },
                "invalid registry file at /repo/.outpost/registry.json: invalid json",
            ),
            (
                OutpostError::BadMetadata {
                    outpost: path("/outpost"),
                    reason: "missing source".to_owned(),
                },
                "invalid outpost metadata at /outpost: missing source",
            ),
            (
                OutpostError::GitFailed {
                    args: "status --short".to_owned(),
                    code: 1,
                    stderr: "fatal".to_owned(),
                },
                "git command failed: `git status --short` (exit 1): fatal",
            ),
            (
                OutpostError::GitTerminatedBySignal {
                    args: "fetch".to_owned(),
                    signal_str: " (signal 9)".to_owned(),
                },
                "git command terminated by signal: `git fetch` (signal 9)",
            ),
            (
                OutpostError::IoAt {
                    path: path("/repo/.outpost/registry.json"),
                    source: std::io::Error::new(std::io::ErrorKind::NotFound, "missing"),
                },
                "io error at /repo/.outpost/registry.json: missing",
            ),
        ];

        for (error, expected) in cases {
            assert_eq!(error.to_string(), expected);
        }
    }

    #[test]
    fn exit_code_maps_each_variant() {
        let cases = [
            (OutpostError::NotARepo(path("/repo")), 2),
            (OutpostError::NotAnOutpost(path("/outpost")), 2),
            (OutpostError::SourceMissing(path("/source")), 2),
            (
                OutpostError::WrongContext {
                    command: "pull",
                    expected: "a managed outpost",
                    cwd: path("/source"),
                },
                2,
            ),
            (
                OutpostError::MissingOutpostPath {
                    command: "lock",
                    cwd: path("/source"),
                },
                2,
            ),
            (OutpostError::DestinationExists(path("/dest")), 3),
            (OutpostError::DestinationInsideRepo(path("/dest")), 3),
            (
                OutpostError::DirtyTree {
                    repo: path("/repo"),
                    hint: "pass --force",
                },
                3,
            ),
            (
                OutpostError::UnpushedCommits {
                    repo: path("/repo"),
                    branch: "main".to_owned(),
                    hint: "push first",
                },
                3,
            ),
            (
                OutpostError::Divergence {
                    branch: "main".to_owned(),
                },
                4,
            ),
            (
                OutpostError::BranchNotFound {
                    branch: "feature".to_owned(),
                    repo: path("/repo"),
                },
                5,
            ),
            (
                OutpostError::NoUpstreamTracking {
                    branch: "feature".to_owned(),
                },
                5,
            ),
            (
                OutpostError::UpstreamNotABranch {
                    merge_ref: "refs/tags/v1".to_owned(),
                },
                5,
            ),
            (
                OutpostError::InvalidRefName {
                    name: "-evil".to_owned(),
                },
                5,
            ),
            (
                OutpostError::PushIntoCheckedOutBranch {
                    source: path("/source"),
                    branch: "main".to_owned(),
                },
                4,
            ),
            (
                OutpostError::AmbiguousBranchCreation {
                    branch: "feature".to_owned(),
                },
                4,
            ),
            (
                OutpostError::OutpostLocked {
                    path: path("/outpost"),
                    reason: ": release".to_owned(),
                },
                3,
            ),
            (OutpostError::RegistryEntryNotManaged(path("/outpost")), 6),
            (OutpostError::RegistryEntryNotFound(path("/missing")), 6),
            (OutpostError::OutpostIdPrefixNotFound("abcde".to_owned()), 6),
            (
                OutpostError::OutpostIdPrefixAmbiguous("abcde".to_owned()),
                6,
            ),
            (
                OutpostError::OutpostSelectorAmbiguous("abcde".to_owned()),
                6,
            ),
            (
                OutpostError::BadRegistry {
                    path: path("/repo/.outpost/registry.json"),
                    reason: "invalid json".to_owned(),
                },
                6,
            ),
            (
                OutpostError::BadMetadata {
                    outpost: path("/outpost"),
                    reason: "missing source".to_owned(),
                },
                6,
            ),
            (
                OutpostError::GitFailed {
                    args: "status".to_owned(),
                    code: 42,
                    stderr: "fatal".to_owned(),
                },
                42,
            ),
            (
                OutpostError::GitTerminatedBySignal {
                    args: "fetch".to_owned(),
                    signal_str: " (signal 9)".to_owned(),
                },
                137,
            ),
            (
                OutpostError::IoAt {
                    path: path("/repo/.outpost/registry.json"),
                    source: std::io::Error::new(std::io::ErrorKind::NotFound, "missing"),
                },
                70,
            ),
        ];

        for (error, expected) in cases {
            assert_eq!(error.exit_code(), expected);
        }

        assert_eq!(
            OutpostError::GitFailed {
                args: "status".to_owned(),
                code: -1,
                stderr: "fatal".to_owned(),
            }
            .exit_code(),
            1
        );
        assert_eq!(
            OutpostError::GitFailed {
                args: "status".to_owned(),
                code: 256,
                stderr: "fatal".to_owned(),
            }
            .exit_code(),
            125
        );
    }
}