grit-lib 0.5.25

Core library for the grit Git implementation
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
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
//! The `promisor-remote` protocol v2 capability (see `gitprotocol-v2(5)` and
//! `Documentation/config/promisor.adoc`).
//!
//! A server that keeps some objects on other remotes, typically large blobs on a
//! *large-object promisor* (LOP), can tell clients about those remotes. A client that
//! accepts one lets the server leave out objects the LOP can provide; the client then
//! fetches them from the LOP when it needs them.
//!
//! The exchange has three parts, each with a typed model here:
//!
//! - The server builds a [`PromisorAdvertisement`] from its own promisor remotes
//!   ([`PromisorAdvertisement::from_server_config`]) and sends it as
//!   `promisor-remote=<value>` ([`PromisorAdvertisement::to_capability_value`]).
//! - The client parses it ([`PromisorAdvertisement::parse`]), reads its
//!   [`PromisorAcceptPolicy`] (`promisor.acceptFromServer`, `promisor.checkFields`,
//!   `promisor.storeFields`) and runs [`PromisorDecision::decide`] against the promisor
//!   remotes it already has ([`configured_promisor_remotes`]). The names it accepted go
//!   back to the server ([`PromisorDecision::reply_value`]).
//! - The server reads that reply with [`parse_accepted_reply`].
//!
//! Accepting a remote never creates it: only remotes the client already configured can be
//! accepted by name, and lazy fetches always use the locally configured URL, never the
//! advertised one. [`store_advertised_fields`] can update the `partialCloneFilter` and
//! `token` of an accepted, already configured remote.
//!
//! The older string-based helpers ([`promisor_remote_info`], [`promisor_remote_reply`],
//! [`parse_advertisement`]) are thin wrappers over these types.

use std::path::Path;

use crate::config::{parse_bool, ConfigFile, ConfigScope, ConfigSet};
use crate::error::{Error, Result};
use crate::partial_clone::ObjectFilterSpec;

/// An optional per-remote field the `promisor-remote` capability can carry besides
/// `name` and `url`.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
#[cfg_attr(feature = "serde-models", serde(rename_all = "snake_case"))]
pub enum PromisorField {
    /// The partial-clone filter the remote is meant to be used with
    /// (`remote.<name>.partialCloneFilter`).
    PartialCloneFilter,
    /// An authentication token for the remote (`remote.<name>.token`).
    Token,
}

impl PromisorField {
    /// Every field, in the order Git writes them in an advertisement.
    pub const ALL: [Self; 2] = [Self::PartialCloneFilter, Self::Token];

    /// The field name as it appears on the wire (case-sensitive).
    ///
    /// # Returns
    ///
    /// `"partialCloneFilter"` or `"token"`.
    #[must_use]
    pub const fn wire_name(self) -> &'static str {
        match self {
            Self::PartialCloneFilter => "partialCloneFilter",
            Self::Token => "token",
        }
    }

    /// The canonical (lowercase) last component of the `remote.<name>.<field>` config key.
    ///
    /// # Returns
    ///
    /// `"partialclonefilter"` or `"token"`.
    #[must_use]
    pub const fn config_key(self) -> &'static str {
        match self {
            Self::PartialCloneFilter => "partialclonefilter",
            Self::Token => "token",
        }
    }

    /// Parse a comma or whitespace separated field list, as in `promisor.sendFields`.
    ///
    /// Names are matched case-insensitively. Duplicates are kept only once.
    ///
    /// # Parameters
    ///
    /// - `raw`: the config value, for example `"partialCloneFilter, token"`.
    ///
    /// # Returns
    ///
    /// The known fields in the order given, and every name that is not a known field.
    #[must_use]
    pub fn parse_list(raw: &str) -> (Vec<Self>, Vec<String>) {
        let mut known = Vec::new();
        let mut unknown = Vec::new();
        for name in raw
            .split([',', ' ', '\t'])
            .map(str::trim)
            .filter(|s| !s.is_empty())
        {
            match Self::ALL
                .into_iter()
                .find(|f| f.wire_name().eq_ignore_ascii_case(name))
            {
                Some(field) if !known.contains(&field) => known.push(field),
                Some(_) => {}
                None => unknown.push(name.to_owned()),
            }
        }
        (known, unknown)
    }

    fn value_of(self, info: &PromisorInfo) -> Option<&str> {
        match self {
            Self::PartialCloneFilter => info.filter.as_deref(),
            Self::Token => info.token.as_deref(),
        }
    }

    fn set_on(self, info: &mut PromisorInfo, value: String) {
        match self {
            Self::PartialCloneFilter => info.filter = Some(value),
            Self::Token => info.token = Some(value),
        }
    }
}

/// Which advertised promisor remotes a client accepts (`promisor.acceptFromServer`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
#[cfg_attr(feature = "serde-models", serde(rename_all = "snake_case"))]
pub enum AcceptFromServer {
    /// Accept nothing (the default).
    #[default]
    None,
    /// Accept a remote configured locally under the same name with the same URL.
    KnownUrl,
    /// Accept a remote configured locally under the same name, whatever its URL.
    KnownName,
    /// Accept every advertised remote. Only for fully trusted servers.
    All,
}

impl AcceptFromServer {
    /// Parse a `promisor.acceptFromServer` value (case-insensitive).
    ///
    /// # Returns
    ///
    /// `None` when `value` is not one of `none`, `knownUrl`, `knownName` or `all`.
    #[must_use]
    pub fn parse(value: &str) -> Option<Self> {
        let value = value.trim();
        [
            ("none", Self::None),
            ("knownurl", Self::KnownUrl),
            ("knownname", Self::KnownName),
            ("all", Self::All),
        ]
        .into_iter()
        .find_map(|(name, policy)| value.eq_ignore_ascii_case(name).then_some(policy))
    }
}

/// A promisor-related config value the library ignored.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
#[cfg_attr(feature = "serde-models", serde(rename_all = "snake_case"))]
pub enum PromisorConfigWarning {
    /// `promisor.acceptFromServer` has a value that is not a known policy; nothing is accepted.
    UnknownAcceptValue(String),
    /// A field list (`promisor.checkFields`, `promisor.storeFields`) names an unknown field.
    UnknownField {
        /// The config key holding the list.
        key: &'static str,
        /// The unknown field name.
        field: String,
    },
    /// An advertised field value was not stored because it is not valid: a filter that does
    /// not parse, or a token containing control characters.
    InvalidAdvertisedValue {
        /// The remote the value was advertised for.
        remote: String,
        /// Which field.
        field: PromisorField,
    },
    /// An accepted remote advertised a filter other than `blob:none` or `blob:limit=<n>`,
    /// which cannot be used as the automatic filter.
    UnusableAutoFilter(String),
}

/// The client's rules for accepting advertised promisor remotes.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
pub struct PromisorAcceptPolicy {
    /// `promisor.acceptFromServer`.
    pub accept: AcceptFromServer,
    /// `promisor.checkFields`: fields whose advertised value must equal the local one.
    pub check_fields: Vec<PromisorField>,
    /// `promisor.storeFields`: fields copied from the advertisement into local config.
    pub store_fields: Vec<PromisorField>,
}

impl PromisorAcceptPolicy {
    /// Read the policy from config.
    ///
    /// # Parameters
    ///
    /// - `cfg`: the client's merged config (local, global, `-c` overrides).
    ///
    /// # Returns
    ///
    /// The policy and any values that were ignored. An unknown `acceptFromServer` value
    /// means [`AcceptFromServer::None`].
    #[must_use]
    pub fn from_config(cfg: &ConfigSet) -> (Self, Vec<PromisorConfigWarning>) {
        let mut warnings = Vec::new();
        let accept = match cfg.get("promisor.acceptFromServer") {
            None => AcceptFromServer::None,
            Some(raw) => AcceptFromServer::parse(&raw).unwrap_or_else(|| {
                warnings.push(PromisorConfigWarning::UnknownAcceptValue(raw));
                AcceptFromServer::None
            }),
        };
        let mut fields = |key: &'static str| {
            let (known, unknown) = PromisorField::parse_list(&cfg.get(key).unwrap_or_default());
            warnings.extend(
                unknown
                    .into_iter()
                    .map(|field| PromisorConfigWarning::UnknownField { key, field }),
            );
            known
        };
        let check_fields = fields("promisor.checkFields");
        let store_fields = fields("promisor.storeFields");
        (
            Self {
                accept,
                check_fields,
                store_fields,
            },
            warnings,
        )
    }
}

/// One advertised or configured promisor remote.
///
/// `Debug` output and serialization never show the token, nor a user name or password
/// in the URL.
#[derive(Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
pub struct PromisorInfo {
    /// Remote name.
    pub name: String,
    /// Remote URL, as configured or advertised. Serialized with any user name and
    /// password removed (see [`Self::display_url`]).
    #[cfg_attr(
        feature = "serde-models",
        serde(serialize_with = "serialize_redacted_url")
    )]
    pub url: Option<String>,
    /// Partial-clone filter (`partialCloneFilter` field).
    pub filter: Option<String>,
    /// Authentication token (`token` field). Never serialized.
    #[cfg_attr(feature = "serde-models", serde(skip))]
    pub token: Option<String>,
}

impl PromisorInfo {
    /// The URL with any user name and password removed, for showing to people.
    ///
    /// # Returns
    ///
    /// An empty string when there is no URL.
    #[must_use]
    pub fn display_url(&self) -> String {
        crate::transport_path::redact_url_for_display(self.url.as_deref().unwrap_or_default())
    }
}

#[cfg(feature = "serde-models")]
fn serialize_redacted_url<S: serde::Serializer>(
    url: &Option<String>,
    serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
    match url {
        Some(url) => serializer.serialize_some(&crate::transport_path::redact_url_for_display(url)),
        None => serializer.serialize_none(),
    }
}

impl std::fmt::Debug for PromisorInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PromisorInfo")
            .field("name", &self.name)
            .field("url", &self.url.as_ref().map(|_| self.display_url()))
            .field("filter", &self.filter)
            .field("token", &self.token.as_ref().map(|_| "<redacted>"))
            .finish()
    }
}

/// The promisor remotes a server advertises with the `promisor-remote` capability.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
pub struct PromisorAdvertisement {
    /// Advertised remotes in advertisement order. Each has a name and a non-empty URL.
    pub remotes: Vec<PromisorInfo>,
}

impl PromisorAdvertisement {
    /// Build the advertisement a server sends.
    ///
    /// Only promisor remotes with a non-empty URL are advertised; the fields listed in
    /// `promisor.sendFields` are included when set to a non-empty value.
    ///
    /// # Parameters
    ///
    /// - `cfg`: the served repository's config.
    ///
    /// # Returns
    ///
    /// `None` when `promisor.advertise` is not true or there is no remote to advertise.
    #[must_use]
    pub fn from_server_config(cfg: &ConfigSet) -> Option<Self> {
        let advertise = cfg
            .get_bool("promisor.advertise")
            .and_then(|r| r.ok())
            .unwrap_or(false);
        if !advertise {
            return None;
        }
        let (fields, _unknown) =
            PromisorField::parse_list(&cfg.get("promisor.sendFields").unwrap_or_default());
        let remotes = configured_promisor_remotes(cfg, &fields);
        (!remotes.is_empty()).then_some(Self { remotes })
    }

    /// Parse the value of a server's `promisor-remote=` capability.
    ///
    /// Remotes are separated by `;` and fields by `,`; values are percent-decoded. An entry
    /// without a name or with a missing or empty URL is dropped, and unknown fields are
    /// ignored, as the protocol requires.
    ///
    /// # Parameters
    ///
    /// - `value`: the text after `promisor-remote=`.
    #[must_use]
    pub fn parse(value: &str) -> Self {
        Self {
            remotes: value.split(';').filter_map(parse_one_advertised).collect(),
        }
    }

    /// Serialize as the value of the `promisor-remote=` capability.
    ///
    /// # Returns
    ///
    /// `name=<n>,url=<u>[,partialCloneFilter=<f>][,token=<t>]` per remote, joined with `;`.
    #[must_use]
    pub fn to_capability_value(&self) -> String {
        let mut out = String::new();
        for (i, p) in self.remotes.iter().enumerate() {
            if i != 0 {
                out.push(';');
            }
            out.push_str("name=");
            out.push_str(&urlencode(&p.name));
            out.push_str(",url=");
            out.push_str(&urlencode(p.url.as_deref().unwrap_or("")));
            for field in PromisorField::ALL {
                if let Some(v) = field.value_of(p) {
                    out.push(',');
                    out.push_str(field.wire_name());
                    out.push('=');
                    out.push_str(&urlencode(v));
                }
            }
        }
        out
    }

    /// Names of the advertised remotes, in order.
    pub fn names(&self) -> impl Iterator<Item = &str> {
        self.remotes.iter().map(|r| r.name.as_str())
    }
}

/// URL-encode a field value: only `,`, `;`, `%` and bytes outside the printable ASCII range
/// are escaped, which is what the capability grammar requires.
fn urlencode(s: &str) -> String {
    use std::fmt::Write as _;
    let mut out = String::with_capacity(s.len());
    for &b in s.as_bytes() {
        if b != b',' && b != b';' && b != b'%' && (33..=126).contains(&b) {
            out.push(char::from(b));
        } else {
            let _ = write!(out, "%{b:02X}");
        }
    }
    out
}

/// Percent-decode a field value. Malformed escapes are kept as they are.
fn urldecode(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' && i + 2 < bytes.len() {
            let hex = |b: u8| char::from(b).to_digit(16);
            if let (Some(hi), Some(lo)) = (hex(bytes[i + 1]), hex(bytes[i + 2])) {
                out.push((hi * 16 + lo) as u8);
                i += 3;
                continue;
            }
        }
        out.push(bytes[i]);
        i += 1;
    }
    String::from_utf8_lossy(&out).into_owned()
}

/// Parse one `name=…,url=…` element. `None` without a name or a non-empty URL.
fn parse_one_advertised(remote_info: &str) -> Option<PromisorInfo> {
    let mut info = PromisorInfo::default();
    for elem in remote_info.split(',') {
        let Some((field, value)) = elem.split_once('=') else {
            continue;
        };
        let value = urldecode(value);
        match field {
            "name" => info.name = value,
            "url" => info.url = Some(value),
            other => {
                if let Some(f) = PromisorField::ALL
                    .into_iter()
                    .find(|f| f.wire_name() == other)
                {
                    f.set_on(&mut info, value);
                }
            }
        }
    }
    if info.name.is_empty() || info.url.as_deref().is_none_or(str::is_empty) {
        return None;
    }
    Some(info)
}

/// Names of the promisor remotes in `cfg`, in first-seen config order, with the
/// `extensions.partialClone` remote moved to the end.
///
/// A remote is a promisor remote when `remote.<name>.promisor` is true or
/// `remote.<name>.partialCloneFilter` is set, or when it is named by `extensions.partialClone`.
fn promisor_remote_names(cfg: &ConfigSet) -> Vec<String> {
    let mut names: Vec<String> = Vec::new();
    for e in cfg.entries() {
        let Some(rest) = e.key.strip_prefix("remote.") else {
            continue;
        };
        let name = if let Some(name) = rest.strip_suffix(".promisor") {
            let on = e.value.as_deref().map_or(Ok(true), parse_bool);
            on.unwrap_or(false).then_some(name)
        } else {
            rest.strip_suffix(".partialclonefilter")
        };
        if let Some(name) = name.filter(|n| !n.is_empty()) {
            if !names.iter().any(|n| n == name) {
                names.push(name.to_owned());
            }
        }
    }
    if let Some(pc) = cfg.get("extensions.partialclone").filter(|s| !s.is_empty()) {
        names.retain(|n| *n != pc);
        names.push(pc);
    }
    names
}

/// Whether `name` is a promisor remote in `cfg`: `remote.<name>.promisor` is true,
/// `remote.<name>.partialCloneFilter` is set, or `extensions.partialClone` names it.
pub(crate) fn is_promisor_remote(cfg: &ConfigSet, name: &str) -> bool {
    promisor_remote_names(cfg).iter().any(|n| n == name)
}

/// The promisor remotes configured in `cfg` that have a non-empty URL.
///
/// Remotes come in first-seen config order with the `extensions.partialClone` remote last,
/// which is the order Git advertises them and tries them for lazy fetches.
///
/// # Parameters
///
/// - `cfg`: merged config.
/// - `fields`: optional fields to read for each remote (`remote.<name>.<field>`); unset or
///   empty values are left `None`.
#[must_use]
pub fn configured_promisor_remotes(cfg: &ConfigSet, fields: &[PromisorField]) -> Vec<PromisorInfo> {
    promisor_remote_names(cfg)
        .into_iter()
        .filter_map(|name| {
            let url = cfg
                .get(&format!("remote.{name}.url"))
                .filter(|u| !u.is_empty())?;
            let mut info = PromisorInfo {
                name,
                url: Some(url),
                ..Default::default()
            };
            for &field in fields {
                if let Some(v) = cfg
                    .get(&format!("remote.{}.{}", info.name, field.config_key()))
                    .filter(|v| !v.is_empty())
                {
                    field.set_on(&mut info, v);
                }
            }
            Some(info)
        })
        .collect()
}

/// The order in which to try promisor remotes for a lazy fetch.
///
/// Names in `preferred` that are still configured promisor remotes come first, then the rest
/// in config order, with the `extensions.partialClone` remote last.
///
/// # Parameters
///
/// - `cfg`: merged config.
/// - `preferred`: remotes to try first, such as the ones a server just had the client accept.
#[must_use]
pub fn lazy_fetch_order(cfg: &ConfigSet, preferred: &[String]) -> Vec<String> {
    let all: Vec<String> = configured_promisor_remotes(cfg, &[])
        .into_iter()
        .map(|p| p.name)
        .collect();
    let mut order: Vec<String> =
        preferred
            .iter()
            .filter(|p| all.contains(p))
            .fold(Vec::new(), |mut acc, p| {
                if !acc.contains(p) {
                    acc.push(p.clone());
                }
                acc
            });
    for name in all {
        if !order.contains(&name) {
            order.push(name);
        }
    }
    order
}

/// Why an advertised promisor remote was not accepted.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
#[cfg_attr(feature = "serde-models", serde(rename_all = "snake_case"))]
pub enum PromisorRejection {
    /// `promisor.acceptFromServer` is `none` (or unset).
    PolicyNone,
    /// No local promisor remote has the advertised name.
    UnknownRemote,
    /// A local remote has the name but a different URL (`knownUrl` policy).
    UrlMismatch {
        /// The locally configured URL, with any user name and password removed.
        configured: String,
    },
    /// A `promisor.checkFields` field was not advertised or is not set locally.
    FieldMissing(PromisorField),
    /// A `promisor.checkFields` field differs between the advertisement and local config.
    FieldMismatch(PromisorField),
    /// The fetch is not a partial fetch, so the client cannot do without the objects the
    /// server would leave out.
    NotPartialFetch,
}

/// One advertised remote and the client's verdict on it.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
pub struct PromisorCandidate {
    /// The remote as the server advertised it.
    pub advertised: PromisorInfo,
    /// `None` when accepted, otherwise why not.
    pub rejection: Option<PromisorRejection>,
}

impl PromisorCandidate {
    /// Whether the client accepted this remote.
    #[must_use]
    pub fn is_accepted(&self) -> bool {
        self.rejection.is_none()
    }
}

/// The client's verdict on every advertised promisor remote.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
pub struct PromisorDecision {
    /// Every advertised remote, in advertisement order.
    pub candidates: Vec<PromisorCandidate>,
}

impl PromisorDecision {
    /// Decide which advertised remotes to accept.
    ///
    /// Follows `promisor.acceptFromServer`: `knownUrl` needs a local promisor remote with the
    /// same name and the same URL, `knownName` the same name, and `all` accepts any remote.
    /// Accepted remotes must then pass `promisor.checkFields`: each checked field must be
    /// advertised, set locally, and equal. Under `all`, a remote not known locally passes a
    /// field check when any local promisor remote has that value. Comparisons are
    /// case-sensitive.
    ///
    /// # Parameters
    ///
    /// - `policy`: the client's policy.
    /// - `local`: the client's promisor remotes, with the checked fields filled
    ///   ([`configured_promisor_remotes`] with `policy.check_fields`).
    /// - `adv`: what the server advertised.
    #[must_use]
    pub fn decide(
        policy: &PromisorAcceptPolicy,
        local: &[PromisorInfo],
        adv: &PromisorAdvertisement,
    ) -> Self {
        let candidates = adv
            .remotes
            .iter()
            .map(|advertised| PromisorCandidate {
                advertised: advertised.clone(),
                rejection: rejection_for(policy, local, advertised),
            })
            .collect();
        Self { candidates }
    }

    /// The accepted remotes, in advertisement order.
    pub fn accepted(&self) -> impl Iterator<Item = &PromisorInfo> {
        self.candidates
            .iter()
            .filter(|c| c.is_accepted())
            .map(|c| &c.advertised)
    }

    /// Names of the accepted remotes, in advertisement order.
    #[must_use]
    pub fn accepted_names(&self) -> Vec<String> {
        self.accepted().map(|p| p.name.clone()).collect()
    }

    /// Whether any remote was accepted.
    #[must_use]
    pub fn any_accepted(&self) -> bool {
        self.accepted().next().is_some()
    }

    /// The value of the client's `promisor-remote=` reply.
    ///
    /// # Returns
    ///
    /// The URL-encoded accepted names joined with `;`, or `None` when nothing was accepted
    /// (then no reply is sent).
    #[must_use]
    pub fn reply_value(&self) -> Option<String> {
        let names: Vec<String> = self.accepted().map(|p| urlencode(&p.name)).collect();
        (!names.is_empty()).then(|| names.join(";"))
    }

    /// Reject every remote that is still accepted, for `why`.
    pub fn decline_all(&mut self, why: PromisorRejection) {
        for c in &mut self.candidates {
            if c.rejection.is_none() {
                c.rejection = Some(why.clone());
            }
        }
    }

    /// The partial-clone filter implied by the accepted remotes' advertised filters, for a
    /// fetch that asked to pick its filter automatically (Git's `--filter=auto`).
    ///
    /// `blob:none` wins over any limit; otherwise the smallest `blob:limit` is used, which
    /// leaves out every blob any accepted remote expects to hold.
    ///
    /// # Returns
    ///
    /// The filter (`None` when no accepted remote advertised a usable one) and the
    /// advertised filters that could not be used because they are not `blob:none` or
    /// `blob:limit=<n>`.
    #[must_use]
    pub fn combined_filter(&self) -> (Option<ObjectFilterSpec>, Vec<String>) {
        let mut best: Option<ObjectFilterSpec> = None;
        let mut unsupported = Vec::new();
        for spec in self.accepted().filter_map(|p| p.filter.as_deref()) {
            let Ok(parsed) = ObjectFilterSpec::parse(spec) else {
                unsupported.push(spec.to_owned());
                continue;
            };
            best = Some(match (best, parsed) {
                (None, p) => p,
                (Some(ObjectFilterSpec::BlobNone), _) | (_, ObjectFilterSpec::BlobNone) => {
                    ObjectFilterSpec::BlobNone
                }
                (Some(ObjectFilterSpec::BlobLimit(a)), ObjectFilterSpec::BlobLimit(b)) => {
                    ObjectFilterSpec::BlobLimit(a.min(b))
                }
            });
        }
        (best, unsupported)
    }
}

fn rejection_for(
    policy: &PromisorAcceptPolicy,
    local: &[PromisorInfo],
    advertised: &PromisorInfo,
) -> Option<PromisorRejection> {
    let known = local.iter().find(|p| p.name == advertised.name);
    match (policy.accept, known) {
        (AcceptFromServer::None, _) => Some(PromisorRejection::PolicyNone),
        (AcceptFromServer::All, _) => fields_rejection(policy, local, advertised, None),
        (_, None) => Some(PromisorRejection::UnknownRemote),
        (AcceptFromServer::KnownName, Some(p)) => {
            fields_rejection(policy, local, advertised, Some(p))
        }
        (AcceptFromServer::KnownUrl, Some(p)) => {
            if p.url != advertised.url {
                return Some(PromisorRejection::UrlMismatch {
                    configured: p.display_url(),
                });
            }
            fields_rejection(policy, local, advertised, Some(p))
        }
    }
}

/// Check `promisor.checkFields` for one advertised remote, against `entry` when the remote is
/// known locally, otherwise against any local promisor remote.
fn fields_rejection(
    policy: &PromisorAcceptPolicy,
    local: &[PromisorInfo],
    advertised: &PromisorInfo,
    entry: Option<&PromisorInfo>,
) -> Option<PromisorRejection> {
    for &field in &policy.check_fields {
        let Some(value) = field.value_of(advertised) else {
            return Some(PromisorRejection::FieldMissing(field));
        };
        let matches = match entry {
            Some(p) => match field.value_of(p) {
                None => return Some(PromisorRejection::FieldMissing(field)),
                Some(local_value) => local_value == value,
            },
            None => local.iter().any(|p| field.value_of(p) == Some(value)),
        };
        if !matches {
            return Some(PromisorRejection::FieldMismatch(field));
        }
    }
    None
}

/// A field [`store_advertised_fields`] wrote into local config.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
pub struct StoredPromisorField {
    /// The remote whose config changed.
    pub remote: String,
    /// The field written to `remote.<remote>.<field>`.
    pub field: PromisorField,
    /// Whether a different value was there before (otherwise the key was unset).
    pub replaced: bool,
}

/// Store the advertised values of `promisor.storeFields` for accepted remotes.
///
/// Only remotes already configured locally as promisor remotes are touched; nothing is
/// created and no URL is ever written. A filter that does not parse and a token containing
/// control characters are skipped with a warning. Unchanged values are left alone.
///
/// # Parameters
///
/// - `git_dir`: the repository whose local `config` file is updated.
/// - `policy`: the client's policy; only `store_fields` is used.
/// - `cfg`: the client's merged config, for the current values.
/// - `decision`: the decision made for this fetch.
///
/// # Returns
///
/// The fields written, and the values skipped as invalid.
///
/// # Errors
///
/// Returns [`Error`] when the local config file cannot be read or written.
pub fn store_advertised_fields(
    git_dir: &Path,
    policy: &PromisorAcceptPolicy,
    cfg: &ConfigSet,
    decision: &PromisorDecision,
) -> Result<(Vec<StoredPromisorField>, Vec<PromisorConfigWarning>)> {
    let mut stored = Vec::new();
    let mut warnings = Vec::new();
    if policy.store_fields.is_empty() {
        return Ok((stored, warnings));
    }
    let local = configured_promisor_remotes(cfg, &policy.store_fields);
    let mut writes: Vec<(String, String)> = Vec::new();
    for advertised in decision.accepted() {
        let Some(known) = local.iter().find(|p| p.name == advertised.name) else {
            continue;
        };
        for &field in &policy.store_fields {
            let Some(value) = field.value_of(advertised) else {
                continue;
            };
            let current = field.value_of(known);
            if current == Some(value) {
                continue;
            }
            let valid = match field {
                PromisorField::PartialCloneFilter => {
                    crate::rev_list::ObjectFilter::parse(value).is_ok()
                }
                PromisorField::Token => !value.chars().any(char::is_control),
            };
            if !valid {
                warnings.push(PromisorConfigWarning::InvalidAdvertisedValue {
                    remote: advertised.name.clone(),
                    field,
                });
                continue;
            }
            writes.push((
                format!("remote.{}.{}", advertised.name, field.config_key()),
                value.to_owned(),
            ));
            stored.push(StoredPromisorField {
                remote: advertised.name.clone(),
                field,
                replaced: current.is_some(),
            });
        }
    }
    if writes.is_empty() {
        return Ok((stored, warnings));
    }
    let path = git_dir.join("config");
    let content = std::fs::read_to_string(&path).map_err(Error::Io)?;
    let mut file = ConfigFile::parse(&path, &content, ConfigScope::Local)?;
    for (key, value) in &writes {
        file.set(key, value)?;
    }
    file.write()?;
    Ok((stored, warnings))
}

/// What the `promisor-remote` exchange decided for one fetch.
///
/// Built by the fetch driver when the server advertises promisor remotes and returned in
/// [`FetchOutcome::promisor`](crate::transfer::FetchOutcome::promisor).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
pub struct PromisorHandshake {
    /// The verdict on every advertised remote.
    pub decision: PromisorDecision,
    /// Fields written into local config (`promisor.storeFields`).
    pub stored: Vec<StoredPromisorField>,
    /// The filter chosen from the accepted remotes, when the fetch asked for one
    /// automatically ([`FetchOptions::auto_filter`](crate::transfer::FetchOptions::auto_filter)).
    pub resolved_filter: Option<ObjectFilterSpec>,
    /// Config values that were ignored, advertised values that were not stored, and
    /// advertised filters too complex to use as the automatic filter.
    pub warnings: Vec<PromisorConfigWarning>,
}

impl PromisorHandshake {
    /// Decide the client side of the exchange.
    ///
    /// Reads the client's policy from `cfg`, decides against its configured promisor remotes,
    /// and declines everything with [`PromisorRejection::NotPartialFetch`] unless
    /// `partial_fetch`: a client that is not doing a partial fetch needs every object from
    /// this server. With `auto_filter`, also picks the filter for the fetch from the accepted
    /// remotes ([`PromisorDecision::combined_filter`]). Writes nothing; see
    /// [`Self::store_fields`].
    ///
    /// # Parameters
    ///
    /// - `cfg`: the client's merged config.
    /// - `advertisement`: what the server advertised.
    /// - `partial_fetch`: whether the fetch is filtered or is from a promisor remote.
    /// - `auto_filter`: whether to resolve the filter from the accepted remotes.
    #[must_use]
    pub fn evaluate(
        cfg: &ConfigSet,
        advertisement: &PromisorAdvertisement,
        partial_fetch: bool,
        auto_filter: bool,
    ) -> Self {
        let (policy, mut warnings) = PromisorAcceptPolicy::from_config(cfg);
        let local = configured_promisor_remotes(cfg, &policy.check_fields);
        let mut decision = PromisorDecision::decide(&policy, &local, advertisement);
        if !partial_fetch {
            decision.decline_all(PromisorRejection::NotPartialFetch);
        }
        let resolved_filter = if auto_filter {
            let (filter, unsupported) = decision.combined_filter();
            warnings.extend(
                unsupported
                    .into_iter()
                    .map(PromisorConfigWarning::UnusableAutoFilter),
            );
            filter
        } else {
            None
        };
        Self {
            decision,
            stored: Vec::new(),
            resolved_filter,
            warnings,
        }
    }

    /// Write `promisor.storeFields` values of the accepted remotes into `git_dir`'s config
    /// (see [`store_advertised_fields`]), recording what was written in [`Self::stored`].
    ///
    /// # Parameters
    ///
    /// - `git_dir`: the repository being fetched into.
    /// - `cfg`: the client's merged config, loaded before the fetch.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] when the local config file cannot be read or written.
    pub fn store_fields(&mut self, git_dir: &Path, cfg: &ConfigSet) -> Result<()> {
        let (policy, _) = PromisorAcceptPolicy::from_config(cfg);
        let (stored, warnings) = store_advertised_fields(git_dir, &policy, cfg, &self.decision)?;
        self.stored.extend(stored);
        self.warnings.extend(warnings);
        Ok(())
    }
}

/// A malformed client `promisor-remote=` reply.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum PromisorReplyError {
    /// The reply carried no remote names.
    #[error("empty promisor-remote reply")]
    Empty,
}

/// Server side: parse the value of a client's `promisor-remote=` reply into remote names.
///
/// # Parameters
///
/// - `value`: the text after `promisor-remote=`; `;`-separated, URL-encoded names.
///
/// # Errors
///
/// [`PromisorReplyError::Empty`] when no name is given.
pub fn parse_accepted_reply(value: &str) -> std::result::Result<Vec<String>, PromisorReplyError> {
    let mut names: Vec<String> = Vec::new();
    for name in value.split(';').map(urldecode).filter(|n| !n.is_empty()) {
        if !names.contains(&name) {
            names.push(name);
        }
    }
    if names.is_empty() {
        return Err(PromisorReplyError::Empty);
    }
    Ok(names)
}

/// Build the server's `promisor-remote=<info>` advertisement value.
///
/// Same as [`PromisorAdvertisement::from_server_config`] followed by
/// [`PromisorAdvertisement::to_capability_value`].
///
/// # Returns
///
/// `None` when `promisor.advertise` is not true or there are no advertisable promisor remotes.
#[must_use]
pub fn promisor_remote_info(cfg: &ConfigSet) -> Option<String> {
    PromisorAdvertisement::from_server_config(cfg).map(|a| a.to_capability_value())
}

/// The client's reply to the server's `promisor-remote` advertisement.
pub struct PromisorReply {
    /// `promisor-remote=<names>` value to send back, or `None` to send nothing.
    pub reply: Option<String>,
    /// Names of accepted remotes (decoded), in advertisement order.
    pub accepted: Vec<String>,
    /// For each accepted remote, its advertised `partialCloneFilter` (if any).
    pub accepted_filters: Vec<(String, String)>,
}

/// Parse a full server advertisement (`;`-separated `pr-fields`) into [`PromisorInfo`] entries,
/// dropping any that lack a name or URL.
///
/// Same as [`PromisorAdvertisement::parse`].
#[must_use]
pub fn parse_advertisement(info: &str) -> Vec<PromisorInfo> {
    PromisorAdvertisement::parse(info).remotes
}

/// Client side: decide which advertised promisor remotes to accept and build the reply.
///
/// A string-based wrapper over [`PromisorAcceptPolicy::from_config`] and
/// [`PromisorDecision::decide`].
///
/// # Parameters
///
/// - `cfg`: the client's config.
/// - `info`: the raw value of the server's `promisor-remote=` capability.
#[must_use]
pub fn promisor_remote_reply(cfg: &ConfigSet, info: &str) -> PromisorReply {
    let (policy, _warnings) = PromisorAcceptPolicy::from_config(cfg);
    let local = configured_promisor_remotes(cfg, &policy.check_fields);
    let decision = PromisorDecision::decide(&policy, &local, &PromisorAdvertisement::parse(info));
    PromisorReply {
        reply: decision.reply_value(),
        accepted: decision.accepted_names(),
        accepted_filters: decision
            .accepted()
            .filter_map(|p| p.filter.clone().map(|f| (p.name.clone(), f)))
            .collect(),
    }
}

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

    fn cfg(pairs: &[(&str, &str)]) -> ConfigSet {
        let mut c = ConfigSet::default();
        for (k, v) in pairs {
            c.add_command_override(k, v).unwrap();
        }
        c
    }

    fn adv(value: &str) -> PromisorAdvertisement {
        PromisorAdvertisement::parse(value)
    }

    fn decide(client: &[(&str, &str)], value: &str) -> PromisorDecision {
        let c = cfg(client);
        let (policy, _) = PromisorAcceptPolicy::from_config(&c);
        let local = configured_promisor_remotes(&c, &policy.check_fields);
        PromisorDecision::decide(&policy, &local, &adv(value))
    }

    fn rejection(d: &PromisorDecision) -> Option<PromisorRejection> {
        d.candidates[0].rejection.clone()
    }

    #[test]
    fn no_advertise_when_disabled() {
        let c = cfg(&[
            ("remote.lop.promisor", "true"),
            ("remote.lop.url", "file:///lop"),
        ]);
        assert_eq!(promisor_remote_info(&c), None);
        let c = cfg(&[
            ("promisor.advertise", "false"),
            ("remote.lop.promisor", "true"),
            ("remote.lop.url", "file:///lop"),
        ]);
        assert_eq!(PromisorAdvertisement::from_server_config(&c), None);
    }

    #[test]
    fn no_advertise_without_promisor_remotes() {
        let c = cfg(&[
            ("promisor.advertise", "true"),
            ("remote.origin.url", "file:///o"),
        ]);
        assert_eq!(promisor_remote_info(&c), None);
    }

    #[test]
    fn advertise_name_and_url() {
        let c = cfg(&[
            ("promisor.advertise", "true"),
            ("remote.lop.promisor", "true"),
            ("remote.lop.url", "file:///lop"),
        ]);
        assert_eq!(
            promisor_remote_info(&c).as_deref(),
            Some("name=lop,url=file:///lop")
        );
    }

    #[test]
    fn advertise_escapes_separators_percent_and_space() {
        let c = cfg(&[
            ("promisor.advertise", "true"),
            ("remote.lop.promisor", "true"),
            ("remote.lop.url", "file:///a b,c;d%e"),
        ]);
        assert_eq!(
            promisor_remote_info(&c).as_deref(),
            Some("name=lop,url=file:///a%20b%2Cc%3Bd%25e")
        );
    }

    #[test]
    fn advertise_send_fields() {
        let c = cfg(&[
            ("promisor.advertise", "true"),
            ("promisor.sendFields", "partialCloneFilter, token"),
            ("remote.lop.promisor", "true"),
            ("remote.lop.url", "file:///lop"),
            ("remote.lop.partialCloneFilter", "blob:none"),
            ("remote.lop.token", "fooBar"),
        ]);
        assert_eq!(
            promisor_remote_info(&c).as_deref(),
            Some("name=lop,url=file:///lop,partialCloneFilter=blob:none,token=fooBar")
        );
    }

    #[test]
    fn advertise_skips_empty_url_and_lists_partial_clone_remote_last() {
        let c = cfg(&[
            ("promisor.advertise", "true"),
            ("extensions.partialClone", "origin"),
            ("remote.origin.promisor", "true"),
            ("remote.origin.url", "file:///origin"),
            ("remote.empty.promisor", "true"),
            ("remote.empty.url", ""),
            ("remote.lop.promisor", "true"),
            ("remote.lop.url", "file:///lop"),
        ]);
        assert_eq!(
            promisor_remote_info(&c).as_deref(),
            Some("name=lop,url=file:///lop;name=origin,url=file:///origin")
        );
    }

    #[test]
    fn partial_clone_extension_alone_makes_a_promisor_remote() {
        let c = cfg(&[
            ("extensions.partialClone", "origin"),
            ("remote.origin.url", "file:///origin"),
            ("remote.other.promisor", "false"),
            ("remote.other.url", "file:///other"),
        ]);
        let names: Vec<String> = configured_promisor_remotes(&c, &[])
            .into_iter()
            .map(|p| p.name)
            .collect();
        assert_eq!(names, ["origin"]);
    }

    #[test]
    fn parse_ignores_unknown_fields_and_drops_bad_entries() {
        let a = adv(
            "name=lop,url=file:///l%20p,foo=bar,partialCloneFilter=blob:limit%3D5k;\
             name=nourl;name=empty,url=;url=file:///noname",
        );
        assert_eq!(a.remotes.len(), 1);
        assert_eq!(a.remotes[0].name, "lop");
        assert_eq!(a.remotes[0].url.as_deref(), Some("file:///l p"));
        assert_eq!(a.remotes[0].filter.as_deref(), Some("blob:limit=5k"));
        assert_eq!(parse_advertisement("name=x").len(), 0);
    }

    #[test]
    fn parse_round_trips_capability_value() {
        let original = PromisorAdvertisement {
            remotes: vec![
                PromisorInfo {
                    name: "a;b".into(),
                    url: Some("https://h/p,q".into()),
                    filter: Some("blob:none".into()),
                    token: Some("t%k".into()),
                },
                PromisorInfo {
                    name: "c".into(),
                    url: Some("file:///c".into()),
                    ..Default::default()
                },
            ],
        };
        let value = original.to_capability_value();
        assert_eq!(PromisorAdvertisement::parse(&value), original);
        assert_eq!(original.names().collect::<Vec<_>>(), ["a;b", "c"]);
    }

    #[test]
    fn field_list_parsing() {
        let (known, unknown) = PromisorField::parse_list("PartialCloneFilter token,foo token");
        assert_eq!(
            known,
            [PromisorField::PartialCloneFilter, PromisorField::Token]
        );
        assert_eq!(unknown, ["foo"]);
        assert_eq!(PromisorField::Token.config_key(), "token");
        assert_eq!(
            PromisorField::PartialCloneFilter.config_key(),
            "partialclonefilter"
        );
    }

    #[test]
    fn policy_values_are_case_insensitive_and_unknown_warns() {
        for (raw, want) in [
            ("ALL", AcceptFromServer::All),
            ("knownurl", AcceptFromServer::KnownUrl),
            ("KnownName", AcceptFromServer::KnownName),
            ("None", AcceptFromServer::None),
        ] {
            let (p, w) =
                PromisorAcceptPolicy::from_config(&cfg(&[("promisor.acceptFromServer", raw)]));
            assert_eq!(p.accept, want);
            assert!(w.is_empty());
        }
        let (p, w) = PromisorAcceptPolicy::from_config(&cfg(&[
            ("promisor.acceptFromServer", "sometimes"),
            ("promisor.checkFields", "bogus"),
        ]));
        assert_eq!(p.accept, AcceptFromServer::None);
        assert_eq!(
            w,
            [
                PromisorConfigWarning::UnknownAcceptValue("sometimes".into()),
                PromisorConfigWarning::UnknownField {
                    key: "promisor.checkFields",
                    field: "bogus".into()
                }
            ]
        );
    }

    const LOP: [(&str, &str); 2] = [
        ("remote.lop.promisor", "true"),
        ("remote.lop.url", "file:///lop"),
    ];

    #[test]
    fn accept_none_by_default() {
        let d = decide(&LOP, "name=lop,url=file:///lop");
        assert_eq!(rejection(&d), Some(PromisorRejection::PolicyNone));
        assert_eq!(d.reply_value(), None);
        assert!(!d.any_accepted());
    }

    #[test]
    fn known_url_needs_matching_url() {
        let mut c = LOP.to_vec();
        c.push(("promisor.acceptFromServer", "KnownUrl"));
        let d = decide(&c, "name=lop,url=file:///lop");
        assert_eq!(d.reply_value().as_deref(), Some("lop"));
        let d = decide(&c, "name=lop,url=file:///elsewhere");
        assert_eq!(
            rejection(&d),
            Some(PromisorRejection::UrlMismatch {
                configured: "file:///lop".into()
            })
        );
        let d = decide(&c, "name=other,url=file:///lop");
        assert_eq!(rejection(&d), Some(PromisorRejection::UnknownRemote));
    }

    #[test]
    fn known_name_ignores_url() {
        let mut c = LOP.to_vec();
        c.push(("promisor.acceptFromServer", "knownName"));
        let d = decide(&c, "name=lop,url=file:///elsewhere");
        assert!(d.any_accepted());
        let d = decide(&c, "name=other,url=file:///lop");
        assert_eq!(rejection(&d), Some(PromisorRejection::UnknownRemote));
    }

    #[test]
    fn all_accepts_unknown_remotes() {
        let d = decide(
            &[("promisor.acceptFromServer", "all")],
            "name=lop,url=file:///lop;name=x,url=file:///x",
        );
        assert_eq!(d.accepted_names(), ["lop", "x"]);
        assert_eq!(d.reply_value().as_deref(), Some("lop;x"));
    }

    #[test]
    fn check_fields_conditions() {
        let mut c = LOP.to_vec();
        c.push(("promisor.acceptFromServer", "knownName"));
        c.push(("promisor.checkFields", "partialCloneFilter"));
        // Not set locally.
        let d = decide(&c, "name=lop,url=file:///lop,partialCloneFilter=blob:none");
        assert_eq!(
            rejection(&d),
            Some(PromisorRejection::FieldMissing(
                PromisorField::PartialCloneFilter
            ))
        );
        c.push(("remote.lop.partialCloneFilter", "blob:none"));
        // Not advertised.
        let d = decide(&c, "name=lop,url=file:///lop");
        assert_eq!(
            rejection(&d),
            Some(PromisorRejection::FieldMissing(
                PromisorField::PartialCloneFilter
            ))
        );
        // Different (case-sensitive).
        let d = decide(&c, "name=lop,url=file:///lop,partialCloneFilter=BLOB:none");
        assert_eq!(
            rejection(&d),
            Some(PromisorRejection::FieldMismatch(
                PromisorField::PartialCloneFilter
            ))
        );
        let d = decide(&c, "name=lop,url=file:///lop,partialCloneFilter=blob:none");
        assert!(d.any_accepted());
    }

    #[test]
    fn check_fields_under_all_match_any_local_remote() {
        let c = [
            ("promisor.acceptFromServer", "all"),
            ("promisor.checkFields", "token"),
            ("remote.lop.promisor", "true"),
            ("remote.lop.url", "file:///lop"),
            ("remote.lop.token", "secret"),
        ];
        assert!(decide(&c, "name=new,url=file:///n,token=secret").any_accepted());
        assert_eq!(
            rejection(&decide(&c, "name=new,url=file:///n,token=other")),
            Some(PromisorRejection::FieldMismatch(PromisorField::Token))
        );
    }

    #[test]
    fn reply_value_encodes_names() {
        let d = decide(
            &[("promisor.acceptFromServer", "all")],
            "name=a%3Bb,url=file:///x",
        );
        assert_eq!(d.reply_value().as_deref(), Some("a%3Bb"));
        assert_eq!(parse_accepted_reply("a%3Bb").unwrap(), ["a;b"]);
    }

    #[test]
    fn decline_all_rejects_accepted_only() {
        let mut c = LOP.to_vec();
        c.push(("promisor.acceptFromServer", "knownName"));
        let mut d = decide(&c, "name=lop,url=file:///lop;name=x,url=file:///x");
        d.decline_all(PromisorRejection::NotPartialFetch);
        assert_eq!(
            d.candidates[0].rejection,
            Some(PromisorRejection::NotPartialFetch)
        );
        assert_eq!(
            d.candidates[1].rejection,
            Some(PromisorRejection::UnknownRemote)
        );
    }

    #[test]
    fn combined_filter_rules() {
        let all = [("promisor.acceptFromServer", "all")];
        let d = decide(
            &all,
            "name=a,url=u,partialCloneFilter=blob:limit=5k;name=b,url=v,partialCloneFilter=blob:none",
        );
        assert_eq!(
            d.combined_filter(),
            (Some(ObjectFilterSpec::BlobNone), vec![])
        );
        let d = decide(
            &all,
            "name=a,url=u,partialCloneFilter=blob:limit=5k;name=b,url=v,partialCloneFilter=blob:limit=100",
        );
        assert_eq!(
            d.combined_filter(),
            (Some(ObjectFilterSpec::BlobLimit(100)), vec![])
        );
        let d = decide(&all, "name=a,url=u,partialCloneFilter=tree:0;name=b,url=v");
        assert_eq!(d.combined_filter(), (None, vec!["tree:0".to_owned()]));
    }

    #[test]
    fn accepted_reply_parsing() {
        assert_eq!(parse_accepted_reply(""), Err(PromisorReplyError::Empty));
        assert_eq!(parse_accepted_reply(";"), Err(PromisorReplyError::Empty));
        assert_eq!(parse_accepted_reply("a;b;a").unwrap(), ["a", "b"]);
    }

    #[test]
    fn lazy_fetch_order_rules() {
        let c = cfg(&[
            ("extensions.partialClone", "origin"),
            ("remote.origin.promisor", "true"),
            ("remote.origin.url", "file:///o"),
            ("remote.a.promisor", "true"),
            ("remote.a.url", "file:///a"),
            ("remote.b.partialCloneFilter", "blob:none"),
            ("remote.b.url", "file:///b"),
        ]);
        assert_eq!(lazy_fetch_order(&c, &[]), ["a", "b", "origin"]);
        assert_eq!(
            lazy_fetch_order(&c, &["b".into(), "gone".into(), "b".into()]),
            ["b", "a", "origin"]
        );
    }

    #[test]
    fn debug_and_serialization_drop_url_credentials() {
        let p = PromisorInfo {
            name: "lop".into(),
            url: Some("https://bob:pw-secret@lop.example.com/r.git".into()),
            filter: None,
            token: Some("hunter2".into()),
        };
        let mut outputs = vec![
            format!("{p:?}"),
            format!(
                "{:?}",
                PromisorAdvertisement {
                    remotes: vec![p.clone()]
                }
            ),
        ];
        #[cfg(feature = "serde-models")]
        outputs.push(serde_json::to_string(&p).unwrap());
        for shown in outputs {
            assert!(!shown.contains("pw-secret"), "{shown}");
            assert!(!shown.contains("hunter2"), "{shown}");
            assert!(shown.contains("lop.example.com/r.git"), "{shown}");
        }

        let d = decide(
            &[
                ("remote.lop.promisor", "true"),
                (
                    "remote.lop.url",
                    "https://bob:pw-secret@lop.example.com/r.git",
                ),
                ("promisor.acceptFromServer", "knownUrl"),
            ],
            "name=lop,url=https://lop.example.com/other.git",
        );
        let Some(PromisorRejection::UrlMismatch { configured }) = rejection(&d) else {
            panic!("expected a URL mismatch");
        };
        assert!(!configured.contains("pw-secret"), "{configured}");
        assert!(configured.contains("lop.example.com/r.git"), "{configured}");
        let shown = format!("{d:?}");
        assert!(!shown.contains("pw-secret"), "{shown}");
        #[cfg(feature = "serde-models")]
        {
            let json = serde_json::to_string(&d).unwrap();
            assert!(!json.contains("pw-secret"), "{json}");
        }
    }

    #[test]
    fn debug_redacts_token() {
        let p = PromisorInfo {
            name: "lop".into(),
            url: Some("u".into()),
            filter: None,
            token: Some("hunter2".into()),
        };
        let shown = format!("{p:?}");
        assert!(!shown.contains("hunter2"), "{shown}");
        assert!(shown.contains("<redacted>"));
    }

    #[test]
    fn display_url_drops_credentials() {
        let p = PromisorInfo {
            name: "lop".into(),
            url: Some("https://bob:pw@lop.example.com/r.git".into()),
            ..Default::default()
        };
        let shown = p.display_url();
        assert!(!shown.contains("pw"), "{shown}");
        assert!(shown.contains("lop.example.com/r.git"), "{shown}");
        assert_eq!(PromisorInfo::default().display_url(), "");
    }

    #[cfg(feature = "serde-models")]
    #[test]
    fn serialized_info_skips_token() {
        let p = PromisorInfo {
            name: "lop".into(),
            url: Some("u".into()),
            filter: None,
            token: Some("hunter2".into()),
        };
        let json = serde_json::to_string(&p).unwrap();
        assert!(!json.contains("hunter2"), "{json}");
        assert!(json.contains("\"name\":\"lop\""), "{json}");
    }

    #[test]
    fn legacy_reply_wrapper() {
        let server = cfg(&[
            ("promisor.advertise", "true"),
            ("promisor.sendFields", "partialCloneFilter"),
            ("remote.lop.promisor", "true"),
            ("remote.lop.url", "file:///lop"),
            ("remote.lop.partialCloneFilter", "blob:limit=5k"),
        ]);
        let info = promisor_remote_info(&server).unwrap();
        let mut client = LOP.to_vec();
        client.push(("promisor.acceptFromServer", "All"));
        let r = promisor_remote_reply(&cfg(&client), &info);
        assert_eq!(r.reply.as_deref(), Some("lop"));
        assert_eq!(r.accepted, ["lop"]);
        assert_eq!(
            r.accepted_filters,
            [("lop".to_owned(), "blob:limit=5k".to_owned())]
        );
    }

    #[test]
    fn handshake_declines_unless_partial_and_resolves_auto_filter() {
        let mut client = LOP.to_vec();
        client.push(("promisor.acceptFromServer", "knownUrl"));
        let c = cfg(&client);
        let a = adv("name=lop,url=file:///lop,partialCloneFilter=blob:limit=5k");

        let h = PromisorHandshake::evaluate(&c, &a, false, true);
        assert_eq!(
            rejection(&h.decision),
            Some(PromisorRejection::NotPartialFetch)
        );
        assert_eq!(h.resolved_filter, None);

        let h = PromisorHandshake::evaluate(&c, &a, true, true);
        assert!(h.decision.any_accepted());
        assert_eq!(
            h.resolved_filter,
            Some(ObjectFilterSpec::BlobLimit(5 * 1024))
        );

        let h = PromisorHandshake::evaluate(&c, &a, true, false);
        assert_eq!(h.resolved_filter, None);

        let a = adv("name=lop,url=file:///lop,partialCloneFilter=tree:0");
        let h = PromisorHandshake::evaluate(&c, &a, true, true);
        assert_eq!(
            h.warnings,
            [PromisorConfigWarning::UnusableAutoFilter("tree:0".into())]
        );
    }

    #[test]
    fn handshake_store_fields() {
        let dir = repo_with_config(
            "[remote \"lop\"]\n\turl = file:///lop\n\tpromisor = true\n\
             [promisor]\n\tacceptFromServer = knownUrl\n\tstoreFields = partialCloneFilter\n",
        );
        let c = load(dir.path());
        let a = adv("name=lop,url=file:///lop,partialCloneFilter=blob:none");
        let mut h = PromisorHandshake::evaluate(&c, &a, true, false);
        h.store_fields(dir.path(), &c).unwrap();
        assert_eq!(h.stored.len(), 1);
        assert_eq!(
            load(dir.path())
                .get("remote.lop.partialclonefilter")
                .as_deref(),
            Some("blob:none")
        );
    }

    fn repo_with_config(body: &str) -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("config"), body).unwrap();
        dir
    }

    fn load(dir: &Path) -> ConfigSet {
        ConfigSet::load_repo_local_only(dir).unwrap()
    }

    #[test]
    fn store_fields_writes_known_remotes_only() {
        let dir = repo_with_config(
            "[remote \"lop\"]\n\turl = file:///lop\n\tpromisor = true\n\ttoken = old\n",
        );
        let c = load(dir.path());
        let policy = PromisorAcceptPolicy {
            accept: AcceptFromServer::All,
            check_fields: vec![],
            store_fields: vec![PromisorField::PartialCloneFilter, PromisorField::Token],
        };
        let d = PromisorDecision::decide(
            &policy,
            &configured_promisor_remotes(&c, &[]),
            &adv(
                "name=lop,url=file:///evil,partialCloneFilter=blob:limit=5k,token=new;\
                  name=unknown,url=file:///u,token=t",
            ),
        );
        let (stored, warnings) = store_advertised_fields(dir.path(), &policy, &c, &d).unwrap();
        assert!(warnings.is_empty());
        assert_eq!(
            stored,
            [
                StoredPromisorField {
                    remote: "lop".into(),
                    field: PromisorField::PartialCloneFilter,
                    replaced: false
                },
                StoredPromisorField {
                    remote: "lop".into(),
                    field: PromisorField::Token,
                    replaced: true
                }
            ]
        );
        let after = load(dir.path());
        assert_eq!(
            after.get("remote.lop.partialclonefilter").as_deref(),
            Some("blob:limit=5k")
        );
        assert_eq!(after.get("remote.lop.token").as_deref(), Some("new"));
        assert_eq!(after.get("remote.lop.url").as_deref(), Some("file:///lop"));
        assert_eq!(after.get("remote.unknown.url"), None);

        // Unchanged values are a no-op.
        let (stored, _) = store_advertised_fields(dir.path(), &policy, &after, &d).unwrap();
        assert!(stored.is_empty());
    }

    #[test]
    fn store_fields_rejects_invalid_values() {
        let dir = repo_with_config("[remote \"lop\"]\n\turl = file:///lop\n\tpromisor = true\n");
        let c = load(dir.path());
        let policy = PromisorAcceptPolicy {
            accept: AcceptFromServer::KnownName,
            check_fields: vec![],
            store_fields: vec![PromisorField::PartialCloneFilter, PromisorField::Token],
        };
        let d = PromisorDecision::decide(
            &policy,
            &configured_promisor_remotes(&c, &[]),
            &adv("name=lop,url=file:///lop,partialCloneFilter=bogus:filter,token=a%0Ab"),
        );
        let (stored, warnings) = store_advertised_fields(dir.path(), &policy, &c, &d).unwrap();
        assert!(stored.is_empty());
        assert_eq!(warnings.len(), 2);
        let after = load(dir.path());
        assert_eq!(after.get("remote.lop.token"), None);
        assert_eq!(after.get("remote.lop.partialclonefilter"), None);
    }
}