|
| 1 | +use std::borrow::Cow; |
| 2 | +use std::collections::HashSet; |
| 3 | + |
| 4 | +use crate::fetch::refmap::{Mapping, Source, SpecIndex}; |
| 5 | +use crate::fetch::RefMap; |
| 6 | +use crate::transport::client::Transport; |
| 7 | +use bstr::{BString, ByteVec}; |
| 8 | +use gix_features::progress::Progress; |
| 9 | + |
| 10 | +/// The error returned by [`RefMap::new()`]. |
| 11 | +#[derive(Debug, thiserror::Error)] |
| 12 | +#[allow(missing_docs)] |
| 13 | +pub enum Error { |
| 14 | + #[error("The object format {format:?} as used by the remote is unsupported")] |
| 15 | + UnknownObjectFormat { format: BString }, |
| 16 | + #[error(transparent)] |
| 17 | + MappingValidation(#[from] gix_refspec::match_group::validate::Error), |
| 18 | + #[error(transparent)] |
| 19 | + ListRefs(#[from] crate::ls_refs::Error), |
| 20 | +} |
| 21 | + |
| 22 | +/// For use in [`RefMap::new()`]. |
| 23 | +#[derive(Debug, Clone)] |
| 24 | +pub struct Options { |
| 25 | + /// Use a two-component prefix derived from the ref-spec's source, like `refs/heads/` to let the server pre-filter refs |
| 26 | + /// with great potential for savings in traffic and local CPU time. Defaults to `true`. |
| 27 | + pub prefix_from_spec_as_filter_on_remote: bool, |
| 28 | + /// A list of refspecs to use as implicit refspecs which won't be saved or otherwise be part of the remote in question. |
| 29 | + /// |
| 30 | + /// This is useful for handling `remote.<name>.tagOpt` for example. |
| 31 | + pub extra_refspecs: Vec<gix_refspec::RefSpec>, |
| 32 | +} |
| 33 | + |
| 34 | +/// For use in [`RefMap::new()`]. |
| 35 | +pub struct Context<'a, T> { |
| 36 | + /// The outcome of the handshake performed with the remote. |
| 37 | + /// |
| 38 | + /// Note that it's mutable as depending on the protocol, it may contain refs that have been sent unconditionally. |
| 39 | + pub handshake: &'a mut crate::handshake::Outcome, |
| 40 | + /// The transport to use when making an `ls-refs` call. This is always done if the underlying protocol is V2, which is |
| 41 | + /// implied by the absence of refs in the `handshake` outcome. |
| 42 | + pub transport: &'a mut T, |
| 43 | + /// How to identify during the `ls-refs` call. |
| 44 | + /// |
| 45 | + /// This could be read from the `gitoxide.userAgent` configuration variable. |
| 46 | + pub user_agent: (&'static str, Option<Cow<'static, str>>), |
| 47 | + /// If `true`, output all packetlines using the the `gix-trace` machinery. |
| 48 | + pub trace_packetlines: bool, |
| 49 | +} |
| 50 | + |
| 51 | +impl Default for Options { |
| 52 | + fn default() -> Self { |
| 53 | + Options { |
| 54 | + prefix_from_spec_as_filter_on_remote: true, |
| 55 | + extra_refspecs: Vec::new(), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl RefMap { |
| 61 | + /// Create a new instance by obtaining all references on the remote that have been filtered through our remote's |
| 62 | + /// for _fetching_. A [context](Context) is provided to bundle what would be additional parameters, |
| 63 | + /// and [options](Options) are used to further configure the call. |
| 64 | + /// |
| 65 | + /// * `progress` is used if `ls-refs` is invoked on the remote. |
| 66 | + /// * `fetch_refspecs` are all explicit refspecs to identify references on the remote that you are interested in. |
| 67 | + /// Note that these are copied to [`RefMap::refspecs`] for convenience, as `RefMap::mappings` refer to them by index. |
| 68 | + #[allow(clippy::result_large_err)] |
| 69 | + #[maybe_async::maybe_async] |
| 70 | + pub async fn new<T>( |
| 71 | + mut progress: impl Progress, |
| 72 | + fetch_refspecs: &[gix_refspec::RefSpec], |
| 73 | + Context { |
| 74 | + handshake, |
| 75 | + transport, |
| 76 | + user_agent, |
| 77 | + trace_packetlines, |
| 78 | + }: Context<'_, T>, |
| 79 | + Options { |
| 80 | + prefix_from_spec_as_filter_on_remote, |
| 81 | + extra_refspecs, |
| 82 | + }: Options, |
| 83 | + ) -> Result<Self, Error> |
| 84 | + where |
| 85 | + T: Transport, |
| 86 | + { |
| 87 | + let _span = gix_trace::coarse!("gix_protocol::fetch::RefMap::new()"); |
| 88 | + let null = gix_hash::ObjectId::null(gix_hash::Kind::Sha1); // OK to hardcode Sha1, it's not supposed to match, ever. |
| 89 | + |
| 90 | + let all_refspecs = { |
| 91 | + let mut s: Vec<_> = fetch_refspecs.to_vec(); |
| 92 | + s.extend(extra_refspecs.clone()); |
| 93 | + s |
| 94 | + }; |
| 95 | + let remote_refs = match handshake.refs.take() { |
| 96 | + Some(refs) => refs, |
| 97 | + None => { |
| 98 | + crate::ls_refs( |
| 99 | + transport, |
| 100 | + &handshake.capabilities, |
| 101 | + |_capabilities, arguments, features| { |
| 102 | + features.push(user_agent); |
| 103 | + if prefix_from_spec_as_filter_on_remote { |
| 104 | + let mut seen = HashSet::new(); |
| 105 | + for spec in &all_refspecs { |
| 106 | + let spec = spec.to_ref(); |
| 107 | + if seen.insert(spec.instruction()) { |
| 108 | + let mut prefixes = Vec::with_capacity(1); |
| 109 | + spec.expand_prefixes(&mut prefixes); |
| 110 | + for mut prefix in prefixes { |
| 111 | + prefix.insert_str(0, "ref-prefix "); |
| 112 | + arguments.push(prefix); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + Ok(crate::ls_refs::Action::Continue) |
| 118 | + }, |
| 119 | + &mut progress, |
| 120 | + trace_packetlines, |
| 121 | + ) |
| 122 | + .await? |
| 123 | + } |
| 124 | + }; |
| 125 | + let num_explicit_specs = fetch_refspecs.len(); |
| 126 | + let group = gix_refspec::MatchGroup::from_fetch_specs(all_refspecs.iter().map(gix_refspec::RefSpec::to_ref)); |
| 127 | + let (res, fixes) = group |
| 128 | + .match_remotes(remote_refs.iter().map(|r| { |
| 129 | + let (full_ref_name, target, object) = r.unpack(); |
| 130 | + gix_refspec::match_group::Item { |
| 131 | + full_ref_name, |
| 132 | + target: target.unwrap_or(&null), |
| 133 | + object, |
| 134 | + } |
| 135 | + })) |
| 136 | + .validated()?; |
| 137 | + |
| 138 | + let mappings = res.mappings; |
| 139 | + let mappings = mappings |
| 140 | + .into_iter() |
| 141 | + .map(|m| Mapping { |
| 142 | + remote: m.item_index.map_or_else( |
| 143 | + || { |
| 144 | + Source::ObjectId(match m.lhs { |
| 145 | + gix_refspec::match_group::SourceRef::ObjectId(id) => id, |
| 146 | + _ => unreachable!("no item index implies having an object id"), |
| 147 | + }) |
| 148 | + }, |
| 149 | + |idx| Source::Ref(remote_refs[idx].clone()), |
| 150 | + ), |
| 151 | + local: m.rhs.map(std::borrow::Cow::into_owned), |
| 152 | + spec_index: if m.spec_index < num_explicit_specs { |
| 153 | + SpecIndex::ExplicitInRemote(m.spec_index) |
| 154 | + } else { |
| 155 | + SpecIndex::Implicit(m.spec_index - num_explicit_specs) |
| 156 | + }, |
| 157 | + }) |
| 158 | + .collect(); |
| 159 | + |
| 160 | + let object_hash = extract_object_format(handshake)?; |
| 161 | + Ok(RefMap { |
| 162 | + mappings, |
| 163 | + refspecs: fetch_refspecs.to_vec(), |
| 164 | + extra_refspecs, |
| 165 | + fixes, |
| 166 | + remote_refs, |
| 167 | + object_hash, |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +/// Assume sha1 if server says nothing, otherwise configure anything beyond sha1 in the local repo configuration |
| 173 | +#[allow(clippy::result_large_err)] |
| 174 | +fn extract_object_format(outcome: &crate::handshake::Outcome) -> Result<gix_hash::Kind, Error> { |
| 175 | + use bstr::ByteSlice; |
| 176 | + let object_hash = |
| 177 | + if let Some(object_format) = outcome.capabilities.capability("object-format").and_then(|c| c.value()) { |
| 178 | + let object_format = object_format.to_str().map_err(|_| Error::UnknownObjectFormat { |
| 179 | + format: object_format.into(), |
| 180 | + })?; |
| 181 | + match object_format { |
| 182 | + "sha1" => gix_hash::Kind::Sha1, |
| 183 | + unknown => return Err(Error::UnknownObjectFormat { format: unknown.into() }), |
| 184 | + } |
| 185 | + } else { |
| 186 | + gix_hash::Kind::Sha1 |
| 187 | + }; |
| 188 | + Ok(object_hash) |
| 189 | +} |
0 commit comments