navid/veins / impl-rust / veins-rust/src/cli/commands/status.rs

status.rs

← veins-rust/src/cli/commands · 1706 bytes · raw · download

use std::path::Path;

use crate::error::{Error, Result};
use crate::object::tree::{KIND_CONFLICT, KIND_MISSING};
use crate::store::graph;
use crate::store::layout;
use crate::store::ops;
use crate::store::reader::Store;
use crate::worktree::snapshot;

pub fn run() -> Result<u8> {
    let root = layout::discover(Path::new("."))?;
    let mut store = Store::open(&root)?;
    snapshot::run_with(&root, &mut store)?;
    let view = ops::current_view(&root, &mut store)?
        .ok_or_else(|| Error::new("no operation state"))?;
    let Some(working_copy) = ops::working_copy_id(&view) else {
        println!("info: working copy clean");
        return Ok(0);
    };
    let commit = graph::load_commit(&mut store, working_copy)?
        .ok_or_else(|| Error::new("no operation state"))?;
    let old_tree = match commit.parents.first() {
        Some(parent) => graph::load_commit(&mut store, *parent)?.map(|parent| parent.root_tree),
        None => None,
    };
    let changes = graph::compare_trees(&mut store, old_tree, Some(commit.root_tree))?;
    let empty = changes.is_empty();
    let mut conflicts = 0usize;
    for change in changes {
        let path = String::from_utf8_lossy(&change.path);
        if change.old_kind == KIND_MISSING {
            println!("A {path}");
        } else if change.new_kind == KIND_MISSING {
            println!("D {path}");
        } else if change.new_kind == KIND_CONFLICT {
            println!("C {path}");
            conflicts += 1;
        } else {
            println!("M {path}");
        }
    }
    if empty {
        println!("info: working copy clean");
    }
    if conflicts > 0 {
        println!("{conflicts} conflict(s)");
    }
    Ok(0)
}