Skip to main content

mkit_cli/commands/
tree.rs

1//! `mkit tree` — snapshot the working directory as a tree object,
2//! printing the resulting tree hash.
3
4use std::io::Write;
5
6use clap::Parser;
7use mkit_core::store::ObjectStore;
8use mkit_core::worktree;
9
10use crate::clap_shim;
11use crate::exit;
12use crate::format;
13
14#[derive(Debug, Parser)]
15#[command(
16    name = "mkit tree",
17    about = "Snapshot the working directory as a tree object."
18)]
19struct TreeOpts {}
20
21#[must_use]
22pub fn run(args: &[String]) -> u8 {
23    if let Err(code) = clap_shim::parse::<TreeOpts>("mkit tree", args) {
24        return code;
25    }
26    let cwd = match std::env::current_dir() {
27        Ok(p) => p,
28        Err(e) => return emit_err(&format!("cwd: {e}"), exit::NOINPUT),
29    };
30    let layout = match super::resolve_layout(&cwd) {
31        Ok(layout) => layout,
32        Err(code) => return code,
33    };
34    let store = match ObjectStore::open(&layout) {
35        Ok(s) => s,
36        Err(e) => return emit_err(&format!("not a mkit repo: {e}"), exit::GENERAL_ERROR),
37    };
38    match worktree::build_tree(&store, &cwd) {
39        Ok(h) => {
40            let mut stdout = std::io::stdout().lock();
41            let _ = writeln!(stdout, "{}", format::hex_hash(&h));
42            exit::OK
43        }
44        Err(e) => emit_err(&format!("build tree: {e}"), exit::GENERAL_ERROR),
45    }
46}
47
48use super::error as emit_err;