girt 0.10.3

An incremental, idiomatic Rust implementation of Git
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
use std::ops::Range;

use super::parse::Layout;
use super::{Config, ConfigError};

/// One losslessly retained configuration file, without include expansion.
///
/// Edits preserve bytes outside targeted syntax ranges, including comments, whitespace, unknown
/// variables and duplicate ordering. New entries use quoted values and LF; existing line endings
/// remain unchanged. Removed syntax leaves its surrounding whitespace/comments in place. No files
/// are read or written. Occurrence indices come from [`Self::config`] and become stale after edits.
///
/// ```
/// use girt::config::Document;
/// let mut document = Document::parse(b"# keep\n[core]\neditor = old\n")?;
/// document.set_value(0, b"new editor")?;
/// assert!(document.as_bytes().starts_with(b"# keep\n"));
/// assert_eq!(
///     document.config().string("core", None, "editor"),
///     Some(&b"new editor"[..])
/// );
/// # Ok::<(), girt::ConfigError>(())
/// ```
#[derive(Debug, Clone)]
pub struct Document {
    bytes: Vec<u8>,
    config: Config,
    layout: Layout,
}

/// One physical section header and its direct-file assignment occurrences.
///
/// Repeated headers are distinct even when their decoded names match. This view borrows its
/// [`Document`]; ordinal and entry indices describe that document's current state and become stale
/// after any edit. Comments and empty sections do not contribute entry occurrences.
///
/// Choose this physical-file view when editing one source. Ordinals and entry ranges describe the
/// current draft, not permanent source identities; reacquire them after an edit.
///
/// ```
/// use girt::config::Document;
/// let mut draft = Document::parse(b"[core]\neditor = chosen\n[empty]\n")?;
/// let empty = draft.sections().nth(1).ok_or("section absent")?;
/// assert!(empty.entry_range().is_empty());
/// draft.remove_sections(&[empty.ordinal()])?;
/// assert_eq!(draft.sections().len(), 1);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct DocumentSection<'a> {
    ordinal: usize,
    name: &'a [u8],
    subsection: Option<&'a [u8]>,
    entries: Range<usize>,
}

impl<'a> DocumentSection<'a> {
    /// Zero-based physical header position, suitable for [`Document::remove_sections`].
    pub fn ordinal(&self) -> usize {
        self.ordinal
    }

    /// Original section-name bytes; name matching usually folds ASCII case.
    pub fn name(&self) -> &'a [u8] {
        self.name
    }

    /// Exact quoted subsection bytes, or lowercase deprecated dotted subsection bytes.
    pub fn subsection(&self) -> Option<&'a [u8]> {
        self.subsection
    }

    /// Assignment occurrence range in [`Document::config`]'s [`Config::entries`].
    ///
    /// Empty sections have an empty range at the next assignment's position, or at the end.
    pub fn entry_range(&self) -> Range<usize> {
        self.entries.clone()
    }
}

impl Document {
    /// Parses one file, retaining its exact bytes (including a leading BOM).
    ///
    /// # Errors
    ///
    /// Returns the same syntax failures as [`Config::parse`].
    ///
    /// ```
    /// use girt::config::Document;
    /// let bytes = b"# hand-written\n[core]\n  editor = 'literal' # keep spelling\n";
    /// let document = Document::parse(bytes)?;
    /// assert_eq!(document.as_bytes(), bytes); // Reading alone preserves all syntax.
    /// assert_eq!(
    ///     document.config().string("core", None, "editor"),
    ///     Some(&b"'literal'"[..])
    /// );
    /// assert!(Document::parse(b"[unterminated").is_err());
    /// # Ok::<(), girt::ConfigError>(())
    /// ```
    pub fn parse(bytes: &[u8]) -> Result<Self, ConfigError> {
        let (config, layout) = Config::parse_layout(bytes)?;
        Ok(Self {
            bytes: bytes.to_vec(),
            config,
            layout,
        })
    }

    /// Exact current file bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Current direct-file entries; includes remain unexpanded directives.
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Iterates physical section headers in file order, including empty and repeated sections.
    ///
    /// Views borrow this document. Their ordinals and entry occurrence ranges become stale after
    /// edits. Includes remain ordinary direct-file entries; no included files are read.
    pub fn sections(&self) -> impl ExactSizeIterator<Item = DocumentSection<'_>> {
        (0..self.layout.sections.len()).map(|ordinal| self.section_at(ordinal))
    }

    #[expect(
        clippy::indexing_slicing,
        reason = "callers pass ordinals below layout.sections.len()"
    )]
    fn section_at(&self, ordinal: usize) -> DocumentSection<'_> {
        let section = &self.layout.sections[ordinal];
        let next_header = self
            .layout
            .sections
            .get(ordinal + 1)
            .map_or(self.bytes.len(), |next| next.range.start);
        let start = self
            .layout
            .entries
            .partition_point(|entry| entry.range.start < section.range.end);
        let end = self
            .layout
            .entries
            .partition_point(|entry| entry.range.start < next_header);
        DocumentSection {
            ordinal,
            name: &section.section,
            subsection: section.subsection.as_deref(),
            entries: start..end,
        }
    }

    /// Removes selected physical headers and their assignments in one atomic edit.
    ///
    /// Preserves comments, whitespace and all unrelated bytes just like [`Self::remove_section`].
    /// Ordinals come from [`Self::sections`] in the current document; edits make previous ordinals
    /// stale. Duplicates are accepted and removed once. An empty selection is an exact no-op.
    ///
    /// # Errors
    ///
    /// Validates every ordinal before editing. An invalid ordinal or resulting syntax error leaves
    /// the entire document unchanged.
    ///
    /// # Examples
    ///
    /// ```
    /// use girt::config::Document;
    /// let mut document =
    ///     Document::parse(b"[remote \"origin\"]\nurl=first\n[remote \"origin\"]\nurl=second\n")?;
    /// let second = document.sections().nth(1).ok_or("second section absent")?;
    /// assert_eq!(second.entry_range(), 1..2);
    /// document.remove_sections(&[second.ordinal()])?;
    /// assert_eq!(document.config().entries().len(), 1);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[expect(
        clippy::indexing_slicing,
        reason = "ordinals are checked above; entry ranges come from the parsed layout"
    )]
    pub fn remove_sections(&mut self, ordinals: &[usize]) -> Result<(), ConfigError> {
        if ordinals
            .iter()
            .any(|&ordinal| ordinal >= self.layout.sections.len())
        {
            return Err(invalid("invalid section ordinal"));
        }
        if ordinals.is_empty() {
            return Ok(());
        }
        let mut selected = ordinals.to_vec();
        selected.sort_unstable();
        selected.dedup();
        let mut edits = Vec::new();
        for ordinal in selected {
            edits.push((self.layout.sections[ordinal].range.clone(), Vec::new()));
            let entries = self.section_at(ordinal).entry_range();
            edits.extend(
                self.layout.entries[entries]
                    .iter()
                    .map(|entry| (entry.range.clone(), Vec::new())),
            );
        }
        self.apply(edits)
    }

    /// Replaces one decoded occurrence value, preserving its key spelling and trailing comment.
    ///
    /// Implicit booleans become explicit assignments. Empty values remain distinct from implicit
    /// booleans. Other occurrences are untouched.
    ///
    /// # Errors
    ///
    /// Invalid indices or NUL values fail without changing the document.
    #[expect(
        clippy::indexing_slicing,
        reason = "layout.entries parallels config.entries, checked by get above"
    )]
    ///
    /// ```
    /// use girt::config::Document;
    /// let mut document = Document::parse(b"[core]\neditor = first\neditor = second\n")?;
    /// document.set_value(1, b"replacement")?;
    /// assert_eq!(
    ///     document
    ///         .config()
    ///         .values("core", None, "editor")
    ///         .collect::<Vec<_>>(),
    ///     [Some(&b"first"[..]), Some(&b"replacement"[..])]
    /// );
    /// let unchanged = document.as_bytes().to_vec();
    /// assert!(document.set_value(99, b"invalid occurrence").is_err());
    /// assert_eq!(document.as_bytes(), unchanged);
    /// # Ok::<(), girt::ConfigError>(())
    /// ```
    pub fn set_value(&mut self, occurrence: usize, value: &[u8]) -> Result<(), ConfigError> {
        let entry = self
            .config
            .entries()
            .get(occurrence)
            .ok_or_else(invalid_index)?;
        let mut replacement = Vec::new();
        if entry.value.is_none() {
            replacement.extend_from_slice(b" = ");
        }
        replacement.extend(quote(value, false)?);
        self.apply(vec![(
            self.layout.entries[occurrence].value.clone(),
            replacement,
        )])
    }

    /// Removes one assignment, preserving neighboring comments and line endings.
    ///
    /// # Errors
    ///
    /// An invalid occurrence index leaves the document unchanged.
    pub fn remove(&mut self, occurrence: usize) -> Result<(), ConfigError> {
        let span = self
            .layout
            .entries
            .get(occurrence)
            .ok_or_else(invalid_index)?;
        self.apply(vec![(span.range.clone(), Vec::new())])
    }

    /// Appends a new section header and explicit assignment at EOF.
    ///
    /// Repeated sections are intentional: existing entries and include ordering stay intact. The
    /// new occurrence follows every existing occurrence in this file. Values are opaque bytes;
    /// typed validation belongs to their consumer.
    ///
    /// # Errors
    ///
    /// Invalid section/key names, NUL bytes or newlines in subsections leave the document
    /// unchanged.
    #[expect(
        clippy::indexing_slicing,
        reason = "last and its entries index the parsed layout, whose spans lie in self.bytes"
    )]
    ///
    /// ```
    /// use girt::config::Document;
    /// let mut document = Document::parse(b"[core]\neditor = first\n")?;
    /// document.append("core", None, "editor", b"second")?;
    /// assert_eq!(document.config().values("core", None, "editor").count(), 2);
    /// assert_eq!(
    ///     document.config().string("core", None, "editor"),
    ///     Some(&b"second"[..])
    /// );
    /// # Ok::<(), girt::ConfigError>(())
    /// ```
    pub fn append(
        &mut self,
        section: &str,
        subsection: Option<&[u8]>,
        name: &str,
        value: &[u8],
    ) -> Result<(), ConfigError> {
        validate_variable_name(name)?;
        let Some(last) = self.layout.sections.iter().rposition(|s| {
            s.section.eq_ignore_ascii_case(section.as_bytes())
                && s.subsection.as_deref() == subsection
        }) else {
            return self.append_section(section, subsection, &[(name, value)]);
        };
        let entries = self.section_at(last).entry_range();
        let anchor = match entries.last() {
            Some(entry) => self.layout.entries[entry].range.end,
            None => self.layout.sections[last].range.end,
        };
        let insert_at = self.bytes[anchor..]
            .iter()
            .position(|b| *b == b'\n')
            .map_or(self.bytes.len(), |i| anchor + i + 1);
        let mut addition = Vec::new();
        if insert_at == self.bytes.len() && !self.bytes.ends_with(b"\n") {
            addition.push(b'\n');
        }
        addition.push(b'\t');
        addition.extend_from_slice(name.as_bytes());
        addition.extend_from_slice(b" = ");
        addition.extend(quote(value, false)?);
        addition.push(b'\n');
        let mut bytes = self.bytes.clone();
        bytes.splice(insert_at..insert_at, addition);
        *self = Self::parse(&bytes)?;
        Ok(())
    }

    /// Appends one section with ordered explicit assignments at EOF.
    ///
    /// Existing bytes and include ordering are preserved. Values use canonical quoting and LF;
    /// empty values remain explicit. An empty entry list appends an empty section. Like
    /// [`Self::append`], this creates a new header even if the section already exists.
    ///
    /// # Errors
    ///
    /// Invalid section/key names, NUL values or newlines in subsections leave the entire document
    /// unchanged, including when a later assignment is invalid.
    pub fn append_section(
        &mut self,
        section: &str,
        subsection: Option<&[u8]>,
        entries: &[(&str, &[u8])],
    ) -> Result<(), ConfigError> {
        let mut addition = header(section.as_bytes(), subsection)?;
        addition.push(b'\n');
        for &(name, value) in entries {
            validate_variable_name(name)?;
            addition.push(b'\t');
            addition.extend_from_slice(name.as_bytes());
            addition.extend_from_slice(b" = ");
            addition.extend(quote(value, false)?);
            addition.push(b'\n');
        }
        let mut bytes = self.bytes.clone();
        // Git starts a new header on its own line. A trailing backslash-newline continuation
        // additionally needs a blank line to terminate it.
        if !bytes.is_empty() && !bytes.ends_with(b"\n") {
            bytes.push(b'\n');
        }
        if bytes.ends_with(b"\\\n") || bytes.ends_with(b"\\\r\n") {
            bytes.push(b'\n');
        }
        bytes.extend(addition);
        *self = Self::parse(&bytes)?;
        Ok(())
    }

    /// Whether a matching header exists, including empty or deprecated dotted sections.
    pub fn contains_section(&self, section: &str, subsection: Option<&[u8]>) -> bool {
        self.layout.sections.iter().any(|s| {
            s.section.eq_ignore_ascii_case(section.as_bytes())
                && s.subsection.as_deref() == subsection
        })
    }

    /// Renames every matching section header without changing its entries or comments.
    ///
    /// # Errors
    ///
    /// Invalid destination subsection bytes fail without changing any header.
    ///
    /// ```
    /// use girt::config::Document;
    /// let mut draft = Document::parse(b"[remote \"old\"]\nurl = value\n")?;
    /// draft.rename_section("remote", Some(b"old"), Some(b"new"))?;
    /// assert_eq!(
    ///     draft.config().string("remote", Some(b"new"), "url"),
    ///     Some(&b"value"[..])
    /// );
    /// # Ok::<(), girt::ConfigError>(())
    /// ```
    pub fn rename_section(
        &mut self,
        section: &str,
        old: Option<&[u8]>,
        new: Option<&[u8]>,
    ) -> Result<(), ConfigError> {
        let replacement = header(section.as_bytes(), new)?;
        let edits = self
            .layout
            .sections
            .iter()
            .filter(|s| {
                s.section.eq_ignore_ascii_case(section.as_bytes()) && s.subsection.as_deref() == old
            })
            .map(|s| (s.range.clone(), replacement.clone()))
            .collect();
        self.apply(edits)
    }

    /// Removes matching headers and assignments, leaving comments and unrelated bytes intact.
    ///
    /// # Errors
    ///
    /// Returns a syntax error if the resulting document cannot be parsed; the original is retained.
    ///
    /// ```
    /// use girt::config::Document;
    /// let mut draft = Document::parse(b"[core]\neditor = chosen\n[alias]\nx = value\n")?;
    /// draft.remove_section("alias", None)?;
    /// assert!(!draft.contains_section("alias", None));
    /// assert!(draft.contains_section("core", None));
    /// # Ok::<(), girt::ConfigError>(())
    /// ```
    pub fn remove_section(
        &mut self,
        section: &str,
        subsection: Option<&[u8]>,
    ) -> Result<(), ConfigError> {
        let ordinals: Vec<_> = self
            .sections()
            .filter(|physical| {
                physical.name().eq_ignore_ascii_case(section.as_bytes())
                    && physical.subsection() == subsection
            })
            .map(|physical| physical.ordinal())
            .collect();
        self.remove_sections(&ordinals)
    }

    fn apply(&mut self, edits: Vec<(Range<usize>, Vec<u8>)>) -> Result<(), ConfigError> {
        // A removal that leaves only whitespace on its line removes the whole line, as
        // `git config --unset` does; otherwise surrounding bytes are preserved.
        let mut edits: Vec<_> = edits
            .into_iter()
            .map(|(range, replacement)| {
                if replacement.is_empty() {
                    (whole_blank_line(&self.bytes, range), replacement)
                } else {
                    (range, replacement)
                }
            })
            .collect();
        edits.sort_by_key(|(range, _)| range.start);
        let mut merged: Vec<(Range<usize>, Vec<u8>)> = Vec::with_capacity(edits.len());
        for (range, replacement) in edits {
            if let Some((last, last_replacement)) = merged.last_mut()
                && range.start < last.end
                && replacement.is_empty()
                && last_replacement.is_empty()
            {
                last.end = last.end.max(range.end);
                continue;
            }
            merged.push((range, replacement));
        }
        let mut bytes = self.bytes.clone();
        for (range, replacement) in merged.into_iter().rev() {
            bytes.splice(range, replacement);
        }
        *self = Self::parse(&bytes)?;
        Ok(())
    }
}

fn header(section: &[u8], subsection: Option<&[u8]>) -> Result<Vec<u8>, ConfigError> {
    if section.is_empty()
        || !section
            .iter()
            .all(|b| b.is_ascii_alphanumeric() || *b == b'-')
    {
        return Err(invalid("invalid section name"));
    }
    let mut bytes = vec![b'['];
    bytes.extend_from_slice(section);
    if let Some(subsection) = subsection {
        bytes.push(b' ');
        bytes.extend(quote(subsection, true)?);
    }
    bytes.push(b']');
    Ok(bytes)
}
fn validate_variable_name(name: &str) -> Result<(), ConfigError> {
    if !name.as_bytes().first().is_some_and(u8::is_ascii_alphabetic)
        || !name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
    {
        return Err(invalid("invalid variable name"));
    }
    Ok(())
}

/// Extends a removed span to its whole physical line when nothing but whitespace would remain.
#[expect(
    clippy::indexing_slicing,
    reason = "callers pass a parsed span of bytes, so both line edges lie within bytes"
)]
fn whole_blank_line(bytes: &[u8], range: Range<usize>) -> Range<usize> {
    let line_start = bytes[..range.start]
        .iter()
        .rposition(|b| *b == b'\n')
        .map_or(0, |i| i + 1);
    let line_end = bytes[range.end..]
        .iter()
        .position(|b| *b == b'\n')
        .map_or(bytes.len(), |i| range.end + i + 1);
    let is_blank = |part: &[u8]| {
        part.iter()
            .all(|b| matches!(b, b' ' | b'\t' | b'\r' | b'\n'))
    };
    if is_blank(&bytes[line_start..range.start]) && is_blank(&bytes[range.end..line_end]) {
        line_start..line_end
    } else {
        range
    }
}

/// Encodes a value the way Git writes it: bare unless quoting is needed to preserve leading or
/// trailing whitespace or comment characters. Subsections are always quoted.
fn quote(value: &[u8], subsection: bool) -> Result<Vec<u8>, ConfigError> {
    let needs_quotes = subsection
        || value.first().is_some_and(|b| matches!(b, b' ' | b'\t'))
        || value.last().is_some_and(|b| matches!(b, b' ' | b'\t'))
        || value.iter().any(|b| matches!(b, b';' | b'#'));
    let mut bytes = Vec::with_capacity(value.len() + 2);
    if needs_quotes {
        bytes.push(b'"');
    }
    for &byte in value {
        match byte {
            0 => return Err(invalid("NUL in value")),
            b'\n' if subsection => return Err(invalid("newline in subsection")),
            b'"' | b'\\' => {
                bytes.push(b'\\');
                bytes.push(byte);
            }
            b'\n' => bytes.extend_from_slice(b"\\n"),
            b'\t' if !subsection => bytes.extend_from_slice(b"\\t"),
            8 if !subsection => bytes.extend_from_slice(b"\\b"),
            byte => bytes.push(byte),
        }
    }
    if needs_quotes {
        bytes.push(b'"');
    }
    Ok(bytes)
}
fn invalid(reason: &'static str) -> ConfigError {
    ConfigError { line: 1, reason }
}
fn invalid_index() -> ConfigError {
    invalid("invalid occurrence index")
}

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

    use super::*;

    #[test]
    fn physical_sections_include_empty_repeated_and_deprecated_headers() {
        let document = Document::parse(b"[Remote \"x\"]\na=one\nb\n[remote]\n[remote.X]\nc=three\n[remote \"X\"]\n[remote \"x\"]\nd=four\n[last]").unwrap();
        let sections: Vec<_> = document.sections().collect();
        assert_eq!(document.sections().len(), 6);
        assert_eq!(
            sections
                .iter()
                .map(DocumentSection::ordinal)
                .collect::<Vec<_>>(),
            [0, 1, 2, 3, 4, 5]
        );
        assert_eq!(sections[0].name(), b"Remote");
        assert_eq!(sections[1].subsection(), None);
        assert_eq!(sections[2].subsection(), Some(b"x".as_slice()));
        assert_eq!(sections[3].subsection(), Some(b"X".as_slice()));
        assert_eq!(
            sections
                .iter()
                .map(DocumentSection::entry_range)
                .collect::<Vec<_>>(),
            [0..2, 2..2, 2..3, 3..3, 3..4, 4..4]
        );
        let entries = &document.config().entries()[sections[0].entry_range()];
        assert_eq!(entries[0].value.as_deref(), Some(b"one".as_slice()));
        assert_eq!(entries[1].value, None);
    }

    #[test]
    fn selective_removal_preserves_same_named_neighbor_and_all_comments() {
        let original = b"\xef\xbb\xbf[remote \"x\"] #header\r\nurl=\"a#b\" #url\r\nimplicit ;bool\r\nmulti=one\\\r\n two #tail\r\n[other]\r\nx=yes\r\n[remote \"x\"]\r\nurl=second\r\n";
        let mut document = Document::parse(original).unwrap();
        assert_eq!(document.sections().next().unwrap().entry_range(), 0..3);
        document.remove_sections(&[0, 0]).unwrap();
        assert_eq!(document.as_bytes(), b"\xef\xbb\xbf #header\r\n #url\r\n ;bool\r\n #tail\r\n[other]\r\nx=yes\r\n[remote \"x\"]\r\nurl=second\r\n");
        assert_eq!(
            document.config().value("remote", Some(b"x"), "url"),
            Some(Some(b"second".as_slice()))
        );
        assert_eq!(document.sections().nth(1).unwrap().entry_range(), 1..2);
    }

    #[test]
    fn removal_deduplicates_unsorted_ordinals_and_removes_empty_headers() {
        let mut document =
            Document::parse(b"[same]a=one\n[same]#empty\n[keep]x=yes\n[same]a=last").unwrap();
        document.remove_sections(&[3, 0, 1, 3]).unwrap();
        assert_eq!(document.as_bytes(), b"\n#empty\n[keep]x=yes\n");
        assert_eq!(document.sections().len(), 1);
    }

    #[rstest]
    #[case::empty(b"")]
    #[case::comments(b"\xef\xbb\xbf # only\r\n")]
    #[case::sections(b"[x]\r\na=\"two\\nlines\" ;comment\r\n[x]\r\n")]
    fn empty_section_selection_is_exact_noop(#[case] bytes: &[u8]) {
        let mut document = Document::parse(bytes).unwrap();
        document.remove_sections(&[]).unwrap();
        assert_eq!(document.as_bytes(), bytes);
    }

    #[rstest]
    #[case(&[0, 2][..])]
    #[case(&[usize::MAX, 0][..])]
    fn invalid_section_ordinal_is_atomic(#[case] ordinals: &[usize]) {
        let bytes = b"[x]a=one\n[x]a=two\n";
        let mut document = Document::parse(bytes).unwrap();
        assert!(document.remove_sections(ordinals).is_err());
        assert_eq!(document.as_bytes(), bytes);
        assert_eq!(document.sections().len(), 2);
        assert_eq!(document.config().entries().len(), 2);
    }

    #[rstest]
    #[case::comments(
        b"# before\r\n[CoRe] x = old  ; keep\r\n# after\r\n",
        b"# before\r\n[CoRe] x = new  ; keep\r\n# after\r\n"
    )]
    #[case::implicit(b"[core]\nx # keep\n", b"[core]\nx = new # keep\n")]
    #[case::empty(b"[core]\nx=  # keep", b"[core]\nx=  new# keep")]
    #[case::continued(b"[core]\nx=ab\\\n cd\n", b"[core]\nx=new\n")]
    #[case::bom(b"\xef\xbb\xbf[core]\nx=old", b"\xef\xbb\xbf[core]\nx=new")]
    fn replaces_only_value(#[case] input: &[u8], #[case] expected: &[u8]) {
        let mut document = Document::parse(input).unwrap();
        document.set_value(0, b"new").unwrap();
        assert_eq!(document.as_bytes(), expected);
    }

    #[test]
    fn preserves_repeated_sections_and_unrelated_bytes() {
        let bytes = b";top\n[remote.OLD] url=one ;first\n[other]\n  x = yes\n[remote \"old\"]\nurl=two #second\n";
        let mut document = Document::parse(bytes).unwrap();
        document
            .rename_section("remote", Some(b"old"), Some(b"n\"\\\xff"))
            .unwrap();
        assert_eq!(
            document
                .config()
                .values("remote", Some(b"n\"\\\xff"), "url")
                .collect::<Vec<_>>(),
            vec![Some(b"one".as_slice()), Some(b"two".as_slice())]
        );
        document
            .remove_section("remote", Some(b"n\"\\\xff"))
            .unwrap();
        assert_eq!(
            document.as_bytes(),
            b";top\n  ;first\n[other]\n  x = yes\n #second\n"
        );
    }

    #[rstest]
    #[case::empty(b"")]
    #[case::no_newline(b"[core]\nx=yes")]
    #[case::comment(b"#tail")]
    #[case::trailing_continuation(b"[core]\nx=yes\\\n")]
    fn append_escapes_values(#[case] input: &[u8]) {
        let mut document = Document::parse(input).unwrap();
        document
            .append("remote", Some(b"a\"\\\xff"), "url", b" \n\t\x08\"\\#;\xff ")
            .unwrap();
        assert_eq!(
            document.config().value("remote", Some(b"a\"\\\xff"), "url"),
            Some(Some(b" \n\t\x08\"\\#;\xff ".as_slice()))
        );
        assert!(document.as_bytes().starts_with(input));
    }

    #[test]
    fn append_section_preserves_order_and_explicit_empty_values() {
        let before = b"# keep\r\n[remote \"origin\"]\r\nurl=old\r\nimplicit\r\n";
        let mut document = Document::parse(before).unwrap();
        document
            .append_section(
                "remote",
                Some(b"origin"),
                &[
                    ("url", b""),
                    ("url", b"new"),
                    ("fetch", b"+refs/heads/*:refs/remotes/origin/*"),
                ],
            )
            .unwrap();
        assert!(document.as_bytes().starts_with(before));
        assert_eq!(
            document
                .config()
                .values("remote", Some(b"origin"), "url")
                .collect::<Vec<_>>(),
            [
                Some(b"old".as_slice()),
                Some(b"".as_slice()),
                Some(b"new".as_slice())
            ]
        );
        assert_eq!(
            document
                .config()
                .value("remote", Some(b"origin"), "implicit"),
            Some(None)
        );
        assert_eq!(&document.as_bytes()[before.len()..], b"[remote \"origin\"]\n\turl = \n\turl = new\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
    }

    #[rstest]
    #[case::key("bad=name", b"value")]
    #[case::value("url", b"bad\0value")]
    fn append_section_invalid_later_entry_retains_bytes(#[case] name: &str, #[case] value: &[u8]) {
        let before = b"[core]\nx=yes\n";
        let mut document = Document::parse(before).unwrap();
        assert!(
            document
                .append_section(
                    "remote",
                    Some(b"origin"),
                    &[("url", b"valid"), (name, value)]
                )
                .is_err()
        );
        assert_eq!(document.as_bytes(), before);
    }

    #[test]
    fn append_empty_section() {
        let mut document = Document::parse(b"").unwrap();
        document
            .append_section("remote", Some(b"origin"), &[])
            .unwrap();
        assert_eq!(document.as_bytes(), b"[remote \"origin\"]\n");
        assert!(document.config().entries().is_empty());
    }

    #[test]
    fn invalid_edits_retain_exact_bytes() {
        let mut document = Document::parse(b"[core]\nx=yes\n").unwrap();
        let before = document.as_bytes().to_vec();
        assert!(document.set_value(0, b"\0").is_err());
        assert!(document.set_value(1, b"x").is_err());
        assert!(document.append("core", None, "x=y\ny", b"x").is_err());
        assert!(
            document
                .rename_section("core", None, Some(b"x\ny"))
                .is_err()
        );
        assert_eq!(document.as_bytes(), before);
    }
}