mise-util 2026.11.14

Environment, directory, and filesystem helpers shared by mise's crates
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
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
//! What mise remembers about each packslip project's signer, the way SSH
//! remembers hosts: the identity that signed the first release it
//! accepted, and the things the specification says a consumer never lets
//! get weaker without a person's say-so. The file lives in the state dir;
//! it is this machine's memory, not something to sync.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

use eyre::{Report, Result, WrapErr, bail, eyre};
use packslip::forge::{self, Check, Expected, ForgePin, IdentityError};
use serde::{Deserialize, Serialize};

use crate::{dirs, file};

/// Where the pins live.
pub fn pins_file() -> PathBuf {
    dirs::STATE.join("packslip").join("pins.toml")
}

/// The signer a project's releases are accepted from.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Pin {
    /// `sigstore-oidc` or `sigstore-key`.
    pub scheme: String,
    /// A workflow path without its ref, or a key id: see [`signer_of`].
    pub signer: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub issuer: Option<String>,
    /// `vendor` or `repackager`.
    pub attested_by: String,
    /// Whether every artifact of an accepted release linked provenance.
    #[serde(default)]
    pub provenance: bool,
    /// Whether a bundle with no transparency log entry was ever accepted.
    #[serde(default)]
    pub unlogged: bool,
    /// RFC 3339, when the pin was set.
    pub pinned_at: String,
    /// For a GitHub or GitLab project, the forge's repository ID from the
    /// last accepted release, and the name it was signed under. A rename or a
    /// transfer to another owner keeps the ID, so the pin follows the
    /// repository rather than the name. Pins written before mise recorded it,
    /// or under signer options that skip the forge check, have none: they
    /// hold by name, and gain it from the next release accepted under that
    /// name with a forge check. An owner ID that older pins recorded is still
    /// read, and ignored.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub forge: Option<ForgePin>,
    /// The names the pin was recorded under before it followed a rename to
    /// the one it is under now. A name its repository left is free for
    /// anyone to take, so each stays held to the pin.
    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
    pub former_names: BTreeSet<String>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct Pins {
    #[serde(default)]
    pins: BTreeMap<String, Pin>,
    /// The highest release-list sequence accepted per project, so a
    /// mirror cannot show an older list than this machine has seen.
    #[serde(default)]
    sequences: BTreeMap<String, u64>,
    /// For a GitHub or GitLab project, the forge identity of the release
    /// list whose sequence is recorded under the same name, so that the
    /// sequence follows the repository through a rename the way a pin does.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    list_forges: BTreeMap<String, ForgePin>,
}

/// Whether two forge identities are the same repository, and in a GitHub
/// monorepo the same tool in it: the same forge, repository ID and subpath.
/// A rename changes none of them.
fn same_repository(a: &ForgePin, b: &ForgePin) -> bool {
    fn host(project: &str) -> &str {
        project.split('/').next().unwrap_or_default()
    }
    a.repository_id == b.repository_id
        && host(&a.project) == host(&b.project)
        && packslip::model::repository_subpath(&a.project)
            == packslip::model::repository_subpath(&b.project)
}

impl Pins {
    /// Every forge identity recorded, a pin's or a release list's, with the
    /// name it is recorded under.
    fn forges(&self) -> impl Iterator<Item = (&str, &ForgePin)> {
        let pins = self
            .pins
            .iter()
            .filter_map(|(key, pin)| Some((key.as_str(), pin.forge.as_ref()?)));
        let lists = self
            .list_forges
            .iter()
            .map(|(key, forge)| (key.as_str(), forge));
        pins.chain(lists)
    }

    /// The names whose pins and release-list sequences speak for `project`:
    /// its own, and any other a rename left them under. Another name is the
    /// same repository by its forge ID: the one a verified certificate shows
    /// (`verified`), or, with none, the IDs recorded under `project`'s own
    /// name or for a release last signed as `project`. A name can only be
    /// added, never taken away, so what is found here is held to in full.
    fn repository_keys(&self, project: &str, verified: Option<&ForgePin>) -> BTreeSet<String> {
        let mut keys = BTreeSet::from([project.to_string()]);
        let ids: Vec<&ForgePin> = match verified {
            Some(verified) => vec![verified],
            None => self
                .forges()
                .filter(|(key, forge)| *key == project || forge.project == project)
                .map(|(_, forge)| forge)
                .collect(),
        };
        for (key, forge) in self.forges() {
            if ids.iter().any(|id| same_repository(id, forge)) {
                keys.insert(key.to_string());
            }
        }
        keys
    }

    /// The names of the pins that speak for `project`, its own first: every
    /// one of [`Self::repository_keys`] that has a pin, one recorded for a
    /// release last signed as `project`, which is how a config that followed
    /// a rename finds the pin the old name set, and one that was recorded
    /// under `project` before it followed a rename, which holds the name to
    /// the repository that left it whatever a certificate shows under it now.
    fn pin_keys(&self, project: &str, verified: Option<&ForgePin>) -> Vec<String> {
        let mut keys = self.repository_keys(project, verified);
        keys.extend(
            self.pins
                .iter()
                .filter(|(_, pin)| {
                    pin.forge.as_ref().is_some_and(|f| f.project == project)
                        || pin.former_names.contains(project)
                })
                .map(|(key, _)| key.clone()),
        );
        let mut keys: Vec<String> = keys
            .into_iter()
            .filter(|key| self.pins.contains_key(key))
            .collect();
        keys.sort_by_key(|key| key.as_str() != project);
        keys
    }

    /// The forge identity the pins under `keys` hold: that of the first with
    /// one. A pin set without a forge check has none, and says nothing
    /// against the ID another name recorded for the same repository.
    fn forge_of(&self, keys: &[String]) -> Option<ForgePin> {
        keys.iter().find_map(|key| self.pins[key].forge.clone())
    }

    /// Move what a release list left under `from` to `to`, keeping the
    /// higher sequence, so a pin that follows a rename takes it along.
    fn move_list(&mut self, from: &str, to: &str) {
        if let Some(sequence) = self.sequences.remove(from) {
            let to = self.sequences.entry(to.to_string()).or_default();
            *to = (*to).max(sequence);
        }
        if let Some(forge) = self.list_forges.remove(from) {
            // What the list under the new name recorded is the newer.
            let kept = self.list_forges.remove(to).unwrap_or(forge);
            self.list_forges.insert(to.to_string(), kept);
        }
    }
}

/// Hold the file lock for a read-modify-write of the pins file, so two
/// installs running at once cannot drop each other's pin or sequence.
/// A lock beside the pins file itself, not under the cache directory:
/// every process that shares the state directory must share the lock,
/// whatever its cache directory is.
fn locked(path: &Path) -> Result<fslock::LockFile> {
    if let Some(parent) = path.parent() {
        file::create_dir_all(parent)?;
    }
    let lock_path = path.with_extension("lock");
    let mut lock = fslock::LockFile::open(&lock_path)?;
    if !lock.try_lock()? {
        debug!("waiting for lock on {}", lock_path.display());
        lock.lock()?;
    }
    Ok(lock)
}

fn load(path: &Path) -> Result<Pins> {
    if !path.is_file() {
        return Ok(Pins::default());
    }
    let text = file::read_to_string(path)?;
    toml::from_str(&text).wrap_err_with(|| {
        format!(
            "{} is not valid; it records which signers mise accepts packslips from. Fix or remove it",
            path.display()
        )
    })
}

fn save(path: &Path, pins: &Pins) -> Result<()> {
    if let Some(parent) = path.parent() {
        file::create_dir_all(parent)?;
    }
    file::write_atomic(path, toml::to_string_pretty(pins)?)
}

/// What a verified packslip showed about who signed it.
#[derive(Debug, Clone, Copy)]
pub struct Observed<'a> {
    pub scheme: &'a str,
    pub key_id: &'a str,
    pub issuer: Option<&'a str>,
    pub attested_by: &'a str,
    pub provenance: bool,
    pub logged: bool,
    /// The forge check the release passed, for a GitHub or GitLab project
    /// verified by repository ID. With it, and a pin that recorded the same
    /// ID, a signer is the pinned one when it is the same workflow of the
    /// same repository under a new name.
    pub forge: Option<&'a Check>,
    /// Signers the tool options name together, as a `workflow` list does for
    /// a project that moved its signing workflow: a person's say-so that a
    /// release may move from one of them to another.
    pub listed: &'a [String],
}

/// The signer a pin records for a key id. For a workflow identity that is
/// the path without its ref, as the specification says: a new tag of the
/// same workflow is the same signer. A key is its id.
///
/// Only a workflow identity has a ref to drop, and it is a URL whose ref
/// comes after the last `@`. Every other keyless identity — an email, a
/// SPIFFE URI — can carry `@` as part of itself, and cutting there would
/// file `alice@example.com` and `alice@example.invalid` under one signer,
/// so the second would pass as a signer already accepted.
pub fn signer_of(scheme: &str, key_id: &str) -> String {
    if scheme == "sigstore-oidc"
        && key_id.starts_with("https://")
        && let Some((path, _ref)) = key_id.rsplit_once('@')
    {
        return path.to_string();
    }
    key_id.to_string()
}

/// Whether the signer of a release that passed `check` is `previous`, a
/// signer mise recorded for the project: the same workflow of the same
/// repository, perhaps under a new name. A release can declare
/// `pin_workflow: false` to be held to its repository alone, but mise records
/// no person's acceptance of that, so it holds every release to the workflow
/// it pinned.
pub fn continues_signer(check: &Check, previous: &str) -> bool {
    check.pins_workflow() && check.continues_signer(previous)
}

/// The forge identity pinned for `project`, if any.
pub fn forge_pin(project: &str) -> Result<Option<ForgePin>> {
    forge_pin_at(&pins_file(), project)
}

pub fn forge_pin_at(path: &Path, project: &str) -> Result<Option<ForgePin>> {
    let pins = load(path)?;
    Ok(pins.forge_of(&pins.pin_keys(project, None)))
}

/// Compare what a release showed with the project's pin, refusing what
/// the specification calls a downgrade. Writes nothing: a release is
/// recorded with [`record`] only once everything else about it has been
/// accepted, so a refused install never leaves a pin behind.
///
/// The pin is the project's own, or one a rename left under another name:
/// the certificate's repository ID finds it, whether the config or the
/// release was the first to use the new name, and it is held to in full.
pub fn check(project: &str, observed: Observed<'_>) -> Result<()> {
    check_at(&pins_file(), project, observed)
}

pub fn check_at(path: &Path, project: &str, observed: Observed<'_>) -> Result<()> {
    let pins = load(path)?;
    let verified = observed.forge.and_then(|check| check.pin.as_ref());
    for key in pins.pin_keys(project, verified) {
        check_against(&pins.pins[&key], &key, project, observed)?;
    }
    Ok(())
}

/// Hold a release to one pin, recorded under `key`, which is `project` or
/// the name a rename left the pin under.
fn check_against(pin: &Pin, key: &str, project: &str, observed: Observed<'_>) -> Result<()> {
    // A pin found under another name holds the release to the repository
    // it recorded, as one found by name does. The pin's repository ID is
    // what shows the release is that repository. Check the certificate's
    // source separately from its signer, which can be a shared workflow;
    // signer continuity is still required below.
    let by_pin = match (&pin.forge, observed.forge) {
        (Some(pinned), Some(check)) => match &check.pin {
            Some(signed) => Some(
                forge::check_source(
                    &Expected::new(project).pinned(Some(pinned)),
                    &signed.project,
                    observed.key_id,
                    observed.issuer,
                    check.source.as_ref(),
                )
                .map_err(|err| forge_refusal(project, key, err))?,
            ),
            None => None,
        },
        _ => None,
    };
    let signer = signer_of(observed.scheme, observed.key_id);
    let mut problems = Vec::new();
    // Only the repository ID the pin itself recorded shows that a signer
    // under another name is the pinned one. A pin that recorded none holds
    // by name: the ID `observed.forge` was checked against is what the forge
    // says the name belongs to today, or what a lockfile recorded, and says
    // nothing about the repository this pin was set for.
    let listed = |signer: &str| observed.listed.iter().any(|listed| listed == signer);
    let same_signer = pin.signer == signer
        || (listed(&pin.signer) && listed(&signer))
        || by_pin
            .as_ref()
            .is_some_and(|check| continues_signer(check, &pin.signer));
    if pin.scheme != observed.scheme || !same_signer {
        problems.push(format!(
            "is signed by {signer} ({}), but {} ({}) signed what mise accepted before",
            observed.scheme, pin.signer, pin.scheme
        ));
    }
    if pin.attested_by == "vendor" && observed.attested_by == "repackager" {
        problems.push(
            "is attested by a repackager, but the vendor's own packslip was accepted before".into(),
        );
    }
    if pin.provenance && !observed.provenance {
        problems.push("drops the build provenance every artifact linked before".into());
    }
    if !problems.is_empty() {
        bail!(
            "packslip:{project}: this release {}{}.\n\nIf the vendor announced the change, run `mise packslip forget {key}` and install again; the next release accepted sets the pin.",
            problems.join(", and "),
            pinned_as(key, project)
        );
    }
    Ok(())
}

/// Where a pin found under another name was recorded, for a refusal.
fn pinned_as(key: &str, project: &str) -> String {
    if key == project {
        String::new()
    } else {
        format!(" (mise pinned the repository as packslip:{key}, before it was renamed)")
    }
}

/// A release that is not the repository a pin recorded.
fn forge_refusal(project: &str, key: &str, err: IdentityError) -> Report {
    eyre!(
        "packslip:{project}: this release is not from the repository mise pinned as packslip:{key}: {err}.\n\n\
         If the vendor announced the change, run `mise packslip forget {key}` and install again."
    )
}

/// Set the project's pin from an accepted release, or strengthen it: what
/// got stronger is remembered, what stayed the same is left alone. Checks
/// again under the lock, since the file may have changed since [`check`].
/// A pin a rename left under another name moves to `project`, with its
/// release list's sequence, so one repository keeps one pin; the name it
/// leaves stays held to it.
pub fn record(project: &str, observed: Observed<'_>) -> Result<Pin> {
    record_at(&pins_file(), project, observed)
}

pub fn record_at(path: &Path, project: &str, observed: Observed<'_>) -> Result<Pin> {
    let _lock = locked(path)?;
    let mut pins = load(path)?;
    let signer = signer_of(observed.scheme, observed.key_id);
    let forge = observed.forge.and_then(|check| check.pin.clone());
    let keys = pins.pin_keys(project, forge.as_ref());
    let Some(first) = keys.first() else {
        let pin = Pin {
            scheme: observed.scheme.to_string(),
            signer,
            issuer: observed.issuer.map(str::to_string),
            attested_by: observed.attested_by.to_string(),
            provenance: observed.provenance,
            unlogged: !observed.logged,
            pinned_at: jiff::Timestamp::now().to_string(),
            forge,
            former_names: BTreeSet::new(),
        };
        pins.pins.insert(project.to_string(), pin.clone());
        save(path, &pins)?;
        return Ok(pin);
    };
    for key in &keys {
        check_against(&pins.pins[key], key, project, observed)?;
    }
    let pin = &pins.pins[first];
    let any = |floor: fn(&Pin) -> bool| keys.iter().any(|key| floor(&pins.pins[key]));
    let updated = Pin {
        // The release passed every pin, so its signer is the pinned one, the
        // pinned workflow under the repository's new name, or one the tool
        // options list beside the pinned one: remember it as it is now.
        signer,
        issuer: observed.issuer.map(str::to_string).or(pin.issuer.clone()),
        attested_by: observed.attested_by.to_string(),
        provenance: observed.provenance || any(|pin| pin.provenance),
        unlogged: !observed.logged || any(|pin| pin.unlogged),
        // Like the floors, from every pin merged: the project's own may
        // have been set without a forge check while another name's has one.
        forge: forge.or_else(|| pins.forge_of(&keys)),
        // The names the pin leaves by moving to `project` stay held to it,
        // with those it left before.
        former_names: keys
            .iter()
            .flat_map(|key| pins.pins[key].former_names.iter().chain([key]))
            .filter(|name| name.as_str() != project)
            .cloned()
            .collect(),
        ..pin.clone()
    };
    let moved: Vec<&String> = keys.iter().filter(|key| key.as_str() != project).collect();
    if !moved.is_empty() || pins.pins.get(project) != Some(&updated) {
        for key in moved {
            pins.pins.remove(key);
            pins.move_list(key, project);
        }
        pins.pins.insert(project.to_string(), updated.clone());
        save(path, &pins)?;
    }
    Ok(updated)
}

/// Refuse a release list whose sequence is below one already accepted for
/// the project, and remember the highest seen. `forge` is the repository a
/// GitHub or GitLab project's list was verified as signed by: a sequence
/// accepted under another name for it counts, and moves to `project`.
pub fn check_sequence(project: &str, sequence: u64, forge: Option<&ForgePin>) -> Result<()> {
    check_sequence_at(&pins_file(), project, sequence, forge)
}

pub fn check_sequence_at(
    path: &Path,
    project: &str,
    sequence: u64,
    forge: Option<&ForgePin>,
) -> Result<()> {
    let _lock = locked(path)?;
    let mut pins = load(path)?;
    let keys = pins.repository_keys(project, forge);
    if let Some((key, last)) = keys
        .iter()
        .filter_map(|key| Some((key, *pins.sequences.get(key)?)))
        .max_by_key(|(_, last)| *last)
        && sequence < last
    {
        bail!(
            "the release list of packslip:{project} has sequence {sequence}, but sequence {last} was already accepted{}; refusing to go back",
            accepted_as(key, project)
        );
    }
    let mut changed = pins.sequences.get(project) != Some(&sequence);
    // Without the repository's ID from the list itself, a sequence found by
    // an ID recorded before still counts, but stays where it is.
    if let Some(forge) = forge {
        for key in keys.iter().filter(|key| key.as_str() != project) {
            changed |= pins.sequences.remove(key).is_some();
            changed |= pins.list_forges.remove(key).is_some();
        }
        changed |= pins.list_forges.get(project) != Some(forge);
        pins.list_forges.insert(project.to_string(), forge.clone());
    }
    if changed {
        pins.sequences.insert(project.to_string(), sequence);
        save(path, &pins)?;
    }
    Ok(())
}

/// Under which name a release list was accepted, for a refusal.
fn accepted_as(key: &str, project: &str) -> String {
    if key == project {
        String::new()
    } else {
        format!(" as packslip:{key}, the same repository before it was renamed")
    }
}

/// An absent supplementary list is allowed only before this machine has
/// accepted one for the repository, under this name or another a rename
/// gave it. Its disappearance must not undo signed withdrawals. `forge`
/// is the repository a release was verified as signed by, once one was:
/// without it, only a repository ID recorded under this name links the
/// names.
pub fn check_missing_list(project: &str, forge: Option<&ForgePin>) -> Result<()> {
    check_missing_list_at(&pins_file(), project, forge)
}

pub fn check_missing_list_at(path: &Path, project: &str, forge: Option<&ForgePin>) -> Result<()> {
    let _lock = locked(path)?;
    let pins = load(path)?;
    if let Some(key) = pins
        .repository_keys(project, forge)
        .into_iter()
        .find(|key| pins.sequences.contains_key(key))
    {
        bail!(
            "the signed release list of packslip:{project} disappeared, but one was accepted{}; restore the list or run `mise packslip forget {key}`",
            accepted_as(&key, project)
        );
    }
    Ok(())
}

/// Every pin, by project.
pub fn list() -> Result<BTreeMap<String, Pin>> {
    list_at(&pins_file())
}

pub fn list_at(path: &Path) -> Result<BTreeMap<String, Pin>> {
    Ok(load(path)?.pins)
}

/// Drop a project's pin and sequence, so the next release accepted sets
/// them again. Returns whether there was one.
pub fn forget(project: &str) -> Result<bool> {
    forget_at(&pins_file(), project)
}

pub fn forget_at(path: &Path, project: &str) -> Result<bool> {
    let _lock = locked(path)?;
    let mut pins = load(path)?;
    // What a rename left under another name goes too, or the next install
    // would find it again by the repository's ID.
    let mut keys = pins.repository_keys(project, None);
    keys.extend(pins.pin_keys(project, None));
    let mut had = false;
    for key in &keys {
        had |= pins.pins.remove(key).is_some();
        had |= pins.sequences.remove(key).is_some();
        pins.list_forges.remove(key);
    }
    if had {
        save(path, &pins)?;
    }
    Ok(had)
}

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

    const WORKFLOW: &str = "https://github.com/o/r/.github/workflows/release.yml";

    fn oidc(tag: &str) -> String {
        format!("{WORKFLOW}@refs/tags/{tag}")
    }

    fn observed<'a>(scheme: &'a str, key_id: &'a str) -> Observed<'a> {
        Observed {
            scheme,
            key_id,
            issuer: Some("https://token.actions.githubusercontent.com"),
            attested_by: "vendor",
            provenance: false,
            logged: true,
            forge: None,
            listed: &[],
        }
    }

    #[test]
    fn an_accepted_list_cannot_disappear_until_forgotten() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        check_missing_list_at(&path, "github.com/o/r", None).unwrap();
        check_sequence_at(&path, "github.com/o/r", 0, None).unwrap();
        assert!(check_missing_list_at(&path, "github.com/o/r", None).is_err());
        check_missing_list_at(&path, "github.com/o/other", None).unwrap();
        assert!(forget_at(&path, "github.com/o/r").unwrap());
        check_missing_list_at(&path, "github.com/o/r", None).unwrap();
    }

    #[test]
    fn a_new_tag_of_the_same_workflow_is_the_same_signer() {
        assert_eq!(signer_of("sigstore-oidc", &oidc("v1")), WORKFLOW);
        assert_eq!(signer_of("sigstore-key", "5A0A"), "5A0A");
        // Only a workflow identity has a ref to drop. An identity that is
        // itself an email or a URI keeps every character of itself, or two
        // signers sharing a local part would share one pin.
        for identity in [
            "alice@example.com",
            "alice@example.invalid",
            "spiffe://example.com/ns/ci/sa/build@v2",
        ] {
            assert_eq!(signer_of("sigstore-oidc", identity), identity);
        }
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let v1 = oidc("v1");
        check_at(&path, "github.com/o/r", observed("sigstore-oidc", &v1)).unwrap();
        assert!(!path.exists(), "a check writes nothing");
        let pin = record_at(&path, "github.com/o/r", observed("sigstore-oidc", &v1)).unwrap();
        assert_eq!(pin.signer, WORKFLOW);
        let v2 = oidc("v2");
        let again = record_at(&path, "github.com/o/r", observed("sigstore-oidc", &v2)).unwrap();
        assert_eq!(again, pin, "nothing changed, nothing rewritten");
        assert!(
            file::read_to_string(&path)
                .unwrap()
                .contains("[pins.\"github.com/o/r\"]")
        );
    }

    #[test]
    fn a_workflow_the_options_list_beside_the_pinned_one_continues_it() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let project = "github.com/o/r";
        let old = "https://github.com/o/r/.github/workflows/release-plz.yml";
        let old_key = format!("{old}@refs/heads/main");
        let new_key = format!("{WORKFLOW}@refs/heads/main");
        record_at(&path, project, observed("sigstore-oidc", &old_key)).unwrap();

        // Without the list, the move is a signer change.
        let err = check_at(&path, project, observed("sigstore-oidc", &new_key)).unwrap_err();
        assert!(err.to_string().contains("mise packslip forget"), "{err}");

        let listed = [old.to_string(), WORKFLOW.to_string()];
        let moved = Observed {
            listed: &listed,
            ..observed("sigstore-oidc", &new_key)
        };
        check_at(&path, project, moved).unwrap();
        assert_eq!(record_at(&path, project, moved).unwrap().signer, WORKFLOW);

        // A list that names only one of them, the pinned signer or the new
        // one, says nothing about the move.
        let other = "https://github.com/o/r/.github/workflows/other.yml";
        let other_key = format!("{other}@refs/heads/main");
        for listed in [[WORKFLOW.to_string()], [other.to_string()]] {
            let unlisted = Observed {
                listed: &listed,
                ..observed("sigstore-oidc", &other_key)
            };
            assert!(check_at(&path, project, unlisted).is_err(), "{listed:?}");
            assert!(record_at(&path, project, unlisted).is_err(), "{listed:?}");
        }
        assert_eq!(load(&path).unwrap().pins[project].signer, WORKFLOW);
    }

    #[test]
    fn downgrades_are_refused_and_upgrades_remembered() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let project = "github.com/o/r";
        let v1 = oidc("v1");
        let strong = Observed {
            provenance: true,
            ..observed("sigstore-oidc", &v1)
        };
        // The first release sets the pin with no provenance; provenance
        // arriving later is remembered as the new floor.
        record_at(&path, project, observed("sigstore-oidc", &v1)).unwrap();
        let pin = record_at(&path, project, strong).unwrap();
        assert!(pin.provenance);

        let other = "https://github.com/o/r/.github/workflows/other.yml@refs/tags/v3";
        let err = record_at(
            &path,
            project,
            Observed {
                provenance: true,
                ..observed("sigstore-oidc", other)
            },
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("signed what mise accepted before"),
            "{err}"
        );
        assert!(err.to_string().contains("mise packslip forget"), "{err}");

        let keyed = Observed {
            provenance: true,
            ..observed("sigstore-key", "5A0A")
        };
        assert!(
            record_at(&path, project, keyed).is_err(),
            "a scheme change is a signer change"
        );

        let dropped = observed("sigstore-oidc", &v1);
        let err = record_at(&path, project, dropped).unwrap_err();
        assert!(
            err.to_string().contains("drops the build provenance"),
            "{err}"
        );

        let repackaged = Observed {
            attested_by: "repackager",
            provenance: true,
            ..observed("sigstore-oidc", &v1)
        };
        let err = record_at(&path, project, repackaged).unwrap_err();
        assert!(err.to_string().contains("repackager"), "{err}");

        assert_eq!(
            load(&path).unwrap().pins[project].signer,
            WORKFLOW,
            "a refused release leaves no mark"
        );

        // Forgetting lets a new signer in, once.
        assert!(forget_at(&path, project).unwrap());
        assert!(!forget_at(&path, project).unwrap());
        record_at(&path, project, keyed).unwrap();
        assert_eq!(load(&path).unwrap().pins[project].scheme, "sigstore-key");
    }

    #[test]
    fn a_repackager_pin_yields_to_the_vendor() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let v1 = oidc("v1");
        let repackaged = Observed {
            attested_by: "repackager",
            ..observed("sigstore-oidc", &v1)
        };
        record_at(&path, "p.example.com", repackaged).unwrap();
        let pin = record_at(&path, "p.example.com", observed("sigstore-oidc", &v1)).unwrap();
        assert_eq!(pin.attested_by, "vendor");
    }

    #[test]
    fn sequences_only_go_up() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        check_sequence_at(&path, "t.example.com", 3, None).unwrap();
        check_sequence_at(&path, "t.example.com", 5, None).unwrap();
        let err = check_sequence_at(&path, "t.example.com", 4, None).unwrap_err();
        assert!(err.to_string().contains("refusing to go back"), "{err}");
        check_sequence_at(&path, "t.example.com", 5, None).unwrap();
        check_sequence_at(&path, "other.example.com", 1, None).unwrap();
        assert!(forget_at(&path, "t.example.com").unwrap());
        check_sequence_at(&path, "t.example.com", 1, None).unwrap();
    }

    #[test]
    fn a_malformed_file_is_an_error() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        file::write(&path, "not = [toml").unwrap();
        let v1 = oidc("v1");
        let err = record_at(&path, "github.com/o/r", observed("sigstore-oidc", &v1)).unwrap_err();
        assert!(err.to_string().contains("is not valid"), "{err}");
    }

    #[test]
    fn a_pin_written_before_forge_ids_still_reads_and_gains_none_by_name() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        file::write(
            &path,
            format!(
                r#"[pins."github.com/o/r"]
scheme = "sigstore-oidc"
signer = "{WORKFLOW}"
issuer = "https://token.actions.githubusercontent.com"
attested_by = "vendor"
provenance = false
unlogged = false
pinned_at = "2026-09-01T00:00:00Z"

[sequences]
"github.com/o/r" = 3
"#
            ),
        )
        .unwrap();
        assert_eq!(forge_pin_at(&path, "github.com/o/r").unwrap(), None);
        let v2 = oidc("v2");
        check_at(&path, "github.com/o/r", observed("sigstore-oidc", &v2)).unwrap();
        let pin = record_at(&path, "github.com/o/r", observed("sigstore-oidc", &v2)).unwrap();
        assert_eq!(pin.forge, None, "nothing to record without a forge check");
        assert_eq!(pin.pinned_at, "2026-09-01T00:00:00Z");
        assert!(
            !file::read_to_string(&path).unwrap().contains("forge"),
            "an unchanged pin is not rewritten"
        );
        assert_eq!(load(&path).unwrap().sequences["github.com/o/r"], 3);
    }

    #[test]
    fn a_forge_pin_round_trips_and_is_found_under_its_new_name() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let mut pins = Pins::default();
        pins.pins.insert(
            "github.com/old/r".into(),
            Pin {
                scheme: "sigstore-oidc".into(),
                signer: "https://github.com/new/r/.github/workflows/release.yml".into(),
                issuer: None,
                attested_by: "vendor".into(),
                provenance: false,
                unlogged: false,
                pinned_at: "2026-09-01T00:00:00Z".into(),
                forge: Some(ForgePin::of("github.com/new/r", "42")),
                former_names: BTreeSet::new(),
            },
        );
        save(&path, &pins).unwrap();
        let text = file::read_to_string(&path).unwrap();
        assert!(text.contains("[pins.\"github.com/old/r\".forge]"), "{text}");
        assert!(text.contains("repository_id = \"42\""), "{text}");
        let pin = ForgePin::of("github.com/new/r", "42");
        assert_eq!(
            forge_pin_at(&path, "github.com/old/r").unwrap().as_ref(),
            Some(&pin)
        );
        // A config that follows the rename keeps what was pinned.
        assert_eq!(
            forge_pin_at(&path, "github.com/new/r").unwrap().as_ref(),
            Some(&pin)
        );
        assert_eq!(forge_pin_at(&path, "github.com/other/r").unwrap(), None);
        // Forgetting the new name forgets the pin found under it.
        assert!(forget_at(&path, "github.com/new/r").unwrap());
        assert_eq!(forge_pin_at(&path, "github.com/old/r").unwrap(), None);
    }

    #[test]
    fn merging_pins_keeps_the_repository_id_another_name_recorded() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let (old, new) = ("github.com/o/old", "github.com/o/r");
        // The new name was pinned without a forge check, as under signer
        // options, and then the old name with one.
        let v1 = oidc("v1");
        let unchecked = record_at(&path, new, observed("sigstore-oidc", &v1)).unwrap();
        assert_eq!(unchecked.forge, None);
        let mut pins = load(&path).unwrap();
        pins.pins.insert(
            old.into(),
            Pin {
                forge: Some(repo(new, "42")),
                ..unchecked
            },
        );
        save(&path, &pins).unwrap();
        assert_eq!(forge_pin_at(&path, new).unwrap(), Some(repo(new, "42")));
        // Another release that brings no ID merges the two into one pin,
        // which keeps the ID.
        let pin = record_at(&path, new, observed("sigstore-oidc", &v1)).unwrap();
        assert_eq!(pin.forge, Some(repo(new, "42")));
        assert_eq!(
            list_at(&path).unwrap(),
            BTreeMap::from([(new.to_string(), pin)])
        );
    }

    fn repo(project: &str, id: &str) -> ForgePin {
        ForgePin::of(project, id)
    }

    /// Model a shared workflow's verified certificate source for pin checks.
    fn shared_check(identity: &str, id: &str) -> Check {
        let source =
            packslip::sigstore::SourceRepository::new("https://github.com/o/r").with_id(id);
        forge::check_source(
            &Expected::new("github.com/o/r"),
            "github.com/o/r",
            identity,
            Some("https://token.actions.githubusercontent.com"),
            Some(&source),
        )
        .unwrap()
    }

    #[test]
    fn a_shared_workflow_keeps_its_saved_pin_on_reinstall_and_update() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let project = "github.com/o/r";
        let workflow = "https://github.com/o/shared/.github/workflows/release.yml";
        let first = format!("{workflow}@refs/tags/v1");
        let check = shared_check(&first, "42");
        let pin = record_at(
            &path,
            project,
            Observed {
                forge: Some(&check),
                ..observed("sigstore-oidc", &first)
            },
        )
        .unwrap();
        let saved = file::read_to_string(&path).unwrap();
        for tag in ["v1", "v2"] {
            let identity = format!("{workflow}@refs/tags/{tag}");
            let check = shared_check(&identity, "42");
            let incoming = Observed {
                forge: Some(&check),
                ..observed("sigstore-oidc", &identity)
            };
            check_at(&path, project, incoming).unwrap();
            assert_eq!(record_at(&path, project, incoming).unwrap(), pin);
            assert_eq!(file::read_to_string(&path).unwrap(), saved);
        }
        assert_eq!(pin.signer, workflow);
        assert_eq!(pin.forge, Some(repo(project, "42")));
    }

    #[test]
    fn a_shared_workflow_pin_still_refuses_changed_signers_and_source_ids() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let project = "github.com/o/r";
        let workflow = "https://github.com/o/shared/.github/workflows/release.yml";
        let first = format!("{workflow}@refs/tags/v1");
        let check = shared_check(&first, "42");
        record_at(
            &path,
            project,
            Observed {
                forge: Some(&check),
                ..observed("sigstore-oidc", &first)
            },
        )
        .unwrap();
        let saved = file::read_to_string(&path).unwrap();
        for (signer, id) in [
            (
                "https://github.com/o/other/.github/workflows/release.yml",
                "42",
            ),
            (
                "https://github.com/o/shared/.github/workflows/other.yml",
                "42",
            ),
            (workflow, "43"),
        ] {
            let identity = format!("{signer}@refs/tags/v2");
            let check = shared_check(&identity, id);
            let incoming = Observed {
                forge: Some(&check),
                ..observed("sigstore-oidc", &identity)
            };
            let err = check_at(&path, project, incoming).unwrap_err().to_string();
            if id == "42" {
                assert!(err.contains(signer) && err.contains(workflow), "{err}");
                assert!(err.contains("signed what mise accepted before"), "{err}");
            } else {
                assert!(err.contains("42") && err.contains("43"), "{err}");
                assert!(err.contains("not from the repository mise pinned"), "{err}");
            }
            assert!(record_at(&path, project, incoming).is_err());
            assert_eq!(file::read_to_string(&path).unwrap(), saved);
        }
    }

    #[test]
    fn a_shared_workflow_pin_requires_source_evidence_and_the_forge_issuer() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let project = "github.com/o/r";
        let identity = "https://github.com/o/shared/.github/workflows/release.yml@refs/tags/v1";
        let mut check = shared_check(identity, "42");
        let incoming = Observed {
            forge: Some(&check),
            ..observed("sigstore-oidc", identity)
        };
        record_at(&path, project, incoming).unwrap();
        let saved = file::read_to_string(&path).unwrap();
        let wrong_issuer = Observed {
            issuer: Some("https://other.example.com"),
            ..incoming
        };
        assert!(check_at(&path, project, wrong_issuer).is_err());
        assert!(record_at(&path, project, wrong_issuer).is_err());
        check.source = None;
        let missing_source = Observed {
            forge: Some(&check),
            ..observed("sigstore-oidc", identity)
        };
        assert!(check_at(&path, project, missing_source).is_err());
        assert!(record_at(&path, project, missing_source).is_err());
        assert_eq!(file::read_to_string(&path).unwrap(), saved);
    }

    #[test]
    fn a_release_list_sequence_follows_the_repository_through_a_rename() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let (old, new) = ("github.com/o/old", "github.com/o/new");
        check_sequence_at(&path, old, 5, Some(&repo(old, "42"))).unwrap();
        // The config follows the rename: an older list signed under the new
        // name is still older.
        let err = check_sequence_at(&path, new, 4, Some(&repo(new, "42"))).unwrap_err();
        assert!(
            err.to_string()
                .contains("sequence 5 was already accepted as packslip:github.com/o/old"),
            "{err}"
        );
        check_sequence_at(&path, new, 6, Some(&repo(new, "42"))).unwrap();
        let pins = load(&path).unwrap();
        assert_eq!(
            pins.sequences,
            BTreeMap::from([(new.to_string(), 6)]),
            "one sequence per repository"
        );
        assert_eq!(pins.list_forges[new], repo(new, "42"));
        // And back again: a config still naming the old one is held to it.
        let err = check_sequence_at(&path, old, 5, Some(&repo(new, "42"))).unwrap_err();
        assert!(err.to_string().contains("refusing to go back"), "{err}");
        // Another repository, or another tool of a monorepo, has its own.
        check_sequence_at(
            &path,
            "github.com/o/other",
            1,
            Some(&repo("github.com/o/other", "43")),
        )
        .unwrap();
        check_sequence_at(
            &path,
            "github.com/o/new/tool",
            1,
            Some(&repo("github.com/o/new/tool", "42")),
        )
        .unwrap();
        check_sequence_at(
            &path,
            "gitlab.com/o/new",
            1,
            Some(&repo("gitlab.com/o/new", "42")),
        )
        .unwrap();
        // Without the list's own forge identity, a sequence found through an
        // ID recorded under the name still counts, and stays where it is.
        let err = check_sequence_at(&path, new, 1, None).unwrap_err();
        assert!(err.to_string().contains("refusing to go back"), "{err}");
        // Forgetting the name it is recorded under, which a refusal gives,
        // lets an older list in.
        assert!(forget_at(&path, new).unwrap());
        check_sequence_at(&path, new, 1, Some(&repo(new, "42"))).unwrap();
    }

    #[test]
    fn a_name_the_pin_moved_away_from_stays_held_to_it() {
        use packslip::sigstore::{GITHUB_ISSUER, SourceRepository};
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let (old, new) = ("github.com/o/old", "github.com/o/new");
        // A release of repository `id`, requested and signed as `name`.
        let release = |name: &str, id: &str| {
            let key_id = format!("https://{name}/.github/workflows/release.yml@refs/tags/v1");
            let source = SourceRepository::new(format!("https://{name}")).with_id(id);
            let check = forge::check(
                &Expected::new(name),
                name,
                &key_id,
                Some(GITHUB_ISSUER),
                Some(&source),
            )
            .unwrap();
            (key_id, check)
        };
        let (key_id, check) = release(old, "42");
        let first = Observed {
            forge: Some(&check),
            ..observed("sigstore-oidc", &key_id)
        };
        record_at(&path, old, first).unwrap();
        // Another config already names the repository as it is called now:
        // the pin moves, and remembers the name it leaves.
        let (key_id, check) = release(new, "42");
        let followed = Observed {
            forge: Some(&check),
            ..observed("sigstore-oidc", &key_id)
        };
        let pin = record_at(&path, new, followed).unwrap();
        let pins = list_at(&path).unwrap();
        assert_eq!(pins.keys().collect::<Vec<_>>(), [new]);
        assert_eq!(pins[new], pin);
        assert_eq!(pin.former_names, BTreeSet::from([old.to_string()]));
        assert!(
            file::read_to_string(&path)
                .unwrap()
                .contains(r#"former_names = ["github.com/o/old"]"#)
        );
        // The old name is still pinned to the repository it was set for...
        assert_eq!(forge_pin_at(&path, old).unwrap(), Some(repo(new, "42")));
        // ...so another repository that takes it is not a first install.
        let (key_id, check) = release(old, "99");
        let taken = Observed {
            forge: Some(&check),
            ..observed("sigstore-oidc", &key_id)
        };
        let err = check_at(&path, old, taken).unwrap_err();
        assert!(
            err.to_string()
                .contains("is not from the repository mise pinned as packslip:github.com/o/new"),
            "{err}"
        );
        assert!(record_at(&path, old, taken).is_err());
        assert_eq!(
            list_at(&path).unwrap().keys().collect::<Vec<_>>(),
            [new],
            "a refusal moves nothing"
        );
        // Nor is a release under the old name checked by name alone.
        assert!(check_at(&path, old, observed("sigstore-oidc", &key_id)).is_err());
        // The repository's own release under the old name still passes,
        // and the pin moving back keeps the other name held.
        check_at(&path, old, first).unwrap();
        let back = record_at(&path, old, first).unwrap();
        assert_eq!(back.former_names, BTreeSet::from([new.to_string()]));
        assert_eq!(list_at(&path).unwrap().keys().collect::<Vec<_>>(), [old]);
        // Forgetting either name forgets the pin found by it, and is the
        // explicit way to accept the new holder.
        assert!(forget_at(&path, new).unwrap());
        assert!(list_at(&path).unwrap().is_empty());
        let pin = record_at(&path, old, taken).unwrap();
        assert_eq!(pin.forge, Some(repo(old, "99")));
        assert!(pin.former_names.is_empty());
    }

    #[test]
    fn an_owner_id_an_older_pin_recorded_is_read_and_ignored() {
        // mise 2026.9 recorded the repository's owner ID too. Such a pin still
        // reads, and a release of the same repository under another owner,
        // which is what a transfer looks like, is the same repository.
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        file::write(
            &path,
            format!(
                r#"[pins."github.com/o/r"]
scheme = "sigstore-oidc"
signer = "{WORKFLOW}"
attested_by = "vendor"
pinned_at = "2026-09-01T00:00:00Z"

[pins."github.com/o/r".forge]
project = "github.com/o/r"
repository_id = "42"
owner_id = "7"

[list_forges."github.com/o/r"]
project = "github.com/o/r"
repository_id = "42"
owner_id = "7"

[sequences]
"github.com/o/r" = 1
"#
            ),
        )
        .unwrap();
        assert_eq!(
            forge_pin_at(&path, "github.com/o/r").unwrap(),
            Some(repo("github.com/o/r", "42"))
        );
        let moved = repo("github.com/acme/r", "42");
        check_sequence_at(&path, "github.com/acme/r", 2, Some(&moved)).unwrap();
        let pins = load(&path).unwrap();
        assert_eq!(
            pins.list_forges.keys().collect::<Vec<_>>(),
            ["github.com/acme/r"]
        );
        assert_eq!(pins.sequences["github.com/acme/r"], 2);
        assert!(
            !file::read_to_string(&path).unwrap().contains("owner_id"),
            "a pin rewritten for another reason drops the owner ID"
        );
    }

    #[test]
    fn a_release_list_accepted_under_another_name_cannot_disappear() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("pins.toml");
        let (old, new) = ("github.com/o/old", "github.com/o/new");
        check_sequence_at(&path, old, 0, Some(&repo(old, "42"))).unwrap();
        // Nothing under the new name says it is the old repository until a
        // release shows its ID...
        check_missing_list_at(&path, new, None).unwrap();
        let err = check_missing_list_at(&path, new, Some(&repo(new, "42"))).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("one was accepted as packslip:github.com/o/old"),
            "{msg}"
        );
        assert!(
            msg.contains("mise packslip forget github.com/o/old"),
            "{msg}"
        );
        check_missing_list_at(&path, new, Some(&repo(new, "43"))).unwrap();
        assert!(check_missing_list_at(&path, old, None).is_err());
        // ...or a pin records a release signed under it.
        let mut pins = load(&path).unwrap();
        pins.pins.insert(
            old.into(),
            Pin {
                scheme: "sigstore-oidc".into(),
                signer: WORKFLOW.into(),
                issuer: None,
                attested_by: "vendor".into(),
                provenance: false,
                unlogged: false,
                pinned_at: "2026-09-01T00:00:00Z".into(),
                forge: Some(repo(new, "42")),
                former_names: BTreeSet::new(),
            },
        );
        pins.list_forges.clear();
        save(&path, &pins).unwrap();
        assert!(check_missing_list_at(&path, new, None).is_err());
    }
}