Skip to content

Commit 2e5c255

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 e20c6ed commit 2e5c255

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

gitoxide-core/src/repository/dirty.rs

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

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ 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| core::repository::dirty::check(repository(Mode::Lenient)?, mode, out),
163+
)
164+
}
149165
#[cfg(feature = "gitoxide-core-tools-clean")]
150166
Subcommands::Clean(crate::plumbing::options::clean::Command {
151167
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)