History

[clone] sketch of delegating simple commands along with arg/feature verification

Sebastian Thiel committed 6 years ago

Commit c2ebc481 parent a6bcdc41 file · +75 −8Browse files

Showing the changes to git-protocol/src (1 of 1 file). Show all

Unified · Split

git-protocol/src/fetch/mod.rs+75−8
@@ -1,14 +1,10 @@
11 use crate::credentials;
2−use bstr::{BStr, BString, ByteSlice};
32 use git_transport::{
43 client::{self, SetServiceResponse},
54 Service,
65 };
76 use quick_error::quick_error;
8−use std::{
9− convert::{TryFrom, TryInto},
10− io,
11−};
7+use std::{convert::TryInto, io};
128
139 mod refs;
1410 pub use refs::Ref;
@@ -17,7 +13,7 @@ pub use refs::Ref; · 2 unmodified lines
1713 mod tests;
1814
1915 // 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])] = &[
2117 ("ls-refs", &["symrefs", "peel", "ref-prefix "]),
2218 (
2319 "fetch",
@@ -70,21 +66,87 @@ quick_error! { · 46 unmodified lines
7066 }
7167 }
7268
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+
73120 pub trait Delegate {
74121 /// A chance to inspect or adjust the Capabilities returned after handshake with the server.
75122 /// They will be used in subsequent calls to the server, but the client is free to cache information as they see fit.
76123 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+ }
77137 }
78138
79139 mod capabilities;
140+use bstr::ByteSlice;
80141 pub use capabilities::Capabilities;
81142
143+/// Note that depending on the `delegate`, the actual action peformed can be `ls-refs`, `clone` or `fetch`.
82144 pub fn fetch<F: FnMut(credentials::Action) -> credentials::Result>(
83145 mut transport: impl client::Transport,
84146 mut delegate: impl Delegate,
85147 mut authenticate: F,
86148 ) -> Result<(), Error> {
87− let (actual_protocol, _refs, _capabilities, call_ls_refs) = {
149+ let (actual_protocol, _refs, capabilities, call_ls_refs) = {
88150 let result = transport.handshake(Service::UploadPack);
89151 let SetServiceResponse {
90152 actual_protocol,
@@ -147,7 +209,12 @@ pub fn fetch<F: FnMut(credentials::Action) -> credentials::Result>( · 56 unmodified lines
147209
148210 // capabilities: impl IntoIterator<Item = (&'a str, Option<&'a str>)>,
149211 // 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+
151218 // transport.request(client::WriteMode::Binary, Vec::new())?;
152219 }
153220

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t