git-lfs-git 0.7.0

Git interop helpers for Git LFS: config, refs, scanners, and `.gitattributes` matching
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
//! Resolve the LFS server endpoint for a repo.
//!
//! Implements the priority chain documented in
//! `docs/api/server-discovery.md`, plus the SSH/git URL → HTTPS rewriting
//! upstream does so a `git@github.com:foo/bar.git` remote yields the
//! expected `https://github.com/foo/bar.git/info/lfs` endpoint.
//!
//! # Priority order
//!
//! 1. `GIT_LFS_URL` environment variable (matches upstream's escape hatch).
//! 2. `lfs.url` from git config — local → global → system → `.lfsconfig`.
//! 3. `remote.<name>.lfsurl` (same scopes as above).
//! 4. `remote.<name>.url` rewritten via [`derive_lfs_url`].
//!
//! `<name>` defaults to `origin` when the caller hasn't passed a remote.
//!
//! # SSH-style URLs
//!
//! `git lfs` itself only speaks HTTP(S); for SSH remotes the *protocol* is
//! still HTTPS, just inferred from the remote's host/path. Upstream also
//! supports the `git-lfs-authenticate` SSH command for handing back a
//! token; that's deferred (see NOTES.md).

use std::path::Path;

use crate::Error;
use crate::aliases;
use crate::config;

const DEFAULT_REMOTE: &str = "origin";

#[derive(Debug, thiserror::Error)]
pub enum EndpointError {
    #[error(transparent)]
    Git(#[from] Error),
    #[error("no LFS endpoint could be determined for remote {0:?}")]
    Unresolved(String),
    #[error("invalid remote URL {url:?}: {reason}")]
    InvalidUrl { url: String, reason: String },
}

/// SSH-shaped remote/endpoint URL parsed into the components `git lfs
/// env` echoes back as `  SSH=<user_and_host>:<path>`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SshInfo {
    /// Either `<user>@<host>` if a user was present, or just `<host>`.
    pub user_and_host: String,
    /// The path portion — `:foo/bar.git` from `git@host:foo/bar.git`,
    /// or `/foo/bar.git` from `ssh://host/foo/bar.git` (we keep
    /// upstream's exact form: leading `/` preserved for `ssh://`,
    /// stripped for bare SSH).
    pub path: String,
    /// Port specified in the URL (e.g. `ssh://host:22/path`). `None` for
    /// bare SSH (`git@host:path`) and for `ssh://` URLs without a port.
    /// Threaded into `git-lfs-authenticate` invocations as `-p <port>`.
    pub port: Option<String>,
}

/// LFS endpoint resolution result with the optional SSH metadata
/// upstream's `git lfs env` displays alongside the HTTPS-equivalent
/// endpoint URL.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EndpointInfo {
    pub url: String,
    pub ssh: Option<SshInfo>,
}

/// Resolve the LFS endpoint URL for `cwd` + `remote`. Pass `None` for the
/// default (`origin`, with a "single remote" fallback when origin doesn't
/// exist and exactly one other remote does).
pub fn endpoint_for_remote(cwd: &Path, remote: Option<&str>) -> Result<String, EndpointError> {
    Ok(resolve_endpoint(cwd, remote)?.url)
}

/// Like [`endpoint_for_remote`], but also returns the SSH metadata
/// when the underlying URL was SSH-shaped. Used by `git lfs env` to
/// render the `  SSH=<user_and_host>:<path>` line alongside the
/// HTTPS-equivalent endpoint.
pub fn resolve_endpoint(cwd: &Path, remote: Option<&str>) -> Result<EndpointInfo, EndpointError> {
    let caller_specified_remote = remote.is_some();
    let mut remote = remote.unwrap_or(DEFAULT_REMOTE).to_owned();

    if let Some(v) = std::env::var_os("GIT_LFS_URL") {
        let s = v.to_string_lossy().into_owned();
        if !s.is_empty() {
            return direct_endpoint(cwd, &s);
        }
    }

    if let Some(v) = config::get_effective(cwd, "lfs.url")? {
        return direct_endpoint(cwd, &v);
    }

    // When the caller didn't pin a remote name and `origin` doesn't
    // exist, fall back to the only configured remote. Mirrors
    // upstream's `git remote` discovery in `lfsfetch` and is what
    // `t-fetch.sh::fetch with no origin remote` exercises (rename
    // origin → something, then bare `git lfs fetch`).
    if !caller_specified_remote && remote_url(cwd, &remote)?.is_none() {
        let remotes = list_remotes(cwd)?;
        if remotes.len() == 1 {
            remote = remotes.into_iter().next().expect("len==1");
        }
    }

    let remote_lfsurl_key = format!("remote.{remote}.lfsurl");
    if let Some(v) = config::get_effective(cwd, &remote_lfsurl_key)? {
        return direct_endpoint(cwd, &v);
    }

    if let Some(remote_url) = remote_url(cwd, &remote)? {
        // Apply insteadOf rewrite *before* deriving the LFS suffix so
        // a `gh:org/repo` style alias resolves to the real URL first
        // and `derive_lfs_url` sees a URL it can parse.
        let rewritten = aliases::rewrite(cwd, &remote_url)?;
        return Ok(EndpointInfo {
            url: derive_lfs_url(&rewritten)?,
            ssh: parse_ssh_url(&rewritten),
        });
    }

    // Last fallback: the caller may have passed a URL directly in
    // place of a remote name (e.g. `git lfs push https://host/repo`).
    // Treat anything that looks URL-shaped as the remote URL and run
    // it through the same rewriter — same outcome as if they'd added
    // a `remote.x.url = <URL>` entry first. Bare-SSH (`git@host:path`)
    // also covers the SCP-style case the rewriter understands.
    if looks_like_url(&remote) {
        let rewritten = aliases::rewrite(cwd, &remote)?;
        return Ok(EndpointInfo {
            url: derive_lfs_url(&rewritten)?,
            ssh: parse_ssh_url(&rewritten),
        });
    }

    // No configured remote, no `lfs.url`, no URL-shaped argument.
    // Last-resort fallback: parse `.git/FETCH_HEAD` for the URL of
    // the most recent `git fetch <url>` and derive an endpoint from
    // that. This is what makes `git archive` work in a bare repo
    // that was populated via a one-off `git fetch <url> refs/...`
    // without setting up a remote — the LFS smudge filter still has
    // somewhere to look up objects (`t-no-remote` test 1). Skipped
    // when the caller pinned a remote name, since that signals an
    // intent that's incompatible with the FETCH_HEAD fallback.
    if !caller_specified_remote && let Some(url) = read_fetch_head_url(cwd)? {
        let rewritten = aliases::rewrite(cwd, &url)?;
        return Ok(EndpointInfo {
            url: derive_lfs_url(&rewritten)?,
            ssh: parse_ssh_url(&rewritten),
        });
    }

    Err(EndpointError::Unresolved(remote))
}

/// Best-effort parse of `.git/FETCH_HEAD` for the source URL of the
/// most recent `git fetch`. Each line is `<sha1>\t[not-for-merge]\t
/// branch 'X' of <url>` — we take the first match's `<url>` token.
/// Returns `None` if the file is missing, empty, or unparseable.
fn read_fetch_head_url(cwd: &Path) -> Result<Option<String>, EndpointError> {
    let git_dir = match crate::path::git_dir(cwd) {
        Ok(p) => p,
        Err(_) => return Ok(None),
    };
    let path = git_dir.join("FETCH_HEAD");
    let content = match std::fs::read_to_string(&path) {
        Ok(s) => s,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(e) => return Err(EndpointError::Git(Error::Io(e))),
    };
    for line in content.lines() {
        if let Some(idx) = line.rfind(" of ") {
            let url = line[idx + 4..].trim();
            if !url.is_empty() {
                return Ok(Some(url.to_owned()));
            }
        }
    }
    Ok(None)
}

/// Build an `EndpointInfo` from a directly-configured LFS URL value
/// (`GIT_LFS_URL`, `lfs.url`, `remote.X.lfsurl`). These values are
/// returned to callers as-is — no `.git/info/lfs` derivation — but we
/// still parse SSH metadata so `git lfs env` can echo the original
/// SSH-shaped string back. Aliases are applied first so users can
/// store something like `lfs.url = gh:org/repo` and have it resolve.
fn direct_endpoint(cwd: &Path, value: &str) -> Result<EndpointInfo, EndpointError> {
    let rewritten = aliases::rewrite(cwd, value)?;
    let ssh = parse_ssh_url(&rewritten);
    Ok(EndpointInfo {
        url: rewritten,
        ssh,
    })
}

/// `git remote` enumeration. Returns the configured remote names in
/// definition order. Used by [`endpoint_for_remote`] to fall back from
/// `origin` to the "only remote" when one exists.
fn list_remotes(cwd: &Path) -> Result<Vec<String>, Error> {
    let out = std::process::Command::new("git")
        .arg("-C")
        .arg(cwd)
        .args(["remote"])
        .output()
        .map_err(Error::Io)?;
    if !out.status.success() {
        return Ok(Vec::new());
    }
    Ok(String::from_utf8_lossy(&out.stdout)
        .lines()
        .filter(|l| !l.is_empty())
        .map(str::to_owned)
        .collect())
}

/// Quick syntactic check: does `s` look like one of the URL forms
/// [`derive_lfs_url`] recognizes? Used to decide whether to treat a
/// "remote name" argument as a literal URL.
pub fn looks_like_url(s: &str) -> bool {
    s.starts_with("http://")
        || s.starts_with("https://")
        || s.starts_with("ssh://")
        || s.starts_with("git+ssh://")
        || s.starts_with("ssh+git://")
        || s.starts_with("git://")
        || s.starts_with("file://")
        || s.contains("://")
        || s.contains('@')
}

/// Read `remote.<name>.url` from the merged git config view (any
/// scope, plus `include.path` and `GIT_CONFIG` resolution).
///
/// We don't currently honor `remote.<name>.pushurl` separately — that's a
/// minor accuracy issue for `git push`-driven LFS uploads, captured in
/// NOTES.md.
pub fn remote_url(cwd: &Path, remote: &str) -> Result<Option<String>, Error> {
    config::get_effective(cwd, &format!("remote.{remote}.url"))
}

/// Convert a clone URL into the matching LFS endpoint URL.
///
/// Rules (mirroring upstream's `NewEndpointFromCloneURL`):
/// - `https://host/path` → `https://host/path.git/info/lfs`
/// - `https://host/path.git` → `https://host/path.git/info/lfs`
/// - `ssh://[user@]host[:port]/path` → `https://host/path.git/info/lfs`
///   (port is dropped — LFS is HTTPS-only at the wire layer)
/// - `git@host:path` → `https://host/path.git/info/lfs`
/// - `git://host/path` → `https://host/path.git/info/lfs`
/// - `file://path` → returned unchanged (used by upstream test infra)
pub fn derive_lfs_url(remote_url: &str) -> Result<String, EndpointError> {
    let trimmed = remote_url.trim();
    if trimmed.is_empty() {
        return Err(EndpointError::InvalidUrl {
            url: remote_url.to_owned(),
            reason: "empty URL".into(),
        });
    }

    if let Some(rest) = trimmed.strip_prefix("file://") {
        // file:// URLs are local — pass through. The transfer/ basic
        // adapter doesn't speak file:// today, but rewriting it would be
        // worse than letting it fall over visibly.
        return Ok(format!("file://{rest}"));
    }

    // URL schemes we handle by parsing.
    if let Some(rest) = trimmed.strip_prefix("https://") {
        return Ok(append_lfs_path(&format!("https://{rest}")));
    }
    if let Some(rest) = trimmed.strip_prefix("http://") {
        return Ok(append_lfs_path(&format!("http://{rest}")));
    }
    if let Some(rest) = trimmed.strip_prefix("ssh://") {
        return ssh_to_https(rest, "ssh://");
    }
    if let Some(rest) = trimmed.strip_prefix("git+ssh://") {
        return ssh_to_https(rest, "git+ssh://");
    }
    if let Some(rest) = trimmed.strip_prefix("ssh+git://") {
        return ssh_to_https(rest, "ssh+git://");
    }
    if let Some(rest) = trimmed.strip_prefix("git://") {
        // `git://` is the bare git protocol — LFS rides on top via HTTPS.
        return Ok(append_lfs_path(&format!("https://{rest}")));
    }

    // Bare-SSH form: `[user@]host:path`. Distinguish from a Windows path
    // (`C:\…`) by requiring the part before `:` to contain a `@` or be a
    // hostname-shaped token (no backslash, no drive-letter pattern).
    if let Some((host_part, path)) = bare_ssh_split(trimmed) {
        let host = host_part.split('@').next_back().unwrap_or(host_part);
        return Ok(append_lfs_path(&format!(
            "https://{host}/{}",
            path.trim_start_matches('/'),
        )));
    }

    Err(EndpointError::InvalidUrl {
        url: remote_url.to_owned(),
        reason: "unrecognized URL form".into(),
    })
}

/// Extract the SSH metadata from a remote URL — the `<user_and_host>`
/// and `<path>` pieces `git lfs env` echoes back as
/// `  SSH=<user_and_host>:<path>`. Returns `None` for URLs that don't
/// look SSH-shaped (HTTP(S), git://, file://, plain paths).
///
/// Mirrors upstream's `EndpointFromSshUrl` / `EndpointFromBareSshUrl`
/// for the metadata fields specifically; the URL itself is rewritten
/// elsewhere (see [`derive_lfs_url`]).
pub fn parse_ssh_url(rawurl: &str) -> Option<SshInfo> {
    let trimmed = rawurl.trim();
    // Schemes upstream classifies as SSH: `ssh://`, `git+ssh://`,
    // `ssh+git://`. Plain HTTP(S) and `git://` are not SSH; `file://`
    // and bare paths aren't either.
    let ssh_rest = trimmed
        .strip_prefix("ssh://")
        .or_else(|| trimmed.strip_prefix("git+ssh://"))
        .or_else(|| trimmed.strip_prefix("ssh+git://"));
    if let Some(rest) = ssh_rest {
        let (authority, path) = rest.split_once('/').unwrap_or((rest, ""));
        if authority.is_empty() {
            return None;
        }
        // Split port from user_and_host — upstream's `EndpointFromSshUrl`
        // keeps user@host but stores the port separately so it can pass
        // `-p <port>` to ssh.
        let (user_and_host, port) = match authority.rsplit_once(':') {
            Some((host, p)) => (host.to_owned(), Some(p.to_owned())),
            None => (authority.to_owned(), None),
        };
        return Some(SshInfo {
            user_and_host,
            // Leading `/` preserved for ssh:// to match upstream.
            path: format!("/{}", path.trim_start_matches('/')),
            port,
        });
    }
    // HTTP/HTTPS/git/file aren't SSH.
    if trimmed.starts_with("http://")
        || trimmed.starts_with("https://")
        || trimmed.starts_with("git://")
        || trimmed.starts_with("file://")
    {
        return None;
    }
    // Bare-SSH form: `[user@]host:path`. Strip leading `/` from path
    // (upstream's `EndpointFromBareSshUrl` does this explicitly). Bare
    // SSH has no port — that's an `ssh://` form thing.
    let (host, path) = bare_ssh_split(trimmed)?;
    Some(SshInfo {
        user_and_host: host.to_owned(),
        path: path.trim_start_matches('/').to_owned(),
        port: None,
    })
}

/// Split `<host>:<path>` if `rawurl` looks like a bare SSH URL. Returns
/// `None` if it doesn't (e.g. a plain filesystem path like `/foo/bar` or
/// a Windows drive letter `C:\foo`).
fn bare_ssh_split(rawurl: &str) -> Option<(&str, &str)> {
    // Reject things that look like local paths.
    if rawurl.starts_with('/') || rawurl.starts_with('.') {
        return None;
    }
    if rawurl.contains('\\') {
        return None;
    }

    let (host, path) = rawurl.split_once(':')?;
    if host.is_empty() || path.is_empty() {
        return None;
    }
    // A single ASCII letter before `:` is almost certainly a Windows
    // drive letter, not a hostname. `git@C:/foo` would be malformed
    // anyway.
    if host.len() == 1 && host.chars().next().is_some_and(|c| c.is_ascii_alphabetic()) {
        return None;
    }
    Some((host, path))
}

/// Convert the post-scheme portion of an `ssh://` URL into the matching
/// HTTPS endpoint.
fn ssh_to_https(rest: &str, scheme_for_error: &str) -> Result<String, EndpointError> {
    let (authority, path) = rest.split_once('/').unwrap_or((rest, ""));
    if authority.is_empty() {
        return Err(EndpointError::InvalidUrl {
            url: format!("{scheme_for_error}{rest}"),
            reason: "missing host".into(),
        });
    }
    // Strip off any `user@` prefix.
    let host_with_port = authority.split('@').next_back().unwrap_or(authority);
    // Drop the port: `ssh://host:22/foo` → host portion is just `host`.
    let host = host_with_port.split(':').next().unwrap_or(host_with_port);
    Ok(append_lfs_path(&format!(
        "https://{host}/{}",
        path.trim_start_matches('/'),
    )))
}

/// Append the LFS protocol suffix to an HTTPS URL — `.git/info/lfs` if
/// the URL doesn't already end in `.git`, just `/info/lfs` if it does.
/// Trailing slash on the input URL is collapsed first.
fn append_lfs_path(url: &str) -> String {
    let trimmed = url.trim_end_matches('/');
    if trimmed.ends_with(".git") {
        format!("{trimmed}/info/lfs")
    } else {
        format!("{trimmed}.git/info/lfs")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ---- derive_lfs_url ---------------------------------------------------

    #[test]
    fn https_url_without_dotgit_gets_dotgit_info_lfs() {
        assert_eq!(
            derive_lfs_url("https://git-server.com/foo/bar").unwrap(),
            "https://git-server.com/foo/bar.git/info/lfs",
        );
    }

    #[test]
    fn https_url_with_dotgit_gets_just_info_lfs() {
        assert_eq!(
            derive_lfs_url("https://git-server.com/foo/bar.git").unwrap(),
            "https://git-server.com/foo/bar.git/info/lfs",
        );
    }

    #[test]
    fn http_url_is_preserved_as_http() {
        assert_eq!(
            derive_lfs_url("http://localhost:8080/foo/bar").unwrap(),
            "http://localhost:8080/foo/bar.git/info/lfs",
        );
    }

    #[test]
    fn trailing_slash_is_collapsed() {
        assert_eq!(
            derive_lfs_url("https://git-server.com/foo/bar/").unwrap(),
            "https://git-server.com/foo/bar.git/info/lfs",
        );
    }

    #[test]
    fn ssh_url_becomes_https() {
        assert_eq!(
            derive_lfs_url("ssh://git-server.com/foo/bar.git").unwrap(),
            "https://git-server.com/foo/bar.git/info/lfs",
        );
    }

    #[test]
    fn ssh_url_strips_user_and_port() {
        assert_eq!(
            derive_lfs_url("ssh://git@git-server.com:22/foo/bar.git").unwrap(),
            "https://git-server.com/foo/bar.git/info/lfs",
        );
    }

    #[test]
    fn bare_ssh_url_becomes_https() {
        assert_eq!(
            derive_lfs_url("git@github.com:user/repo.git").unwrap(),
            "https://github.com/user/repo.git/info/lfs",
        );
    }

    #[test]
    fn bare_ssh_without_user_becomes_https() {
        // `host:path/to/repo.git` is a valid bare SSH form.
        assert_eq!(
            derive_lfs_url("git-server.com:foo/bar.git").unwrap(),
            "https://git-server.com/foo/bar.git/info/lfs",
        );
    }

    #[test]
    fn git_protocol_url_becomes_https() {
        assert_eq!(
            derive_lfs_url("git://git-server.com/foo/bar.git").unwrap(),
            "https://git-server.com/foo/bar.git/info/lfs",
        );
    }

    #[test]
    fn ssh_git_variants_are_recognized() {
        for prefix in ["git+ssh", "ssh+git"] {
            let url = format!("{prefix}://git@git-server.com/foo/bar.git");
            assert_eq!(
                derive_lfs_url(&url).unwrap(),
                "https://git-server.com/foo/bar.git/info/lfs",
            );
        }
    }

    #[test]
    fn file_url_is_passed_through_unchanged() {
        assert_eq!(
            derive_lfs_url("file:///srv/repos/foo.git").unwrap(),
            "file:///srv/repos/foo.git",
        );
    }

    #[test]
    fn empty_url_errors() {
        assert!(matches!(
            derive_lfs_url(""),
            Err(EndpointError::InvalidUrl { .. }),
        ));
    }

    #[test]
    fn windows_path_is_not_misread_as_ssh() {
        // `C:\repos\foo` would otherwise look like `host:path`, but a
        // single drive letter is not a valid hostname.
        let err = derive_lfs_url("C:\\repos\\foo").unwrap_err();
        assert!(
            matches!(err, EndpointError::InvalidUrl { .. }),
            "got {err:?}"
        );
    }

    #[test]
    fn relative_path_is_rejected_not_treated_as_ssh() {
        for input in ["./relative/path", "/abs/path"] {
            let err = derive_lfs_url(input).unwrap_err();
            assert!(
                matches!(err, EndpointError::InvalidUrl { .. }),
                "input {input:?} got {err:?}"
            );
        }
    }

    // ---- parse_ssh_url ----------------------------------------------------

    #[test]
    fn ssh_metadata_for_bare_user_at_host() {
        let info = parse_ssh_url("git@github.com:user/repo.git").unwrap();
        assert_eq!(info.user_and_host, "git@github.com");
        assert_eq!(info.path, "user/repo.git");
    }

    #[test]
    fn ssh_metadata_for_bare_host_only() {
        let info = parse_ssh_url("badalias:rest").unwrap();
        assert_eq!(info.user_and_host, "badalias");
        assert_eq!(info.path, "rest");
    }

    #[test]
    fn ssh_metadata_for_ssh_scheme_keeps_leading_slash() {
        let info = parse_ssh_url("ssh://git@host.example/path/to/repo.git").unwrap();
        assert_eq!(info.user_and_host, "git@host.example");
        assert_eq!(info.path, "/path/to/repo.git");
    }

    #[test]
    fn ssh_metadata_for_ssh_scheme_drops_port_from_host() {
        let info = parse_ssh_url("ssh://git@host.example:2222/path").unwrap();
        assert_eq!(info.user_and_host, "git@host.example");
        assert_eq!(info.path, "/path");
    }

    #[test]
    fn ssh_metadata_for_https_returns_none() {
        assert!(parse_ssh_url("https://host.example/path").is_none());
        assert!(parse_ssh_url("http://host.example/path").is_none());
    }

    #[test]
    fn ssh_metadata_for_git_protocol_returns_none() {
        assert!(parse_ssh_url("git://host.example/path").is_none());
    }

    #[test]
    fn ssh_metadata_for_file_url_returns_none() {
        assert!(parse_ssh_url("file:///srv/repos/foo.git").is_none());
    }

    #[test]
    fn ssh_metadata_for_local_path_returns_none() {
        assert!(parse_ssh_url("/abs/path").is_none());
        assert!(parse_ssh_url("./relative").is_none());
    }

    // ---- endpoint_for_remote ---------------------------------------------
    //
    // Every test in this section reads `GIT_LFS_URL` indirectly via
    // `endpoint_for_remote`. cargo runs tests in parallel by default, so we
    // serialize them through a single mutex to keep one test's env-var
    // mutation from leaking into another's expectations.

    use std::sync::{Mutex, MutexGuard};
    use tempfile::TempDir;

    static ENV_LOCK: Mutex<()> = Mutex::new(());

    fn lock_env() -> MutexGuard<'static, ()> {
        // PoisonError can happen if a previous test panicked while holding
        // the lock — that's a test bug, but recovering keeps the rest of
        // the suite useful instead of cascading-failing.
        ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner())
    }

    fn fresh_repo() -> TempDir {
        let tmp = TempDir::new().unwrap();
        let s = std::process::Command::new("git")
            .args(["init", "--quiet"])
            .arg(tmp.path())
            .status()
            .unwrap();
        assert!(s.success());
        tmp
    }

    fn git_in(repo: &Path, args: &[&str]) {
        let s = std::process::Command::new("git")
            .arg("-C")
            .arg(repo)
            .args(args)
            .status()
            .unwrap();
        assert!(s.success(), "git {args:?} failed");
    }

    #[test]
    fn endpoint_prefers_explicit_lfs_url() {
        let _g = lock_env();
        unsafe { std::env::remove_var("GIT_LFS_URL") };
        let repo = fresh_repo();
        git_in(
            repo.path(),
            &["config", "--local", "lfs.url", "https://example.com/lfs"],
        );
        git_in(
            repo.path(),
            &[
                "config",
                "--local",
                "remote.origin.url",
                "git@github.com:x/y.git",
            ],
        );
        let url = endpoint_for_remote(repo.path(), None).unwrap();
        assert_eq!(url, "https://example.com/lfs");
    }

    #[test]
    fn endpoint_uses_remote_lfsurl_when_no_lfs_url() {
        let _g = lock_env();
        unsafe { std::env::remove_var("GIT_LFS_URL") };
        let repo = fresh_repo();
        git_in(
            repo.path(),
            &[
                "config",
                "--local",
                "remote.origin.lfsurl",
                "https://lfs.dev/repo",
            ],
        );
        git_in(
            repo.path(),
            &[
                "config",
                "--local",
                "remote.origin.url",
                "git@github.com:x/y.git",
            ],
        );
        let url = endpoint_for_remote(repo.path(), None).unwrap();
        assert_eq!(url, "https://lfs.dev/repo");
    }

    #[test]
    fn endpoint_derives_from_remote_url() {
        let _g = lock_env();
        unsafe { std::env::remove_var("GIT_LFS_URL") };
        let repo = fresh_repo();
        git_in(
            repo.path(),
            &[
                "config",
                "--local",
                "remote.origin.url",
                "git@github.com:x/y.git",
            ],
        );
        let url = endpoint_for_remote(repo.path(), None).unwrap();
        assert_eq!(url, "https://github.com/x/y.git/info/lfs");
    }

    #[test]
    fn endpoint_uses_named_remote_over_origin() {
        let _g = lock_env();
        unsafe { std::env::remove_var("GIT_LFS_URL") };
        let repo = fresh_repo();
        git_in(
            repo.path(),
            &[
                "config",
                "--local",
                "remote.upstream.url",
                "https://other.example.com/foo",
            ],
        );
        let url = endpoint_for_remote(repo.path(), Some("upstream")).unwrap();
        assert_eq!(url, "https://other.example.com/foo.git/info/lfs");
    }

    #[test]
    fn endpoint_reads_lfsconfig_at_repo_root() {
        let _g = lock_env();
        unsafe { std::env::remove_var("GIT_LFS_URL") };
        let repo = fresh_repo();
        // Write a .lfsconfig file (it's just a git config file).
        std::fs::write(
            repo.path().join(".lfsconfig"),
            "[lfs]\n\turl = https://from-lfsconfig.example/\n",
        )
        .unwrap();
        let url = endpoint_for_remote(repo.path(), None).unwrap();
        assert_eq!(url, "https://from-lfsconfig.example/");
    }

    #[test]
    fn endpoint_local_config_overrides_lfsconfig() {
        let _g = lock_env();
        unsafe { std::env::remove_var("GIT_LFS_URL") };
        let repo = fresh_repo();
        std::fs::write(
            repo.path().join(".lfsconfig"),
            "[lfs]\n\turl = https://from-lfsconfig.example/\n",
        )
        .unwrap();
        git_in(
            repo.path(),
            &[
                "config",
                "--local",
                "lfs.url",
                "https://from-localconfig.example/",
            ],
        );
        let url = endpoint_for_remote(repo.path(), None).unwrap();
        assert_eq!(url, "https://from-localconfig.example/");
    }

    #[test]
    fn endpoint_unresolved_when_nothing_configured() {
        let _g = lock_env();
        unsafe { std::env::remove_var("GIT_LFS_URL") };
        let repo = fresh_repo();
        let err = endpoint_for_remote(repo.path(), None).unwrap_err();
        assert!(matches!(err, EndpointError::Unresolved(_)));
    }

    #[test]
    fn endpoint_env_var_wins_over_everything() {
        let _g = lock_env();
        let repo = fresh_repo();
        git_in(
            repo.path(),
            &["config", "--local", "lfs.url", "https://lo.cal/lfs"],
        );

        let prev = std::env::var_os("GIT_LFS_URL");
        unsafe { std::env::set_var("GIT_LFS_URL", "https://from-env.example/") };
        let url = endpoint_for_remote(repo.path(), None).unwrap();
        assert_eq!(url, "https://from-env.example/");
        unsafe {
            match prev {
                Some(v) => std::env::set_var("GIT_LFS_URL", v),
                None => std::env::remove_var("GIT_LFS_URL"),
            }
        }
    }
}