Showing the changes to git-protocol/src (1 of 1 file). Show all
Unified · Split
| @@ -1,14 +1,10 @@ | |||
| 1 | use crate::credentials; | 1 | use crate::credentials; |
| 2 | use bstr::{BStr, BString, ByteSlice}; | ||
| 3 | use git_transport::{ | 2 | use git_transport::{ |
| 4 | client::{self, SetServiceResponse}, | 3 | client::{self, SetServiceResponse}, |
| 5 | Service, | 4 | Service, |
| 6 | }; | 5 | }; |
| 7 | use quick_error::quick_error; | 6 | use quick_error::quick_error; |
| 8 | use std::{ | 7 | use std::{convert::TryInto, io}; |
| 9 | convert::{TryFrom, TryInto}, | ||
| 10 | io, | ||
| 11 | }; | ||
| 12 | 8 | ||
| 13 | mod refs; | 9 | mod refs; |
| 14 | pub use refs::Ref; | 10 | pub use refs::Ref; |
| @@ -17,7 +13,7 @@ pub use refs::Ref; · 2 unmodified lines | |||
| 17 | mod tests; | 13 | mod tests; |
| 18 | 14 | ||
| 19 | // Note that arguments suffixed by spaces take another value. | 15 | // Note that arguments suffixed by spaces take another value. |
| 20 | const _BUILTIN_V2_COMMAND_ARGUMENT_NAMES: &[(&str, &[&str])] = &[ | 16 | const BUILTIN_V2_COMMAND_ARGUMENT_NAMES: &[(&str, &[&str])] = &[ |
| 21 | ("ls-refs", &["symrefs", "peel", "ref-prefix "]), | 17 | ("ls-refs", &["symrefs", "peel", "ref-prefix "]), |
| 22 | ( | 18 | ( |
| 23 | "fetch", | 19 | "fetch", |
| @@ -70,21 +66,87 @@ quick_error! { · 46 unmodified lines | |||
| 70 | } | 66 | } |
| 71 | } | 67 | } |
| 72 | 68 | ||
| 69 | /// Define what to do next. | ||
| 70 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)] | ||
| 71 | pub enum Action { | ||
| 72 | /// Continue the typical flow of operations in this flow. | ||
| 73 | Continue, | ||
| 74 | /// Close the connection without making any further requests. | ||
| 75 | Close, | ||
| 76 | } | ||
| 77 | |||
| 78 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)] | ||
| 79 | pub enum Command { | ||
| 80 | LsRefs, | ||
| 81 | } | ||
| 82 | |||
| 83 | impl Command { | ||
| 84 | fn as_str(&self) -> &'static str { | ||
| 85 | match self { | ||
| 86 | Command::LsRefs => "ls-refs", | ||
| 87 | } | ||
| 88 | } | ||
| 89 | fn builtin_argument_prefixes(&self) -> &'static [&'static str] { | ||
| 90 | let name = self.as_str(); | ||
| 91 | BUILTIN_V2_COMMAND_ARGUMENT_NAMES | ||
| 92 | .iter() | ||
| 93 | .find_map(|(n, v)| if *n == name { Some(v) } else { None }) | ||
| 94 | .expect("command to be found") | ||
| 95 | } | ||
| 96 | |||
| 97 | /// Panics if the given arguments and features don't match what's statically known. It's considered a bug in the delegate. | ||
| 98 | fn validate_prefixes_or_panic(&self, server: &Capabilities, arguments: &[String], features: &[&str]) { | ||
| 99 | let allowed = self.builtin_argument_prefixes(); | ||
| 100 | for arg in arguments { | ||
| 101 | if allowed.iter().any(|allowed| arg.starts_with(allowed)) { | ||
| 102 | continue; | ||
| 103 | } | ||
| 104 | panic!("{}: argument {} is not known or allowed", self.as_str(), arg); | ||
| 105 | } | ||
| 106 | if let Some(allowed) = server | ||
| 107 | .values_of(self.as_str()) | ||
| 108 | .map(|v| v.map(|f| f.to_str_lossy()).collect::<Vec<_>>()) | ||
| 109 | { | ||
| 110 | for feature in features { | ||
| 111 | if allowed.iter().any(|allowed| feature.starts_with(allowed.as_ref())) { | ||
| 112 | continue; | ||
| 113 | } | ||
| 114 | panic!("{}: feature/capability {} is not supported", self.as_str(), feature); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 73 | pub trait Delegate { | 120 | pub trait Delegate { |
| 74 | /// A chance to inspect or adjust the Capabilities returned after handshake with the server. | 121 | /// A chance to inspect or adjust the Capabilities returned after handshake with the server. |
| 75 | /// They will be used in subsequent calls to the server, but the client is free to cache information as they see fit. | 122 | /// They will be used in subsequent calls to the server, but the client is free to cache information as they see fit. |
| 76 | fn adjust_capabilities(&mut self, _version: git_transport::Protocol, _capabilities: &mut Capabilities) {} | 123 | fn adjust_capabilities(&mut self, _version: git_transport::Protocol, _capabilities: &mut Capabilities) {} |
| 124 | |||
| 125 | /// Called before invoking a given `command` to allow providing it with additional `arguments` and to enable `features`. | ||
| 126 | /// Note that some arguments might be preset based on typical usage. | ||
| 127 | /// The `server` capabilities can be used to see which additional capabilities the server supports as per the handshake. | ||
| 128 | fn prepare_command( | ||
| 129 | &mut self, | ||
| 130 | _command: Command, | ||
| 131 | _server: &Capabilities, | ||
| 132 | _arguments: &mut Vec<String>, | ||
| 133 | _features: &mut Vec<&str>, | ||
| 134 | ) -> Action { | ||
| 135 | Action::Continue | ||
| 136 | } | ||
| 77 | } | 137 | } |
| 78 | 138 | ||
| 79 | mod capabilities; | 139 | mod capabilities; |
| 140 | use bstr::ByteSlice; | ||
| 80 | pub use capabilities::Capabilities; | 141 | pub use capabilities::Capabilities; |
| 81 | 142 | ||
| 143 | /// Note that depending on the `delegate`, the actual action peformed can be `ls-refs`, `clone` or `fetch`. | ||
| 82 | pub fn fetch<F: FnMut(credentials::Action) -> credentials::Result>( | 144 | pub fn fetch<F: FnMut(credentials::Action) -> credentials::Result>( |
| 83 | mut transport: impl client::Transport, | 145 | mut transport: impl client::Transport, |
| 84 | mut delegate: impl Delegate, | 146 | mut delegate: impl Delegate, |
| 85 | mut authenticate: F, | 147 | mut authenticate: F, |
| 86 | ) -> Result<(), Error> { | 148 | ) -> Result<(), Error> { |
| 87 | let (actual_protocol, _refs, _capabilities, call_ls_refs) = { | 149 | let (actual_protocol, _refs, capabilities, call_ls_refs) = { |
| 88 | let result = transport.handshake(Service::UploadPack); | 150 | let result = transport.handshake(Service::UploadPack); |
| 89 | let SetServiceResponse { | 151 | let SetServiceResponse { |
| 90 | actual_protocol, | 152 | actual_protocol, |
| @@ -147,7 +209,12 @@ pub fn fetch<F: FnMut(credentials::Action) -> credentials::Result>( · 56 unmodified lines | |||
| 147 | 209 | ||
| 148 | // capabilities: impl IntoIterator<Item = (&'a str, Option<&'a str>)>, | 210 | // capabilities: impl IntoIterator<Item = (&'a str, Option<&'a str>)>, |
| 149 | // arguments: Option<impl IntoIterator<Item = BString>>, | 211 | // arguments: Option<impl IntoIterator<Item = BString>>, |
| 150 | // let next = delegate.prepare_command("ls-refs", &capabilities); | 212 | let mut ls_features = Vec::new(); |
| 213 | let mut ls_args = vec!["peel".to_string(), "symrefs".into()]; | ||
| 214 | let ls_refs = Command::LsRefs; | ||
| 215 | let _next = delegate.prepare_command(ls_refs, &capabilities, &mut ls_args, &mut ls_features); | ||
| 216 | ls_refs.validate_prefixes_or_panic(&capabilities, &ls_args, &ls_features); | ||
| 217 | |||
| 151 | // transport.request(client::WriteMode::Binary, Vec::new())?; | 218 | // transport.request(client::WriteMode::Binary, Vec::new())?; |
| 152 | } | 219 | } |
| 153 | 220 | ||