girt 0.18.0

An incremental, idiomatic Rust implementation of Git
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Fetching objects a partial clone's promisor remotes promised, in explicit batches.

use std::ops::ControlFlow;
use std::sync::atomic::AtomicBool;

use super::{Endpoint, Environment, TransferCallbacks, TransferError, is_stream_failure};
use crate::fetch::{FetchLimits, FetchWorkflowError, KnownHistory, ObjectFilter};
use crate::remote::{Direction, Remote};
use crate::{IdSet, MissingObjectsError, ObjectId, Repository};

/// What [`fetch_missing`] or [`fetch_promised`] fetched.
///
/// # Examples
///
/// ```
/// use girt::workflow::PromisorFetch;
///
/// let report = PromisorFetch::default();
/// assert!(report.fetched.is_empty() && report.already_present.is_empty());
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PromisorFetch {
    /// Requested objects the remote sent, in request order without duplicates.
    pub fetched: Vec<ObjectId>,
    /// Requested objects the repository already stored, so they were not requested.
    pub already_present: Vec<ObjectId>,
}

/// Lists the promisor remotes [`fetch_promised`] asks, in the order it asks them.
///
/// A remote is a promisor when `remote.<name>.promisor` is true or `extensions.partialClone`
/// names it, the settings `git clone --filter` writes. Like Git, remotes with `promisor = true`
/// come first in configuration order, and the remote `extensions.partialClone` names comes last.
/// Remote names that are not UTF-8 are left out, because the transfer functions take `&str` names.
///
/// # Examples
///
/// ```
/// use girt::{InitKind, ObjectFormat, Repository, workflow};
///
/// let directory = tempfile::tempdir()?;
/// let repo = Repository::init(
///     ObjectFormat::Sha1,
///     directory.path().join("r.git"),
///     InitKind::Bare,
/// )?;
/// assert!(workflow::promisor_remotes(&repo).is_empty());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn promisor_remotes(repository: &Repository) -> Vec<String> {
    let config = repository.config();
    let partial_clone = config
        .string("extensions", None, "partialClone")
        .filter(|name| !name.is_empty());
    let mut names: Vec<String> = Remote::names(config)
        .into_iter()
        .filter(|&name| Some(name) != partial_clone)
        .filter(|&name| config.boolean("remote", Some(name), "promisor") == Ok(Some(true)))
        .filter_map(|name| String::from_utf8(name.to_vec()).ok())
        .collect();
    if let Some(name) = partial_clone.and_then(|name| String::from_utf8(name.to_vec()).ok()) {
        names.push(name);
    }
    names
}

/// Fetches objects a partial clone is missing from one promisor remote.
///
/// Object reads never contact a remote: a promised object that isn't stored reads as absent. Call
/// this to fetch a batch of such objects explicitly, as Git's lazy fetch does when a command reads
/// one. Like that fetch, this sends one want per object that isn't already stored, no haves, and
/// the `blob:none` filter, so asking for a tree or commit brings its trees but not their files.
/// The received pack is validated, then installed with a `.promisor` marker. No reference
/// changes.
///
/// The remote must be a promisor (see [`promisor_remotes`]) and must allow wants for objects that
/// no reference names. Git's `upload-pack` allows them in protocol v2, which this module requests
/// by default, but not in v0 unless `uploadpack.allowAnySHA1InWant` or a related setting is on.
/// Local paths and `file://` URLs read the other repository directly and always allow them, but
/// send whole objects: a commit brings its full history.
///
/// Open a new [`Objects`](crate::Objects) reader, or reopen the repository, to read the fetched
/// objects.
///
/// # Examples
///
/// Asking a remote that isn't a promisor fails before anything is sent:
///
/// ```
/// use girt::workflow::{self, Environment, NoCallbacks, TransferError};
/// use girt::{ObjectFormat, ObjectKind};
/// # let (_directory, _, repo, _, _) = include!("../doctest/remote.rs");
///
/// let blob = ObjectFormat::Sha1.hash_object(ObjectKind::Blob, b"some file\n");
/// let result = workflow::fetch_missing(
///     &repo,
///     "origin",
///     &[blob],
///     &Environment::default(),
///     &mut NoCallbacks,
/// );
/// assert!(matches!(result, Err(TransferError::NotPromisor(name)) if name == "origin"));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// Returns [`TransferError::NotPromisor`] when the remote isn't a promisor, and
/// [`TransferError::NotFetched`] when the remote's pack lacks some requested objects; that pack is
/// still installed, so the objects it did send are readable. Otherwise returns the errors of
/// [`fetch`](super::fetch) other than reference publication failures.
/// [`TransferError::FetchFinish`] means installation failed after the pack, and possibly its
/// `.promisor` marker, may have been published without an index; readers ignore such a pack and
/// fetching again is safe. Every other error except `NotFetched` means no local change was made.
pub fn fetch_missing(
    repository: &Repository,
    remote_name: &str,
    ids: &[ObjectId],
    environment: &Environment,
    callbacks: &mut dyn TransferCallbacks,
) -> Result<PromisorFetch, TransferError> {
    if !is_promisor(repository, remote_name) {
        return Err(TransferError::NotPromisor(remote_name.to_owned()));
    }
    let (wants, already_present) = partition(repository, ids)?;
    if wants.is_empty() {
        return Ok(PromisorFetch {
            fetched: Vec::new(),
            already_present,
        });
    }
    let sent = receive(repository, remote_name, &wants, environment, callbacks)?;
    let (fetched, missing): (Vec<_>, Vec<_>) = wants.into_iter().partition(|id| sent.contains(id));
    if !missing.is_empty() {
        return Err(TransferError::NotFetched(missing));
    }
    Ok(PromisorFetch {
        fetched,
        already_present,
    })
}

/// Fetches objects a partial clone is missing from its promisor remotes, trying each in turn.
///
/// Asks the remotes [`promisor_remotes`] lists, in that order, for the objects still missing,
/// as Git's lazy fetch does. A remote that fails or sends only some objects is followed by the
/// next one. See [`fetch_missing`] for what each fetch sends and installs.
///
/// # Examples
///
/// A repository without promisor remotes has nowhere to fetch from:
///
/// ```
/// use girt::workflow::{self, Environment, NoCallbacks, TransferError};
/// use girt::{ObjectFormat, ObjectKind};
/// # let (_directory, _, repo, _, _) = include!("../doctest/remote.rs");
///
/// let blob = ObjectFormat::Sha1.hash_object(ObjectKind::Blob, b"some file\n");
/// let result =
///     workflow::fetch_promised(&repo, &[blob], &Environment::default(), &mut NoCallbacks);
/// assert!(matches!(result, Err(TransferError::NoPromisorRemote)));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// Returns [`TransferError::NoPromisorRemote`] when no remote is a promisor. When objects are
/// still missing after every remote, returns the first remote's error if any remote failed, and
/// otherwise [`TransferError::NotFetched`] listing them. Packs installed before the failure stay
/// installed.
pub fn fetch_promised(
    repository: &Repository,
    ids: &[ObjectId],
    environment: &Environment,
    callbacks: &mut dyn TransferCallbacks,
) -> Result<PromisorFetch, TransferError> {
    let remotes = promisor_remotes(repository);
    if remotes.is_empty() {
        return Err(TransferError::NoPromisorRemote);
    }
    let (mut wants, already_present) = partition(repository, ids)?;
    // Every requested object is fetched on success, so the report keeps request order.
    let fetched = wants.clone();
    let mut first_error = None;
    for remote in &remotes {
        if wants.is_empty() {
            break;
        }
        match receive(repository, remote, &wants, environment, callbacks) {
            Ok(sent) => {
                wants.retain(|id| !sent.contains(id));
            }
            Err(error) => {
                first_error.get_or_insert(error);
            }
        }
    }
    if !wants.is_empty() {
        return Err(first_error.unwrap_or(TransferError::NotFetched(wants)));
    }
    Ok(PromisorFetch {
        fetched,
        already_present,
    })
}

/// Fetches, in one batch, the objects an operation reported missing, so it can run again.
///
/// Library operations never fetch. Checkout, patch rendering, tree merges and blame fail with
/// [`MissingObjectsError`] when a partial clone lacks objects they need; checkout and patches list
/// every missing blob at once. Pass that error here, then repeat the operation with a new
/// [`Objects`](crate::Objects) reader or a reopened repository. This is [`fetch_promised`] for
/// [`MissingObjectsError::ids`].
///
/// # Examples
///
/// ```
/// use girt::workflow::{self, Environment, NoCallbacks, TransferError};
/// use girt::{MissingObjectsError, ObjectFormat, ObjectKind};
/// # let (_directory, _, repo, _, _) = include!("../doctest/remote.rs");
///
/// let blob = ObjectFormat::Sha1.hash_object(ObjectKind::Blob, b"some file\n");
/// let missing = MissingObjectsError::new(vec![(blob, ObjectKind::Blob)]);
/// let result = workflow::fetch_needed(&repo, &missing, &Environment::default(), &mut NoCallbacks);
/// assert!(matches!(result, Err(TransferError::NoPromisorRemote)));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// Returns the errors of [`fetch_promised`].
pub fn fetch_needed(
    repository: &Repository,
    missing: &MissingObjectsError,
    environment: &Environment,
    callbacks: &mut dyn TransferCallbacks,
) -> Result<PromisorFetch, TransferError> {
    let ids: Vec<ObjectId> = missing.ids().collect();
    fetch_promised(repository, &ids, environment, callbacks)
}

/// Whether `remote.<name>.promisor` is true or `extensions.partialClone` names the remote.
fn is_promisor(repository: &Repository, remote_name: &str) -> bool {
    let config = repository.config();
    let name = remote_name.as_bytes();
    config.string("extensions", None, "partialClone") == Some(name)
        || config.boolean("remote", Some(name), "promisor") == Ok(Some(true))
}

/// Splits `ids` into objects to request, deduplicated in order, and objects already stored.
fn partition(
    repository: &Repository,
    ids: &[ObjectId],
) -> Result<(Vec<ObjectId>, Vec<ObjectId>), TransferError> {
    let objects = repository
        .objects_with(crate::PackLimits::trusted())
        .map_err(TransferError::local)?;
    let format = repository.object_format();
    let mut seen = IdSet::default();
    let mut wants = Vec::new();
    let mut present = Vec::new();
    for &id in ids {
        if id.format() != format {
            return Err(TransferError::local(
                crate::fetch::FetchError::FormatMismatch {
                    remote: id.format(),
                    local: format,
                },
            ));
        }
        if !seen.insert(id) {
            continue;
        }
        if objects.contains(id).map_err(TransferError::local)? {
            present.push(id);
        } else {
            wants.push(id);
        }
    }
    Ok((wants, present))
}

/// Fetches `wants` from one remote and installs the pack, returning the wants it contained.
fn receive(
    repository: &Repository,
    remote_name: &str,
    wants: &[ObjectId],
    environment: &Environment,
    callbacks: &mut dyn TransferCallbacks,
) -> Result<IdSet, TransferError> {
    let (endpoint, _) =
        repository.endpoint(remote_name, Direction::Fetch, environment, &*callbacks)?;
    let cancel = AtomicBool::new(false);
    let limits = FetchLimits::trusted();
    // Git's lazy fetch asks for `blob:none` whatever the remote's `partialclonefilter` says.
    let mut options = crate::fetch::FetchOptions::default()
        .with_limits(limits)
        .with_filter(Some(ObjectFilter::BlobNone));
    options.explicit_wants = true;
    let select = |_: &crate::fetch::Advertisement| wants.to_vec();
    let transfer = |error| TransferError::Fetch(Box::new(FetchWorkflowError::Transfer(error)));
    let received = match endpoint {
        Endpoint::Local(path) => crate::fetch::receive_local_observed(
            path,
            select,
            &KnownHistory::default(),
            None,
            true,
            limits,
            crate::transport::TransportControl::new(&cancel),
            |message| {
                callbacks.remote_message(message);
                ControlFlow::Continue(())
            },
            |_| {},
            |_| {},
        )
        .map_err(transfer)?,
        Endpoint::Custom(custom) => {
            let mut connection = custom.connect(crate::transport::Service::UploadPack)?;
            let download = crate::fetch::stream::download(
                &mut **connection.get_mut(),
                custom.protocol,
                select,
                None,
                options,
                &cancel,
                |message| callbacks.remote_message(message),
            );
            // A service that fails after a good response says nothing about it, but its failure
            // explains a broken or invalid one.
            let finished = connection.into_inner().finish();
            let validated = download.and_then(|download| {
                download.validate_with_progress(
                    &cancel,
                    |_| ControlFlow::Continue(()),
                    |event| callbacks.validation_progress(event),
                )
            });
            if let (Err(cause), Err(error)) = (&validated, finished)
                && is_stream_failure(cause)
            {
                return Err(error.into());
            }
            validated.map_err(transfer)?
        }
        #[cfg(feature = "http")]
        Endpoint::Http(http) => {
            let runtime = super::runtime()?;
            let control = crate::transport::TransportControl::new(&cancel);
            let receive = |remote: crate::transport::http::HttpRemote,
                           callbacks: &mut dyn TransferCallbacks| {
                runtime.block_on(crate::fetch::receive_http_with_options(
                    &remote,
                    select,
                    None,
                    options.clone(),
                    control,
                    |message| callbacks.remote_message(message),
                ))
            };
            let mut download = receive(http.remote(None)?, callbacks);
            http.settle_proxy(super::fetch_status(&download));
            if matches!(
                &download,
                Err(crate::fetch::FetchError::Http(
                    crate::transport::http::HttpError::Status(401)
                ))
            ) {
                let session = http.credentials(callbacks)?;
                download = receive(http.remote(Some(session))?, callbacks);
            }
            download
                .and_then(|download| {
                    download.validate_with_progress(
                        &cancel,
                        |_| ControlFlow::Continue(()),
                        |event| callbacks.validation_progress(event),
                    )
                })
                .map_err(transfer)?
        }
        #[cfg(all(feature = "ssh", any(target_os = "macos", target_os = "linux")))]
        Endpoint::Ssh(ssh) => {
            let callbacks = std::cell::RefCell::new(&mut *callbacks);
            let download = super::runtime()?.block_on(crate::fetch::receive_ssh_with_progress(
                &ssh,
                select,
                None::<std::sync::Arc<KnownHistory>>,
                options,
                crate::transport::TransportControl::new(&cancel),
                |message| callbacks.borrow_mut().transport_message(message),
                |message| callbacks.borrow_mut().remote_message(message),
            ));
            download
                .and_then(|download| {
                    download.validate_with_progress(
                        &cancel,
                        |_| ControlFlow::Continue(()),
                        |event| callbacks.borrow_mut().validation_progress(event),
                    )
                })
                .map_err(transfer)?
        }
    };
    // Like `fetch`, an installation failure can leave the pack, and its `.promisor` marker,
    // published without an index, so it is reported as a finishing failure.
    received
        .install(repository, crate::PackLimits::trusted(), &cancel)
        .map_err(|error| {
            TransferError::FetchFinish(Box::new(crate::fetch::FetchFinishError {
                report: Box::new(crate::fetch::FetchReport {
                    updates: Vec::new(),
                    pack_bytes: received.pack_bytes(),
                    objects: received.object_count(),
                    installed: None,
                    integrity: None,
                    shallow_published: false,
                    references: Vec::new(),
                    missing: Vec::new(),
                }),
                source: Box::new(crate::fetch::FetchFinishFailure::Installation(error)),
            }))
        })?;
    Ok(received.wants().iter().copied().collect())
}