gix_submodule/lib.rs
1//! Primitives for describing git submodules.
2#![deny(missing_docs)]
3#![forbid(unsafe_code)]
4
5use bstr::ByteSlice;
6use gix_error::Result;
7
8/// All relevant information about a git module, typically from `.gitmodules` files.
9///
10/// Note that overrides from other configuration might be relevant, which is why this type
11/// can be used to take these into consideration when presented with other configuration
12/// from the superproject.
13#[derive(Clone)]
14pub struct File {
15 config: gix_config::File,
16}
17
18mod access;
19
20///
21pub mod config;
22
23///
24pub mod is_active_platform;
25
26/// A platform to keep the state necessary to perform repeated active checks, created by [File::is_active_platform()].
27pub struct IsActivePlatform {
28 pub(crate) search: Option<gix_pathspec::Search>,
29}
30
31/// Mutation
32impl File {
33 /// This can be used to let `config` override some values we know about submodules, namely…
34 ///
35 /// * `url`
36 /// * `fetchRecurseSubmodules`
37 /// * `ignore`
38 /// * `update`
39 /// * `branch`
40 ///
41 /// These values aren't validated yet, which will happen upon query. The caller must provide
42 /// trusted configuration; each copied value retains its section's origin metadata.
43 pub fn append_submodule_overrides(&mut self, config: &gix_config::File) -> Result<&mut Self> {
44 let mut config_to_append = gix_config::File::new(config.meta_owned());
45 for (module_name, section) in config
46 .sections_by_name("submodule")
47 .into_iter()
48 .flatten()
49 .filter_map(|s| s.header().subsection_name().map(|n| (n, s)))
50 {
51 let values = ["url", "fetchRecurseSubmodules", "ignore", "update", "branch"]
52 .map(|field| (field, section.value(field)));
53 if values.iter().all(|(_, value)| value.is_none()) {
54 continue;
55 }
56 config_to_append.set_meta(section.meta().clone());
57 let mut destination = config_to_append.new_section("submodule", module_name)?;
58 for (field, value) in values {
59 if let Some(value) = value {
60 destination.push(field, Some(value.as_bstr()))?;
61 }
62 }
63 }
64
65 self.config.append(config_to_append)?;
66 Ok(self)
67 }
68}
69
70///
71pub mod init {
72 use gix_error::{Result, validation};
73 use std::path::PathBuf;
74
75 use gix_error::ResultExt;
76
77 use crate::File;
78
79 impl std::fmt::Debug for File {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 f.debug_struct("File")
82 .field("config_path", &self.config_path())
83 .field("config", &format_args!("r#\"{}\"#", self.config))
84 .finish()
85 }
86 }
87
88 impl File {
89 /// Parse `bytes` as git configuration, typically from `.gitmodules`, without doing any further validation.
90 /// `path` can be provided to keep track of where the file was read from in the underlying [`config`](Self::config())
91 /// instance.
92 /// `config` is used to [apply value overrides](File::append_submodule_overrides), which can be empty if overrides
93 /// should be applied at a later time.
94 ///
95 /// Future access to the module information is lazy and configuration errors are exposed there on a per-value basis.
96 ///
97 /// ### Security Considerations
98 ///
99 /// The information itself should be used with care as it can direct the caller to fetch from remotes. It is, however,
100 /// on the caller to assure the input data can be trusted.
101 pub fn from_bytes(bytes: &[u8], path: impl Into<Option<PathBuf>>, config: &gix_config::File) -> Result<Self> {
102 let metadata = {
103 let mut meta = gix_config::file::Metadata::api();
104 meta.path = path.into();
105 meta
106 };
107 let modules = gix_config::File::from_parse_events_no_includes(
108 gix_config::parse::Events::from_bytes(bytes, None)
109 .or_raise(|| validation("Could not parse submodule configuration"))?,
110 metadata,
111 );
112
113 let mut res = Self { config: modules };
114 res.append_submodule_overrides(config)
115 .or_raise(|| validation("Could not apply submodule configuration overrides"))?;
116 Ok(res)
117 }
118
119 /// Turn ourselves into the underlying parsed configuration file.
120 pub fn into_config(self) -> gix_config::File {
121 self.config
122 }
123 }
124}