1use std::{fs, io, path::PathBuf};
20
21use gix_path::realpath::MAX_SYMLINKS;
22
23pub mod parse;
25
26#[derive(thiserror::Error, Debug)]
28#[allow(missing_docs)]
29pub enum Error {
30 #[error(transparent)]
31 Io(#[from] io::Error),
32 #[error(transparent)]
33 Realpath(#[from] gix_path::realpath::Error),
34 #[error(transparent)]
35 Parse(#[from] parse::Error),
36 #[error("Alternates form a cycle: {} -> {}", .0.iter().map(|p| format!("'{}'", p.display())).collect::<Vec<_>>().join(" -> "), .0.first().expect("more than one directories").display())]
37 Cycle(Vec<PathBuf>),
38}
39
40pub fn resolve(objects_directory: PathBuf, current_dir: &std::path::Path) -> Result<Vec<PathBuf>, Error> {
46 let mut dirs = vec![(0, objects_directory.clone())];
47 let mut out = Vec::new();
48 let mut seen = vec![gix_path::realpath_opts(&objects_directory, current_dir, MAX_SYMLINKS)?];
49 while let Some((depth, dir)) = dirs.pop() {
50 match fs::read(dir.join("info").join("alternates")) {
51 Ok(input) => {
52 for path in parse::content(&input)?.into_iter() {
53 let path = objects_directory.join(path);
54 let path_canonicalized = gix_path::realpath_opts(&path, current_dir, MAX_SYMLINKS)?;
55 if seen.contains(&path_canonicalized) {
56 return Err(Error::Cycle(seen));
57 }
58 seen.push(path_canonicalized);
59 dirs.push((depth + 1, path));
60 }
61 }
62 Err(err) if err.kind() == io::ErrorKind::NotFound => {}
63 Err(err) => return Err(err.into()),
64 };
65 if depth != 0 {
66 out.push(dir);
67 }
68 }
69 Ok(out)
70}