gitoxide_core/repository/
dirty.rs

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
use crate::OutputFormat;
use anyhow::bail;

pub enum Mode {
    IsClean,
    IsDirty,
}

pub fn check(
    repo: gix::Repository,
    mode: Mode,
    out: &mut dyn std::io::Write,
    format: OutputFormat,
) -> anyhow::Result<()> {
    if format != OutputFormat::Human {
        bail!("JSON output isn't implemented yet");
    }
    let is_dirty = repo.is_dirty()?;
    let res = match (is_dirty, mode) {
        (false, Mode::IsClean) => Ok("The repository is clean"),
        (true, Mode::IsClean) => Err("The repository has changes"),
        (false, Mode::IsDirty) => Err("The repository is clean"),
        (true, Mode::IsDirty) => Ok("The repository has changes"),
    };

    let suffix = "(not counting untracked files)";
    match res {
        Ok(msg) => writeln!(out, "{msg} {suffix}")?,
        Err(msg) => bail!("{msg} {suffix}"),
    }
    Ok(())
}