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