gitoxide_core/index/
mod.rs

1use std::path::Path;
2
3pub struct Options {
4    pub object_hash: gix::hash::Kind,
5    pub format: crate::OutputFormat,
6}
7
8pub mod information;
9
10fn parse_file(index_path: impl AsRef<Path>, object_hash: gix::hash::Kind) -> anyhow::Result<gix::index::File> {
11    gix::index::File::at(index_path.as_ref(), object_hash, false, Default::default()).map_err(Into::into)
12}
13
14pub mod checkout_exclusive {
15    pub struct Options {
16        pub index: super::Options,
17        /// If true, all files will be written with zero bytes despite having made an ODB lookup.
18        pub empty_files: bool,
19        pub keep_going: bool,
20        /// If set, don't use more than this amount of threads.
21        /// Otherwise, usually use as many threads as there are logical cores.
22        /// A value of 0 is interpreted as no-limit
23        pub thread_limit: Option<usize>,
24    }
25}
26
27mod checkout;
28pub use checkout::checkout_exclusive;
29
30pub fn verify(
31    index_path: impl AsRef<Path>,
32    mut out: impl std::io::Write,
33    Options { object_hash, format }: Options,
34) -> anyhow::Result<()> {
35    let file = parse_file(index_path, object_hash)?;
36    file.verify_integrity()?;
37    file.verify_entries()?;
38    file.verify_extensions(false, gix::objs::find::Never)?;
39    #[cfg_attr(not(feature = "serde"), allow(irrefutable_let_patterns))]
40    if let crate::OutputFormat::Human = format {
41        writeln!(out, "OK").ok();
42    }
43    Ok(())
44}
45
46#[cfg_attr(not(feature = "serde"), allow(unused_variables, unused_mut))]
47pub fn information(
48    index_path: impl AsRef<Path>,
49    out: impl std::io::Write,
50    mut err: impl std::io::Write,
51    information::Options {
52        index: Options {
53            object_hash,
54            mut format,
55        },
56        extension_details,
57    }: information::Options,
58) -> anyhow::Result<()> {
59    use crate::OutputFormat::*;
60    #[cfg(feature = "serde")]
61    if let Human = format {
62        writeln!(err, "Defaulting to JSON printing as nothing else will be implemented.").ok();
63        format = Json;
64    }
65    match format {
66        Human => {
67            anyhow::bail!("Cannot print information using 'human' format.")
68        }
69        #[cfg(feature = "serde")]
70        Json => {
71            let info = information::Collection::try_from_file(parse_file(index_path, object_hash)?, extension_details)?;
72            serde_json::to_writer_pretty(out, &info)?;
73            Ok(())
74        }
75    }
76}