ostrya 0.2.4

Pure-Rust async reimplementation of the ostree repository library
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
//! Ref resolution, listing, and writing.
//!
//! A ref is a loose file under `refs/` holding a 64-char hex checksum and a
//! trailing newline (65 bytes). A local ref `name` maps to `refs/heads/name`; a
//! refspec `remote:name` maps to `refs/remotes/remote/name`; a collection ref
//! maps to `refs/mirrors/collection/name`. A ref may be a relative symlink
//! aliasing another ref; opening follows the link and reads the target ref's
//! checksum. Refspec components are validated to keep resolution inside the
//! `refs/` tree.
//!
//! A revision string is a refspec, a 64-char lowercase hex checksum, an
//! abbreviated checksum -- a shorter run of lowercase hex naming the one commit
//! object whose checksum starts with it -- or any of those followed by one or
//! more `^` characters, each of which steps one generation back along the
//! commit's `parent` field.
//!
//! Writes are individually atomic -- a fresh file written, `fdatasync`-ed when
//! fsync is enabled, and renamed over the target, with the parent directories
//! created for a `/`-bearing name. Under fsync the directory holding the ref is
//! `fsync`-ed after the rename, so the name is durable together with the
//! content. Where the write created parent directories, the directory holding
//! each created name is `fsync`-ed too, deepest first, so the whole path of a
//! `/`-bearing name is durable and not the leaf entry alone. A removal and an
//! alias write carry no content of their own and sync directories alone. A
//! `None` checksum removes the ref file. A transaction queues its ref writes
//! with [`Transaction::set_ref`] and applies them at commit after object
//! publication, under the fsync policy the transaction resolved for its object
//! writes; [`Repo::set_ref_immediate`](Repo::set_ref_immediate) writes one
//! outside a transaction and reads `[core] fsync` itself.

use std::io::{Read, Write};
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};

use ostrya_core::{Checksum, RepoMode};
use rustix::fs::{AtFlags, FileType, Mode, OFlags};
use rustix::io::Errno;

use crate::error::{Error, Result};
use crate::perm;
use crate::repo::Repo;
use crate::transaction::Transaction;
use crate::traverse::read_dir_names;

/// A collection-qualified ref: a ref name optionally bound to a collection id.
///
/// A ref with a collection id maps to `refs/mirrors/<collection>/<name>`; one
/// without maps to a local `refs/heads/<name>`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CollectionRef {
    /// The collection id, or `None` for a local ref.
    pub collection_id: Option<String>,
    /// The ref name, which may contain `/`.
    pub ref_name: String,
}

impl CollectionRef {
    /// A ref bound to a collection id.
    pub fn new(collection_id: impl Into<String>, ref_name: impl Into<String>) -> CollectionRef {
        CollectionRef {
            collection_id: Some(collection_id.into()),
            ref_name: ref_name.into(),
        }
    }

    /// A local ref with no collection id.
    pub fn local(ref_name: impl Into<String>) -> CollectionRef {
        CollectionRef {
            collection_id: None,
            ref_name: ref_name.into(),
        }
    }
}

/// One ref stored as an alias: a relative symlink to another ref's file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RefAlias {
    /// The refspec the alias itself is stored under.
    pub refspec: String,
    /// The symlink target, verbatim.
    pub target: String,
}

/// A queued ref target: either a refspec or a collection ref.
enum QueuedRef {
    Ref(String),
    Collection(CollectionRef),
}

impl QueuedRef {
    /// The path under `refs/` this target writes to, rejecting a name that
    /// would escape the tree.
    fn relpath(&self) -> Result<String> {
        match self {
            QueuedRef::Ref(refspec) => refspec_to_relpath(refspec),
            QueuedRef::Collection(cref) => collection_ref_to_relpath(cref),
        }
    }
}

/// One queued ref write: a target and the checksum to point it at (`None`
/// removes the ref).
pub(crate) struct RefWrite {
    target: QueuedRef,
    checksum: Option<Checksum>,
}

impl Transaction {
    /// Queue a refspec-to-checksum write, applied at
    /// [`commit`](Transaction::commit) after object publication. A `None`
    /// checksum queues the ref's removal. The refspec is validated at commit,
    /// before any object is published.
    pub fn set_ref(&self, refspec: &str, checksum: Option<&Checksum>) {
        self.refs.lock().unwrap().push(RefWrite {
            target: QueuedRef::Ref(refspec.to_owned()),
            checksum: checksum.copied(),
        });
    }

    /// Queue a collection-ref write, applied at
    /// [`commit`](Transaction::commit). A `None` checksum queues the ref's
    /// removal.
    pub fn set_collection_ref(&self, cref: &CollectionRef, checksum: Option<&Checksum>) {
        self.refs.lock().unwrap().push(RefWrite {
            target: QueuedRef::Collection(cref.clone()),
            checksum: checksum.copied(),
        });
    }

    /// Map every queued ref to its path under `refs/`, failing on a malformed
    /// refspec. Called at the start of [`commit`](Transaction::commit) so a bad
    /// refspec fails before any object is published.
    pub(crate) fn resolve_ref_queue(&self) -> Result<Vec<(String, Option<Checksum>)>> {
        let queue = self.refs.lock().unwrap();
        queue
            .iter()
            .map(|w| Ok((w.target.relpath()?, w.checksum)))
            .collect()
    }

    /// Write the resolved refs on the blocking pool, each atomically, under the
    /// transaction's resolved fsync policy, so the whole transaction -- the
    /// per-object writes, the publication step, and the ref writes -- reads one
    /// value.
    pub(crate) async fn write_resolved_refs(
        &self,
        refs: &[(String, Option<Checksum>)],
    ) -> Result<()> {
        if refs.is_empty() {
            return Ok(());
        }
        let (fsync, _) = self.fsync_flags()?;
        let repo_mode = self.repo().mode();
        let repo_fd = self.repo().repo_fd().try_clone_to_owned()?;
        let refs = refs.to_vec();
        ostrya_rt::unblock(move || {
            for (relpath, checksum) in &refs {
                write_ref_blocking(repo_fd.as_fd(), relpath, *checksum, fsync, repo_mode)?;
            }
            Ok(())
        })
        .await
    }
}

/// The largest ref file the reader will load; a ref is 65 bytes.
const REF_READ_CAP: u64 = 4096;

impl Repo {
    /// Resolve a revision string to a commit id. A 64-char lowercase hex string
    /// resolves to itself; a shorter run of lowercase hex resolves to the one
    /// commit object whose checksum it prefixes; anything else is a refspec. A
    /// trailing run of `^` characters steps that many generations back along the
    /// resolved commit's `parent` field. When the refspec names no ref,
    /// `allow_noent` chooses between `Ok(None)` and [`Error::RefNotFound`];
    /// walking past a root commit is [`Error::NoParentCommit`] and a prefix more
    /// than one commit carries is [`Error::AmbiguousRefspec`] whatever
    /// `allow_noent` says, since neither is an absent name.
    pub async fn resolve_rev(&self, rev: &str, allow_noent: bool) -> Result<Option<Checksum>> {
        let (base, generations) = split_ancestry(rev);
        let Some(mut checksum) = self.resolve_base_rev(base, allow_noent).await? else {
            return Ok(None);
        };
        for _ in 0..generations {
            let (commit, _) = self.load_commit(&checksum).await?;
            checksum = commit.parent.ok_or(Error::NoParentCommit(checksum))?;
        }
        Ok(Some(checksum))
    }

    /// Resolve a revision with no ancestry suffix: a bare checksum, an
    /// abbreviated checksum, or a refspec, tried in that order.
    ///
    /// A 64-character name is a checksum in lowercase hex alone, so an
    /// uppercase or mixed-case name of that length is read as a refspec
    /// (`docs/format-reference.md`, "Revision syntax"). The checksum parser
    /// keeps its tolerance where a checksum is read as stored bytes, which is
    /// ref file content and delta metadata.
    ///
    /// A shorter run of lowercase hex is an abbreviated checksum, and it stands
    /// ahead of the ref store: a name the store carries as a ref resolves to the
    /// commit it prefixes rather than to that ref's target. A prefix no commit
    /// object carries falls through to the ref store, so a hex name is a ref
    /// name for as long as no commit begins with it.
    async fn resolve_base_rev(&self, rev: &str, allow_noent: bool) -> Result<Option<Checksum>> {
        if let Ok(checksum) = Checksum::from_hex_lower(rev) {
            return Ok(Some(checksum));
        }

        if is_abbreviated_checksum(rev) {
            let repo = self.clone();
            let prefix = rev.to_owned();
            match ostrya_rt::unblock(move || match_abbreviated(repo.objects_fd(), &prefix)).await? {
                AbbrevMatch::One(checksum) => return Ok(Some(checksum)),
                AbbrevMatch::Ambiguous => return Err(Error::AmbiguousRefspec(rev.to_owned())),
                AbbrevMatch::None => {}
            }
        }

        match self.resolve_ref_tip(rev).await? {
            Some(checksum) => Ok(Some(checksum)),
            None if allow_noent => Ok(None),
            None => Err(Error::RefNotFound(rev.to_owned())),
        }
    }

    /// The commit a refspec names, reading the ref store alone: no checksum
    /// syntax, no abbreviated checksum, and no ancestry suffix. This is the
    /// ref-store read for a caller that holds a ref name rather than a
    /// revision -- a pull's local tip, the metadata ref a summary chains, and
    /// the CLI's alias-target check, an alias recording a name. `None` says the
    /// store carries no such ref.
    pub async fn resolve_ref_tip(&self, refspec: &str) -> Result<Option<Checksum>> {
        let relpath = refspec_to_relpath(refspec)?;
        let repo = self.clone();
        let bytes = ostrya_rt::unblock(move || read_ref_file(repo.repo_fd(), &relpath)).await?;
        match bytes {
            Some(bytes) => Ok(Some(parse_ref_content(&bytes)?)),
            None => Ok(None),
        }
    }

    /// List local refs (under `refs/heads`) as (name, commit) pairs, sorted by
    /// name. `prefix`, when given, keeps only the ref equal to it or nested
    /// under it.
    pub async fn list_refs(&self, prefix: Option<&str>) -> Result<Vec<(String, Checksum)>> {
        let repo = self.clone();
        let mut refs = ostrya_rt::unblock(move || collect_heads(&repo)).await?;
        if let Some(prefix) = prefix {
            let nested = format!("{prefix}/");
            refs.retain(|(name, _)| name == prefix || name.starts_with(&nested));
        }
        refs.sort_by(|a, b| a.0.cmp(&b.0));
        Ok(refs)
    }

    /// List remote refs (under `refs/remotes`) as `(refspec, commit)` pairs,
    /// sorted by refspec. The first path component under `refs/remotes` is the
    /// remote name, so each ref is named by its `remote:name` refspec.
    pub async fn list_remote_refs(&self) -> Result<Vec<(String, Checksum)>> {
        let repo = self.clone();
        let mut refs = ostrya_rt::unblock(move || collect_remotes(&repo)).await?;
        refs.sort_by(|a, b| a.0.cmp(&b.0));
        Ok(refs)
    }

    /// List collection-mirror refs (under `refs/mirrors`) as
    /// `(collection_id, ref_name, commit)` triples. The first path component
    /// under `refs/mirrors` is the collection id; the remainder is the ref
    /// name, which may contain `/`. Results are unsorted; callers that need a
    /// stable order sort them.
    pub async fn list_mirror_refs(&self) -> Result<Vec<(String, String, Checksum)>> {
        let repo = self.clone();
        ostrya_rt::unblock(move || collect_mirrors(&repo)).await
    }

    /// List the local and remote refs stored as aliases, sorted by refspec. An
    /// alias whose target names no ref is listed too, since the listing reads
    /// the link and not the ref behind it.
    pub async fn list_ref_aliases(&self) -> Result<Vec<RefAlias>> {
        let repo = self.clone();
        let mut aliases = ostrya_rt::unblock(move || collect_aliases(&repo)).await?;
        aliases.sort_by(|a, b| a.refspec.cmp(&b.refspec));
        Ok(aliases)
    }

    /// Probe one path below `refs/`, as a listing prefix names it, reporting the
    /// one condition that ends an enumeration: a component above the last that is
    /// not a directory, which is `ENOTDIR`.
    ///
    /// A path naming nothing is `Ok(())`, since a prefix matching no ref
    /// enumerates nothing, and so is every other probe failure -- the prefix
    /// filters the listing in that case rather than ending it. The last component
    /// is not followed, so a path naming an alias symlink is the link itself.
    pub async fn check_refs_path(&self, relpath: &str) -> Result<()> {
        let path = format!("refs/{relpath}");
        let repo = self.clone();
        ostrya_rt::unblock(move || {
            match rustix::fs::statat(repo.repo_fd(), &path, AtFlags::SYMLINK_NOFOLLOW) {
                Err(Errno::NOTDIR) => Err(Error::Io(Errno::NOTDIR.into())),
                _ => Ok(()),
            }
        })
        .await
    }

    /// Write one ref outside a transaction, atomically. A `None` checksum
    /// removes the ref file. The write follows the same tmpfile, `fdatasync`,
    /// rename, directory `fsync` sequence a transaction uses, honoring
    /// `[core] fsync`.
    pub async fn set_ref_immediate(
        &self,
        refspec: &str,
        checksum: Option<&Checksum>,
    ) -> Result<()> {
        let relpath = refspec_to_relpath(refspec)?;
        self.write_ref_relpath(relpath, checksum).await
    }

    /// Write one collection ref outside a transaction, atomically, the way
    /// [`set_ref_immediate`](Repo::set_ref_immediate) writes a refspec. A
    /// `None` checksum removes the ref file.
    pub async fn set_collection_ref_immediate(
        &self,
        cref: &CollectionRef,
        checksum: Option<&Checksum>,
    ) -> Result<()> {
        let relpath = collection_ref_to_relpath(cref)?;
        self.write_ref_relpath(relpath, checksum).await
    }

    async fn write_ref_relpath(&self, relpath: String, checksum: Option<&Checksum>) -> Result<()> {
        let fsync = self.config().fsync()?;
        let repo_mode = self.mode();
        let checksum = checksum.copied();
        let repo_fd = self.repo_fd().try_clone_to_owned()?;
        ostrya_rt::unblock(move || {
            write_ref_blocking(repo_fd.as_fd(), &relpath, checksum, fsync, repo_mode)
        })
        .await
    }

    /// Write one ref as an alias of another: a relative symlink from
    /// `refspec`'s file to `target`'s, replacing whatever `refspec` named
    /// before. Both refspecs are validated; neither ref need already exist,
    /// since the link records a name and not a checksum. The write honors
    /// `[core] fsync`, which for a symlink reaches the directory holding it.
    pub async fn set_ref_alias_immediate(&self, refspec: &str, target: &str) -> Result<()> {
        let fsync = self.config().fsync()?;
        let repo_mode = self.mode();
        let relpath = refspec_to_relpath(refspec)?;
        let link = relative_link(&relpath, &refspec_to_relpath(target)?);
        let repo_fd = self.repo_fd().try_clone_to_owned()?;
        ostrya_rt::unblock(move || {
            write_alias_blocking(repo_fd.as_fd(), &relpath, &link, fsync, repo_mode)
        })
        .await
    }
}

/// Split a revision string into its base and the number of trailing `^`
/// characters, each of which asks for one more generation of ancestry.
fn split_ancestry(rev: &str) -> (&str, usize) {
    let base = rev.trim_end_matches('^');
    (base, rev.len() - base.len())
}

/// Whether a revision names a commit by an abbreviated checksum: a run of one
/// to 63 lowercase hex characters. A 64-character run is the checksum itself,
/// and one uppercase character makes the name a refspec, the case rule a full
/// checksum carries (`docs/format-reference.md`, "Revision syntax").
fn is_abbreviated_checksum(rev: &str) -> bool {
    !rev.is_empty()
        && rev.len() < 64
        && rev
            .bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}

/// What an abbreviated checksum matched among the commit objects present.
enum AbbrevMatch {
    /// No commit object's checksum starts with the prefix.
    None,
    /// Exactly one does.
    One(Checksum),
    /// More than one does.
    Ambiguous,
}

/// Scan the loose commit objects for the ones whose checksum starts with
/// `prefix`.
///
/// The match set holds commit objects alone: a `dirtree`, a `dirmeta`, or a file
/// object sharing the prefix takes no part, and a prefix only such an object
/// carries matches nothing. The `objects/<xx>/` fanout carries the first two
/// characters of a checksum. A prefix of two or more characters names one
/// fanout with its first two characters, and that directory is opened by name.
/// A one-character prefix scans the sixteen fanouts whose name starts with it,
/// found by listing `objects/`.
fn match_abbreviated(objects_fd: BorrowedFd<'_>, prefix: &str) -> Result<AbbrevMatch> {
    let mut found: Option<Checksum> = None;
    if prefix.len() >= 2 {
        let (fanout, within) = prefix.split_at(2);
        if scan_fanout(objects_fd, fanout, within, &mut found)? {
            return Ok(AbbrevMatch::Ambiguous);
        }
    } else {
        for fanout in read_dir_names(objects_fd)? {
            // Object fanout directories are exactly two hex characters; anything
            // else under `objects/` is not a loose-object fanout.
            if fanout.len() != 2 || !fanout.bytes().all(|b| b.is_ascii_hexdigit()) {
                continue;
            }
            if !fanout.starts_with(prefix) {
                continue;
            }
            if scan_fanout(objects_fd, &fanout, "", &mut found)? {
                return Ok(AbbrevMatch::Ambiguous);
            }
        }
    }
    Ok(match found {
        Some(checksum) => AbbrevMatch::One(checksum),
        None => AbbrevMatch::None,
    })
}

/// Scan one `objects/<fanout>/` directory for the commit objects whose name
/// starts with `within`, recording each in `found`. An absent fanout directory
/// holds no match. The returned flag reports the prefix ambiguous.
fn scan_fanout(
    objects_fd: BorrowedFd<'_>,
    fanout: &str,
    within: &str,
    found: &mut Option<Checksum>,
) -> Result<bool> {
    let dir = match rustix::fs::openat(
        objects_fd,
        fanout,
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
        Mode::empty(),
    ) {
        Ok(fd) => fd,
        Err(Errno::NOENT) => return Ok(false),
        Err(e) => return Err(Error::Io(e.into())),
    };
    for entry in read_dir_names(dir.as_fd())? {
        let Some(checksum) = matching_commit(fanout, &entry, within) else {
            continue;
        };
        // The fanout carries a checksum's first two characters, so two entries
        // are two commits and a second match is ambiguity.
        if found.replace(checksum).is_some() {
            return Ok(true);
        }
    }
    Ok(false)
}

/// The checksum of one `objects/<fanout>/<rest>.commit` entry whose name starts
/// with `within`, or `None` for any other entry.
fn matching_commit(fanout: &str, entry: &str, within: &str) -> Option<Checksum> {
    let (rest, ext) = entry.rsplit_once('.')?;
    if ext != "commit" || rest.len() != 62 || !rest.starts_with(within) {
        return None;
    }
    Checksum::from_hex_lower(&format!("{fanout}{rest}")).ok()
}

/// The symlink body an alias at `from_relpath` needs to point at
/// `to_relpath`, both relative to the repository root: the shared leading
/// components are dropped, one `..` is emitted per component the alias's own
/// directory holds beyond them, and the target's remaining components follow.
fn relative_link(from_relpath: &str, to_relpath: &str) -> String {
    let from: Vec<&str> = from_relpath.split('/').collect();
    let to: Vec<&str> = to_relpath.split('/').collect();
    // The alias's own name is not part of the directory the link is read in,
    // and the target's own name is never a shared component.
    let from_dir = &from[..from.len() - 1];
    let common = from_dir
        .iter()
        .zip(&to[..to.len() - 1])
        .take_while(|(a, b)| a == b)
        .count();
    let mut parts = vec![".."; from_dir.len() - common];
    parts.extend_from_slice(&to[common..]);
    parts.join("/")
}

/// Collect every ref under `refs/heads`, walking subdirectories.
fn collect_heads(repo: &Repo) -> Result<Vec<(String, Checksum)>> {
    let Some(heads) = open_refs_dir(repo, "refs/heads")? else {
        return Ok(Vec::new());
    };
    let mut out = Vec::new();
    walk_ref_dir(heads.as_fd(), "", &mut |entry| {
        if let Some(bytes) = read_ref_file(entry.dir, entry.name)? {
            out.push((entry.path.to_owned(), parse_ref_content(&bytes)?));
        }
        Ok(())
    })?;
    Ok(out)
}

/// Collect every ref under `refs/remotes`, naming each by its `remote:name`
/// refspec: the first path component is the remote, the remainder the ref name.
fn collect_remotes(repo: &Repo) -> Result<Vec<(String, Checksum)>> {
    let Some(remotes) = open_refs_dir(repo, "refs/remotes")? else {
        return Ok(Vec::new());
    };
    let mut out = Vec::new();
    walk_ref_dir(remotes.as_fd(), "", &mut |entry| {
        let Some(bytes) = read_ref_file(entry.dir, entry.name)? else {
            return Ok(());
        };
        let checksum = parse_ref_content(&bytes)?;
        // A file directly under refs/remotes names no remote; ignore it, the
        // way collect_mirrors ignores one naming no collection.
        if let Some((remote, ref_name)) = entry.path.split_once('/') {
            out.push((format!("{remote}:{ref_name}"), checksum));
        }
        Ok(())
    })?;
    Ok(out)
}

/// Collect the aliases under `refs/heads` and `refs/remotes`, naming each the
/// way the corresponding listing names a ref.
fn collect_aliases(repo: &Repo) -> Result<Vec<RefAlias>> {
    let mut out = Vec::new();
    if let Some(heads) = open_refs_dir(repo, "refs/heads")? {
        walk_ref_dir(heads.as_fd(), "", &mut |entry| {
            if let Some(target) = read_alias_target(&entry)? {
                out.push(RefAlias {
                    refspec: entry.path.to_owned(),
                    target,
                });
            }
            Ok(())
        })?;
    }
    if let Some(remotes) = open_refs_dir(repo, "refs/remotes")? {
        walk_ref_dir(remotes.as_fd(), "", &mut |entry| {
            if let Some(target) = read_alias_target(&entry)?
                && let Some((remote, ref_name)) = entry.path.split_once('/')
            {
                out.push(RefAlias {
                    refspec: format!("{remote}:{ref_name}"),
                    target,
                });
            }
            Ok(())
        })?;
    }
    Ok(out)
}

/// Open one directory under `refs/`, or `None` when it does not exist.
fn open_refs_dir(repo: &Repo, relpath: &str) -> Result<Option<OwnedFd>> {
    match rustix::fs::openat(
        repo.repo_fd(),
        relpath,
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
        Mode::empty(),
    ) {
        Ok(fd) => Ok(Some(fd)),
        Err(Errno::NOENT) => Ok(None),
        Err(e) => Err(Error::Io(e.into())),
    }
}

/// Collect every ref under `refs/mirrors`, splitting each into its collection
/// id (the first path component) and ref name (the remainder).
fn collect_mirrors(repo: &Repo) -> Result<Vec<(String, String, Checksum)>> {
    let Some(mirrors) = open_refs_dir(repo, "refs/mirrors")? else {
        return Ok(Vec::new());
    };
    let mut out = Vec::new();
    walk_ref_dir(mirrors.as_fd(), "", &mut |entry| {
        let Some(bytes) = read_ref_file(entry.dir, entry.name)? else {
            return Ok(());
        };
        let checksum = parse_ref_content(&bytes)?;
        // A mirror ref is <collection>/<ref>; the collection id is a single
        // component, the ref name is everything after the first slash. A file
        // directly under refs/mirrors has no collection component; ignore it,
        // matching the tool's collection-qualified layout.
        if let Some((collection, ref_name)) = entry.path.split_once('/') {
            out.push((collection.to_owned(), ref_name.to_owned(), checksum));
        }
        Ok(())
    })?;
    Ok(out)
}

/// One entry a ref-tree walk reports.
pub(crate) struct RefEntry<'a> {
    /// The open directory holding the entry.
    pub(crate) dir: BorrowedFd<'a>,
    /// The entry's name in that directory.
    pub(crate) name: &'a str,
    /// The path walked to reach the entry, from the walk's root.
    pub(crate) path: &'a str,
    /// The entry's type, classified without following symlinks.
    pub(crate) file_type: FileType,
}

/// Walk a directory under `refs/`, descending into each subdirectory and
/// reporting every other entry to `visit`.
///
/// Entries are classified with `SYMLINK_NOFOLLOW`, so a real directory alone is
/// descended into and an alias carries [`FileType::Symlink`] whatever it names.
/// A link naming a directory is reported rather than joined to the walk: a
/// caller reading it as a ref fails the read with `EISDIR`, and one reading it
/// as an alias reads the link. A name that is not UTF-8 is skipped, as is one
/// removed between the directory read and the classification.
pub(crate) fn walk_ref_dir(
    dir: BorrowedFd<'_>,
    prefix: &str,
    visit: &mut impl FnMut(RefEntry<'_>) -> Result<()>,
) -> Result<()> {
    for name in read_dir_names(dir)? {
        let stat = match rustix::fs::statat(dir, name.as_str(), AtFlags::SYMLINK_NOFOLLOW) {
            Ok(stat) => stat,
            Err(Errno::NOENT) => continue, // removed between readdir and stat
            Err(e) => return Err(Error::Io(e.into())),
        };
        let path = if prefix.is_empty() {
            name.clone()
        } else {
            format!("{prefix}/{name}")
        };
        let file_type = FileType::from_raw_mode(stat.st_mode);
        if file_type == FileType::Directory {
            let sub = rustix::fs::openat(
                dir,
                name.as_str(),
                OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
                Mode::empty(),
            )
            .map_err(|e| Error::Io(e.into()))?;
            walk_ref_dir(sub.as_fd(), &path, visit)?;
        } else {
            visit(RefEntry {
                dir,
                name: &name,
                path: &path,
                file_type,
            })?;
        }
    }
    Ok(())
}

/// The symlink target of an alias entry, verbatim. `None` for an entry that is
/// not a symlink and for a target that is not UTF-8.
fn read_alias_target(entry: &RefEntry<'_>) -> Result<Option<String>> {
    if entry.file_type != FileType::Symlink {
        return Ok(None);
    }
    let target = rustix::fs::readlinkat(entry.dir, entry.name, Vec::new())
        .map_err(|e| Error::Io(e.into()))?;
    Ok(target.into_string().ok())
}

/// Read a ref file relative to `dir`, following alias symlinks. `None` when the
/// file does not exist.
fn read_ref_file(
    dir: rustix::fd::BorrowedFd<'_>,
    relpath: &str,
) -> std::io::Result<Option<Vec<u8>>> {
    let fd = match rustix::fs::openat(
        dir,
        relpath,
        OFlags::RDONLY | OFlags::CLOEXEC,
        Mode::empty(),
    ) {
        Ok(fd) => fd,
        Err(Errno::NOENT) => return Ok(None),
        Err(e) => return Err(e.into()),
    };
    let mut buf = Vec::new();
    std::fs::File::from(fd)
        .take(REF_READ_CAP)
        .read_to_end(&mut buf)?;
    Ok(Some(buf))
}

/// Parse a ref file's content: a hex checksum with trailing whitespace.
fn parse_ref_content(bytes: &[u8]) -> Result<Checksum> {
    let text = std::str::from_utf8(bytes)
        .map_err(|_| Error::InvalidFormat("ref content is not valid UTF-8".into()))?;
    Ok(Checksum::from_hex(text.trim())?)
}

/// Whether a refspec names a path under `refs/`: a ref name, optionally
/// preceded by a `<remote>:` prefix. A refspec that would escape the tree is
/// [`Error::InvalidRefspec`], carrying the refspec as given.
pub fn validate_refspec(refspec: &str) -> Result<()> {
    refspec_to_relpath(refspec).map(drop)
}

/// Map a refspec to its path under `refs/`, rejecting anything that would
/// escape the tree.
fn refspec_to_relpath(refspec: &str) -> Result<String> {
    if let Some((remote, name)) = refspec.split_once(':') {
        if !is_component(remote) || !is_ref_path(name) {
            return Err(Error::InvalidRefspec(refspec.to_owned()));
        }
        Ok(format!("refs/remotes/{remote}/{name}"))
    } else {
        if !is_ref_path(refspec) {
            return Err(Error::InvalidRefspec(refspec.to_owned()));
        }
        Ok(format!("refs/heads/{refspec}"))
    }
}

/// The same rule as [`Error::InvalidRefspec`]-bearing validation, for a bare
/// ref name.
///
/// An HTTP pull applies it before a ref name becomes a request path: a
/// traversal component there asks the server for a different resource, the way
/// it would name a different file here.
pub(crate) fn check_ref_path(name: &str) -> Result<()> {
    if is_ref_path(name) {
        Ok(())
    } else {
        Err(Error::InvalidRefspec(name.to_owned()))
    }
}

/// A ref name may contain `/` but no empty, `.`, or `..` components, and no
/// interior NUL.
fn is_ref_path(name: &str) -> bool {
    !name.is_empty() && name.split('/').all(is_component)
}

/// A single path component: non-empty, not a traversal, no slash or NUL.
fn is_component(component: &str) -> bool {
    !(component.is_empty()
        || component == "."
        || component == ".."
        || component.contains('/')
        || component.contains('\0'))
}

/// Map a collection ref to its path under `refs/`. A collection id places the
/// ref under `refs/mirrors/<collection>/`; a `None` id is a local
/// `refs/heads/` ref. The collection id is a single component (dots allowed, no
/// slash or traversal).
fn collection_ref_to_relpath(cref: &CollectionRef) -> Result<String> {
    let name = &cref.ref_name;
    match &cref.collection_id {
        Some(collection_id) => {
            if !is_component(collection_id) || !is_ref_path(name) {
                return Err(Error::InvalidRefspec(format!("{collection_id}:{name}")));
            }
            Ok(format!("refs/mirrors/{collection_id}/{name}"))
        }
        None => {
            if !is_ref_path(name) {
                return Err(Error::InvalidRefspec(name.to_owned()));
            }
            Ok(format!("refs/heads/{name}"))
        }
    }
}

/// The permission bits forced on a ref file, independent of the umask, matching
/// the tool's `0644` ref files.
const REF_FILE_MODE: u32 = 0o644;
/// The request mode for a created ref parent directory, reduced by the umask
/// (the tool's ref subdirectories are `0755` under a `022` umask). In a
/// `bare-user-shared` repository a parent directory the write creates is then
/// forced to [`perm::SHARED_DIR_MODE`], so every member of the repository group
/// publishes a ref under it.
const REF_DIR_MODE: u32 = 0o777;

/// Write or remove one ref file relative to `repo_fd`, atomically.
///
/// `Some(checksum)` writes the 65-byte `<hex>\n` content to a fresh temp file
/// in the target's parent directory (created as needed), `fdatasync`-es it when
/// `fsync` is set, and renames it over the target. `None` unlinks the ref,
/// treating an already-absent file as success. Under `fsync` the directory
/// holding the ref is `fsync`-ed after the rename or the unlink, so the name
/// the operation created or removed is durable and not only the file's content,
/// and [`sync_created_ref_parents`] then makes durable the name of every parent
/// directory this write created.
fn write_ref_blocking(
    repo_fd: BorrowedFd<'_>,
    relpath: &str,
    checksum: Option<Checksum>,
    fsync: bool,
    repo_mode: RepoMode,
) -> Result<()> {
    let Some(checksum) = checksum else {
        return match rustix::fs::unlinkat(repo_fd, relpath, AtFlags::empty()) {
            Ok(()) => {
                if fsync {
                    sync_ref_parent(repo_fd, relpath)?;
                }
                Ok(())
            }
            Err(Errno::NOENT) => Ok(()),
            Err(e) => Err(e.into()),
        };
    };

    let created = create_ref_parents(repo_fd, relpath, repo_mode)?;
    let content = format!("{}\n", checksum.to_hex());
    let tmp = format!(
        "{relpath}.tmp-{}-{}",
        std::process::id(),
        crate::write::unique()
    );
    let fd = rustix::fs::openat(
        repo_fd,
        tmp.as_str(),
        OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::CLOEXEC | OFlags::NOFOLLOW,
        Mode::from_raw_mode(REF_FILE_MODE),
    )?;
    let write_and_rename = || -> Result<()> {
        let mut file = std::fs::File::from(fd);
        file.write_all(content.as_bytes())?;
        file.flush()?;
        rustix::fs::fchmod(file.as_fd(), Mode::from_raw_mode(REF_FILE_MODE))?;
        if fsync {
            rustix::fs::fdatasync(file.as_fd())?;
        }
        drop(file);
        rustix::fs::renameat(repo_fd, tmp.as_str(), repo_fd, relpath)?;
        if fsync {
            sync_ref_parent(repo_fd, relpath)?;
            sync_created_ref_parents(repo_fd, &created)?;
        }
        Ok(())
    };
    write_and_rename().inspect_err(|_| {
        let _ = rustix::fs::unlinkat(repo_fd, tmp.as_str(), AtFlags::empty());
    })
}

/// Write one alias symlink relative to `repo_fd`, atomically.
///
/// The link is created under a fresh temp name in the target's parent
/// directory, then renamed over the target, so an existing ref file or an
/// existing alias is replaced in one step. A symlink carries no content of its
/// own to sync, so `fsync` reaches the directory holding the link and the
/// directory holding each parent this write created.
fn write_alias_blocking(
    repo_fd: BorrowedFd<'_>,
    relpath: &str,
    link: &str,
    fsync: bool,
    repo_mode: RepoMode,
) -> Result<()> {
    let created = create_ref_parents(repo_fd, relpath, repo_mode)?;
    let tmp = format!(
        "{relpath}.tmp-{}-{}",
        std::process::id(),
        crate::write::unique()
    );
    rustix::fs::symlinkat(link, repo_fd, tmp.as_str())?;
    rustix::fs::renameat(repo_fd, tmp.as_str(), repo_fd, relpath).inspect_err(|_| {
        let _ = rustix::fs::unlinkat(repo_fd, tmp.as_str(), AtFlags::empty());
    })?;
    if fsync {
        sync_ref_parent(repo_fd, relpath)?;
        sync_created_ref_parents(repo_fd, &created)?;
    }
    Ok(())
}

/// `fsync` the directory holding the ref at `relpath`, making a rename or an
/// unlink of that name durable. A refspec always maps below `refs/`, so the
/// path carries a parent; a bare name would sync the repository root.
fn sync_ref_parent(repo_fd: BorrowedFd<'_>, relpath: &str) -> Result<()> {
    let parent = relpath.rsplit_once('/').map_or(".", |(dir, _)| dir);
    let dir = rustix::fs::openat(
        repo_fd,
        parent,
        OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
        Mode::empty(),
    )?;
    rustix::fs::fsync(dir.as_fd())?;
    Ok(())
}

/// Create the parent directories of a ref path, idempotently. Every component
/// but the last is created; existing directories are left in place. Returns the
/// paths this call created, shallowest first, for
/// [`sync_created_ref_parents`] to make durable.
///
/// In a `bare-user-shared` repository each directory this call creates is
/// forced to [`perm::SHARED_DIR_MODE`]. A directory that already stands keeps
/// the mode and the group it has.
fn create_ref_parents(
    repo_fd: BorrowedFd<'_>,
    relpath: &str,
    repo_mode: RepoMode,
) -> Result<Vec<String>> {
    let mut created = Vec::new();
    let mut acc = String::new();
    let mut components: Vec<&str> = relpath.split('/').collect();
    components.pop(); // the final component is the ref file itself
    for component in components {
        if !acc.is_empty() {
            acc.push('/');
        }
        acc.push_str(component);
        match rustix::fs::mkdirat(repo_fd, acc.as_str(), Mode::from_raw_mode(REF_DIR_MODE)) {
            Ok(()) => {
                perm::force_created_dir(repo_fd, acc.as_str(), repo_mode)?;
                created.push(acc.clone());
            }
            Err(Errno::EXIST) => {}
            Err(e) => return Err(e.into()),
        }
    }
    Ok(created)
}

/// `fsync` the directory that holds each entry [`create_ref_parents`] created,
/// deepest first.
///
/// A `mkdirat` adds a name to the directory it is called in, so the directory
/// made durable for a created `refs/heads/deep/nest` is `refs/heads/deep`, the
/// one that holds the `nest` entry. Syncing the created directory's own file
/// descriptor makes its contents durable and leaves its name unrecorded.
///
/// The order is child before parent, the order the object fanout uses: the ref
/// file's own name, which [`sync_ref_parent`] makes durable, then the name of
/// the directory holding it, then the name of the directory above that. A crash
/// part way through therefore leaves a prefix of the path recorded and never a
/// directory entry naming a directory whose own contents are unrecorded.
///
/// A directory the call found already in place is not synced; its name is
/// already durable.
fn sync_created_ref_parents(repo_fd: BorrowedFd<'_>, created: &[String]) -> Result<()> {
    for dir in created.iter().rev() {
        sync_ref_parent(repo_fd, dir)?;
    }
    Ok(())
}

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

    #[test]
    fn refspec_maps_local_and_remote() {
        assert_eq!(
            refspec_to_relpath("test/main").unwrap(),
            "refs/heads/test/main"
        );
        assert_eq!(
            refspec_to_relpath("origin:test/main").unwrap(),
            "refs/remotes/origin/test/main"
        );
    }

    #[test]
    fn refspec_rejects_traversal() {
        // The error names the whole refspec, which is what a caller reporting a
        // refused name has in hand, and not the component that failed.
        for bad in [
            "",
            "..",
            "a/../b",
            "/a",
            "a/",
            "a//b",
            "a/..",
            "origin:../escape",
            // A remote is one component, so a `/` in it names no remote.
            "a/b:x",
            ":x",
            "origin:",
        ] {
            let err = refspec_to_relpath(bad).unwrap_err();
            assert!(
                matches!(&err, Error::InvalidRefspec(name) if name == bad),
                "should reject {bad:?}, got {err}"
            );
        }
        assert!(validate_refspec("test/main").is_ok());
        assert!(validate_refspec("origin:test/main").is_ok());
    }

    #[test]
    fn collection_ref_names_the_pair_it_rejects() {
        let err = collection_ref_to_relpath(&CollectionRef::new("org.example.Foo", "a/../b"))
            .unwrap_err();
        assert!(
            matches!(&err, Error::InvalidRefspec(name) if name == "org.example.Foo:a/../b"),
            "{err}"
        );
        let err = collection_ref_to_relpath(&CollectionRef::local("a/../b")).unwrap_err();
        assert!(
            matches!(&err, Error::InvalidRefspec(name) if name == "a/../b"),
            "{err}"
        );
    }

    #[test]
    fn splits_the_ancestry_suffix() {
        assert_eq!(split_ancestry("test/main"), ("test/main", 0));
        assert_eq!(split_ancestry("test/main^"), ("test/main", 1));
        assert_eq!(split_ancestry("test/main^^^"), ("test/main", 3));
        assert_eq!(split_ancestry("^"), ("", 1));
    }

    #[test]
    fn builds_the_alias_link_body() {
        // A sibling alias names the target directly.
        assert_eq!(
            relative_link("refs/heads/alias", "refs/heads/test/main"),
            "test/main"
        );
        // A nested alias climbs out of its own directory first.
        assert_eq!(relative_link("refs/heads/p/q", "refs/heads/one"), "../one");
        // A target one level up from the alias's directory.
        assert_eq!(relative_link("refs/heads/a/b", "refs/heads/a"), "../a");
        // Across the two ref roots.
        assert_eq!(
            relative_link("refs/heads/alias", "refs/remotes/origin/main"),
            "../remotes/origin/main"
        );
    }

    #[test]
    fn parses_ref_content_with_newline() {
        let hex = "b3c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89";
        let checksum = parse_ref_content(format!("{hex}\n").as_bytes()).unwrap();
        assert_eq!(checksum.to_hex(), hex);
        assert!(parse_ref_content(b"not-a-checksum\n").is_err());
    }
}