gix_ref/store/file/
find.rs

1use std::{
2    borrow::Cow,
3    io::{self, Read},
4    path::{Path, PathBuf},
5};
6
7pub use error::Error;
8
9use crate::name::is_pseudo_ref;
10use crate::{
11    file,
12    store_impl::{file::loose, packed},
13    BStr, BString, FullNameRef, PartialName, PartialNameRef, Reference,
14};
15
16/// ### Finding References - notes about precomposed unicode.
17///
18/// Generally, ref names and the target of symbolic refs are stored as-is if [`Self::precompose_unicode`] is `false`.
19/// If `true`, refs are stored as precomposed unicode in `packed-refs`, but stored as is on disk as it is then assumed
20/// to be indifferent, i.e. `"a\u{308}"` is the same as `"รค"`.
21///
22/// This also means that when refs are packed for transmission to another machine, both their names and the target of
23/// symbolic references need to be precomposed.
24///
25/// Namespaces are left as is as they never get past the particular repository that uses them.
26impl file::Store {
27    /// Find a single reference by the given `path` which is required to be a valid reference name.
28    ///
29    /// Returns `Ok(None)` if no such ref exists.
30    ///
31    /// ### Note
32    ///
33    /// * The lookup algorithm follows the one in [the git documentation][git-lookup-docs].
34    /// * The packed buffer is checked for modifications each time the method is called. See [`file::Store::try_find_packed()`]
35    ///   for a version with more control.
36    ///
37    /// [git-lookup-docs]: https://github.com/git/git/blob/5d5b1473453400224ebb126bf3947e0a3276bdf5/Documentation/revisions.txt#L34-L46
38    pub fn try_find<'a, Name, E>(&self, partial: Name) -> Result<Option<Reference>, Error>
39    where
40        Name: TryInto<&'a PartialNameRef, Error = E>,
41        Error: From<E>,
42    {
43        let packed = self.assure_packed_refs_uptodate()?;
44        self.find_one_with_verified_input(partial.try_into()?, packed.as_ref().map(|b| &***b))
45    }
46
47    /// Similar to [`file::Store::find()`] but a non-existing ref is treated as error.
48    ///
49    /// Find only loose references, that is references that aren't in the packed-refs buffer.
50    /// All symbolic references are loose references.
51    /// `HEAD` is always a loose reference.
52    pub fn try_find_loose<'a, Name, E>(&self, partial: Name) -> Result<Option<loose::Reference>, Error>
53    where
54        Name: TryInto<&'a PartialNameRef, Error = E>,
55        Error: From<E>,
56    {
57        self.find_one_with_verified_input(partial.try_into()?, None)
58            .map(|r| r.map(Into::into))
59    }
60
61    /// Similar to [`file::Store::find()`], but allows to pass a snapshotted packed buffer instead.
62    pub fn try_find_packed<'a, Name, E>(
63        &self,
64        partial: Name,
65        packed: Option<&packed::Buffer>,
66    ) -> Result<Option<Reference>, Error>
67    where
68        Name: TryInto<&'a PartialNameRef, Error = E>,
69        Error: From<E>,
70    {
71        self.find_one_with_verified_input(partial.try_into()?, packed)
72    }
73
74    pub(crate) fn find_one_with_verified_input(
75        &self,
76        partial_name: &PartialNameRef,
77        packed: Option<&packed::Buffer>,
78    ) -> Result<Option<Reference>, Error> {
79        fn decompose_if(mut r: Reference, input_changed_to_precomposed: bool) -> Reference {
80            if input_changed_to_precomposed {
81                use gix_object::bstr::ByteSlice;
82                let decomposed = r
83                    .name
84                    .0
85                    .to_str()
86                    .ok()
87                    .map(|name| gix_utils::str::decompose(name.into()));
88                if let Some(Cow::Owned(decomposed)) = decomposed {
89                    r.name.0 = decomposed.into();
90                }
91            }
92            r
93        }
94        let mut buf = BString::default();
95        let mut precomposed_partial_name_storage = packed.filter(|_| self.precompose_unicode).and_then(|_| {
96            use gix_object::bstr::ByteSlice;
97            let precomposed = partial_name.0.to_str().ok()?;
98            let precomposed = gix_utils::str::precompose(precomposed.into());
99            match precomposed {
100                Cow::Owned(precomposed) => Some(PartialName(precomposed.into())),
101                Cow::Borrowed(_) => None,
102            }
103        });
104        let precomposed_partial_name = precomposed_partial_name_storage
105            .as_ref()
106            .map(std::convert::AsRef::as_ref);
107        for consider_pseudo_ref in [true, false] {
108            if !consider_pseudo_ref && !is_pseudo_ref(partial_name.as_bstr()) {
109                break;
110            }
111            'try_directories: for inbetween in &["", "tags", "heads", "remotes"] {
112                match self.find_inner(
113                    inbetween,
114                    partial_name,
115                    precomposed_partial_name,
116                    packed,
117                    &mut buf,
118                    consider_pseudo_ref,
119                ) {
120                    Ok(Some(r)) => return Ok(Some(decompose_if(r, precomposed_partial_name.is_some()))),
121                    Ok(None) => {
122                        if consider_pseudo_ref && is_pseudo_ref(partial_name.as_bstr()) {
123                            break 'try_directories;
124                        }
125                        continue;
126                    }
127                    Err(err) => return Err(err),
128                }
129            }
130        }
131        if partial_name.as_bstr() != "HEAD" {
132            if let Some(mut precomposed) = precomposed_partial_name_storage {
133                precomposed = precomposed.join("HEAD".into()).expect("HEAD is valid name");
134                precomposed_partial_name_storage = Some(precomposed);
135            }
136            self.find_inner(
137                "remotes",
138                partial_name
139                    .to_owned()
140                    .join("HEAD".into())
141                    .expect("HEAD is valid name")
142                    .as_ref(),
143                precomposed_partial_name_storage
144                    .as_ref()
145                    .map(std::convert::AsRef::as_ref),
146                None,
147                &mut buf,
148                true, /* consider-pseudo-ref */
149            )
150            .map(|res| res.map(|r| decompose_if(r, precomposed_partial_name_storage.is_some())))
151        } else {
152            Ok(None)
153        }
154    }
155
156    fn find_inner(
157        &self,
158        inbetween: &str,
159        partial_name: &PartialNameRef,
160        precomposed_partial_name: Option<&PartialNameRef>,
161        packed: Option<&packed::Buffer>,
162        path_buf: &mut BString,
163        consider_pseudo_ref: bool,
164    ) -> Result<Option<Reference>, Error> {
165        let full_name = precomposed_partial_name
166            .unwrap_or(partial_name)
167            .construct_full_name_ref(inbetween, path_buf, consider_pseudo_ref);
168        let content_buf = self.ref_contents(full_name).map_err(|err| Error::ReadFileContents {
169            source: err,
170            path: self.reference_path(full_name),
171        })?;
172
173        match content_buf {
174            None => {
175                if let Some(packed) = packed {
176                    if let Some(full_name) = packed::find::transform_full_name_for_lookup(full_name) {
177                        let full_name_backing;
178                        let full_name = match &self.namespace {
179                            Some(namespace) => {
180                                full_name_backing = namespace.to_owned().into_namespaced_name(full_name);
181                                full_name_backing.as_ref()
182                            }
183                            None => full_name,
184                        };
185                        if let Some(packed_ref) = packed.try_find_full_name(full_name)? {
186                            let mut res: Reference = packed_ref.into();
187                            if let Some(namespace) = &self.namespace {
188                                res.strip_namespace(namespace);
189                            }
190                            return Ok(Some(res));
191                        };
192                    }
193                }
194                Ok(None)
195            }
196            Some(content) => Ok(Some(
197                loose::Reference::try_from_path(full_name.to_owned(), &content)
198                    .map(Into::into)
199                    .map(|mut r: Reference| {
200                        if let Some(namespace) = &self.namespace {
201                            r.strip_namespace(namespace);
202                        }
203                        r
204                    })
205                    .map_err(|err| Error::ReferenceCreation {
206                        source: err,
207                        relative_path: full_name.to_path().to_owned(),
208                    })?,
209            )),
210        }
211    }
212}
213
214impl file::Store {
215    pub(crate) fn to_base_dir_and_relative_name<'a>(
216        &self,
217        name: &'a FullNameRef,
218        is_reflog: bool,
219    ) -> (Cow<'_, Path>, &'a FullNameRef) {
220        let commondir = self.common_dir_resolved();
221        let linked_git_dir =
222            |worktree_name: &BStr| commondir.join("worktrees").join(gix_path::from_bstr(worktree_name));
223        name.category_and_short_name()
224            .map(|(c, sn)| {
225                use crate::Category::*;
226                let sn = FullNameRef::new_unchecked(sn);
227                match c {
228                    LinkedPseudoRef { name: worktree_name } => is_reflog
229                        .then(|| (linked_git_dir(worktree_name).into(), sn))
230                        .unwrap_or((commondir.into(), name)),
231                    Tag | LocalBranch | RemoteBranch | Note => (commondir.into(), name),
232                    MainRef | MainPseudoRef => (commondir.into(), sn),
233                    LinkedRef { name: worktree_name } => sn
234                        .category()
235                        .is_some_and(|cat| cat.is_worktree_private())
236                        .then(|| {
237                            if is_reflog {
238                                (linked_git_dir(worktree_name).into(), sn)
239                            } else {
240                                (commondir.into(), name)
241                            }
242                        })
243                        .unwrap_or((commondir.into(), sn)),
244                    PseudoRef | Bisect | Rewritten | WorktreePrivate => (self.git_dir.as_path().into(), name),
245                }
246            })
247            .unwrap_or((commondir.into(), name))
248    }
249
250    /// Implements the logic required to transform a fully qualified refname into a filesystem path
251    pub(crate) fn reference_path_with_base<'b>(&self, name: &'b FullNameRef) -> (Cow<'_, Path>, Cow<'b, Path>) {
252        let (base, name) = self.to_base_dir_and_relative_name(name, false);
253        (
254            base,
255            match &self.namespace {
256                None => gix_path::to_native_path_on_windows(name.as_bstr()),
257                Some(namespace) => {
258                    gix_path::to_native_path_on_windows(namespace.to_owned().into_namespaced_name(name).into_inner())
259                }
260            },
261        )
262    }
263
264    /// Implements the logic required to transform a fully qualified refname into a filesystem path
265    pub(crate) fn reference_path(&self, name: &FullNameRef) -> PathBuf {
266        let (base, relative_path) = self.reference_path_with_base(name);
267        base.join(relative_path)
268    }
269
270    /// Read the file contents with a verified full reference path and return it in the given vector if possible.
271    pub(crate) fn ref_contents(&self, name: &FullNameRef) -> io::Result<Option<Vec<u8>>> {
272        let (base, relative_path) = self.reference_path_with_base(name);
273        if self.prohibit_windows_device_names
274            && relative_path
275                .components()
276                .filter_map(|c| gix_path::try_os_str_into_bstr(c.as_os_str().into()).ok())
277                .any(|c| gix_validate::path::component_is_windows_device(c.as_ref()))
278        {
279            return Err(std::io::Error::new(
280                std::io::ErrorKind::Other,
281                format!("Illegal use of reserved Windows device name in \"{}\"", name.as_bstr()),
282            ));
283        }
284
285        let ref_path = base.join(relative_path);
286        match std::fs::File::open(&ref_path) {
287            Ok(mut file) => {
288                let mut buf = Vec::with_capacity(128);
289                if let Err(err) = file.read_to_end(&mut buf) {
290                    return if ref_path.is_dir() { Ok(None) } else { Err(err) };
291                }
292                Ok(buf.into())
293            }
294            Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(None),
295            #[cfg(windows)]
296            Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => Ok(None),
297            Err(err) => Err(err),
298        }
299    }
300}
301
302///
303pub mod existing {
304    pub use error::Error;
305
306    use crate::{
307        file::{self},
308        store_impl::{
309            file::{find, loose},
310            packed,
311        },
312        PartialNameRef, Reference,
313    };
314
315    impl file::Store {
316        /// Similar to [`file::Store::try_find()`] but a non-existing ref is treated as error.
317        pub fn find<'a, Name, E>(&self, partial: Name) -> Result<Reference, Error>
318        where
319            Name: TryInto<&'a PartialNameRef, Error = E>,
320            crate::name::Error: From<E>,
321        {
322            let packed = self.assure_packed_refs_uptodate().map_err(find::Error::PackedOpen)?;
323            self.find_existing_inner(partial, packed.as_ref().map(|b| &***b))
324        }
325
326        /// Similar to [`file::Store::find()`], but supports a stable packed buffer.
327        pub fn find_packed<'a, Name, E>(
328            &self,
329            partial: Name,
330            packed: Option<&packed::Buffer>,
331        ) -> Result<Reference, Error>
332        where
333            Name: TryInto<&'a PartialNameRef, Error = E>,
334            crate::name::Error: From<E>,
335        {
336            self.find_existing_inner(partial, packed)
337        }
338
339        /// Similar to [`file::Store::find()`] won't handle packed-refs.
340        pub fn find_loose<'a, Name, E>(&self, partial: Name) -> Result<loose::Reference, Error>
341        where
342            Name: TryInto<&'a PartialNameRef, Error = E>,
343            crate::name::Error: From<E>,
344        {
345            self.find_existing_inner(partial, None).map(Into::into)
346        }
347
348        /// Similar to [`file::Store::find()`] but a non-existing ref is treated as error.
349        pub(crate) fn find_existing_inner<'a, Name, E>(
350            &self,
351            partial: Name,
352            packed: Option<&packed::Buffer>,
353        ) -> Result<Reference, Error>
354        where
355            Name: TryInto<&'a PartialNameRef, Error = E>,
356            crate::name::Error: From<E>,
357        {
358            let path = partial
359                .try_into()
360                .map_err(|err| Error::Find(find::Error::RefnameValidation(err.into())))?;
361            match self.find_one_with_verified_input(path, packed) {
362                Ok(Some(r)) => Ok(r),
363                Ok(None) => Err(Error::NotFound {
364                    name: path.to_partial_path().to_owned(),
365                }),
366                Err(err) => Err(err.into()),
367            }
368        }
369    }
370
371    mod error {
372        use std::path::PathBuf;
373
374        use crate::store_impl::file::find;
375
376        /// The error returned by [file::Store::find_existing()][crate::file::Store::find()].
377        #[derive(Debug, thiserror::Error)]
378        #[allow(missing_docs)]
379        pub enum Error {
380            #[error("An error occurred while trying to find a reference")]
381            Find(#[from] find::Error),
382            #[error("The ref partially named {name:?} could not be found")]
383            NotFound { name: PathBuf },
384        }
385    }
386}
387
388mod error {
389    use std::{convert::Infallible, io, path::PathBuf};
390
391    use crate::{file, store_impl::packed};
392
393    /// The error returned by [file::Store::find()].
394    #[derive(Debug, thiserror::Error)]
395    #[allow(missing_docs)]
396    pub enum Error {
397        #[error("The ref name or path is not a valid ref name")]
398        RefnameValidation(#[from] crate::name::Error),
399        #[error("The ref file {path:?} could not be read in full")]
400        ReadFileContents { source: io::Error, path: PathBuf },
401        #[error("The reference at \"{relative_path}\" could not be instantiated")]
402        ReferenceCreation {
403            source: file::loose::reference::decode::Error,
404            relative_path: PathBuf,
405        },
406        #[error("A packed ref lookup failed")]
407        PackedRef(#[from] packed::find::Error),
408        #[error("Could not open the packed refs buffer when trying to find references.")]
409        PackedOpen(#[from] packed::buffer::open::Error),
410    }
411
412    impl From<Infallible> for Error {
413        fn from(_: Infallible) -> Self {
414            unreachable!("this impl is needed to allow passing a known valid partial path as parameter")
415        }
416    }
417}