mkit-cli 0.4.1

The mkit command-line tool: a content-addressed VCS with native attestation support
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! `mkit cherry-pick <commit> | --continue | --abort` — replay a single
//! commit onto HEAD, with a resolvable-conflict workflow (#177).
//!
//! On a clean merge we create a new commit on the current branch using
//! the original commit's message. On conflict we materialise the
//! conflict material into the worktree + index, persist
//! `CHERRY_PICK_HEAD`/`CHERRY_PICK_MSG`/`ORIG_HEAD` and the
//! `mkit-conflicts` sidecar, and exit non-zero. The user resolves,
//! `mkit add`s, then runs `mkit cherry-pick --continue`.
//!
//! `--continue` refuses unless `CHERRY_PICK_HEAD` exists and no
//! marker-bearing file remains; it builds the final tree from the
//! resolved index. `--abort` restores HEAD/ref/index/worktree to
//! `ORIG_HEAD`.

use std::io::Write;

use clap::{Parser, ValueEnum};
use mkit_core::hash::Hash;
use mkit_core::layout::RepoLayout;
use mkit_core::object::{Commit, Object};
use mkit_core::ops::cherry_pick::{CherryPickError, cherry_pick};
use mkit_core::ops::conflict_state::{
    self, CherryPickState, in_progress_op_name, is_cherry_pick_in_progress,
};
use mkit_core::refs::{self, Head};
use mkit_core::serialize;
use mkit_core::store::ObjectStore;
use mkit_core::worktree;

use super::{advance_head, error as emit_err, load_tree_hash};
use crate::clap_shim;
use crate::config;
use crate::exit;
use crate::format::{self, JsonObject, json_string_array};

#[derive(Debug, Clone, Copy, ValueEnum)]
enum CherryPickFormat {
    Default,
    Json,
}

#[derive(Debug, Parser)]
#[command(name = "mkit cherry-pick", about = "Apply a single commit onto HEAD.")]
struct CherryPickOpts {
    /// Continue an in-progress cherry-pick after resolving conflicts.
    #[arg(long = "continue", conflicts_with_all = ["abort", "commit"])]
    cont: bool,
    /// Abort the in-progress cherry-pick and restore the original HEAD.
    #[arg(long, conflicts_with_all = ["cont", "commit"])]
    abort: bool,
    /// Apply the picked change to the index + worktree without creating a
    /// commit (like `git cherry-pick -n`). Run `mkit commit` when ready;
    /// the result has the current branch as its single parent.
    #[arg(short = 'n', long = "no-commit", conflicts_with_all = ["cont", "abort"])]
    no_commit: bool,
    /// Select the mainline parent (1-based) when replaying a merge commit,
    /// like `git cherry-pick -m`. Required for a merge (mkit refuses to
    /// guess which side is the mainline) and rejected for a non-merge.
    #[arg(short = 'm', long = "mainline", value_name = "PARENT-NUMBER", conflicts_with_all = ["cont", "abort"])]
    mainline: Option<usize>,
    /// Emit a machine-readable JSON result object to stdout describing
    /// the outcome: a new commit, `--no-commit` staging, a conflict
    /// pause (`"conflicts":[<path>,...]`), or an error.
    #[arg(long, value_enum, default_value = "default")]
    format: CherryPickFormat,
    /// Commit to replay: a ref, full/short hash, or `HEAD~n` revspec.
    commit: Option<String>,
}

/// `error(msg, code)` plus, when `json` is set, a `{"ok":false,...}`
/// line on stdout.
fn emit_err_json(msg: &str, code: u8, json: bool) -> u8 {
    if json {
        let mut obj = JsonObject::new();
        obj.field_bool("ok", false).field_str("error", msg);
        let mut stdout = std::io::stdout().lock();
        let _ = writeln!(stdout, "{}", obj.finish());
    }
    emit_err(msg, code)
}

#[must_use]
pub fn run(args: &[String]) -> u8 {
    let opts = match clap_shim::parse::<CherryPickOpts>("mkit cherry-pick", args) {
        Ok(o) => o,
        Err(code) => return code,
    };
    let json = matches!(opts.format, CherryPickFormat::Json);
    let cwd = match std::env::current_dir() {
        Ok(p) => p,
        Err(e) => return emit_err(&format!("cwd: {e}"), exit::NOINPUT),
    };
    let layout = match super::resolve_layout(&cwd) {
        Ok(layout) => layout,
        Err(code) => return code,
    };
    let store = match ObjectStore::open(&layout) {
        Ok(s) => s,
        Err(e) => return emit_err(&format!("not a mkit repo: {e}"), exit::GENERAL_ERROR),
    };
    let _lock = match super::acquire_worktree_lock(&layout) {
        Ok(l) => l,
        Err(code) => return code,
    };

    if opts.abort {
        abort(&layout, &store, json)
    } else if opts.cont {
        cont(&layout, &store, json)
    } else if let Some(hex) = opts.commit.as_deref() {
        start(&layout, &store, hex, opts.no_commit, opts.mainline, json)
    } else {
        super::usage_error("usage: mkit cherry-pick <commit> | --continue | --abort")
    }
}

#[allow(clippy::too_many_lines)]
fn start(
    layout: &RepoLayout,
    store: &ObjectStore,
    hex: &str,
    no_commit: bool,
    mainline: Option<usize>,
    json: bool,
) -> u8 {
    let emit_err = |msg: &str, code: u8| emit_err_json(msg, code, json);
    if let Some(op) = in_progress_op_name(layout) {
        return emit_err(
            &format!("a {op} is already in progress (use --continue or --abort)"),
            exit::GENERAL_ERROR,
        );
    }
    let target: Hash = match super::revspec::resolve_revision(store, layout, hex) {
        // Peel annotated/signed tags to their target commit so
        // `mkit cherry-pick <annotated-tag>` works like git (a tag is a
        // ref, which the doc comment advertises as acceptable). Mirrors
        // `merge`'s behavior.
        Ok(h) => super::log::peel_tags(store, h),
        Err(e) => return emit_err(&format!("bad commit: {e}"), exit::DATAERR),
    };

    let ours = match refs::resolve_head(layout) {
        Ok(Some(h)) => h,
        Ok(None) => return emit_err("no commits on current branch", exit::GENERAL_ERROR),
        Err(e) => return emit_err(&format!("resolve HEAD: {e}"), exit::GENERAL_ERROR),
    };
    let ours_tree = match store.read_object(&ours) {
        Ok(Object::Commit(c)) => c.tree_hash,
        Ok(_) => return emit_err("HEAD is not a commit", exit::DATAERR),
        Err(e) => return emit_err(&format!("read HEAD: {e}"), exit::GENERAL_ERROR),
    };

    let result = match cherry_pick(store, target, ours_tree, mainline) {
        Ok(r) => r,
        // Mainline-selection misuse is a usage error (bad invocation),
        // distinct from a runtime store/merge failure.
        Err(
            e @ (CherryPickError::MergeNeedsMainline
            | CherryPickError::MainlineForNonMerge
            | CherryPickError::BadMainline { .. }),
        ) => return emit_err(&format!("cherry-pick: {e}"), exit::USAGE),
        Err(e) => return emit_err(&format!("cherry-pick: {e}"), exit::GENERAL_ERROR),
    };

    if result.has_conflicts() {
        // `-n` must never leave a committable conflict. mkit cannot represent
        // a "staged but unresolved" conflict the way git's index can, and
        // recording markers with no sequencer state would let a later
        // `mkit commit` (which only guards merges) commit unresolved `<<<<<<<`
        // markers. So we refuse the conflicting `-n` pick BEFORE touching the
        // worktree — nothing is written. Re-run without `-n` (resumable via
        // `--continue`/`--abort`) or resolve manually.
        if no_commit {
            return emit_err(
                &format!(
                    "cherry-pick -n of {} conflicts; mkit cannot stage an unresolved \
                     conflict without committing — re-run without -n, then resolve and \
                     `mkit cherry-pick --continue`",
                    format::short_hash(&target, 8)
                ),
                exit::GENERAL_ERROR,
            );
        }
        if let Err(e) = super::ensure_restore_safe(layout, store, result.tree_hash) {
            return emit_err(&e, exit::GENERAL_ERROR);
        }
        let records = match super::conflict::materialize_conflicts(
            layout,
            store,
            result.tree_hash,
            &result.conflicts,
        ) {
            Ok(r) => r,
            Err(e) => return emit_err(&e, exit::GENERAL_ERROR),
        };
        let state = CherryPickState {
            cherry_pick_head: target,
            orig_head: ours,
            message: result.original_message.clone(),
        };
        if let Err(e) = conflict_state::write_cherry_pick_state(layout, &state, &records) {
            return emit_err(&format!("write cherry-pick state: {e}"), exit::CANTCREAT);
        }
        // Record the result tree so `--abort` treats the operation's clean
        // hunks (not just conflict paths) as discardable.
        if let Err(e) =
            conflict_state::write_result_tree(layout.worktree_state_dir(), &result.tree_hash)
        {
            return emit_err(&format!("write cherry-pick state: {e}"), exit::CANTCREAT);
        }
        let mut stderr = std::io::stderr().lock();
        // git-shaped per-path conflict lines (additive), then mkit's
        // resumable-flow hint.
        for rec in &records {
            let _ = writeln!(stderr, "CONFLICT (content): Merge conflict in {}", rec.path);
        }
        let _ = writeln!(
            stderr,
            "hint: resolve the files above, `mkit add` them, then run \
             `mkit cherry-pick --continue` (or `mkit cherry-pick --abort`)"
        );
        drop(stderr);
        if json {
            let paths: Vec<&str> = records.iter().map(|r| r.path.as_str()).collect();
            let mut obj = JsonObject::new();
            obj.field_bool("ok", false)
                .field_str("kind", "conflict")
                .field_raw("conflicts", &json_string_array(&paths))
                .field_str(
                    "error",
                    "cherry-pick conflict; resolve and continue or abort",
                );
            let mut stdout = std::io::stdout().lock();
            let _ = writeln!(stdout, "{}", obj.finish());
        }
        return exit::GENERAL_ERROR;
    }

    if let Err(e) = super::ensure_restore_safe(layout, store, result.tree_hash) {
        return emit_err(&e, exit::GENERAL_ERROR);
    }

    // `--no-commit`: stage the picked tree into the index + worktree but do
    // not commit or move HEAD. The next `mkit commit` records it as an
    // ordinary single-parent commit on the current branch.
    if no_commit {
        if let Err(e) = super::restore_worktree_and_index(layout, store, result.tree_hash) {
            return emit_err(&e, exit::GENERAL_ERROR);
        }
        // Restoring from a tree drops staged DELETIONS; re-stage them as
        // tombstones so a deletion-bearing pick (or an all-deletions pick)
        // stays staged and `mkit commit` records it.
        if let Err(e) =
            super::stage_removed_tombstones(layout, store, Some(ours_tree), result.tree_hash)
        {
            return emit_err(&e, exit::GENERAL_ERROR);
        }
        let mut stderr = std::io::stderr().lock();
        let _ = writeln!(
            stderr,
            "staged cherry-pick of {} (no commit; run `mkit commit` when ready)",
            format::short_hash(&target, 8),
        );
        drop(stderr);
        if json {
            let mut obj = JsonObject::new();
            obj.field_bool("ok", true)
                .field_str("kind", "no-commit")
                .field_hash("picked", &target)
                .field_hash("tree", &result.tree_hash);
            let mut stdout = std::io::stdout().lock();
            let _ = writeln!(stdout, "{}", obj.finish());
        }
        return exit::OK;
    }

    let commit_hash = match create_commit(
        layout,
        store,
        result.tree_hash,
        ours,
        &result.original_message,
        target,
    ) {
        Ok(h) => h,
        Err(code) => return code,
    };
    if let Err(e) = super::restore_worktree_and_index(layout, store, result.tree_hash) {
        return emit_err(&e, exit::GENERAL_ERROR);
    }
    if let Err(e) = advance_head(layout, &commit_hash) {
        return emit_err(&e, exit::CANTCREAT);
    }
    // git-shaped summary: `[<branch> <hash>] <subject>` + diffstat.
    let subject = String::from_utf8_lossy(&result.original_message)
        .lines()
        .next()
        .unwrap_or("")
        .to_owned();
    let branch_name = match refs::read_head(layout) {
        Ok(Head::Branch(b)) => Some(b),
        _ => None,
    };
    let head_ref = match &branch_name {
        Some(b) => super::summary::HeadRef::Branch(b),
        None => super::summary::HeadRef::Detached,
    };
    let mut stderr = std::io::stderr().lock();
    super::summary::print_commit_summary(
        &mut stderr,
        store,
        &head_ref,
        &commit_hash,
        &subject,
        false,
        Some(ours_tree),
        Some(result.tree_hash),
    );
    drop(stderr);
    if json {
        let mut obj = JsonObject::new();
        obj.field_bool("ok", true)
            .field_str("kind", "commit")
            .field_hash("hash", &commit_hash)
            .field_hash("picked", &target)
            .field_hash("tree", &result.tree_hash);
        let mut stdout = std::io::stdout().lock();
        let _ = writeln!(stdout, "{}", obj.finish());
    }
    exit::OK
}

fn cont(layout: &RepoLayout, store: &ObjectStore, json: bool) -> u8 {
    let emit_err = |msg: &str, code: u8| emit_err_json(msg, code, json);
    if !is_cherry_pick_in_progress(layout) {
        return emit_err("no cherry-pick in progress", exit::GENERAL_ERROR);
    }
    let state = match conflict_state::read_cherry_pick_state(layout) {
        Ok(Some(s)) => s,
        Ok(None) => return emit_err("no cherry-pick in progress", exit::GENERAL_ERROR),
        Err(e) => return emit_err(&format!("read cherry-pick state: {e}"), exit::GENERAL_ERROR),
    };
    let records = match conflict_state::read_conflicts(layout.worktree_state_dir()) {
        Ok(r) => r,
        Err(e) => return emit_err(&format!("read conflicts: {e}"), exit::GENERAL_ERROR),
    };
    match super::conflict::first_unresolved_marker(layout.worktree_root(), &records) {
        Ok(Some(path)) => {
            return emit_err(
                &format!(
                    "unresolved conflict markers remain in '{path}'; resolve and `mkit add` it"
                ),
                exit::GENERAL_ERROR,
            );
        }
        Ok(None) => {}
        Err(e) => return emit_err(&e, exit::GENERAL_ERROR),
    }
    if let Err(e) = super::conflict::ensure_conflict_paths_staged(layout, store, &records) {
        return emit_err(&e, exit::GENERAL_ERROR);
    }

    // Single parent = current HEAD (== orig_head). Build tree from the
    // resolved index, NOT the conflict-time tree.
    let idx = match super::read_or_seed_index_from_head(layout, store) {
        Ok(i) => i,
        Err(e) => return emit_err(&e, exit::GENERAL_ERROR),
    };
    let tree_hash = match worktree::build_tree_from_index(store, &idx) {
        Ok(t) => t,
        Err(e) => return emit_err(&format!("build tree from index: {e}"), exit::GENERAL_ERROR),
    };

    let parent = match refs::resolve_head(layout) {
        Ok(Some(h)) => h,
        Ok(None) => state.orig_head,
        Err(e) => return emit_err(&format!("resolve HEAD: {e}"), exit::GENERAL_ERROR),
    };
    let commit_hash = match create_commit(
        layout,
        store,
        tree_hash,
        parent,
        &state.message,
        state.cherry_pick_head,
    ) {
        Ok(h) => h,
        Err(code) => return code,
    };
    // Sync the index to the committed tree WITHOUT rewriting the worktree:
    // the tree was built from the index, so the worktree already holds the
    // resolved content; restoring it would clobber any unstaged edits the
    // user made (e.g. on a cleanly-merged path) before `--continue`.
    if let Err(e) = super::sync_index_to_tree(layout, store, tree_hash) {
        return emit_err(&e, exit::GENERAL_ERROR);
    }
    if let Err(e) = advance_head(layout, &commit_hash) {
        return emit_err(&e, exit::CANTCREAT);
    }
    if let Err(e) = conflict_state::clear_cherry_pick_state(layout) {
        return emit_err(
            &format!("clear cherry-pick state: {e}"),
            exit::GENERAL_ERROR,
        );
    }
    let mut stderr = std::io::stderr().lock();
    let _ = writeln!(
        stderr,
        "cherry-picked {} as {}",
        format::short_hash(&state.cherry_pick_head, 8),
        format::short_hash(&commit_hash, 8),
    );
    drop(stderr);
    if json {
        let mut obj = JsonObject::new();
        obj.field_bool("ok", true)
            .field_str("kind", "commit")
            .field_hash("hash", &commit_hash)
            .field_hash("picked", &state.cherry_pick_head)
            .field_hash("tree", &tree_hash);
        let mut stdout = std::io::stdout().lock();
        let _ = writeln!(stdout, "{}", obj.finish());
    }
    exit::OK
}

fn abort(layout: &RepoLayout, store: &ObjectStore, json: bool) -> u8 {
    let emit_err = |msg: &str, code: u8| emit_err_json(msg, code, json);
    if !is_cherry_pick_in_progress(layout) {
        return emit_err("no cherry-pick in progress", exit::GENERAL_ERROR);
    }
    let state = match conflict_state::read_cherry_pick_state(layout) {
        Ok(Some(s)) => s,
        Ok(None) => return emit_err("no cherry-pick in progress", exit::GENERAL_ERROR),
        Err(e) => return emit_err(&format!("read cherry-pick state: {e}"), exit::GENERAL_ERROR),
    };
    let records = match conflict_state::read_conflicts(layout.worktree_state_dir()) {
        Ok(r) => r,
        Err(e) => return emit_err(&format!("read conflicts: {e}"), exit::GENERAL_ERROR),
    };
    if let Err(code) = restore_to(layout, store, state.orig_head, &records) {
        return code;
    }
    if let Err(e) = conflict_state::clear_cherry_pick_state(layout) {
        return emit_err(
            &format!("clear cherry-pick state: {e}"),
            exit::GENERAL_ERROR,
        );
    }
    let mut stderr = std::io::stderr().lock();
    let _ = writeln!(stderr, "cherry-pick aborted; HEAD restored");
    drop(stderr);
    if json {
        let mut obj = JsonObject::new();
        obj.field_bool("ok", true)
            .field_str("kind", "aborted")
            .field_hash("hash", &state.orig_head);
        let mut stdout = std::io::stdout().lock();
        let _ = writeln!(stdout, "{}", obj.finish());
    }
    exit::OK
}

fn restore_to(
    layout: &RepoLayout,
    store: &ObjectStore,
    target: Hash,
    records: &[mkit_core::ops::conflict_state::ConflictRecord],
) -> Result<(), u8> {
    let target_tree = load_tree_hash(store, target)?;
    // The operation's result tree lets the guards treat its clean hunks (not
    // just conflict paths) as discardable.
    let op_result = conflict_state::read_result_tree(layout.worktree_state_dir())
        .ok()
        .flatten();
    // Pre-flight: refuse before any mutation when the abort would clobber
    // genuine user work on a non-discardable path (the reset below discards
    // the user's in-progress conflict resolution, so it must not run if
    // the abort is going to fail).
    if let Err(e) =
        super::conflict::ensure_abort_safe(layout, store, records, target_tree, op_result)
    {
        return Err(emit_err(&e, exit::GENERAL_ERROR));
    }
    if let Err(e) =
        super::conflict::reset_conflict_paths(layout, store, records, target_tree, op_result)
    {
        return Err(emit_err(&e, exit::GENERAL_ERROR));
    }
    if let Err(e) = super::ensure_restore_safe(layout, store, target_tree) {
        return Err(emit_err(&e, exit::GENERAL_ERROR));
    }
    if let Err(e) = super::restore_worktree_and_index(layout, store, target_tree) {
        return Err(emit_err(&e, exit::GENERAL_ERROR));
    }
    super::restore_head_ref(layout, &target)
}

fn create_commit(
    layout: &RepoLayout,
    store: &ObjectStore,
    tree_hash: Hash,
    parent: Hash,
    message: &[u8],
    picked: Hash,
) -> Result<Hash, u8> {
    let cfg = config::read_or_default(layout)
        .map_err(|e| emit_err(&format!("config: {e}"), exit::CONFIG_ERROR))?;
    let mut signer = super::commit::load_commit_signer(layout, &cfg)
        .map_err(|(msg, code)| emit_err(&msg, code))?;
    let signer_public = signer
        .public_key()
        .map_err(|(msg, code)| emit_err(&msg, code))?;
    // A replay keeps the picked commit's authorship + timestamp (the
    // fresh signature/signer mark the replay), matching git.
    let (author, timestamp) = match store.read_object(&picked) {
        Ok(Object::Commit(c)) => (c.author, c.timestamp),
        Ok(_) => return Err(emit_err("picked object is not a commit", exit::DATAERR)),
        Err(e) => {
            return Err(emit_err(
                &format!("read picked commit: {e}"),
                exit::GENERAL_ERROR,
            ));
        }
    };
    let mut unsigned = Commit::new_unannotated(
        tree_hash,
        vec![parent],
        author,
        signer_public,
        message.to_vec(),
        timestamp,
        [0u8; 64],
    );
    let sig = signer
        .sign_commit(&unsigned)
        .map_err(|(msg, code)| emit_err(&msg, code))?;
    unsigned.signature = sig;
    let bytes = serialize::serialize(&Object::Commit(unsigned))
        .map_err(|e| emit_err(&format!("serialize: {e}"), exit::DATAERR))?;
    store
        .write(&bytes)
        .map_err(|e| emit_err(&format!("store commit: {e}"), exit::CANTCREAT))
}