gix_protocol/handshake/mod.rs
1use bstr::BString;
2
3#[cfg(all(feature = "handshake", feature = "fetch"))]
4pub use hero::ObtainRefMap;
5
6///
7pub mod refs;
8
9/// A git reference, commonly referred to as 'ref', as returned by a git server before sending a pack.
10#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub enum Ref {
13 /// A ref pointing to a `tag` object, which in turns points to an `object`, usually a commit
14 Peeled {
15 /// The name at which the ref is located, like `refs/tags/1.0`.
16 full_ref_name: BString,
17 /// The hash of the tag the ref points to.
18 tag: gix_hash::ObjectId,
19 /// The hash of the object the `tag` points to.
20 object: gix_hash::ObjectId,
21 },
22 /// A ref pointing to a commit object
23 Direct {
24 /// The name at which the ref is located, like `refs/heads/main` or `refs/tags/v1.0` for lightweight tags.
25 full_ref_name: BString,
26 /// The hash of the object the ref points to.
27 object: gix_hash::ObjectId,
28 },
29 /// A symbolic ref pointing to `target` ref, which in turn, ultimately after possibly following `tag`, points to an `object`
30 Symbolic {
31 /// The name at which the symbolic ref is located, like `HEAD`.
32 full_ref_name: BString,
33 /// The path of the ref the symbolic ref points to, like `refs/heads/main`.
34 ///
35 /// See issue [#205] for details
36 ///
37 /// [#205]: https://github.com/GitoxideLabs/gitoxide/issues/205
38 target: BString,
39 /// The hash of the annotated tag the ref points to, if present.
40 ///
41 /// Note that this field is also `None` if `full_ref_name` is a lightweight tag.
42 tag: Option<gix_hash::ObjectId>,
43 /// The hash of the object the `target` ref ultimately points to.
44 object: gix_hash::ObjectId,
45 },
46 /// A ref is unborn on the remote and just points to the initial, unborn branch, as is the case in a newly initialized repository
47 /// or dangling symbolic refs.
48 Unborn {
49 /// The name at which the ref is located, typically `HEAD`.
50 full_ref_name: BString,
51 /// The path of the ref the symbolic ref points to, like `refs/heads/main`, even though the `target` does not yet exist.
52 target: BString,
53 },
54}
55
56#[cfg(feature = "handshake")]
57pub(crate) mod hero {
58 use crate::handshake::Ref;
59
60 /// The result of the [`handshake()`](crate::handshake()) function.
61 #[derive(Default, Debug, Clone)]
62 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
63 pub struct Handshake {
64 /// The protocol version the server responded with. It might have downgraded the desired version.
65 pub server_protocol_version: gix_transport::Protocol,
66 /// The references reported as part of the `Protocol::V1` handshake, or `None` otherwise as V2 requires a separate request.
67 pub refs: Option<Vec<Ref>>,
68 /// Shallow updates as part of the `Protocol::V1`, to shallow a particular object.
69 /// Note that unshallowing isn't supported here.
70 pub v1_shallow_updates: Option<Vec<crate::fetch::response::ShallowUpdate>>,
71 /// The server capabilities.
72 pub capabilities: gix_transport::client::Capabilities,
73 }
74
75 #[cfg(feature = "fetch")]
76 pub use fetch::ObtainRefMap;
77
78 #[cfg(feature = "fetch")]
79 mod fetch {
80 use crate::{Handshake, command::Feature, fetch::RefMap, ls_refs::RefPrefixes};
81
82 use gix_error::Result;
83 use gix_error::{ResultExt, message};
84 use gix_utils::progress::Progress;
85
86 /// Intermediate state while potentially fetching a refmap after the handshake.
87 pub enum ObtainRefMap<'a> {
88 /// We already got a refmap to use from the V1 handshake, which always sends them.
89 Existing(RefMap),
90 /// We need to invoke another `ls-refs` command to retrieve the refmap as part of the V2 protocol.
91 LsRefsCommand(crate::LsRefsCommand<'a>, crate::fetch::refmap::init::Context),
92 }
93
94 macro_rules! fetch {
95 ($name:ident, $bisync:path, $transport:path, $invoke:ident, $mode:literal) => {
96 /// Fetch the refmap, either by returning the existing one or invoking the `ls-refs` command.
97 #[$bisync]
98 pub async fn $name(
99 self,
100 mut progress: impl Progress,
101 transport: &mut impl $transport,
102 trace_packetlines: bool,
103 ) -> gix_error::Result<RefMap> {
104 let (cmd, cx) = match self {
105 ObtainRefMap::Existing(map) => return Ok(map),
106 ObtainRefMap::LsRefsCommand(cmd, cx) => (cmd, cx),
107 };
108
109 let _span = gix_trace::coarse!(
110 "gix_protocol::handshake::ObtainRefMap::fetch()",
111 mode = $mode
112 );
113 let capabilities = cmd.capabilities;
114 let remote_refs = cmd
115 .$invoke(transport, &mut progress, trace_packetlines)
116 .await
117 .or_raise(|| message("Failed to list references on the remote"))?;
118 RefMap::from_refs(remote_refs, capabilities, cx)
119 }
120 };
121 }
122
123 impl ObtainRefMap<'_> {
124 #[cfg(feature = "async-client")]
125 fetch!(
126 fetch_async,
127 ::gix_macros::keep,
128 crate::transport::client::async_io::Transport,
129 invoke_async,
130 "async"
131 );
132
133 #[cfg(feature = "blocking-client")]
134 fetch!(
135 fetch_blocking,
136 ::gix_macros::sync,
137 crate::transport::client::blocking_io::Transport,
138 invoke_blocking,
139 "blocking"
140 );
141 }
142
143 impl Handshake {
144 /// Prepare fetching a [refmap](RefMap) if not present in the handshake.
145 pub fn prepare_lsrefs_or_extract_refmap(
146 &mut self,
147 user_agent: Feature,
148 prefix_from_spec_as_filter_on_remote: bool,
149 refmap_context: crate::fetch::refmap::init::Context,
150 ) -> Result<ObtainRefMap<'_>> {
151 if let Some(refs) = self.refs.take() {
152 return Ok(ObtainRefMap::Existing(RefMap::from_refs(
153 refs,
154 &self.capabilities,
155 refmap_context,
156 )?));
157 }
158
159 let prefix_refs = prefix_from_spec_as_filter_on_remote.then(|| {
160 let all_refspecs = refmap_context.aggregate_refspecs();
161 RefPrefixes::from_refspecs(&all_refspecs)
162 });
163 Ok(ObtainRefMap::LsRefsCommand(
164 crate::LsRefsCommand::new(prefix_refs, &self.capabilities, user_agent),
165 refmap_context,
166 ))
167 }
168 }
169 }
170}
171
172#[cfg(any(feature = "blocking-client", feature = "async-client"))]
173#[cfg(feature = "handshake")]
174pub(crate) mod function;