Skip to content

Commit d78d21c

Browse files
committed
feat: add gix is-clean|is-changed
It's a good way to compare the time it takes to run a full status compared to a quick is-dirty check.
1 parent baae41d commit d78d21c

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

gitoxide-core/src/repository/dirty.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::OutputFormat;
2+
use anyhow::bail;
3+
4+
pub enum Mode {
5+
IsClean,
6+
IsDirty,
7+
}
8+
9+
pub fn check(
10+
repo: gix::Repository,
11+
mode: Mode,
12+
out: &mut dyn std::io::Write,
13+
format: OutputFormat,
14+
) -> anyhow::Result<()> {
15+
if format != OutputFormat::Human {
16+
bail!("JSON output isn't implemented yet");
17+
}
18+
let is_dirty = repo.is_dirty()?;
19+
let res = match (is_dirty, mode) {
20+
(false, Mode::IsClean) => Ok("The repository is clean"),
21+
(true, Mode::IsClean) => Err("The repository has changes"),
22+
(false, Mode::IsDirty) => Err("The repository is clean"),
23+
(true, Mode::IsDirty) => Ok("The repository has changes"),
24+
};
25+
26+
let suffix = "(not counting untracked files)";
27+
match res {
28+
Ok(msg) => writeln!(out, "{msg} {suffix}")?,
29+
Err(msg) => bail!("{msg} {suffix}"),
30+
}
31+
Ok(())
32+
}

gitoxide-core/src/repository/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub use credential::function as credential;
2626
pub mod attributes;
2727
#[cfg(feature = "clean")]
2828
pub mod clean;
29+
pub mod dirty;
2930
#[cfg(feature = "clean")]
3031
pub use clean::function::clean;
3132
#[cfg(feature = "blocking-client")]

src/plumbing/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ pub fn main() -> Result<()> {
146146
}
147147

148148
match cmd {
149+
Subcommands::IsClean | Subcommands::IsChanged => {
150+
let mode = if matches!(cmd, Subcommands::IsClean) {
151+
core::repository::dirty::Mode::IsClean
152+
} else {
153+
core::repository::dirty::Mode::IsDirty
154+
};
155+
prepare_and_run(
156+
"clean",
157+
trace,
158+
verbose,
159+
progress,
160+
progress_keep_open,
161+
None,
162+
move |_progress, out, _err| {
163+
core::repository::dirty::check(repository(Mode::Lenient)?, mode, out, format)
164+
},
165+
)
166+
}
149167
#[cfg(feature = "gitoxide-core-tools-clean")]
150168
Subcommands::Clean(crate::plumbing::options::clean::Command {
151169
debug,

src/plumbing/options/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ pub enum Subcommands {
130130
/// Interact with submodules.
131131
#[clap(alias = "submodules")]
132132
Submodule(submodule::Platform),
133+
IsClean,
134+
IsChanged,
133135
/// Show which git configuration values are used or planned.
134136
ConfigTree,
135137
Status(status::Platform),

0 commit comments

Comments
 (0)