Skip to content

Commit 886289f

Browse files
committed
feat: add gix free discover to inform about repository discovery.
It's mainly to better understand what's causing certain failures if a repository can't be opened, in different modes of operations.
1 parent 20f962e commit 886289f

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

gitoxide-core/src/discover.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::path::Path;
2+
3+
pub fn discover(repo: &Path, mut out: impl std::io::Write) -> anyhow::Result<()> {
4+
let mut has_err = false;
5+
writeln!(out, "open (strict) {}:", repo.display())?;
6+
has_err |= print_result(
7+
&mut out,
8+
gix::open_opts(repo, gix::open::Options::default().strict_config(true)),
9+
)?;
10+
11+
if has_err {
12+
writeln!(out, "open (lenient) {}:", repo.display())?;
13+
has_err |= print_result(
14+
&mut out,
15+
gix::open_opts(repo, gix::open::Options::default().strict_config(false)),
16+
)?;
17+
}
18+
19+
writeln!(out)?;
20+
writeln!(out, "discover from {}:", repo.display())?;
21+
has_err |= print_result(&mut out, gix::discover(repo))?;
22+
23+
writeln!(out)?;
24+
writeln!(out, "discover (plumbing) from {}:", repo.display())?;
25+
has_err |= print_result(&mut out, gix::discover::upwards(repo))?;
26+
27+
if has_err {
28+
writeln!(out)?;
29+
anyhow::bail!("At least one operation failed")
30+
}
31+
32+
Ok(())
33+
}
34+
35+
fn print_result<T, E>(mut out: impl std::io::Write, res: Result<T, E>) -> std::io::Result<bool>
36+
where
37+
T: std::fmt::Debug,
38+
E: std::error::Error + Send + Sync + 'static,
39+
{
40+
let mut has_err = false;
41+
let to_print = match res {
42+
Ok(good) => {
43+
format!("{good:#?}")
44+
}
45+
Err(err) => {
46+
has_err = true;
47+
format!("{:?}", anyhow::Error::from(err))
48+
}
49+
};
50+
indent(&mut out, to_print)?;
51+
Ok(has_err)
52+
}
53+
54+
fn indent(mut out: impl std::io::Write, msg: impl Into<String>) -> std::io::Result<()> {
55+
for line in msg.into().lines() {
56+
writeln!(out, "\t{line}")?;
57+
}
58+
Ok(())
59+
}

gitoxide-core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,8 @@ pub mod pack;
7979
pub mod query;
8080
pub mod repository;
8181

82+
mod discover;
83+
pub use discover::discover;
84+
8285
#[cfg(all(feature = "async-client", feature = "blocking-client"))]
8386
compile_error!("Cannot set both 'blocking-client' and 'async-client' features as they are mutually exclusive");

src/plumbing/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub fn main() -> Result<()> {
7272
let object_hash = args.object_hash;
7373
let config = args.config;
7474
let repository = args.repository;
75+
let repository_path = repository.clone();
7576
enum Mode {
7677
Strict,
7778
StrictWithGitInstallConfig,
@@ -449,6 +450,15 @@ pub fn main() -> Result<()> {
449450
)
450451
.map(|_| ()),
451452
Subcommands::Free(subcommands) => match subcommands {
453+
free::Subcommands::Discover => prepare_and_run(
454+
"discover",
455+
trace,
456+
verbose,
457+
progress,
458+
progress_keep_open,
459+
None,
460+
move |_progress, out, _err| core::discover(&repository_path, out),
461+
),
452462
free::Subcommands::CommitGraph(cmd) => match cmd {
453463
free::commitgraph::Subcommands::Verify { path, statistics } => prepare_and_run(
454464
"commitgraph-verify",

src/plumbing/options/free.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub enum Subcommands {
1414
Pack(pack::Subcommands),
1515
/// Subcommands for interacting with a worktree index, typically at .git/index
1616
Index(index::Platform),
17+
/// Show information about repository discovery and when opening a repository at the current path.
18+
Discover,
1719
}
1820

1921
///

0 commit comments

Comments
 (0)