|
| 1 | +/// Either an object id that the remote has or the matched remote ref itself. |
| 2 | +#[derive(Debug, Clone)] |
| 3 | +#[cfg(any(feature = "blocking-client", feature = "async-client"))] |
| 4 | +pub enum Source { |
| 5 | + /// An object id, as the matched ref-spec was an object id itself. |
| 6 | + ObjectId(gix_hash::ObjectId), |
| 7 | + /// The remote reference that matched the ref-specs name. |
| 8 | + Ref(crate::handshake::Ref), |
| 9 | +} |
| 10 | + |
| 11 | +#[cfg(any(feature = "blocking-client", feature = "async-client"))] |
| 12 | +impl Source { |
| 13 | + /// Return either the direct object id we refer to or the direct target that a reference refers to. |
| 14 | + /// The latter may be a direct or a symbolic reference. |
| 15 | + /// If unborn, `None` is returned. |
| 16 | + pub fn as_id(&self) -> Option<&gix_hash::oid> { |
| 17 | + match self { |
| 18 | + Source::ObjectId(id) => Some(id), |
| 19 | + Source::Ref(r) => r.unpack().1, |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + /// Return the target that this symbolic ref is pointing to, or `None` if it is no symbolic ref. |
| 24 | + pub fn as_target(&self) -> Option<&bstr::BStr> { |
| 25 | + match self { |
| 26 | + Source::ObjectId(_) => None, |
| 27 | + Source::Ref(r) => match r { |
| 28 | + crate::handshake::Ref::Peeled { .. } | crate::handshake::Ref::Direct { .. } => None, |
| 29 | + crate::handshake::Ref::Symbolic { target, .. } | crate::handshake::Ref::Unborn { target, .. } => { |
| 30 | + Some(target.as_ref()) |
| 31 | + } |
| 32 | + }, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Returns the peeled id of this instance, that is the object that can't be de-referenced anymore. |
| 37 | + pub fn peeled_id(&self) -> Option<&gix_hash::oid> { |
| 38 | + match self { |
| 39 | + Source::ObjectId(id) => Some(id), |
| 40 | + Source::Ref(r) => { |
| 41 | + let (_name, target, peeled) = r.unpack(); |
| 42 | + peeled.or(target) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// Return ourselves as the full name of the reference we represent, or `None` if this source isn't a reference but an object. |
| 48 | + pub fn as_name(&self) -> Option<&bstr::BStr> { |
| 49 | + match self { |
| 50 | + Source::ObjectId(_) => None, |
| 51 | + Source::Ref(r) => match r { |
| 52 | + crate::handshake::Ref::Unborn { full_ref_name, .. } |
| 53 | + | crate::handshake::Ref::Symbolic { full_ref_name, .. } |
| 54 | + | crate::handshake::Ref::Direct { full_ref_name, .. } |
| 55 | + | crate::handshake::Ref::Peeled { full_ref_name, .. } => Some(full_ref_name.as_ref()), |
| 56 | + }, |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/// An index into various lists of refspecs that have been used in a [Mapping] of remote references to local ones. |
| 62 | +#[cfg(any(feature = "blocking-client", feature = "async-client"))] |
| 63 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] |
| 64 | +pub enum SpecIndex { |
| 65 | + /// An index into the _refspecs of the remote_ that triggered a fetch operation. |
| 66 | + /// These refspecs are explicit and visible to the user. |
| 67 | + ExplicitInRemote(usize), |
| 68 | + /// An index into the list of [extra refspecs][crate::remote::fetch::RefMap::extra_refspecs] that are implicit |
| 69 | + /// to a particular fetch operation. |
| 70 | + Implicit(usize), |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(any(feature = "blocking-client", feature = "async-client"))] |
| 74 | +impl SpecIndex { |
| 75 | + /// Depending on our index variant, get the index either from `refspecs` or from `extra_refspecs` for `Implicit` variants. |
| 76 | + pub fn get<'a>( |
| 77 | + self, |
| 78 | + refspecs: &'a [gix_refspec::RefSpec], |
| 79 | + extra_refspecs: &'a [gix_refspec::RefSpec], |
| 80 | + ) -> Option<&'a gix_refspec::RefSpec> { |
| 81 | + match self { |
| 82 | + SpecIndex::ExplicitInRemote(idx) => refspecs.get(idx), |
| 83 | + SpecIndex::Implicit(idx) => extra_refspecs.get(idx), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// If this is an `Implicit` variant, return its index. |
| 88 | + pub fn implicit_index(self) -> Option<usize> { |
| 89 | + match self { |
| 90 | + SpecIndex::Implicit(idx) => Some(idx), |
| 91 | + SpecIndex::ExplicitInRemote(_) => None, |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// A mapping between a single remote reference and its advertised objects to a local destination which may or may not exist. |
| 97 | +#[derive(Debug, Clone)] |
| 98 | +#[cfg(any(feature = "blocking-client", feature = "async-client"))] |
| 99 | +pub struct Mapping { |
| 100 | + /// The reference on the remote side, along with information about the objects they point to as advertised by the server. |
| 101 | + pub remote: Source, |
| 102 | + /// The local tracking reference to update after fetching the object visible via `remote`. |
| 103 | + pub local: Option<bstr::BString>, |
| 104 | + /// The index into the fetch ref-specs used to produce the mapping, allowing it to be recovered. |
| 105 | + pub spec_index: SpecIndex, |
| 106 | +} |
0 commit comments