git-lfs-transfer 0.7.0

Concurrent transfer queue and basic adapter for Git LFS uploads and downloads
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Basic transfer adapter — direct GET/PUT/POST against the action URLs
//! the batch endpoint hands back.
//!
//! See `docs/api/basic-transfers.md`.

use std::io::{Read as _, Write as _};
use std::path::Path;
use std::sync::Arc;

use futures::StreamExt;
use git_lfs_api::{Action, Actions};
use git_lfs_pointer::Oid;
use git_lfs_store::Store;
use reqwest::header::{CONTENT_LENGTH, RANGE};
use reqwest::{Body, Method, Response};
use serde::Serialize;
use sha2::{Digest as _, Sha256};
use tokio::sync::mpsc::UnboundedSender;
use tokio_util::io::{ReaderStream, StreamReader, SyncIoBridge};

use crate::error::TransferError;
use crate::event::Event;

/// Fallback Content-Type for raw object uploads — used when the action
/// response didn't set one and the file's content didn't match any
/// signature we sniff for. Also the value we send unconditionally when
/// `lfs.<url>.contenttype=false`. Servers/CDNs may override per-action
/// via `action.header` regardless.
const OCTET_STREAM: &str = "application/octet-stream";

#[derive(Debug, Serialize)]
struct VerifyBody<'a> {
    oid: &'a str,
    size: u64,
}

/// Stream-download `oid` from `action.href` into `store`.
///
/// Writes go to `<lfs_dir>/incomplete/<oid>.part` and only rename into
/// the object store once the bytes hash to `expected`. The partial
/// file persists across retry attempts: a previous interrupted download
/// becomes the resume point for the next call (which sends a `Range:`
/// header). Mirrors upstream's `tq/basic_download.go`.
///
/// Status-code handling:
/// - 200 OK without a Range request → fresh download, overwrites any
///   stale partial.
/// - 200 OK with a Range request → server ignored the Range; same as
///   a fresh download (truncate, write full body).
/// - 206 Partial Content → append to the existing partial; the bytes
///   from the prior attempt remain at the front of the file.
/// - 416 Requested Range Not Satisfiable → clear the partial and
///   recursively retry without `Range:` (`xfer: server rejected
///   resume … re-downloading from start`).
pub(crate) async fn download(
    http: &reqwest::Client,
    store: Arc<Store>,
    oid: &str,
    size: u64,
    action: &Action,
    events: Option<&UnboundedSender<Event>>,
) -> Result<u64, TransferError> {
    let expected = Oid::from_hex(oid)?;
    let partial_path = store.incomplete_path(expected);

    // A partial whose size meets or exceeds the expected total is
    // useless as a resume point — the bytes might be corrupt and
    // `bytes=<size>-<size-1>` is an invalid Range (the upstream
    // gitserver rejects with 400). Drop it before deciding whether to
    // resume. t-batch-storage-retries.sh test 5 part B asserts no
    // Range / 400 line in this case.
    if let Ok(m) = std::fs::metadata(&partial_path)
        && m.len() >= size
    {
        let _ = std::fs::remove_file(&partial_path);
    }
    let resume_offset: Option<u64> = std::fs::metadata(&partial_path)
        .ok()
        .map(|m| m.len())
        .filter(|&n| n > 0 && n < size);

    // Build the GET. Action headers (auth tokens, etc.) ride along on
    // every attempt; Range only when resuming.
    let mut req = http.request(Method::GET, &action.href);
    for (k, v) in &action.header {
        req = req.header(k, v);
    }
    let range_header = resume_offset.map(|offset| {
        // RFC 7233 closed range: end byte is inclusive.
        format!("bytes={offset}-{}", size.saturating_sub(1))
    });
    if let Some(range) = &range_header {
        req = req.header(RANGE, range);
        if trace_enabled() {
            // Upstream's `xfer: Attempting to resume download of %q
            // from byte %d` in basic_download.go:105. Test grep is
            // just the substring with the quoted oid.
            eprintln!(
                "xfer: Attempting to resume download of {oid:?} from byte {}",
                resume_offset.unwrap()
            );
        }
    }
    dump_verbose_request(&action.href, range_header.as_deref(), &action.header);

    let resp = req.send().await?;
    dump_verbose_response(&resp);

    // 416 + we sent Range → server rejected the resume. Clear and
    // restart without Range. Upstream's
    // `xfer: server rejected resume … re-downloading from start`
    // trace at basic_download.go:182.
    if let Some(offset) = resume_offset
        && resp.status().as_u16() == 416
    {
        if trace_enabled() {
            eprintln!(
                "xfer: server rejected resume download request for {oid:?} from byte {offset}; re-downloading from start"
            );
        }
        let _ = std::fs::remove_file(&partial_path);
        return Box::pin(download(http, store, oid, size, action, events)).await;
    }

    check_status(&resp, &action.href)?;

    // 206 only counts as "server accepted resume" when we actually
    // asked. A 200 to a Range request means the server ignored it —
    // treat like a fresh download.
    let server_resumed = matches!((resume_offset, resp.status().as_u16()), (Some(_), 206),);
    if let Some(offset) = resume_offset
        && server_resumed
        && trace_enabled()
    {
        eprintln!("xfer: server accepted resume download request: {oid:?} from byte {offset}");
    }

    let mut bytes_done: u64 = if server_resumed {
        resume_offset.unwrap_or(0)
    } else {
        0
    };
    let oid_owned = oid.to_owned();
    let events_clone = events.cloned();
    let body_stream = resp.bytes_stream().map(move |chunk| {
        if let Ok(ref c) = chunk {
            bytes_done += c.len() as u64;
            if let Some(s) = &events_clone {
                let _ = s.send(Event::Progress {
                    oid: oid_owned.clone(),
                    bytes_done,
                });
            }
        }
        chunk.map_err(std::io::Error::other)
    });

    let async_reader = StreamReader::new(body_stream);
    let mut bridge = SyncIoBridge::new(async_reader);

    let partial_path_owned = partial_path.clone();
    let store_for_blocking = store.clone();
    let total = tokio::task::spawn_blocking(move || -> Result<u64, TransferError> {
        store_for_blocking.prepare_incomplete_dir()?;
        write_partial(&partial_path_owned, server_resumed, &mut bridge)?;
        let actual = hash_file(&partial_path_owned)?;
        if actual != expected {
            return Err(TransferError::Store(
                git_lfs_store::StoreError::HashMismatch { expected, actual },
            ));
        }
        let total = std::fs::metadata(&partial_path_owned)?.len();
        store_for_blocking.commit_partial(expected, &partial_path_owned)?;
        Ok(total)
    })
    .await
    .map_err(|join_err| std::io::Error::other(join_err.to_string()))??;

    Ok(total)
}

/// Returns true when the user asked for `GIT_TRACE`. Kept in sync with
/// the same gate in `transfer.rs` — both files use this signal.
fn trace_enabled() -> bool {
    std::env::var_os("GIT_TRACE").is_some_and(|v| !v.is_empty() && v != "0")
}

/// Returns true when the user asked for `GIT_CURL_VERBOSE`.
fn verbose_enabled() -> bool {
    std::env::var_os("GIT_CURL_VERBOSE").is_some_and(|v| !v.is_empty() && v != "0")
}

/// Emit the outgoing request line + selected headers in curl-verbose
/// shape so `t-batch-storage-retries.sh` can grep `Range: bytes=`.
fn dump_verbose_request(
    url: &str,
    range: Option<&str>,
    extra_headers: &std::collections::HashMap<String, String>,
) {
    if !verbose_enabled() {
        return;
    }
    let mut err = std::io::stderr().lock();
    let _ = writeln!(err, "> GET {url}");
    if let Some(r) = range {
        let _ = writeln!(err, "> Range: {r}");
    }
    for (k, v) in extra_headers {
        let _ = writeln!(err, "> {k}: {v}");
    }
    let _ = writeln!(err);
}

/// Emit the response status line + headers. Tests grep for the curl-
/// style verbose output (`206 Partial Content`, `416 Requested Range
/// Not Satisfiable`, `Content-Range: bytes …`), so headers go through
/// `title_case_header` to undo the http crate's lowercase
/// normalization, and 416 is forced to the RFC 2616 reason phrase
/// `Requested Range Not Satisfiable` rather than RFC 7233's renamed
/// `Range Not Satisfiable` — that's what the upstream test suite is
/// written against.
fn dump_verbose_response(resp: &Response) {
    if !verbose_enabled() {
        return;
    }
    let mut err = std::io::stderr().lock();
    let code = resp.status().as_u16();
    let reason = match code {
        416 => "Requested Range Not Satisfiable",
        _ => resp.status().canonical_reason().unwrap_or(""),
    };
    let _ = writeln!(err, "< HTTP/1.1 {code} {reason}");
    for (k, v) in resp.headers() {
        if let Ok(value) = v.to_str() {
            let _ = writeln!(err, "< {}: {value}", title_case_header(k.as_str()));
        }
    }
    let _ = writeln!(err);
}

/// Title-case a hyphen-separated header name: `content-range` →
/// `Content-Range`. The http crate normalizes to lowercase, but the
/// vendored shell tests grep for capitalized header names (the form
/// curl emits in `-v` output).
fn title_case_header(name: &str) -> String {
    let mut out = String::with_capacity(name.len());
    let mut at_start = true;
    for c in name.chars() {
        if at_start {
            out.extend(c.to_uppercase());
            at_start = false;
        } else {
            out.push(c);
        }
        if c == '-' {
            at_start = true;
        }
    }
    out
}

/// Write `src`'s contents into `path`. `append == true` (server-accepted
/// resume): open with O_APPEND so the prior partial bytes stay at the
/// front. `append == false`: truncate first — either a fresh download
/// or the server ignored our `Range`.
fn write_partial(path: &Path, append: bool, src: &mut impl std::io::Read) -> std::io::Result<()> {
    let mut opts = std::fs::OpenOptions::new();
    opts.create(true).write(true);
    if append {
        opts.append(true);
    } else {
        opts.truncate(true);
    }
    let mut file = opts.open(path)?;
    let mut buf = vec![0u8; 64 * 1024];
    loop {
        let n = src.read(&mut buf)?;
        if n == 0 {
            break;
        }
        file.write_all(&buf[..n])?;
    }
    file.flush()?;
    Ok(())
}

/// Compute the SHA-256 of `path` in 64 KiB chunks. Called after a
/// successful stream to decide whether the assembled bytes match what
/// the batch endpoint promised.
fn hash_file(path: &Path) -> std::io::Result<Oid> {
    let mut file = std::fs::File::open(path)?;
    let mut hasher = Sha256::new();
    let mut buf = vec![0u8; 64 * 1024];
    loop {
        let n = file.read(&mut buf)?;
        if n == 0 {
            break;
        }
        hasher.update(&buf[..n]);
    }
    let bytes: [u8; 32] = hasher.finalize().into();
    Ok(Oid::from_bytes(bytes))
}

/// Stream-upload the local copy of `oid` to `action.href`, then call the
/// verify callback if present.
///
/// `detect_content_type` follows `lfs.<url>.contenttype` (default
/// `true`). When `true` and the batch response didn't already pin a
/// `Content-Type`, sniff the file's first 512 bytes; when `false`,
/// send `application/octet-stream` unconditionally. Mirrors upstream's
/// `tq/basic_upload.go::setContentTypeFor`.
pub(crate) async fn upload(
    http: &reqwest::Client,
    store: Arc<Store>,
    oid: &str,
    size: u64,
    actions: &Actions,
    detect_content_type: bool,
    events: Option<&UnboundedSender<Event>>,
) -> Result<(), TransferError> {
    let upload_action = actions
        .upload
        .as_ref()
        .ok_or_else(|| TransferError::Io(std::io::Error::other("missing upload action")))?;

    let oid_parsed = Oid::from_hex(oid)?;
    let path = store.object_path(oid_parsed);

    // Sniff before opening the stream — `tokio::fs::File::open` consumes
    // the path once and the sniff needs its own read. Cheap: at most
    // 512 bytes off the disk. Only runs when detection is enabled AND
    // the batch response didn't already set a Content-Type (we still
    // need to know that before deciding whether to sniff).
    let action_has_content_type = upload_action
        .header
        .keys()
        .any(|k| k.eq_ignore_ascii_case("content-type"));
    let sniffed_content_type = if !action_has_content_type && detect_content_type {
        Some(sniff_content_type(&path).await.unwrap_or(OCTET_STREAM))
    } else {
        None
    };

    let file = tokio::fs::File::open(&path).await?;

    let mut bytes_done: u64 = 0;
    let oid_owned = oid.to_owned();
    let events_clone = events.cloned();
    let stream = ReaderStream::new(file).map(move |chunk| {
        if let Ok(ref c) = chunk {
            bytes_done += c.len() as u64;
            if let Some(s) = &events_clone {
                let _ = s.send(Event::Progress {
                    oid: oid_owned.clone(),
                    bytes_done,
                });
            }
        }
        chunk
    });
    let body = Body::wrap_stream(stream);

    let mut req = http
        .request(Method::PUT, &upload_action.href)
        .header(CONTENT_LENGTH, size);
    for (k, v) in &upload_action.header {
        req = req.header(k, v);
    }
    let effective_content_type = if action_has_content_type {
        // Action header pinned a Content-Type — find it for verbose
        // logging but don't override.
        upload_action
            .header
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case("content-type"))
            .map(|(_, v)| v.as_str().to_owned())
    } else {
        let ct = sniffed_content_type.unwrap_or(OCTET_STREAM);
        req = req.header(reqwest::header::CONTENT_TYPE, ct);
        Some(ct.to_owned())
    };

    // GIT_CURL_VERBOSE: dump the request line + headers so shell tests
    // like `t-content-type.sh` can grep for the chosen Content-Type.
    // Stays cheap behind the env-var gate; no per-request cost when
    // verbose isn't asked for.
    if std::env::var_os("GIT_CURL_VERBOSE").is_some_and(|v| !v.is_empty() && v != "0") {
        let mut err = std::io::stderr().lock();
        let _ = writeln!(err, "> PUT {}", upload_action.href);
        let _ = writeln!(err, "> Content-Length: {size}");
        if let Some(ct) = &effective_content_type {
            let _ = writeln!(err, "> Content-Type: {ct}");
        }
        for (k, v) in &upload_action.header {
            if k.eq_ignore_ascii_case("content-type") {
                continue; // already printed above
            }
            let _ = writeln!(err, "> {k}: {v}");
        }
        let _ = writeln!(err);
    }

    let resp = req.body(body).send().await?;
    let status = resp.status();
    if status.as_u16() == 422 {
        // 422 = "Unprocessable Entity" — typically a CDN rejecting the
        // Content-Type we sniffed. Print upstream's three-line hint so
        // the user knows the disable knob exists. Stays best-effort:
        // we still propagate the failure, the message just nudges
        // toward a fix. `t-content-type.sh` test 3 greps for these.
        let mut err = std::io::stderr().lock();
        let _ = writeln!(
            err,
            "info: Uploading failed due to unsupported Content-Type header(s)."
        );
        let _ = writeln!(err, "info: Consider disabling Content-Type detection with:");
        let _ = writeln!(err);
        let _ = writeln!(err, "info:   $ git config lfs.contenttype false");
        let _ = writeln!(err);
    }
    check_status(&resp, &upload_action.href)?;

    if let Some(verify_action) = &actions.verify {
        verify(http, oid, size, verify_action).await?;
    }
    Ok(())
}

/// Minimal content-type sniffing. Read the first 512 bytes of `path`
/// and match against a small magic-number table — extend as future
/// tests demand. Returns `None` if the file can't be read; the caller
/// falls back to `application/octet-stream`, which is also what we
/// return for any content that doesn't match a known signature
/// (matches Go's `http.DetectContentType` fall-through).
async fn sniff_content_type(path: &std::path::Path) -> Option<&'static str> {
    use tokio::io::AsyncReadExt;
    let mut file = tokio::fs::File::open(path).await.ok()?;
    let mut buf = [0u8; 512];
    let n = file.read(&mut buf).await.ok()?;
    Some(detect_content_type_bytes(&buf[..n]))
}

/// Magic-number sniff for `bytes`. Returns the same MIME strings Go's
/// `net/http.DetectContentType` produces — `t-content-type.sh` grep
/// patterns are written against those.
///
/// Only a handful of signatures are recognized today (the test suite
/// only exercises gzip); broader coverage is deferred until a new
/// test forces it. The default is `application/octet-stream`, same
/// as upstream's fall-through.
fn detect_content_type_bytes(bytes: &[u8]) -> &'static str {
    // Gzip: `1f 8b` magic. Go labels this `application/x-gzip` (with
    // the `x-` prefix, not the RFC 6713 `application/gzip` form);
    // matching that exactly is what `t-content-type.sh` test 1
    // greps for.
    if bytes.len() >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b {
        return "application/x-gzip";
    }
    OCTET_STREAM
}

async fn verify(
    http: &reqwest::Client,
    oid: &str,
    size: u64,
    action: &Action,
) -> Result<(), TransferError> {
    let mut req = http
        .request(Method::POST, &action.href)
        .header(reqwest::header::ACCEPT, "application/vnd.git-lfs+json")
        .header(
            reqwest::header::CONTENT_TYPE,
            "application/vnd.git-lfs+json",
        );
    for (k, v) in &action.header {
        req = req.header(k, v);
    }
    let resp = req.json(&VerifyBody { oid, size }).send().await?;
    check_status(&resp, &action.href)?;
    Ok(())
}

fn check_status(resp: &Response, url: &str) -> Result<(), TransferError> {
    if resp.status().is_success() {
        Ok(())
    } else {
        let retry_after = resp
            .headers()
            .get(reqwest::header::RETRY_AFTER)
            .and_then(|v| v.to_str().ok())
            .and_then(git_lfs_api::parse_retry_after);
        Err(TransferError::ActionStatus {
            status: resp.status().as_u16(),
            url: strip_query(url).to_owned(),
            retry_after,
        })
    }
}

/// Strip the query string from `url`. Mirrors upstream's
/// `strings.SplitN(url, "?", 2)[0]` in `lfshttp.defaultError` —
/// auth tokens / signed-URL params shouldn't leak into error
/// messages or test grep patterns.
fn strip_query(url: &str) -> &str {
    url.split_once('?').map_or(url, |(base, _)| base)
}