Skip to content

Commit

Permalink
feat: add gix is-clean|is-changed
Browse files Browse the repository at this point in the history
It's a good way to compare the time it takes to run a full status
compared to a quick is-dirty check.
  • Loading branch information
Byron committed Mar 10, 2024
1 parent afd20ca commit 98b3680
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
32 changes: 32 additions & 0 deletions gitoxide-core/src/repository/dirty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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(())
}
1 change: 1 addition & 0 deletions gitoxide-core/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub use credential::function as credential;
pub mod attributes;
#[cfg(feature = "clean")]
pub mod clean;
pub mod dirty;
#[cfg(feature = "clean")]
pub use clean::function::clean;
#[cfg(feature = "blocking-client")]
Expand Down
18 changes: 18 additions & 0 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ pub fn main() -> Result<()> {
}

match cmd {
Subcommands::IsClean | Subcommands::IsChanged => {
let mode = if matches!(cmd, Subcommands::IsClean) {
core::repository::dirty::Mode::IsClean
} else {
core::repository::dirty::Mode::IsDirty
};
prepare_and_run(
"clean",
trace,
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::dirty::check(repository(Mode::Lenient)?, mode, out, format)
},
)
}
#[cfg(feature = "gitoxide-core-tools-clean")]
Subcommands::Clean(crate::plumbing::options::clean::Command {
debug,
Expand Down
2 changes: 2 additions & 0 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ pub enum Subcommands {
/// Interact with submodules.
#[clap(alias = "submodules")]
Submodule(submodule::Platform),
IsClean,
IsChanged,
/// Show which git configuration values are used or planned.
ConfigTree,
Status(status::Platform),
Expand Down

0 comments on commit 98b3680

Please sign in to comment.