sley-config 0.5.0

Native-Rust implementation of Git's configuration system: parsing, serialization, typed values, and include resolution.
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
//! Byte-faithful in-place config editing — git's `git_config_set_multivar_in_file`.
//!
//! The canonical writer ([`crate::GitConfig::to_canonical_bytes`]) re-serialises
//! the entire document, which is fine for files sley itself authored but loses
//! fidelity when editing a *user-authored* file: it re-indents untouched lines,
//! rewrites `;` comments as `#`, collapses `[section] key = v` onto two lines,
//! drops blank lines, and normalises `key=v` spacing. Git never does this. Git
//! performs a *surgical* edit: it parses the file into a list of byte-offset
//! "events" (section headers, entries, comments, whitespace), locates the spans
//! that must change, and splices the new bytes in place — every untouched byte is
//! copied verbatim.
//!
//! This module re-implements that algorithm (`config.c:store_aux` +
//! `git_config_set_multivar_in_file_gently` + `write_pair`/`write_section` +
//! `maybe_remove_section`) so that `git config <set/unset/replace-all/add>` and
//! `--rename-section`/`--remove-section` preserve the original file byte-for-byte
//! apart from the lines they genuinely touch.

use std::fmt;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

use crate::quote_config_value;

/// A value-pattern predicate: given an entry's value (`None` for a bare
/// boolean-true key), report whether it matches git's value-pattern. The
/// `'a` borrow lets callers close over a compiled regex.
pub type ValueMatcher<'a> = &'a dyn Fn(Option<&str>) -> bool;

/// The kind of a parsed event, mirroring git's `enum config_event_t`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EventType {
    Section,
    Entry,
    Comment,
    Whitespace,
}

/// One parsed element with its byte span `[begin, end)` in the source.
#[derive(Debug, Clone)]
struct Event {
    ty: EventType,
    begin: usize,
    end: usize,
    /// For `Section` events: whether this header names the target section.
    /// For `Entry` events: whether the entry is inside such a section, so an
    /// absent key can be inserted there.
    is_keys_section: bool,
    /// For `Entry` events: whether the entry's section/subsection prefix is an
    /// exact key match. Deprecated dotted headers are section-compatible
    /// case-insensitively, but their parsed subsection is lower-cased for full
    /// key matching.
    is_key_match_section: bool,
    /// For `Entry` events: the parsed key (lower-cased) and decoded value.
    key: Option<String>,
    value: Option<String>,
}

/// Result of [`RawConfigEditor::set_multivar`] mirroring git's `CONFIG_*` codes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RawEditOutcome {
    /// The edit was applied; the buffer was rewritten.
    Changed,
    /// Nothing matched on an unset, or more than one entry matched a
    /// single-replace set (git's `CONFIG_NOTHING_SET`, exit code 5).
    NothingSet,
}

/// Options for writing a config file through `<path>.lock`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ConfigFileWriteOptions {
    /// Sync the lockfile contents to disk before promoting it into place.
    pub fsync: bool,
}

/// A config file write failed before the lockfile could be atomically promoted.
#[derive(Debug)]
pub enum ConfigFileWriteError {
    /// `<path>.lock` already exists; the original config was left untouched.
    ExistingLock(PathBuf),
    /// Filesystem failure at the given path.
    Io {
        path: PathBuf,
        source: std::io::Error,
    },
}

/// A locked config read-modify-write transaction failed either while applying
/// the caller's edit or while managing the lockfile.
#[derive(Debug)]
pub enum ConfigFileEditError<E> {
    /// The caller rejected or could not construct the replacement bytes.
    Edit(E),
    /// Lock acquisition, reading, writing, or atomic promotion failed.
    Write(ConfigFileWriteError),
}

impl<E: fmt::Display> fmt::Display for ConfigFileEditError<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Edit(error) => error.fmt(f),
            Self::Write(error) => error.fmt(f),
        }
    }
}

impl<E> std::error::Error for ConfigFileEditError<E>
where
    E: std::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Edit(error) => Some(error),
            Self::Write(error) => Some(error),
        }
    }
}

impl fmt::Display for ConfigFileWriteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ExistingLock(path) => {
                write!(f, "config lock already exists: {}", path.display())
            }
            Self::Io { path, source } => write!(f, "{}: {source}", path.display()),
        }
    }
}

impl std::error::Error for ConfigFileWriteError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::ExistingLock(_) => None,
            Self::Io { source, .. } => Some(source),
        }
    }
}

impl ConfigFileWriteError {
    fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
        Self::Io {
            path: path.into(),
            source,
        }
    }
}

/// Write `contents` to `path` via `<path>.lock` and an atomic rename.
///
/// The helper mirrors git's config writer shape: create the parent directory if
/// needed, refuse to reuse an existing lockfile, write the complete replacement
/// bytes to the lock, optionally fsync them, then promote the lock into place.
/// On errors before promotion the lockfile is removed when possible and the
/// original config file is left untouched.
///
/// Two behaviours match git's lockfile + config-writer plumbing exactly:
///
/// * **Symlink dereference.** git's `hold_lock_file_for_update` calls
///   `resolve_symlink` before forming the `.lock` path, so when the config path
///   is a symlink the lock (and the eventual rename) target the *resolved* real
///   file. We replicate this so `git config --file=<symlink>` updates the
///   pointed-at file and leaves the symlink intact (t1300 "symlinked
///   configuration").
/// * **Mode preservation.** git's config writer `chmod`s the lockfile to the
///   original file's permission bits (`st.st_mode & 07777`) before promoting it,
///   so an existing `0600` config stays `0600` across edits (t1300 "preserves
///   existing permissions"). We do the same on Unix.
pub fn write_config_file_locked(
    path: impl AsRef<Path>,
    contents: &[u8],
    options: ConfigFileWriteOptions,
) -> Result<(), ConfigFileWriteError> {
    // git resolves the symlink chain (`resolve_symlink`) before locking, so the
    // lock, the mode probe, and the final rename all act on the real file — the
    // original symlink is never replaced by a regular file.
    let path = resolve_symlink(path.as_ref());
    let path = path.as_path();
    if let Some(parent) = path.parent()
        && !parent.as_os_str().is_empty()
    {
        fs::create_dir_all(parent).map_err(|err| ConfigFileWriteError::io(parent, err))?;
    }

    let lock_path = config_lock_path(path)?;
    let mut lock = ConfigFileLock::acquire(lock_path)?;
    lock.write_all(contents, options.fsync)?;
    let lock_path = lock.close();
    // git: `chmod(lockfile, st.st_mode & 07777)` using the original config's
    // mode, before the rename, so an existing file's permissions are preserved.
    preserve_existing_mode(path, &lock_path);
    if let Err(err) = fs::rename(&lock_path, path) {
        let _ = fs::remove_file(&lock_path);
        return Err(ConfigFileWriteError::io(path, err));
    }
    Ok(())
}

/// Atomically edit a config file while holding `<path>.lock` across the entire
/// read-modify-write transaction.
///
/// The callback receives the bytes read after the lock is acquired (or an
/// empty slice when the config does not yet exist) and returns the complete
/// replacement. This prevents two editors from both reading stale bytes before
/// serializing their writes. Symlink resolution and permission preservation
/// match [`write_config_file_locked`].
pub fn edit_config_file_locked<E>(
    path: impl AsRef<Path>,
    options: ConfigFileWriteOptions,
    edit: impl FnOnce(&[u8]) -> Result<Vec<u8>, E>,
) -> Result<(), ConfigFileEditError<E>> {
    let path = resolve_symlink(path.as_ref());
    let path = path.as_path();
    if let Some(parent) = path.parent()
        && !parent.as_os_str().is_empty()
    {
        fs::create_dir_all(parent)
            .map_err(|error| ConfigFileEditError::Write(ConfigFileWriteError::io(parent, error)))?;
    }

    let lock_path = config_lock_path(path).map_err(ConfigFileEditError::Write)?;
    let mut lock = ConfigFileLock::acquire(lock_path).map_err(ConfigFileEditError::Write)?;
    let original = match fs::read(path) {
        Ok(bytes) => bytes,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Vec::new(),
        Err(error) => {
            return Err(ConfigFileEditError::Write(ConfigFileWriteError::io(
                path, error,
            )));
        }
    };
    let replacement = edit(&original).map_err(ConfigFileEditError::Edit)?;
    lock.write_all(&replacement, options.fsync)
        .map_err(ConfigFileEditError::Write)?;
    let lock_path = lock.close();
    preserve_existing_mode(path, &lock_path);
    if let Err(error) = fs::rename(&lock_path, path) {
        let _ = fs::remove_file(&lock_path);
        return Err(ConfigFileEditError::Write(ConfigFileWriteError::io(
            path, error,
        )));
    }
    Ok(())
}

/// git's `resolve_symlink` (`lockfile.c`): if `path` is a symlink, follow the
/// chain (up to a depth bound) to the real file, which may or may not exist.
/// An absolute link target replaces the whole path; a relative one replaces the
/// last path component. Non-symlinks and unreadable links are returned as-is —
/// this is a best-effort routine, exactly like git's.
fn resolve_symlink(path: &Path) -> PathBuf {
    // Mirror git's MAXDEPTH so a symlink cycle terminates instead of looping.
    const MAXDEPTH: usize = 5;
    let mut current = path.to_path_buf();
    for _ in 0..MAXDEPTH {
        match fs::read_link(&current) {
            Ok(link) => {
                if link.is_absolute() {
                    current = link;
                } else {
                    // Replace the last component with the relative target.
                    current.pop();
                    current.push(link);
                }
            }
            // Not a symlink (or unreadable): git stops and uses what it has.
            Err(_) => break,
        }
    }
    current
}

/// git's config-writer `chmod(lockfile, st.st_mode & 07777)`: copy the existing
/// config file's permission bits onto the lockfile before it is renamed into
/// place, so edits preserve the original mode. No-op when the file does not yet
/// exist (a fresh config keeps the lockfile's default mode, as in git) or on
/// non-Unix platforms (where mode bits do not apply).
#[cfg(unix)]
fn preserve_existing_mode(path: &Path, lock_path: &Path) {
    use std::os::unix::fs::PermissionsExt;
    if let Ok(meta) = fs::metadata(path) {
        let mode = meta.permissions().mode() & 0o7777;
        let _ = fs::set_permissions(lock_path, fs::Permissions::from_mode(mode));
    }
}

#[cfg(not(unix))]
fn preserve_existing_mode(_path: &Path, _lock_path: &Path) {}

fn config_lock_path(path: &Path) -> Result<PathBuf, ConfigFileWriteError> {
    let file_name = path.file_name().ok_or_else(|| {
        ConfigFileWriteError::io(
            path,
            std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "config path has no filename",
            ),
        )
    })?;
    let mut lock_name = file_name.to_os_string();
    lock_name.push(".lock");
    Ok(path.with_file_name(lock_name))
}

struct ConfigFileLock {
    path: PathBuf,
    file: Option<fs::File>,
    active: bool,
}

impl ConfigFileLock {
    fn acquire(path: PathBuf) -> Result<Self, ConfigFileWriteError> {
        match fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&path)
        {
            Ok(file) => Ok(Self {
                path,
                file: Some(file),
                active: true,
            }),
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
                Err(ConfigFileWriteError::ExistingLock(path))
            }
            Err(err) => Err(ConfigFileWriteError::io(path, err)),
        }
    }

    fn write_all(&mut self, contents: &[u8], fsync: bool) -> Result<(), ConfigFileWriteError> {
        let Some(file) = self.file.as_mut() else {
            return Err(ConfigFileWriteError::io(
                &self.path,
                std::io::Error::other("config lock is already closed"),
            ));
        };
        file.write_all(contents)
            .map_err(|err| ConfigFileWriteError::io(&self.path, err))?;
        if fsync {
            file.sync_all()
                .map_err(|err| ConfigFileWriteError::io(&self.path, err))?;
        }
        Ok(())
    }

    fn close(mut self) -> PathBuf {
        self.active = false;
        let _ = self.file.take();
        self.path.clone()
    }
}

impl Drop for ConfigFileLock {
    fn drop(&mut self) {
        if self.active {
            let _ = self.file.take();
            let _ = fs::remove_file(&self.path);
        }
    }
}

/// A byte-faithful editor over a single config file's raw contents.
pub struct RawConfigEditor {
    contents: Vec<u8>,
    events: Vec<Event>,
    /// The matched key section's normalised `section.subsection` prefix used for
    /// header synthesis when the key/section is absent.
    section: String,
    subsection: Option<String>,
    /// The variable name (suffix after the last dot), lower-cased for matching.
    name: String,
    /// The variable name as typed on the command line, written verbatim into a
    /// newly-synthesised line (git's `write_pair` preserves the caller's case,
    /// e.g. `git config Section.Movie x` writes `Movie`, not `movie`).
    name_as_typed: String,
}

impl RawConfigEditor {
    /// Parse `contents` into byte-offset events for the variable `key`
    /// (`section[.subsection].name`). Section/name comparisons are
    /// case-insensitive; quoted subsections are case-sensitive. The `section`
    /// and `name` are written verbatim (preserving the caller's case) when a new
    /// header/line must be synthesised.
    pub fn new(contents: Vec<u8>, section: &str, subsection: Option<&str>, name: &str) -> Self {
        let mut editor = Self {
            contents,
            events: Vec::new(),
            section: section.to_string(),
            subsection: subsection.map(str::to_string),
            name: name.to_ascii_lowercase(),
            name_as_typed: name.to_string(),
        };
        editor.parse_events();
        editor
    }

    /// Walk the raw bytes emitting contiguous events with byte spans, matching
    /// git's `git_parse_source` event sequence. Each event's `end` is the next
    /// event's `begin` (git makes spans contiguous via `do_event`).
    fn parse_events(&mut self) {
        let bytes = self.contents.clone();
        let len = bytes.len();
        let mut i = 0usize;
        // Whether the most recent section header names the target section, so
        // entries know whether they belong to it.
        let mut cur_is_keys_section = false;
        let mut cur_is_key_match_section = false;

        // Skip a UTF-8 BOM exactly as git does.
        if bytes.starts_with(b"\xEF\xBB\xBF") {
            i = 3;
        }

        while i < len {
            let c = bytes[i];
            if c == b'\n' {
                self.push_ws(i, i + 1);
                i += 1;
                continue;
            }
            if c == b' ' || c == b'\t' || c == b'\r' {
                // Coalesce a run of whitespace into one event (git collapses
                // consecutive WHITESPACE events).
                let begin = i;
                while i < len && matches!(bytes[i], b' ' | b'\t' | b'\r') {
                    i += 1;
                }
                self.push_ws(begin, i);
                continue;
            }
            if c == b'#' || c == b';' {
                // Comment runs to end of line (the newline is a separate WS event).
                let begin = i;
                while i < len && bytes[i] != b'\n' {
                    i += 1;
                }
                self.events.push(Event {
                    ty: EventType::Comment,
                    begin,
                    end: i,
                    is_keys_section: false,
                    is_key_match_section: false,
                    key: None,
                    value: None,
                });
                continue;
            }
            if c == b'[' {
                let begin = i;
                let (section, sub, subsection_case_sensitive, next) =
                    parse_section_header(&bytes, i);
                i = next;
                let is_keys = section.as_ref().is_some_and(|s| {
                    self.section_matches(s, sub.as_deref(), subsection_case_sensitive)
                });
                let is_key_match = section
                    .as_ref()
                    .is_some_and(|s| self.section_exact_matches(s, sub.as_deref()));
                cur_is_keys_section = is_keys;
                cur_is_key_match_section = is_key_match;
                self.events.push(Event {
                    ty: EventType::Section,
                    begin,
                    end: i,
                    is_keys_section: is_keys,
                    is_key_match_section: is_key_match,
                    key: None,
                    value: None,
                });
                continue;
            }
            if c.is_ascii_alphabetic() {
                let begin = i;
                let (key, value, next) = parse_entry(&bytes, i);
                i = next;
                self.events.push(Event {
                    ty: EventType::Entry,
                    begin,
                    end: i,
                    is_keys_section: cur_is_keys_section,
                    is_key_match_section: cur_is_key_match_section,
                    key,
                    value,
                });
                continue;
            }
            // An unexpected character: treat the rest of the line as opaque so we
            // never corrupt a file we cannot fully model (parse already validated
            // it upstream before this editor runs).
            let begin = i;
            while i < len && bytes[i] != b'\n' {
                i += 1;
            }
            self.push_ws(begin, i);
        }
    }

    fn push_ws(&mut self, begin: usize, end: usize) {
        if begin >= end {
            return;
        }
        // git collapses consecutive WHITESPACE events into one.
        if let Some(last) = self.events.last_mut()
            && last.ty == EventType::Whitespace
            && last.end == begin
        {
            last.end = end;
            return;
        }
        self.events.push(Event {
            ty: EventType::Whitespace,
            begin,
            end,
            is_keys_section: false,
            is_key_match_section: false,
            key: None,
            value: None,
        });
    }

    fn section_matches(
        &self,
        section: &str,
        subsection: Option<&str>,
        subsection_case_sensitive: bool,
    ) -> bool {
        if !section.eq_ignore_ascii_case(&self.section) {
            return false;
        }
        match (self.subsection.as_deref(), subsection) {
            (None, None) => true,
            (Some(a), Some(b)) => {
                if subsection_case_sensitive {
                    a == b
                } else {
                    a.eq_ignore_ascii_case(b)
                }
            }
            _ => false,
        }
    }

    fn section_exact_matches(&self, section: &str, subsection: Option<&str>) -> bool {
        if !section.eq_ignore_ascii_case(&self.section) {
            return false;
        }
        match (self.subsection.as_deref(), subsection) {
            (None, None) => true,
            (Some(a), Some(b)) => a == b,
            _ => false,
        }
    }

    /// Apply git's `git_config_set_multivar_in_file_gently`.
    ///
    /// * `value == None` → unset (remove matching entries; error if none match).
    /// * `value == Some` → set: replace matching entries with one new pair.
    /// * `value_matches` filters which existing entries are considered a match
    ///   (git's value-pattern; pass a closure that already folds in `!` negation).
    ///   `None` means "every entry of the key matches".
    /// * `multi_replace` is git's `CONFIG_FLAGS_MULTI_REPLACE` (`--replace-all`):
    ///   when false and more than one entry matches, the edit is refused
    ///   (`NothingSet`).
    ///
    /// On `Changed`, the internal buffer is rewritten; read it with
    /// [`RawConfigEditor::into_bytes`].
    pub fn set_multivar(
        &mut self,
        value: Option<&str>,
        comment: Option<&str>,
        value_matches: Option<ValueMatcher>,
        multi_replace: bool,
    ) -> RawEditOutcome {
        // Faithfully replicate git's `store.seen[]` / `seen_nr` / `key_seen` /
        // `section_seen` state machine (`store_aux` + `store_aux_event`).
        //
        // `seen` carries `seen_nr` *committed* matches plus one optional
        // *speculative* slot at `seen[seen_nr]` (git writes the slot before it
        // knows whether the entry matches, and only bumps `seen_nr` on a match).
        let mut seen: Vec<usize> = Vec::new();
        let mut seen_nr = 0usize;
        let mut key_seen = false;
        let mut section_seen = false;

        let set_slot = |seen: &mut Vec<usize>, slot: usize, idx: usize| {
            if seen.len() <= slot {
                seen.resize(slot + 1, 0);
            }
            seen[slot] = idx;
        };

        for (idx, ev) in self.events.iter().enumerate() {
            match ev.ty {
                EventType::Section if ev.is_keys_section => {
                    // store_aux_event: record speculatively, mark section_seen.
                    section_seen = true;
                    set_slot(&mut seen, seen_nr, idx);
                }
                EventType::Entry => {
                    if key_seen {
                        if self.entry_matches(ev, value_matches) {
                            set_slot(&mut seen, seen_nr, idx);
                            seen_nr += 1;
                        }
                    } else if ev.is_keys_section {
                        set_slot(&mut seen, seen_nr, idx);
                        section_seen = true;
                        if self.entry_matches(ev, value_matches) {
                            seen_nr += 1;
                            key_seen = true;
                        }
                    }
                }
                _ => {}
            }
        }

        // Error conditions (git): nothing to unset, or >1 match on single replace.
        if (seen_nr == 0 && value.is_none()) || (seen_nr > 1 && !multi_replace) {
            return RawEditOutcome::NothingSet;
        }

        // git: when seen_nr == 0 here (insert, key absent), fall back to the
        // speculative slot (last entry of the target section, or the section
        // header, or — if even the section is absent — the last parsed element).
        if seen_nr == 0 {
            if seen.is_empty()
                && let Some(last) = self.events.len().checked_sub(1)
            {
                // Did not see key nor section: target the last parsed element.
                seen.push(last);
            }
            // else: keep the speculative slot at index 0.
            seen_nr = 1;
        }

        let seen_indices: Vec<usize> = seen.into_iter().take(seen_nr).collect();
        self.splice(&seen_indices, key_seen, section_seen, value, comment);
        RawEditOutcome::Changed
    }

    fn entry_matches(&self, ev: &Event, value_matches: Option<ValueMatcher>) -> bool {
        // git's `matches()` compares the FULL key (`section.subsection.name`), so
        // an entry only matches when it is BOTH in the target section and has the
        // target variable name. (Without the section guard a same-named variable
        // in a different section would be wrongly collected — t1300 #235.)
        if !ev.is_key_match_section || ev.key.as_deref() != Some(self.name.as_str()) {
            return false;
        }
        match value_matches {
            None => true,
            Some(pred) => pred(ev.value.as_deref()),
        }
    }

    /// The core byte-splice, mirroring the final loop of
    /// `git_config_set_multivar_in_file_gently`. A single loop drives both set
    /// (replace each matched entry with one new pair at the end) and unset
    /// (delete each matched entry's span, optionally extending to swallow an
    /// emptied section).
    fn splice(
        &mut self,
        seen: &[usize],
        key_seen: bool,
        section_seen: bool,
        value: Option<&str>,
        comment: Option<&str>,
    ) {
        let contents = self.contents.clone();
        let contents_sz = contents.len();
        let mut out: Vec<u8> = Vec::with_capacity(contents_sz + 64);
        let mut copy_begin = 0usize;

        let mut i = 0usize;
        while i < seen.len() {
            let j = seen[i];
            let mut new_line = false;
            let copy_end;
            let replace_end;
            if !key_seen {
                // Inserting a fresh key after the speculative slot (section header
                // or last entry of the section). Copy up to its end; include the
                // trailing '\n' when present.
                let mut ce = self.events[j].end;
                if ce > 0 && ce < contents_sz && contents[ce - 1] != b'\n' && contents[ce] == b'\n'
                {
                    ce += 1;
                }
                copy_end = ce;
                replace_end = ce;
            } else {
                let mut re = self.events[j].end;
                let mut ce = self.events[j].begin;
                if value.is_none() {
                    // Unset: maybe extend to swallow the whole emptied section.
                    let (nb, ne) = self.maybe_remove_section(seen, &mut i, ce, re);
                    ce = nb;
                    re = ne;
                }
                // Swallow preceding whitespace on the same line.
                while ce > 0 {
                    let ch = contents[ce - 1];
                    if (ch == b' ' || ch == b'\t' || ch == b'\r') && ch != b'\n' {
                        ce -= 1;
                    } else {
                        break;
                    }
                }
                copy_end = ce;
                replace_end = re;
            }

            if copy_end > 0 && contents[copy_end - 1] != b'\n' {
                new_line = true;
            }
            if copy_end > copy_begin {
                out.extend_from_slice(&contents[copy_begin..copy_end]);
                if new_line {
                    out.push(b'\n');
                }
            }
            copy_begin = replace_end;
            i += 1;
        }

        // Write the new pair (value == None means pure unset).
        if let Some(value) = value {
            if !section_seen {
                write_section(&mut out, &self.section, self.subsection.as_deref());
            }
            write_pair(&mut out, &self.name_as_typed, value, comment);
        }

        if copy_begin < contents_sz {
            out.extend_from_slice(&contents[copy_begin..contents_sz]);
        }

        self.contents = out;
    }

    /// git's `maybe_remove_section`: if unsetting the first/only key of a section
    /// and there are no comments inside or just before the section, extend the
    /// removal span to cover the whole section header and any trailing entries.
    ///
    /// Returns the (begin, end) of the span to remove.
    fn maybe_remove_section(
        &self,
        seen: &[usize],
        seen_ptr: &mut usize,
        begin_in: usize,
        end_in: usize,
    ) -> (usize, usize) {
        // git writes to `*begin_offset`/`*end_offset` (and `*seen_ptr`) only on
        // the final success; every early return leaves the caller's span
        // untouched. So we compute a *local* `begin`/`end`/`seen_idx` and only
        // commit them at the end.
        let parsed = &self.events;
        let parsed_nr = parsed.len();
        let mut seen_idx = *seen_ptr;

        // First, ensure this is the section's first key and that no comment
        // precedes the entry or its section header. Mirrors git's
        // `for (i = seen[seen]; i > 0; i--)` over `parsed[i-1]`, breaking with `i`
        // pointing at the keys-section header (or 0).
        let mut section_seen = false;
        let mut i = seen[seen_idx];
        while i > 0 {
            let ty = parsed[i - 1].ty;
            match ty {
                EventType::Comment => return (begin_in, end_in),
                EventType::Entry => {
                    if !section_seen {
                        return (begin_in, end_in);
                    }
                    break;
                }
                EventType::Section => {
                    if !parsed[i - 1].is_keys_section {
                        break;
                    }
                    section_seen = true;
                    i -= 1;
                }
                EventType::Whitespace => {
                    i -= 1;
                }
            }
        }
        let begin = parsed[i].begin;

        // Next, ensure we remove the last key(s) and there are no enclosing or
        // surrounding comments.
        let mut k = seen[seen_idx] + 1;
        while k < parsed_nr {
            let ty = parsed[k].ty;
            match ty {
                EventType::Comment => return (begin_in, end_in),
                EventType::Section => {
                    if parsed[k].is_keys_section {
                        k += 1;
                        continue;
                    }
                    break;
                }
                EventType::Entry => {
                    seen_idx += 1;
                    if seen_idx < seen.len() && k == seen[seen_idx] {
                        // We want to remove this entry too.
                        k += 1;
                        continue;
                    }
                    // Another entry survives in this section.
                    return (begin_in, end_in);
                }
                EventType::Whitespace => {
                    k += 1;
                }
            }
        }

        // Really removing the section's last entry/entries with no comments.
        *seen_ptr = seen_idx;
        let end = if k < parsed_nr {
            parsed[k].begin
        } else {
            parsed[parsed_nr - 1].end
        };
        (begin, end)
    }

    pub fn into_bytes(self) -> Vec<u8> {
        self.contents
    }
}

/// git's `GIT_CONFIG_MAX_LINE_LEN`.
const GIT_CONFIG_MAX_LINE_LEN: usize = 512 * 1024;

/// Outcome of [`rename_or_remove_section`].
pub enum SectionEditOutcome {
    /// One or more sections matched; the new file bytes are returned.
    Changed(Vec<u8>),
    /// No section matched `old_name` (git's `ret == 0` → exit 128).
    NotFound,
    /// A line exceeded `GIT_CONFIG_MAX_LINE_LEN`; carries the 1-based line
    /// number for git's "refusing to work with overly long line" diagnostic.
    LineTooLong(usize),
}

/// git's `repo_config_copy_or_rename_section_in_file` (copy = false): rename
/// every `[old_name]` header to `[new_name]` (when `new_name` is `Some`) or
/// remove the matching sections (`new_name == None`), preserving every other
/// byte. `old_name`/`new_name` are git's normalised `section[.subsection]`
/// form (section lower-cased).
pub fn rename_or_remove_section(
    contents: &[u8],
    old_name: &str,
    new_name: Option<&str>,
) -> SectionEditOutcome {
    let mut out: Vec<u8> = Vec::with_capacity(contents.len() + 16);
    let mut matched = 0usize;
    let mut removing = false;
    let mut line_nr = 0usize;
    let mut pos = 0usize;
    let len = contents.len();

    while pos < len {
        // Read one whole physical line including its trailing '\n'.
        let line_start = pos;
        while pos < len && contents[pos] != b'\n' {
            pos += 1;
        }
        if pos < len {
            pos += 1; // include the '\n'
        }
        let line = &contents[line_start..pos];
        line_nr += 1;

        if line.len() >= GIT_CONFIG_MAX_LINE_LEN {
            return SectionEditOutcome::LineTooLong(line_nr);
        }

        // Find the first non-space char (git skips leading whitespace).
        let mut i = 0usize;
        while i < line.len() && (line[i] as char).is_whitespace() {
            i += 1;
        }
        if i < line.len() && line[i] == b'[' {
            let offset = section_name_match(&line[i..], old_name);
            if offset > 0 {
                matched += 1;
                if let Some(new_name) = new_name {
                    write_section_normalised(&mut out, new_name);
                    // Skip the old section header; emit the remainder of the line
                    // (an inline declaration) indented with a tab. git gobbles the
                    // trailing newline into `offset` for a bare header, so a header
                    // with nothing after it leaves `consumed == line.len()`.
                    let consumed = i + offset;
                    if consumed < line.len() {
                        // There is more content on this line beyond the header.
                        out.push(b'\t');
                        out.extend_from_slice(&line[consumed..]);
                    }
                    removing = false;
                    continue;
                } else {
                    // Remove mode: drop this header and following lines until the
                    // next section.
                    removing = true;
                    continue;
                }
            }
            removing = false;
        }
        if removing {
            continue;
        }
        out.extend_from_slice(line);
    }

    if matched == 0 {
        SectionEditOutcome::NotFound
    } else {
        SectionEditOutcome::Changed(out)
    }
}

/// git's `section_name_match`: returns the byte length consumed (including
/// trailing whitespace after `]`) when `buf` (starting at `[`) is a header for
/// `name` (`section[.subsection]`), else 0.
fn section_name_match(buf: &[u8], name: &str) -> usize {
    // Treat `buf` as a NUL-terminated C string: `gb(k)` returns 0 past the end,
    // mirroring git's reliance on the trailing NUL to stop the scan.
    let nb = name.as_bytes();
    let gb = |k: usize| -> u8 { *buf.get(k).unwrap_or(&0) };
    let gn = |k: usize| -> u8 { *nb.get(k).unwrap_or(&0) };

    if gb(0) != b'[' {
        return 0;
    }
    // Faithful port of git's `for (i = 1; buf[i] && buf[i] != ']'; i++)`: the
    // loop's `i++` fires at the end of EVERY iteration, including after the
    // `continue` in the dot-transition branch (this is the subtle part — the
    // opening `"` of a quoted subsection is skipped by that trailing `i++`).
    let mut i = 1usize;
    let mut j = 0usize;
    let mut dot = false;
    // git's `isspace`: space, tab, newline, CR, form-feed, vertical-tab. The
    // trailing-whitespace gobble after `]` deliberately swallows the line's `\n`,
    // so a bare header consumes its whole line (leaving no inline remainder).
    let is_space =
        |c: u8| c == b' ' || c == b'\t' || c == b'\n' || c == b'\r' || c == 0x0c || c == 0x0b;
    'outer: while gb(i) != 0 && gb(i) != b']' {
        // `continue` in C re-runs the for-increment; emulate with a flag.
        let mut did_continue = false;
        if !dot && is_space(gb(i)) {
            dot = true;
            let nj = gn(j);
            j += 1;
            if nj != b'.' {
                break;
            }
            // for (i++; isspace(buf[i]); i++);
            i += 1;
            while is_space(gb(i)) {
                i += 1;
            }
            if gb(i) != b'"' {
                break;
            }
            did_continue = true;
        }
        if !did_continue {
            if gb(i) == b'\\' && dot {
                i += 1;
            } else if gb(i) == b'"' && dot {
                i += 1;
                while is_space(gb(i)) {
                    i += 1;
                }
                break 'outer;
            }
            // buf[i] != name[j++]
            let bc = gb(i);
            let nc = gn(j);
            j += 1;
            if bc != nc {
                break 'outer;
            }
        }
        i += 1; // the for-loop increment, run for every iteration
    }
    if gb(i) == b']' && gn(j) == 0 {
        i += 1;
        while gb(i) != 0 && is_space(gb(i)) {
            i += 1;
        }
        return i;
    }
    0
}

/// git's `store_create_section`/`write_section` for the rename path: render the
/// normalised `section[.subsection]` name as `[section "subsection"]` (or
/// `[section]`) followed by a newline.
fn write_section_normalised(out: &mut Vec<u8>, name: &str) {
    out.push(b'[');
    if let Some((section, subsection)) = name.split_once('.') {
        out.extend_from_slice(section.as_bytes());
        out.extend_from_slice(b" \"");
        for ch in subsection.chars() {
            if ch == '"' || ch == '\\' {
                out.push(b'\\');
            }
            let mut b = [0u8; 4];
            out.extend_from_slice(ch.encode_utf8(&mut b).as_bytes());
        }
        out.push(b'"');
    } else {
        out.extend_from_slice(name.as_bytes());
    }
    out.extend_from_slice(b"]\n");
}

/// git's `write_pair`: always `\t<name> = <quoted-value>[<comment>]\n`.
fn write_pair(out: &mut Vec<u8>, name: &str, value: &str, comment: Option<&str>) {
    out.push(b'\t');
    out.extend_from_slice(name.as_bytes());
    out.extend_from_slice(b" = ");
    out.extend_from_slice(quote_config_value(value).as_bytes());
    if let Some(comment) = comment {
        out.extend_from_slice(comment.as_bytes());
    }
    out.push(b'\n');
}

/// git's `write_section`: `[<section>]\n` or `[<section> "<subsection>"]\n`.
fn write_section(out: &mut Vec<u8>, section: &str, subsection: Option<&str>) {
    out.push(b'[');
    out.extend_from_slice(section.as_bytes());
    if let Some(sub) = subsection {
        out.extend_from_slice(b" \"");
        for ch in sub.chars() {
            if ch == '"' || ch == '\\' {
                out.push(b'\\');
            }
            let mut buf = [0u8; 4];
            out.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
        }
        out.push(b'"');
    }
    out.extend_from_slice(b"]\n");
}

/// Parse a section header starting at `bytes[start] == b'['`, returning the
/// (section, subsection, subsection_case_sensitive, next_index). On a malformed
/// header the whole rest of the line is consumed and `None` is returned for the
/// name.
fn parse_section_header(
    bytes: &[u8],
    start: usize,
) -> (Option<String>, Option<String>, bool, usize) {
    let len = bytes.len();
    let mut i = start + 1; // past '['
    let name_start = i;
    while i < len {
        let c = bytes[i];
        if c.is_ascii_alphanumeric() || c == b'-' || c == b'.' {
            i += 1;
        } else {
            break;
        }
    }
    let raw_name = String::from_utf8_lossy(&bytes[name_start..i]).into_owned();

    // Dotted deprecated form: `[section.subsection]`.
    if let Some((head, rest)) = raw_name.split_once('.') {
        // consume up to closing ']'
        while i < len && bytes[i] != b']' && bytes[i] != b'\n' {
            i += 1;
        }
        if i < len && bytes[i] == b']' {
            i += 1;
        }
        return (
            Some(head.to_ascii_lowercase()),
            Some(rest.to_ascii_lowercase()),
            false,
            i,
        );
    }

    // Skip blanks, optional quoted subsection.
    while i < len && matches!(bytes[i], b' ' | b'\t' | b'\r') {
        i += 1;
    }
    let mut subsection = None;
    if i < len && bytes[i] == b'"' {
        i += 1;
        let mut sub = String::new();
        while i < len {
            match bytes[i] {
                b'"' => {
                    i += 1;
                    break;
                }
                b'\\' if i + 1 < len => {
                    i += 1;
                    sub.push(bytes[i] as char);
                    i += 1;
                }
                b'\n' => break,
                other => {
                    sub.push(other as char);
                    i += 1;
                }
            }
        }
        subsection = Some(sub);
        while i < len && matches!(bytes[i], b' ' | b'\t' | b'\r') {
            i += 1;
        }
    }
    if i < len && bytes[i] == b']' {
        i += 1;
    } else {
        // Malformed: consume to end of line.
        while i < len && bytes[i] != b'\n' {
            i += 1;
        }
        return (None, None, true, i);
    }
    (Some(raw_name), subsection, true, i)
}

/// Parse an entry starting at `bytes[start]` (an alpha key char). Returns
/// (lower-cased key, decoded value, next_index). Continuation lines (`\`-newline)
/// are consumed so the entry span covers the full logical entry.
fn parse_entry(bytes: &[u8], start: usize) -> (Option<String>, Option<String>, usize) {
    let len = bytes.len();
    let mut i = start;
    let key_start = i;
    while i < len {
        let c = bytes[i];
        if c.is_ascii_alphanumeric() || c == b'-' {
            i += 1;
        } else {
            break;
        }
    }
    let key = String::from_utf8_lossy(&bytes[key_start..i]).to_ascii_lowercase();
    // Skip blanks.
    while i < len && matches!(bytes[i], b' ' | b'\t' | b'\r') {
        i += 1;
    }
    if i >= len || bytes[i] == b'\n' {
        // Bare boolean-true key.
        if i < len && bytes[i] == b'\n' {
            i += 1;
        }
        return (Some(key), None, i);
    }
    if bytes[i] != b'=' {
        // Malformed — consume the line.
        while i < len && bytes[i] != b'\n' {
            i += 1;
        }
        if i < len {
            i += 1;
        }
        return (Some(key), None, i);
    }
    i += 1; // past '='
    let (value, next) = parse_value_span(bytes, i);
    (Some(key), Some(value), next)
}

/// Decode a value starting after `=`, returning (decoded value, next_index).
/// Mirrors the value parser in `lib.rs` enough to know the entry's byte span and
/// the decoded value used for pattern matching. The newline that ends the value
/// is consumed into the span.
fn parse_value_span(bytes: &[u8], start: usize) -> (String, usize) {
    let len = bytes.len();
    let mut i = start;
    let mut out = String::new();
    let mut trailing_ws = 0usize;
    let mut leading = true;
    let mut in_quotes = false;
    while i < len {
        let c = bytes[i];
        match c {
            b'\n' if !in_quotes => {
                i += 1;
                break;
            }
            b'"' => {
                i += 1;
                in_quotes = !in_quotes;
                leading = false;
            }
            b'\\' => {
                i += 1;
                if i >= len {
                    break;
                }
                let e = bytes[i];
                i += 1;
                match e {
                    b'\n' => {} // line continuation
                    b'\r' if i < len && bytes[i] == b'\n' => {
                        i += 1;
                    }
                    b'n' => {
                        out.push('\n');
                        trailing_ws = 0;
                        leading = false;
                    }
                    b't' => {
                        out.push('\t');
                        trailing_ws = 0;
                        leading = false;
                    }
                    b'b' => {
                        out.push('\u{0008}');
                        trailing_ws = 0;
                        leading = false;
                    }
                    b'"' => {
                        out.push('"');
                        trailing_ws = 0;
                        leading = false;
                    }
                    b'\\' => {
                        out.push('\\');
                        trailing_ws = 0;
                        leading = false;
                    }
                    _ => {
                        // Invalid escape; keep going to find the span end.
                        leading = false;
                    }
                }
            }
            b'#' | b';' if !in_quotes => {
                // Comment terminates the value; consume to end of line.
                while i < len && bytes[i] != b'\n' {
                    i += 1;
                }
                if i < len {
                    i += 1;
                }
                break;
            }
            b' ' | b'\t' if !in_quotes => {
                i += 1;
                if leading {
                    // drop
                } else {
                    out.push(c as char);
                    trailing_ws += 1;
                }
            }
            b'\r' if !in_quotes => {
                i += 1;
                if i < len && bytes[i] == b'\n' {
                    // trailing CR before newline — line ending
                } else if !leading {
                    out.push('\r');
                    trailing_ws = 0;
                    leading = false;
                }
            }
            other => {
                i += 1;
                out.push(other as char);
                trailing_ws = 0;
                leading = false;
            }
        }
    }
    out.truncate(out.len().saturating_sub(trailing_ws));
    (out, i)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::too_many_arguments)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU64, Ordering};

    static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

    struct TempDir {
        path: PathBuf,
    }

    impl TempDir {
        fn new() -> Self {
            let path = std::env::temp_dir().join(format!(
                "sley-config-raw-edit-{}-{}",
                std::process::id(),
                TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
            ));
            fs::create_dir_all(&path).expect("create temp dir");
            Self { path }
        }
    }

    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.path);
        }
    }

    fn edit(
        src: &str,
        sec: &str,
        sub: Option<&str>,
        name: &str,
        value: Option<&str>,
        comment: Option<&str>,
        vm: Option<ValueMatcher>,
        multi: bool,
    ) -> (String, RawEditOutcome) {
        let mut e = RawConfigEditor::new(src.as_bytes().to_vec(), sec, sub, name);
        let out = e.set_multivar(value, comment, vm, multi);
        (String::from_utf8(e.into_bytes()).unwrap(), out)
    }

    #[test]
    fn write_config_file_locked_writes_and_cleans_lock() {
        let temp = TempDir::new();
        let path = temp.path.join("nested").join("config");

        write_config_file_locked(
            &path,
            b"[user]\n\tname = Ada\n",
            ConfigFileWriteOptions::default(),
        )
        .expect("write config");

        assert_eq!(
            fs::read(&path).expect("read config"),
            b"[user]\n\tname = Ada\n"
        );
        assert!(!path.with_file_name("config.lock").exists());
    }

    #[test]
    fn edit_config_file_locked_reads_each_serialized_predecessor_under_lock() {
        let temp = TempDir::new();
        let path = temp.path.join("config");
        for line in [b"one = 1\n".as_slice(), b"two = 2\n".as_slice()] {
            edit_config_file_locked(&path, ConfigFileWriteOptions::default(), |original| {
                assert!(
                    path.with_file_name("config.lock").exists(),
                    "the callback must run while the config lock is held"
                );
                let mut replacement = original.to_vec();
                replacement.extend_from_slice(line);
                Ok::<_, std::convert::Infallible>(replacement)
            })
            .expect("serialized config edit");
        }

        assert_eq!(
            fs::read(&path).expect("read edited config"),
            b"one = 1\ntwo = 2\n"
        );
        assert!(!path.with_file_name("config.lock").exists());
    }

    #[cfg(unix)]
    #[test]
    fn write_config_file_locked_follows_symlink_and_keeps_it() {
        // git's `resolve_symlink`: writing through a symlinked config path must
        // update the pointed-at file and leave the symlink itself intact
        // (t1300 "symlinked configuration").
        let temp = TempDir::new();
        let real = temp.path.join("notyet");
        let link = temp.path.join("myconfig");
        std::os::unix::fs::symlink("notyet", &link).expect("create symlink");

        write_config_file_locked(
            &link,
            b"[test]\n\tfrotz = nitfol\n",
            ConfigFileWriteOptions::default(),
        )
        .expect("write through symlink");

        // The symlink is still a symlink; the real file received the bytes.
        assert!(
            fs::symlink_metadata(&link)
                .expect("stat link")
                .file_type()
                .is_symlink(),
            "myconfig must remain a symlink"
        );
        assert!(
            fs::symlink_metadata(&real)
                .expect("stat real")
                .file_type()
                .is_file(),
            "notyet must be a regular file"
        );
        assert_eq!(
            fs::read(&real).expect("read real"),
            b"[test]\n\tfrotz = nitfol\n"
        );
        // No stray lock for either name.
        assert!(!real.with_file_name("notyet.lock").exists());
        assert!(!link.with_file_name("myconfig.lock").exists());
    }

    #[cfg(unix)]
    #[test]
    fn write_config_file_locked_preserves_existing_mode() {
        use std::os::unix::fs::PermissionsExt;
        // git chmods the lockfile to the original config's mode before the
        // rename, so an existing 0600 file stays 0600 (t1300 "preserves
        // existing permissions").
        let temp = TempDir::new();
        let path = temp.path.join("config");
        fs::write(&path, b"[user]\n\tname = Old\n").expect("write original");
        fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).expect("chmod 0600");

        write_config_file_locked(
            &path,
            b"[user]\n\tname = New\n",
            ConfigFileWriteOptions::default(),
        )
        .expect("rewrite config");

        let mode = fs::metadata(&path).expect("stat").permissions().mode() & 0o7777;
        assert_eq!(mode, 0o600, "mode must be preserved across the edit");
        assert_eq!(fs::read(&path).expect("read"), b"[user]\n\tname = New\n");
    }

    #[test]
    fn write_config_file_locked_existing_lock_preserves_original() {
        let temp = TempDir::new();
        let path = temp.path.join("config");
        let lock_path = path.with_file_name("config.lock");
        fs::write(&path, b"[user]\n\tname = Old\n").expect("write original");
        fs::write(&lock_path, b"held\n").expect("write lock");

        let err = write_config_file_locked(
            &path,
            b"[user]\n\tname = New\n",
            ConfigFileWriteOptions::default(),
        )
        .expect_err("held lock must fail");

        assert!(matches!(err, ConfigFileWriteError::ExistingLock(_)));
        assert_eq!(
            fs::read(&path).expect("read original"),
            b"[user]\n\tname = Old\n"
        );
        assert_eq!(fs::read(&lock_path).expect("read lock"), b"held\n");
    }

    #[test]
    fn unset_cont_lines_preserves_layout() {
        let src = "[alpha]\nbar = foo\n[beta]\nbaz = multiple \\\nlines\nfoo = bar\n";
        let (out, _) = edit(src, "beta", None, "baz", None, None, None, true);
        assert_eq!(out, "[alpha]\nbar = foo\n[beta]\nfoo = bar\n");
    }

    #[test]
    fn unset_all_silly_comments_preserved() {
        let src = "[beta] ; silly comment # another comment\nnoIndent= sillyValue ; 'nother silly comment\n\n# empty line\n\t\t; comment\n\t\thaha   =\"beta\" # last silly comment\nhaha = hello\n\thaha = bello\n[nextSection] noNewline = ouch\n";
        let (out, _) = edit(src, "beta", None, "haha", None, None, None, true);
        let expect = "[beta] ; silly comment # another comment\nnoIndent= sillyValue ; 'nother silly comment\n\n# empty line\n\t\t; comment\n[nextSection] noNewline = ouch\n";
        assert_eq!(out, expect);
    }

    #[test]
    fn replace_all_preserves_other_lines() {
        let src = "[beta] ; silly comment # another comment\nnoIndent= sillyValue ; 'nother silly comment\n\n# empty line\n\t\t; comment\n\t\thaha   =\"beta\" # last silly comment\nhaha = hello\n\thaha = bello\n[nextSection] noNewline = ouch\n";
        let (out, _) = edit(src, "beta", None, "haha", Some("gamma"), None, None, true);
        let expect = "[beta] ; silly comment # another comment\nnoIndent= sillyValue ; 'nother silly comment\n\n# empty line\n\t\t; comment\n\thaha = gamma\n[nextSection] noNewline = ouch\n";
        assert_eq!(out, expect);
    }

    #[test]
    fn replace_all_does_not_touch_same_name_in_other_section() {
        // `abc.key` must NOT match `xyz.key` — a same-named variable in another
        // section (t1300 #235). The `[abc]key` header has an inline bare key.
        let src = "[abc]key\n\tkeepSection\n[xyz]\n\tkey = 1\n[abc]\n\tkey = a\n";
        let (out, _) = edit(src, "abc", None, "key", Some("b"), None, None, true);
        let expect = "[abc]\n\tkeepSection\n[xyz]\n\tkey = 1\n[abc]\n\tkey = b\n";
        assert_eq!(out, expect);
    }

    #[test]
    fn set_uses_case_compatible_dotted_subsection_for_insert() {
        let src = "[V.A]\n\tx = old\n";
        let (out, outcome) = edit(src, "V", Some("A"), "r", Some("new"), None, None, false);
        assert_eq!(outcome, RawEditOutcome::Changed);
        assert_eq!(out, "[V.A]\n\tx = old\n\tr = new\n");
    }

    #[test]
    fn set_keeps_dotted_subsection_exactness_for_replacement() {
        let src = "[V.A]\n\tr = old\n";
        let (out, outcome) = edit(src, "v", Some("a"), "r", Some("new"), None, None, false);
        assert_eq!(outcome, RawEditOutcome::Changed);
        assert_eq!(out, "[V.A]\n\tr = new\n");

        let (out, outcome) = edit(src, "V", Some("A"), "r", Some("new"), None, None, false);
        assert_eq!(outcome, RawEditOutcome::Changed);
        assert_eq!(out, "[V.A]\n\tr = old\n\tr = new\n");
    }

    #[test]
    fn quoted_subsection_stays_case_sensitive_for_insert() {
        let src = "[V \"a\"]\n\tx = old\n";
        let (out, outcome) = edit(src, "V", Some("A"), "r", Some("new"), None, None, false);
        assert_eq!(outcome, RawEditOutcome::Changed);
        assert_eq!(out, "[V \"a\"]\n\tx = old\n[V \"A\"]\n\tr = new\n");
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod section_tests {
    use super::*;

    fn rr(src: &str, old: &str, new: Option<&str>) -> SectionEditOutcome {
        rename_or_remove_section(src.as_bytes(), old, new)
    }

    #[test]
    fn rename_quoted_and_dotted_forms() {
        let src = "# Hallo\n\t#Bello\n[branch \"eins\"]\n\tx = 1\n[branch.eins]\n\ty = 1\n\t[branch \"1 234 blabl/a\"]\nweird\n";
        let SectionEditOutcome::Changed(out) = rr(src, "branch.eins", Some("branch.zwei")) else {
            panic!("expected Changed");
        };
        let expect = "# Hallo\n\t#Bello\n[branch \"zwei\"]\n\tx = 1\n[branch \"zwei\"]\n\ty = 1\n\t[branch \"1 234 blabl/a\"]\nweird\n";
        assert_eq!(String::from_utf8(out).unwrap(), expect);
    }

    #[test]
    fn rename_inline_var_indents_remainder() {
        let src = "[branch \"vier\"] z = 1\n";
        let SectionEditOutcome::Changed(out) = rr(src, "branch.vier", Some("branch.zwei")) else {
            panic!("expected Changed");
        };
        assert_eq!(
            String::from_utf8(out).unwrap(),
            "[branch \"zwei\"]\n\tz = 1\n"
        );
    }

    #[test]
    fn remove_section_drops_lines() {
        let src = "[a]\n\tx = 1\n[b]\n\ty = 2\n";
        let SectionEditOutcome::Changed(out) = rr(src, "a", None) else {
            panic!("expected Changed");
        };
        assert_eq!(String::from_utf8(out).unwrap(), "[b]\n\ty = 2\n");
    }

    #[test]
    fn rename_nonexistent_is_not_found() {
        let src = "[a]\n\tx = 1\n";
        assert!(matches!(
            rr(src, "zzz", Some("q.r")),
            SectionEditOutcome::NotFound
        ));
    }
}