Skip to content

Commit

Permalink
feat: gix submodules list --dirty-suffix for dirty-information
Browse files Browse the repository at this point in the history
This is a submodule-centric and greatly simplified way of obtaining
describe information with dirty-suffix.

Note that `status` information is also possible, but it seems
hard to display nicely, which this command isn't great at
in the first place.
  • Loading branch information
Byron committed Mar 10, 2024
1 parent 58231b4 commit afd20ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
16 changes: 12 additions & 4 deletions gitoxide-core/src/repository/submodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use gix::{commit::describe::SelectRef, prelude::ObjectIdExt, Repository, Submodu

use crate::OutputFormat;

pub fn list(repo: Repository, mut out: impl std::io::Write, format: OutputFormat) -> anyhow::Result<()> {
pub fn list(
repo: Repository,
mut out: impl std::io::Write,
format: OutputFormat,
dirty_suffix: Option<String>,
) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("Only human output is supported for now")
}
Expand All @@ -12,12 +17,12 @@ pub fn list(repo: Repository, mut out: impl std::io::Write, format: OutputFormat
return Ok(());
};
for sm in submodules {
print_sm(sm, &mut out)?;
print_sm(sm, dirty_suffix.as_deref(), &mut out)?;
}
Ok(())
}

fn print_sm(sm: Submodule<'_>, out: &mut impl std::io::Write) -> anyhow::Result<()> {
fn print_sm(sm: Submodule<'_>, dirty_suffix: Option<&str>, out: &mut impl std::io::Write) -> anyhow::Result<()> {
let _span = gix::trace::coarse!("print_sm", path = ?sm.path());
let state = sm.state()?;
let mut sm_repo = sm.open()?;
Expand Down Expand Up @@ -48,7 +53,10 @@ fn print_sm(sm: Submodule<'_>, out: &mut impl std::io::Write) -> anyhow::Result<
repo.head_commit()?
.describe()
.names(SelectRef::AllRefs)
.format()?
.id_as_fallback(true)
.try_resolve()?
.expect("resolution present if ID can be used as fallback")
.format_with_dirty_suffix(dirty_suffix.map(ToOwned::to_owned))?
.to_string()
}
None => {
Expand Down
13 changes: 10 additions & 3 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,23 @@ pub fn main() -> Result<()> {
),
Subcommands::Submodule(platform) => match platform
.cmds
.unwrap_or(crate::plumbing::options::submodule::Subcommands::List)
.unwrap_or(crate::plumbing::options::submodule::Subcommands::List { dirty_suffix: None })
{
crate::plumbing::options::submodule::Subcommands::List => prepare_and_run(
crate::plumbing::options::submodule::Subcommands::List { dirty_suffix } => prepare_and_run(
"submodule-list",
trace,
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| core::repository::submodule::list(repository(Mode::Lenient)?, out, format),
move |_progress, out, _err| {
core::repository::submodule::list(
repository(Mode::Lenient)?,
out,
format,
dirty_suffix.map(|suffix| suffix.unwrap_or_else(|| "dirty".to_string())),
)
},
),
},
#[cfg(feature = "gitoxide-core-tools-archive")]
Expand Down
6 changes: 5 additions & 1 deletion src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,11 @@ pub mod submodule {
#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
/// Print all direct submodules to standard output
List,
List {
/// Set the suffix to append if the repository is dirty (not counting untracked files).
#[clap(short = 'd', long)]
dirty_suffix: Option<Option<String>>,
},
}
}

Expand Down

0 comments on commit afd20ca

Please sign in to comment.