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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
type ConfigureRemoteFn = Box<dyn FnMut(crate::Remote<'_>) -> Result<crate::Remote<'_>, crate::remote::init::Error>>;
pub struct Prepare {
repo: Option<crate::Repository>,
remote_name: Option<String>,
configure_remote: Option<ConfigureRemoteFn>,
#[cfg(any(feature = "async-network-client", feature = "blocking-network-client"))]
fetch_options: crate::remote::ref_map::Options,
#[allow(dead_code)]
url: git_url::Url,
}
#[cfg(feature = "blocking-network-client")]
pub mod fetch {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Connect(#[from] crate::remote::connect::Error),
#[error(transparent)]
PrepareFetch(#[from] crate::remote::fetch::prepare::Error),
#[error(transparent)]
Fetch(#[from] crate::remote::fetch::Error),
#[error(transparent)]
RemoteConfiguration(#[from] crate::remote::init::Error),
#[error("Default remote configured at `clone.defaultRemoteName` is invalid")]
RemoteName(#[from] crate::remote::name::Error),
#[error("Failed to load repo-local git configuration before writing")]
LoadConfig(#[from] git_config::file::init::from_paths::Error),
#[error("Failed to store configured remote in memory")]
SaveConfig(#[from] crate::remote::save::AsError),
#[error("Failed to write repository configuration to disk")]
SaveConfigIo(#[from] std::io::Error),
}
}
pub mod prepare {
use std::convert::TryInto;
use crate::{clone::Prepare, Repository};
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Init(#[from] crate::init::Error),
#[error(transparent)]
UrlParse(#[from] git_url::parse::Error),
}
impl Prepare {
pub fn new<Url, E>(
url: Url,
path: impl AsRef<std::path::Path>,
create_opts: crate::create::Options,
open_opts: crate::open::Options,
) -> Result<Self, Error>
where
Url: TryInto<git_url::Url, Error = E>,
git_url::parse::Error: From<E>,
{
let url = url.try_into().map_err(git_url::parse::Error::from)?;
let repo = crate::ThreadSafeRepository::init_opts(path, create_opts, open_opts)?.to_thread_local();
Ok(Prepare {
url,
#[cfg(any(feature = "async-network-client", feature = "blocking-network-client"))]
fetch_options: Default::default(),
repo: Some(repo),
remote_name: None,
configure_remote: None,
})
}
}
impl Prepare {
#[cfg(feature = "blocking-network-client")]
pub fn fetch_only(
&mut self,
progress: impl crate::Progress,
should_interrupt: &std::sync::atomic::AtomicBool,
) -> Result<(Repository, crate::remote::fetch::Outcome), super::fetch::Error> {
let repo = self
.repo
.as_mut()
.expect("user error: multiple calls are allowed only until it succeeds");
let remote_name = match self.remote_name.as_deref() {
Some(name) => name.to_owned(),
None => repo
.config
.resolved
.string("clone", None, "defaultRemoteName")
.map(|n| crate::remote::name::validated(n.to_string()))
.unwrap_or_else(|| Ok("origin".into()))?,
};
let mut remote = repo
.remote_at(self.url.clone())?
.with_refspec("+refs/heads/*:refs/remotes/origin/*", crate::remote::Direction::Fetch)
.expect("valid static spec");
if let Some(f) = self.configure_remote.as_mut() {
remote = f(remote)?;
}
let mut metadata = git_config::file::Metadata::from(git_config::Source::Local);
let config_path = repo.git_dir().join("config");
metadata.path = Some(config_path.clone());
let mut config =
git_config::File::from_paths_metadata(Some(metadata), Default::default())?.expect("one file to load");
remote.save_as_to(remote_name, &mut config)?;
std::fs::write(config_path, config.to_bstring())?;
let outcome = remote
.connect(crate::remote::Direction::Fetch, progress)?
.prepare_fetch(self.fetch_options.clone())?
.receive(should_interrupt)?;
let repo_config = git_features::threading::OwnShared::make_mut(&mut repo.config.resolved);
let ids_to_remove: Vec<_> = repo_config
.sections_and_ids()
.filter_map(|(s, id)| (s.meta().source == git_config::Source::Local).then(|| id))
.collect();
for id in ids_to_remove {
repo_config.remove_section_by_id(id);
}
repo_config.append(config);
Ok((self.repo.take().expect("still present"), outcome))
}
}
impl Prepare {
#[cfg(any(feature = "async-network-client", feature = "blocking-network-client"))]
pub fn with_fetch_options(mut self, opts: crate::remote::ref_map::Options) -> Self {
self.fetch_options = opts;
self
}
pub fn configure_remote(
mut self,
f: impl FnMut(crate::Remote<'_>) -> Result<crate::Remote<'_>, crate::remote::init::Error> + 'static,
) -> Self {
self.configure_remote = Some(Box::new(f));
self
}
pub fn with_remote_name(mut self, name: impl Into<String>) -> Result<Self, crate::remote::name::Error> {
self.remote_name = Some(crate::remote::name::validated(name)?);
Ok(self)
}
}
impl Prepare {
pub fn persist(mut self) -> Repository {
self.repo.take().expect("present and consumed once")
}
}
impl Drop for Prepare {
fn drop(&mut self) {
if let Some(repo) = self.repo.take() {
std::fs::remove_dir_all(repo.work_dir().unwrap_or_else(|| repo.path())).ok();
}
}
}
impl From<Prepare> for Repository {
fn from(prep: Prepare) -> Self {
prep.persist()
}
}
}