1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#![allow(clippy::result_large_err)]
use git_protocol::transport::client::Transport;
use crate::{remote::Connection, Progress, Remote};
mod error {
use crate::{bstr::BString, config, remote};
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Could not obtain options for connecting via ssh")]
SshOptions(#[from] config::ssh_connect_options::Error),
#[error("Could not obtain the current directory")]
CurrentDir(#[from] std::io::Error),
#[error("Could not access remote repository at \"{}\"", directory.display())]
InvalidRemoteRepositoryPath { directory: std::path::PathBuf },
#[error(transparent)]
SchemePermission(#[from] config::protocol::allow::Error),
#[error("Protocol {scheme:?} of url {url:?} is denied per configuration")]
ProtocolDenied { url: BString, scheme: git_url::Scheme },
#[error(transparent)]
Connect(#[from] git_protocol::transport::client::connect::Error),
#[error("The {} url was missing - don't know where to establish a connection to", direction.as_str())]
MissingUrl { direction: remote::Direction },
#[error("Protocol named {given:?} is not a valid protocol. Choose between 1 and 2")]
UnknownProtocol { given: BString },
#[error("Could not verify that \"{}\" url is a valid git directory before attempting to use it", url.to_bstring())]
FileUrl {
source: Box<git_discover::is_git::Error>,
url: git_url::Url,
},
}
impl git_protocol::transport::IsSpuriousError for Error {
fn is_spurious(&self) -> bool {
match self {
Error::Connect(err) => err.is_spurious(),
_ => false,
}
}
}
}
pub use error::Error;
impl<'repo> Remote<'repo> {
pub fn to_connection_with_transport<T, P>(&self, transport: T, progress: P) -> Connection<'_, 'repo, T, P>
where
T: Transport,
P: Progress,
{
Connection {
remote: self,
authenticate: None,
transport_options: None,
transport,
progress,
}
}
#[cfg(any(feature = "blocking-network-client", feature = "async-network-client-async-std"))]
#[git_protocol::maybe_async::maybe_async]
pub async fn connect<P>(
&self,
direction: crate::remote::Direction,
progress: P,
) -> Result<Connection<'_, 'repo, Box<dyn Transport + Send>, P>, Error>
where
P: Progress,
{
let (url, version) = self.sanitized_url_and_version(direction)?;
#[cfg(feature = "blocking-network-client")]
let scheme_is_ssh = url.scheme == git_url::Scheme::Ssh;
let transport = git_protocol::transport::connect(
url,
git_protocol::transport::client::connect::Options {
version,
#[cfg(feature = "blocking-network-client")]
ssh: scheme_is_ssh
.then(|| self.repo.ssh_connect_options())
.transpose()?
.unwrap_or_default(),
},
)
.await?;
Ok(self.to_connection_with_transport(transport, progress))
}
pub fn sanitized_url_and_version(
&self,
direction: crate::remote::Direction,
) -> Result<(git_url::Url, git_protocol::transport::Protocol), Error> {
fn sanitize(mut url: git_url::Url) -> Result<git_url::Url, Error> {
if url.scheme == git_url::Scheme::File {
let mut dir = git_path::to_native_path_on_windows(url.path.as_ref());
let kind = git_discover::is_git(dir.as_ref())
.or_else(|_| {
dir.to_mut().push(git_discover::DOT_GIT_DIR);
git_discover::is_git(dir.as_ref())
})
.map_err(|err| Error::FileUrl {
source: err.into(),
url: url.clone(),
})?;
let (git_dir, _work_dir) = git_discover::repository::Path::from_dot_git_dir(
dir.clone().into_owned(),
kind,
std::env::current_dir()?,
)
.ok_or_else(|| Error::InvalidRemoteRepositoryPath {
directory: dir.into_owned(),
})?
.into_repository_and_work_tree_directories();
url.path = git_path::into_bstr(git_dir).into_owned();
}
Ok(url)
}
use git_protocol::transport::Protocol;
let version = self
.repo
.config
.resolved
.integer("protocol", None, "version")
.unwrap_or(Ok(2))
.map_err(|err| Error::UnknownProtocol { given: err.input })
.and_then(|num| {
Ok(match num {
1 => Protocol::V1,
2 => Protocol::V2,
num => {
return Err(Error::UnknownProtocol {
given: num.to_string().into(),
})
}
})
})?;
let url = self.url(direction).ok_or(Error::MissingUrl { direction })?.to_owned();
if !self.repo.config.url_scheme()?.allow(&url.scheme) {
return Err(Error::ProtocolDenied {
url: url.to_bstring(),
scheme: url.scheme,
});
}
Ok((sanitize(url)?, version))
}
}