Skip to content

Commit

Permalink
feat: add gix free discover to inform about repository discovery.
Browse files Browse the repository at this point in the history
It's mainly to better understand what's causing certain failures
if a repository can't be opened, in different modes of operations.
  • Loading branch information
Byron committed Oct 17, 2023
1 parent 20f962e commit 886289f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
59 changes: 59 additions & 0 deletions gitoxide-core/src/discover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::path::Path;

pub fn discover(repo: &Path, mut out: impl std::io::Write) -> anyhow::Result<()> {
let mut has_err = false;
writeln!(out, "open (strict) {}:", repo.display())?;
has_err |= print_result(
&mut out,
gix::open_opts(repo, gix::open::Options::default().strict_config(true)),
)?;

if has_err {
writeln!(out, "open (lenient) {}:", repo.display())?;
has_err |= print_result(
&mut out,
gix::open_opts(repo, gix::open::Options::default().strict_config(false)),
)?;
}

writeln!(out)?;
writeln!(out, "discover from {}:", repo.display())?;
has_err |= print_result(&mut out, gix::discover(repo))?;

writeln!(out)?;
writeln!(out, "discover (plumbing) from {}:", repo.display())?;
has_err |= print_result(&mut out, gix::discover::upwards(repo))?;

if has_err {
writeln!(out)?;
anyhow::bail!("At least one operation failed")
}

Ok(())
}

fn print_result<T, E>(mut out: impl std::io::Write, res: Result<T, E>) -> std::io::Result<bool>
where
T: std::fmt::Debug,
E: std::error::Error + Send + Sync + 'static,
{
let mut has_err = false;
let to_print = match res {
Ok(good) => {
format!("{good:#?}")
}
Err(err) => {
has_err = true;
format!("{:?}", anyhow::Error::from(err))
}
};
indent(&mut out, to_print)?;
Ok(has_err)
}

fn indent(mut out: impl std::io::Write, msg: impl Into<String>) -> std::io::Result<()> {
for line in msg.into().lines() {
writeln!(out, "\t{line}")?;
}
Ok(())
}
3 changes: 3 additions & 0 deletions gitoxide-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,8 @@ pub mod pack;
pub mod query;
pub mod repository;

mod discover;
pub use discover::discover;

#[cfg(all(feature = "async-client", feature = "blocking-client"))]
compile_error!("Cannot set both 'blocking-client' and 'async-client' features as they are mutually exclusive");
10 changes: 10 additions & 0 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub fn main() -> Result<()> {
let object_hash = args.object_hash;
let config = args.config;
let repository = args.repository;
let repository_path = repository.clone();
enum Mode {
Strict,
StrictWithGitInstallConfig,
Expand Down Expand Up @@ -449,6 +450,15 @@ pub fn main() -> Result<()> {
)
.map(|_| ()),
Subcommands::Free(subcommands) => match subcommands {
free::Subcommands::Discover => prepare_and_run(
"discover",
trace,
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| core::discover(&repository_path, out),
),
free::Subcommands::CommitGraph(cmd) => match cmd {
free::commitgraph::Subcommands::Verify { path, statistics } => prepare_and_run(
"commitgraph-verify",
Expand Down
2 changes: 2 additions & 0 deletions src/plumbing/options/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum Subcommands {
Pack(pack::Subcommands),
/// Subcommands for interacting with a worktree index, typically at .git/index
Index(index::Platform),
/// Show information about repository discovery and when opening a repository at the current path.
Discover,
}

///
Expand Down

0 comments on commit 886289f

Please sign in to comment.