Skip to main content

cargo/sources/git/
mod.rs

1//! Home of the [`GitSource`].
2//!
3//! Apparently, the most important type in this module is [`GitSource`].
4//! [`utils`] provides libgit2 utilities like fetch and checkout, whereas
5//! [`oxide`] is the counterpart for gitoxide integration. [`known_hosts`]
6//! is the mitigation of [CVE-2022-46176].
7//!
8//! [CVE-2022-46176]: https://blog.rust-lang.org/2023/01/10/cve-2022-46176.html
9
10pub use self::source::GitSource;
11pub use self::utils::{GitCheckout, GitDatabase, GitRemote, fetch, resolve_ref};
12mod cli;
13mod known_hosts;
14mod libgit2;
15mod oxide;
16mod source;
17mod utils;
18
19/// For `-Zgitoxide` integration.
20pub mod fetch {
21    use crate::GlobalContext;
22    use crate::workspace::features::GitFeatures;
23
24    /// The kind remote repository to fetch.
25    #[derive(Debug, Copy, Clone)]
26    pub enum RemoteKind {
27        /// A repository belongs to a git dependency.
28        GitDependency,
29        /// A repository belongs to a Cargo registry.
30        Registry,
31    }
32
33    impl RemoteKind {
34        /// Obtain the kind of history we would want for a fetch from our remote knowing if the target repo is already shallow
35        /// via `repo_is_shallow` along with gitoxide-specific feature configuration via `config`.
36        /// `rev_and_ref` is additional information that affects whether or not we may be shallow.
37        pub(crate) fn to_shallow_setting(
38            &self,
39            repo_is_shallow: bool,
40            gctx: &GlobalContext,
41        ) -> gix::remote::fetch::Shallow {
42            let has_feature = |cb: &dyn Fn(GitFeatures) -> bool| {
43                gctx.cli_unstable()
44                    .git
45                    .map_or(false, |features| cb(features))
46            };
47
48            // maintain shallow-ness and keep downloading single commits, or see if we can do shallow clones
49            if !repo_is_shallow {
50                match self {
51                    RemoteKind::GitDependency if has_feature(&|features| features.shallow_deps) => {
52                    }
53                    RemoteKind::Registry if has_feature(&|features| features.shallow_index) => {}
54                    _ => return gix::remote::fetch::Shallow::NoChange,
55                }
56            };
57
58            gix::remote::fetch::Shallow::DepthAtRemote(1.try_into().expect("non-zero"))
59        }
60    }
61
62    pub type Error = gix::Error;
63}