gix_submodule/config.rs
1use bstr::{BStr, BString, ByteSlice};
2use gix_error::ExnMessageResult;
3
4/// Determine how the submodule participates in `git status` queries. This setting also affects `git diff`.
5#[derive(Default, Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
6pub enum Ignore {
7 /// Submodule changes won't be considered at all, which is the fastest option.
8 All,
9 /// Ignore any changes to the submodule working tree, only show committed differences between the `HEAD` of the submodule
10 /// compared to the recorded commit in the superproject.
11 Dirty,
12 /// Only ignore untracked files in the submodule, but show modifications to the submodule working tree as well as differences
13 /// between the recorded commit in the superproject and the checked-out commit in the submodule.
14 Untracked,
15 /// No modifications to the submodule are ignored, which shows untracked files, modified files in the submodule worktree as well as
16 /// differences between the recorded commit in the superproject and the checked-out commit in the submodule.
17 #[default]
18 None,
19}
20
21impl TryFrom<&BStr> for Ignore {
22 type Error = ();
23
24 fn try_from(value: &BStr) -> Result<Self, Self::Error> {
25 Ok(match value.as_bytes() {
26 b"all" => Ignore::All,
27 b"dirty" => Ignore::Dirty,
28 b"untracked" => Ignore::Untracked,
29 b"none" => Ignore::None,
30 _ => return Err(()),
31 })
32 }
33}
34
35/// Determine how to recurse into this module from the superproject when fetching.
36///
37/// Generally, a fetch is only performed if the submodule commit referenced by the superproject isn't already
38/// present in the submodule repository.
39///
40/// Note that when unspecified, the `fetch.recurseSubmodules` configuration variable should be used instead.
41#[derive(Default, Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
42pub enum FetchRecurse {
43 /// Fetch only changed submodules.
44 #[default]
45 OnDemand,
46 /// Fetch all populated submodules, changed or not.
47 ///
48 /// This skips the work needed to determine whether a submodule has changed in the first place, but may work
49 /// more as some fetches might not be necessary.
50 Always,
51 /// Submodules are never fetched.
52 Never,
53}
54
55impl FetchRecurse {
56 /// Check if `boolean` is set and translate it the respective variant, or check the underlying string
57 /// value for non-boolean options.
58 /// On error, it returns the obtained string value which would be the invalid value.
59 pub fn new(boolean: ExnMessageResult<Option<bool>>) -> Result<Option<Self>, BString> {
60 Ok(match boolean {
61 Ok(Some(value)) => Some(if value {
62 FetchRecurse::Always
63 } else {
64 FetchRecurse::Never
65 }),
66 Ok(None) => None,
67 Err(err) => {
68 let Some(gix_error::MetadataValue::Bytes(input)) = err.into_inner().values.remove("input") else {
69 unreachable!("gix-config-value validation errors retain their input as bytes");
70 };
71 if input != "on-demand" {
72 return Err(input);
73 }
74 Some(FetchRecurse::OnDemand)
75 }
76 })
77 }
78}
79
80/// Describes the branch that should be tracked on the remote.
81#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
82pub enum Branch {
83 /// The name of the remote branch should be the same as the one currently checked out in the superproject.
84 CurrentInSuperproject,
85 /// The validated remote-only branch that could be used for fetching.
86 Name(BString),
87}
88
89impl Default for Branch {
90 fn default() -> Self {
91 Branch::Name("HEAD".into())
92 }
93}
94
95impl TryFrom<&BStr> for Branch {
96 type Error = gix_error::Exn<gix_error::Message>;
97
98 fn try_from(value: &BStr) -> Result<Self, Self::Error> {
99 if value == "." {
100 return Ok(Branch::CurrentInSuperproject);
101 }
102
103 gix_refspec::parse(value, gix_refspec::parse::Operation::Fetch)
104 .map(|spec| Branch::Name(spec.source().expect("no object").to_owned()))
105 }
106}
107
108/// Determine how `git submodule update` should deal with this submodule to bring it up-to-date with the
109/// super-project's expectations.
110#[derive(Default, Debug, Clone, Hash, PartialOrd, PartialEq, Ord, Eq)]
111pub enum Update {
112 /// The commit recorded in the superproject should be checked out on a detached `HEAD`.
113 #[default]
114 Checkout,
115 /// The current branch in the submodule will be rebased onto the commit recorded in the superproject.
116 Rebase,
117 /// The commit recorded in the superproject will merged into the current branch of the submodule.
118 Merge,
119 /// A custom command to be called like `<command> hash-of-submodule-commit` that is to be executed to
120 /// perform the submodule update.
121 ///
122 /// Note that this variant is only allowed if the value is coming from an override. Thus it's not allowed to distribute
123 /// arbitrary commands via `.gitmodules` for security reasons.
124 Command(BString),
125 /// The submodule update is not performed at all.
126 None,
127}
128
129impl TryFrom<&BStr> for Update {
130 type Error = ();
131
132 fn try_from(value: &BStr) -> Result<Self, Self::Error> {
133 Ok(match value.as_bstr().as_bytes() {
134 b"checkout" => Update::Checkout,
135 b"rebase" => Update::Rebase,
136 b"merge" => Update::Merge,
137 b"none" => Update::None,
138 command if command.first() == Some(&b'!') => Update::Command(command[1..].to_owned().into()),
139 _ => return Err(()),
140 })
141 }
142}