gitoxide_core/
discover.rs1use std::path::Path;
2
3pub 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
35fn print_result<T, E>(mut out: impl std::io::Write, res: Result<T, E>) -> std::io::Result<bool>
36where
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
54fn 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}