grit-lib 0.5.18

Core library for the grit Git implementation
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
//! Git bundle format ([gitformat-bundle](https://git-scm.com/docs/gitformat-bundle)).
//!
//! Bundles combine a text header (signature, optional v3 capabilities, prerequisite
//! commits, ref tips) with a thin packfile. This module reads and writes that format,
//! verifies prerequisite connectivity against a local repository, and ingests the pack
//! without updating refs (callers apply [`BundleHeader::refs`] themselves).

use std::collections::{HashMap, HashSet, VecDeque};
use std::fs::File;
use std::io::{BufRead, Seek, Write};
use std::path::{Path, PathBuf};

use thiserror::Error;

use crate::commit_pretty::message_subject;
use crate::connectivity::bundle_prerequisites_connected_to_refs;
use crate::error::{Error, Result};
use crate::index_pack::{ingest_received_pack_path_with_progress, IngestPackOptions};
use crate::objects::{parse_commit, HashAlgo, ObjectId, ObjectKind};
use crate::odb::Odb;
use crate::progress::{NullProgress, ProgressPhase, ProgressSink, ProgressUnit};
use crate::repo::Repository;
use crate::rev_list::{
    FilterObjectKind, MissingAction, ObjectFilter, RevListOptions, RevListResult,
};
use crate::transfer::{build_pack, build_pack_from_send_list, reachable_closure, PackBuildOptions};

const V2_SIGNATURE: &str = "# v2 git bundle\n";
const V3_SIGNATURE: &str = "# v3 git bundle\n";

/// Supported on-disk bundle header versions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BundleVersion {
    /// Legacy header (SHA-1 repositories without filter capabilities).
    V2,
    /// Capabilities for object format and partial-clone filter.
    V3,
}

/// Wire filter specification carried in v3 `@filter=` capabilities.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilterSpec(ObjectFilter);

impl FilterSpec {
    /// Parse a filter capability value (same grammar as `--filter=` on fetch).
    ///
    /// # Errors
    ///
    /// Returns [`BundleError::MalformedLine`] when the spec is invalid.
    pub fn parse(spec: &str) -> std::result::Result<Self, BundleError> {
        ObjectFilter::parse(spec)
            .map(Self)
            .map_err(BundleError::MalformedLine)
    }

    /// Borrow the parsed filter.
    #[must_use]
    pub fn as_filter(&self) -> &ObjectFilter {
        &self.0
    }

    /// Serialize for an `@filter=` capability line (percent-encoding when needed).
    #[must_use]
    pub fn to_capability_value(&self) -> String {
        filter_to_capability_value(&self.0)
    }
}

/// Parsed bundle header (everything before the `PACK` stream).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BundleHeader {
    /// Header version (`2` or `3`).
    pub version: BundleVersion,
    /// Object hash used in the pack and OID lines.
    pub object_format: HashAlgo,
    /// Partial-clone filter advertised by the bundle (v3 only).
    pub filter: Option<FilterSpec>,
    /// Prerequisite commits that must already exist (`-oid` lines).
    pub prerequisites: Vec<(ObjectId, Option<String>)>,
    /// Ref tips included in the pack (`oid refname` lines).
    pub refs: Vec<(String, ObjectId)>,
}

/// A single ref entry returned from [`Bundle::unbundle`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnbundledRef {
    /// Full ref name (e.g. `refs/heads/main`).
    pub name: String,
    /// Tip object id.
    pub oid: ObjectId,
}

/// Result of ingesting a bundle's embedded pack into a repository.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnbundleOutcome {
    /// Ref tips from the bundle header (refs are not updated on disk by unbundle).
    pub refs: Vec<UnbundledRef>,
    /// Objects and bytes installed from the bundle pack.
    pub transfer: crate::transfer::TransferStats,
    /// Object ids indexed from the installed pack.
    pub pack_object_ids: HashSet<ObjectId>,
}

/// Outcome of [`Bundle::verify`].
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct BundleVerifyReport {
    /// Prerequisites missing from the object database.
    pub missing_prerequisites: Vec<(ObjectId, Option<String>)>,
    /// Prerequisites exist as objects but are not reachable from local refs.
    pub prerequisites_not_connected: bool,
}

impl BundleVerifyReport {
    /// Whether verification succeeded with no issues.
    #[must_use]
    pub fn is_ok(&self) -> bool {
        self.missing_prerequisites.is_empty() && !self.prerequisites_not_connected
    }

    /// Convert validation failures into a [`BundleError`].
    ///
    /// Returns `Ok(())` when [`Self::is_ok`].
    ///
    /// # Errors
    ///
    /// Returns the first missing prerequisite, or [`BundleError::PrerequisitesNotConnected`].
    pub fn into_result(self) -> std::result::Result<(), BundleError> {
        if let Some((oid, subject)) = self.missing_prerequisites.into_iter().next() {
            return Err(BundleError::MissingPrerequisite { oid, subject });
        }
        if self.prerequisites_not_connected {
            return Err(BundleError::PrerequisitesNotConnected);
        }
        Ok(())
    }
}

/// Bundle read/write/verify failures.
#[non_exhaustive]
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum BundleError {
    /// The file does not begin with a v2/v3 bundle signature.
    #[error("not a v2 or v3 git bundle")]
    BadSignature,
    /// Only versions 2 and 3 are supported.
    #[error("unsupported bundle version")]
    UnsupportedVersion,
    /// A v3 `@` capability was not recognized.
    #[error("unknown bundle capability: {0}")]
    UnsupportedCapability(String),
    /// A header line could not be parsed.
    #[error("malformed bundle header: {0}")]
    MalformedLine(String),
    /// Header object format disagrees with the repository.
    #[error("bundle object format {bundle_name} does not match repository {repo_name}")]
    ObjectFormatMismatch {
        /// Format recorded in the bundle header.
        bundle_name: &'static str,
        /// Format configured for the open repository.
        repo_name: &'static str,
    },
    /// A prerequisite commit is absent from the ODB.
    #[error("missing prerequisite commit {oid}")]
    MissingPrerequisite {
        /// Required commit oid.
        oid: ObjectId,
        /// Optional subject from the header line.
        subject: Option<String>,
    },
    /// Prerequisite objects exist but are not connected to local refs.
    #[error("prerequisite commits are not connected to local refs")]
    PrerequisitesNotConnected,
    /// No ref tips were listed in the header.
    #[error("bundle contains no refs")]
    EmptyRefs,
    /// The pack stream did not start where expected.
    #[error("bundle pack stream missing or invalid")]
    MissingPack,
    /// Underlying I/O failure.
    #[error("{0}")]
    Io(String),
}

impl From<std::io::Error> for BundleError {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e.to_string())
    }
}

/// Input for [`write_bundle`].
#[derive(Debug, Clone, Default)]
pub struct BundleSpec {
    /// Tips to include (`oid` + ref name for the header).
    pub include: Vec<(ObjectId, String)>,
    /// Commits treated as already present (incremental bundles).
    pub exclude: Vec<ObjectId>,
    /// Optional partial-clone filter (forces v3).
    pub filter: Option<FilterSpec>,
}

/// An opened bundle file on disk.
#[derive(Debug, Clone)]
pub struct Bundle {
    path: PathBuf,
    header: BundleHeader,
    pack_offset: u64,
}

impl Bundle {
    /// Open `path` and parse the header, leaving the pack at `pack_offset`.
    ///
    /// # Errors
    ///
    /// Returns [`BundleError`] when the signature or header lines are invalid.
    pub fn open(path: impl AsRef<Path>) -> std::result::Result<Self, BundleError> {
        let path = path.as_ref().to_path_buf();
        let (header, pack_offset) = read_header_from_file(&path)?;
        Ok(Self {
            path,
            header,
            pack_offset,
        })
    }

    /// Parsed header.
    #[must_use]
    pub fn header(&self) -> &BundleHeader {
        &self.header
    }

    /// Path passed to [`Self::open`].
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Byte offset in [`Self::path`] where the `PACK` stream begins.
    #[must_use]
    pub fn pack_byte_offset(&self) -> u64 {
        self.pack_offset
    }

    /// Check prerequisites against `repo` and ref connectivity.
    ///
    /// # Errors
    ///
    /// Returns [`BundleError::ObjectFormatMismatch`] when hash algorithms disagree.
    pub fn verify(
        &self,
        repo: &Repository,
    ) -> std::result::Result<BundleVerifyReport, BundleError> {
        let repo_algo = repo.odb().hash_algo();
        if self.header.object_format != repo_algo {
            return Err(BundleError::ObjectFormatMismatch {
                bundle_name: self.header.object_format.name(),
                repo_name: repo_algo.name(),
            });
        }
        let mut report = BundleVerifyReport::default();
        for (oid, subject) in &self.header.prerequisites {
            if repo.odb().read(oid).is_err() {
                report.missing_prerequisites.push((*oid, subject.clone()));
            }
        }
        if report.missing_prerequisites.is_empty() {
            let prereq_oids: Vec<ObjectId> =
                self.header.prerequisites.iter().map(|(o, _)| *o).collect();
            let connected = bundle_prerequisites_connected_to_refs(repo, &prereq_oids)
                .map_err(|e| BundleError::Io(e.to_string()))?;
            report.prerequisites_not_connected = !connected;
        }
        Ok(report)
    }

    /// Stream the embedded pack into `repo`'s ODB (does not update refs).
    ///
    /// # Errors
    ///
    /// Returns [`BundleError`] or ingest failures wrapped as [`BundleError::Io`].
    pub fn unbundle(
        &self,
        repo: &Repository,
    ) -> std::result::Result<Vec<UnbundledRef>, BundleError> {
        Ok(self.unbundle_with_progress(repo, &mut NullProgress)?.refs)
    }

    /// Like [`Self::unbundle`], reporting receive and index-pack progress through `progress`.
    pub fn unbundle_with_progress(
        &self,
        repo: &Repository,
        progress: &mut dyn ProgressSink,
    ) -> std::result::Result<UnbundleOutcome, BundleError> {
        if self.header.object_format != repo.odb().hash_algo() {
            return Err(BundleError::ObjectFormatMismatch {
                bundle_name: self.header.object_format.name(),
                repo_name: repo.odb().hash_algo().name(),
            });
        }
        let pack_bytes = std::fs::metadata(&self.path)
            .map(|m| m.len().saturating_sub(self.pack_offset))
            .unwrap_or(0);
        progress.start_with_unit(
            ProgressPhase::ReceivingObjects,
            ProgressUnit::Bytes,
            Some(pack_bytes),
        );
        let tmp = tempfile::NamedTempFile::new_in(repo.git_dir().join("objects").join("pack"))
            .map_err(|e| BundleError::Io(e.to_string()))?;
        {
            let mut input = File::open(&self.path)?;
            Seek::seek(&mut input, std::io::SeekFrom::Start(self.pack_offset))
                .map_err(BundleError::from)?;
            let mut output = std::io::BufWriter::new(tmp.as_file());
            let copied = std::io::copy(&mut input, &mut output).map_err(BundleError::from)?;
            output.flush().map_err(BundleError::from)?;
            progress.set(copied);
        }
        progress.finish();
        let opts = IngestPackOptions {
            fix_thin: true,
            ..Default::default()
        };
        let ingested = ingest_received_pack_path_with_progress(
            tmp.path().to_path_buf(),
            repo.odb(),
            &opts,
            Some(progress),
        )
        .map_err(|e| BundleError::Io(e.to_string()))?;
        if self.header.filter.is_some() {
            std::fs::write(ingested.pack_path.with_extension("promisor"), b"")
                .map_err(|e| BundleError::Io(e.to_string()))?;
            repo.odb().invalidate_packs();
        }
        let transfer = crate::transfer::TransferStats::from_ingested_pack(&ingested)
            .map_err(|e| BundleError::Io(e.to_string()))?;
        let refs = self
            .header
            .refs
            .iter()
            .map(|(name, oid)| UnbundledRef {
                name: name.clone(),
                oid: *oid,
            })
            .collect();
        Ok(UnbundleOutcome {
            refs,
            transfer,
            pack_object_ids: ingested.object_ids,
        })
    }
}

/// Parse a bundle header from `reader`, leaving the cursor at the first `PACK` byte.
///
/// # Errors
///
/// Returns [`BundleError`] when the signature or header is invalid.
pub fn read_header(reader: &mut dyn BufRead) -> std::result::Result<BundleHeader, BundleError> {
    let header = read_header_lines(reader)?;
    ensure_pack_magic(reader)?;
    Ok(header)
}

/// Parse the signature, capability, prerequisite and ref lines up to and including the
/// blank line that ends the header, without inspecting the pack that follows.
fn read_header_lines(reader: &mut dyn BufRead) -> std::result::Result<BundleHeader, BundleError> {
    let signature = read_line(reader)?;
    let version = match signature.as_str() {
        s if s == V2_SIGNATURE.trim_end() => BundleVersion::V2,
        s if s == V3_SIGNATURE.trim_end() => BundleVersion::V3,
        _ => return Err(BundleError::BadSignature),
    };

    let mut object_format = HashAlgo::Sha1;
    let mut filter = None;
    let mut prerequisites = Vec::new();
    let mut refs = Vec::new();

    loop {
        let line = read_line(reader)?;
        if line.is_empty() {
            break;
        }
        if version == BundleVersion::V3 && line.starts_with('@') {
            parse_capability(&line[1..], &mut object_format, &mut filter)?;
            continue;
        }
        let (is_prereq, rest) = if let Some(stripped) = line.strip_prefix('-') {
            (true, stripped)
        } else {
            (false, line.as_str())
        };
        let (oid, tail) = parse_oid_field(rest, object_format)?;
        if is_prereq {
            let subject = tail
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .map(str::to_owned);
            prerequisites.push((oid, subject));
        } else {
            let refname = tail.ok_or_else(|| {
                BundleError::MalformedLine("ref line missing ref name".to_owned())
            })?;
            let refname = refname.trim();
            if refname.is_empty() {
                return Err(BundleError::MalformedLine(
                    "ref line missing ref name".to_owned(),
                ));
            }
            refs.push((refname.to_owned(), oid));
        }
    }

    Ok(BundleHeader {
        version,
        object_format,
        filter,
        prerequisites,
        refs,
    })
}

/// Buffer size for streaming the header; most headers fit in one read, and the pack that
/// follows (often far larger than the header) is never pulled into memory.
const HEADER_READ_BUF: usize = 64 * 1024;

fn read_header_from_file(path: &Path) -> std::result::Result<(BundleHeader, u64), BundleError> {
    let file = File::open(path).map_err(BundleError::from)?;
    let mut reader = std::io::BufReader::with_capacity(HEADER_READ_BUF, file);
    let header = read_header_lines(&mut reader)?;
    let mut pack_offset = reader.stream_position().map_err(BundleError::from)?;
    let mut magic = [0u8; 4];
    loop {
        read_exact_or_missing_pack(&mut reader, &mut magic[..1])?;
        if magic[0] != b'\n' && magic[0] != b'\r' {
            break;
        }
        pack_offset += 1;
    }
    read_exact_or_missing_pack(&mut reader, &mut magic[1..])?;
    if &magic != b"PACK" {
        return Err(BundleError::MissingPack);
    }
    Ok((header, pack_offset))
}

fn read_exact_or_missing_pack(
    reader: &mut dyn std::io::Read,
    buf: &mut [u8],
) -> std::result::Result<(), BundleError> {
    reader.read_exact(buf).map_err(|e| match e.kind() {
        std::io::ErrorKind::UnexpectedEof => BundleError::MissingPack,
        _ => BundleError::from(e),
    })
}

fn ensure_pack_magic(reader: &mut dyn BufRead) -> std::result::Result<(), BundleError> {
    loop {
        let buf = reader.fill_buf().map_err(BundleError::from)?;
        if buf.is_empty() {
            return Err(BundleError::MissingPack);
        }
        if buf.starts_with(b"PACK") {
            return Ok(());
        }
        if buf[0] == b'\n' || buf[0] == b'\r' {
            reader.consume(1);
            continue;
        }
        return Err(BundleError::MissingPack);
    }
}

/// Write a bundle to `out` from `repo` using `spec`.
///
/// # Errors
///
/// Returns [`BundleError::EmptyRefs`] when `spec.include` is empty, or I/O / pack errors.
pub fn write_bundle(
    repo: &Repository,
    spec: &BundleSpec,
    out: &mut dyn Write,
) -> std::result::Result<(), BundleError> {
    if spec.include.is_empty() {
        return Err(BundleError::EmptyRefs);
    }
    let algo = repo.odb().hash_algo();
    let version = if algo != HashAlgo::Sha1 || spec.filter.is_some() {
        BundleVersion::V3
    } else {
        BundleVersion::V2
    };
    write_signature(out, version)?;
    if version == BundleVersion::V3 {
        write_capability_object_format(out, algo)?;
        if let Some(filter) = &spec.filter {
            write_capability_filter(out, filter)?;
        }
    }

    let include_oids: Vec<ObjectId> = spec.include.iter().map(|(o, _)| *o).collect();
    let boundaries = prerequisite_commits(repo.odb(), &include_oids, &spec.exclude)?;
    for (oid, subject) in &boundaries {
        write!(out, "-{} ", oid.to_hex()).map_err(|e| BundleError::Io(e.to_string()))?;
        if let Some(s) = subject {
            write!(out, "{s}").map_err(|e| BundleError::Io(e.to_string()))?;
        }
        writeln!(out).map_err(|e| BundleError::Io(e.to_string()))?;
    }

    for (oid, name) in &spec.include {
        writeln!(out, "{} {name}", oid.to_hex()).map_err(|e| BundleError::Io(e.to_string()))?;
    }
    writeln!(out).map_err(|e| BundleError::Io(e.to_string()))?;

    let mut haves: Vec<ObjectId> = spec
        .exclude
        .iter()
        .map(|oid| peel_to_commit(repo.odb(), *oid))
        .collect::<std::result::Result<Vec<_>, Error>>()
        .map_err(|e| BundleError::Io(e.to_string()))?;
    haves.sort();
    haves.dedup();
    let pack_opts = PackBuildOptions::for_local_push(None);
    let empty_shallow = HashSet::new();
    let have_closure = reachable_closure(repo.odb(), &haves, &HashSet::new(), true, &empty_shallow)
        .map_err(|e| BundleError::Io(e.to_string()))?;
    let pack = if let Some(filter) = spec.filter.as_ref() {
        let send = filtered_pack_objects(
            repo,
            &include_oids,
            &spec.exclude,
            filter.as_filter(),
            &have_closure,
        )
        .map_err(|e| BundleError::Io(e.to_string()))?;
        build_pack_from_send_list(
            repo.odb(),
            &send,
            &have_closure,
            &pack_opts,
            &HashMap::new(),
        )
        .map_err(|e| BundleError::Io(e.to_string()))?
    } else {
        build_pack(repo.odb(), &include_oids, &haves, &pack_opts)
            .map_err(|e| BundleError::Io(e.to_string()))?
    };
    out.write_all(&pack)
        .map_err(|e| BundleError::Io(e.to_string()))?;
    Ok(())
}

/// Incremental writer for bundle bytes (header + pack).
pub struct BundleWriter<W: Write> {
    inner: W,
    finished_header: bool,
}

impl<W: Write> BundleWriter<W> {
    /// Create a writer; call [`Self::write_header_and_pack`] to emit a complete bundle.
    pub fn new(inner: W) -> Self {
        Self {
            inner,
            finished_header: false,
        }
    }

    /// Write the header and pack in one step.
    ///
    /// # Errors
    ///
    /// Same as [`write_bundle`].
    pub fn write_header_and_pack(
        mut self,
        repo: &Repository,
        spec: &BundleSpec,
    ) -> std::result::Result<(), BundleError> {
        write_bundle(repo, spec, &mut self.inner)?;
        self.finished_header = true;
        Ok(())
    }
}

fn write_signature(
    out: &mut dyn Write,
    version: BundleVersion,
) -> std::result::Result<(), BundleError> {
    let sig = match version {
        BundleVersion::V2 => V2_SIGNATURE,
        BundleVersion::V3 => V3_SIGNATURE,
    };
    out.write_all(sig.as_bytes())
        .map_err(|e| BundleError::Io(e.to_string()))
}

fn write_capability_object_format(
    out: &mut dyn Write,
    algo: HashAlgo,
) -> std::result::Result<(), BundleError> {
    writeln!(out, "@object-format={}", algo.name()).map_err(|e| BundleError::Io(e.to_string()))
}

fn write_capability_filter(
    out: &mut dyn Write,
    filter: &FilterSpec,
) -> std::result::Result<(), BundleError> {
    writeln!(out, "@filter={}", filter.to_capability_value())
        .map_err(|e| BundleError::Io(e.to_string()))
}

fn parse_capability(
    cap: &str,
    object_format: &mut HashAlgo,
    filter: &mut Option<FilterSpec>,
) -> std::result::Result<(), BundleError> {
    if let Some(name) = cap.strip_prefix("object-format=") {
        *object_format = HashAlgo::from_name(name)
            .ok_or_else(|| BundleError::MalformedLine(format!("unknown object format: {name}")))?;
        return Ok(());
    }
    if let Some(spec) = cap.strip_prefix("filter=") {
        *filter = Some(FilterSpec::parse(spec)?);
        return Ok(());
    }
    Err(BundleError::UnsupportedCapability(cap.to_owned()))
}

fn read_line(reader: &mut dyn BufRead) -> std::result::Result<String, BundleError> {
    let mut line = String::new();
    let n = reader.read_line(&mut line).map_err(BundleError::from)?;
    if n == 0 {
        return Err(BundleError::MalformedLine(
            "unexpected end of header".to_owned(),
        ));
    }
    if line.ends_with('\n') {
        line.pop();
        if line.ends_with('\r') {
            line.pop();
        }
    }
    Ok(line)
}

fn parse_oid_field(
    line: &str,
    algo: HashAlgo,
) -> std::result::Result<(ObjectId, Option<&str>), BundleError> {
    let hex_len = algo.hex_len();
    if line.len() < hex_len {
        return Err(BundleError::MalformedLine("short object id".to_owned()));
    }
    let (hex, tail) = line.split_at(hex_len);
    if !hex.chars().all(|c| c.is_ascii_hexdigit()) {
        return Err(BundleError::MalformedLine("invalid object id".to_owned()));
    }
    let oid = ObjectId::from_hex(hex)
        .map_err(|_| BundleError::MalformedLine("invalid object id".to_owned()))?;
    if hex.len() != algo.hex_len() {
        return Err(BundleError::MalformedLine(
            "object id length mismatch".to_owned(),
        ));
    }
    let tail = match tail.chars().next() {
        None => None,
        Some(c) if c.is_whitespace() => Some(tail.trim_start()),
        Some(_) => {
            return Err(BundleError::MalformedLine(
                "garbage after object id".to_owned(),
            ))
        }
    };
    Ok((oid, tail))
}

fn peel_to_commit(odb: &Odb, oid: ObjectId) -> Result<ObjectId> {
    match odb.peel_tags(oid)? {
        (commit, ObjectKind::Commit) => Ok(commit),
        (other, _) => Err(Error::CorruptObject(format!(
            "object {other} is not commit or tag"
        ))),
    }
}

fn commit_parent_set(odb: &Odb, commit: ObjectId) -> Result<Vec<ObjectId>> {
    let obj = odb.read(&commit)?;
    let commit = parse_commit(&obj.data)?;
    Ok(commit.parents)
}

fn commit_closure_roots(odb: &Odb, roots: &[ObjectId]) -> Result<HashSet<ObjectId>> {
    let mut seen = HashSet::new();
    let mut q: VecDeque<ObjectId> = VecDeque::new();
    for &root in roots {
        let c = peel_to_commit(odb, root)?;
        if seen.insert(c) {
            q.push_back(c);
        }
    }
    while let Some(c) = q.pop_front() {
        for p in commit_parent_set(odb, c)? {
            if seen.insert(p) {
                q.push_back(p);
            }
        }
    }
    Ok(seen)
}

fn prerequisite_commits(
    odb: &Odb,
    includes: &[ObjectId],
    excludes: &[ObjectId],
) -> std::result::Result<Vec<(ObjectId, Option<String>)>, BundleError> {
    if excludes.is_empty() {
        return Ok(Vec::new());
    }
    let exclude_set =
        commit_closure_roots(odb, excludes).map_err(|e| BundleError::Io(e.to_string()))?;
    let mut prerequisites = Vec::new();
    let mut seen_prereq = HashSet::new();
    let mut walk_seen = HashSet::new();
    let mut q: VecDeque<ObjectId> = VecDeque::new();
    for &root in includes {
        let c = peel_to_commit(odb, root).map_err(|e| BundleError::Io(e.to_string()))?;
        if exclude_set.contains(&c) {
            continue;
        }
        if walk_seen.insert(c) {
            q.push_back(c);
        }
    }
    while let Some(c) = q.pop_front() {
        let parents = commit_parent_set(odb, c).map_err(|e| BundleError::Io(e.to_string()))?;
        for p in parents {
            if exclude_set.contains(&p) && seen_prereq.insert(p) {
                let subject = commit_subject(odb, p).map_err(|e| BundleError::Io(e.to_string()))?;
                prerequisites.push((p, subject));
            }
            if !exclude_set.contains(&p) && walk_seen.insert(p) {
                q.push_back(p);
            }
        }
    }
    Ok(prerequisites)
}

fn commit_subject(odb: &Odb, commit: ObjectId) -> Result<Option<String>> {
    let obj = odb.read(&commit)?;
    let parsed = parse_commit(&obj.data)?;
    let subj = message_subject(&parsed.message);
    if subj.is_empty() {
        Ok(None)
    } else {
        Ok(Some(subj))
    }
}

fn filter_to_capability_value(filter: &ObjectFilter) -> String {
    match filter {
        ObjectFilter::BlobNone => "blob:none".to_owned(),
        ObjectFilter::BlobLimit(n) => format!("blob:limit={n}"),
        ObjectFilter::TreeDepth(d) => format!("tree:{d}"),
        ObjectFilter::SparseOid(s) => format!("sparse:oid={s}"),
        ObjectFilter::ObjectType(k) => format!("object:type={}", filter_object_kind_name(*k)),
        ObjectFilter::Combine(parts) => {
            let encoded: Vec<String> = parts
                .iter()
                .map(|p| {
                    crate::rev_list::url_encode_object_filter_subspec(&filter_to_capability_value(
                        p,
                    ))
                })
                .collect();
            format!("combine:{}", encoded.join("+"))
        }
    }
}

fn filter_object_kind_name(kind: FilterObjectKind) -> &'static str {
    match kind {
        FilterObjectKind::Blob => "blob",
        FilterObjectKind::Tree => "tree",
        FilterObjectKind::Commit => "commit",
        FilterObjectKind::Tag => "tag",
    }
}

/// Object list for a filtered bundle pack (`rev-list --objects --filter`).
fn filtered_pack_objects(
    repo: &Repository,
    include: &[ObjectId],
    exclude: &[ObjectId],
    filter: &ObjectFilter,
    have_closure: &HashSet<ObjectId>,
) -> Result<Vec<ObjectId>> {
    let positive: Vec<String> = include.iter().map(ObjectId::to_hex).collect();
    let negative: Vec<String> = exclude
        .iter()
        .map(|oid| format!("^{}", oid.to_hex()))
        .collect();
    let options = RevListOptions {
        objects: true,
        no_object_names: true,
        quiet: true,
        filter: Some(filter.clone()),
        missing_action: MissingAction::Error,
        ..Default::default()
    };
    let walk = crate::rev_list::rev_list(repo, &positive, &negative, &options)?;
    Ok(collect_filtered_send_list(&walk, have_closure))
}

fn collect_filtered_send_list(
    walk: &RevListResult,
    have_closure: &HashSet<ObjectId>,
) -> Vec<ObjectId> {
    let mut send = Vec::new();
    let mut seen = HashSet::new();
    let mut push = |oid: ObjectId| {
        if have_closure.contains(&oid) {
            return;
        }
        if seen.insert(oid) {
            send.push(oid);
        }
    };
    for oid in &walk.commits {
        push(*oid);
    }
    for (oid, _) in &walk.objects {
        push(*oid);
    }
    for tag_oid in walk.tip_annotated_tag_by_commit.values() {
        push(*tag_oid);
    }
    send
}

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

    #[test]
    fn parse_v2_header_and_refs() {
        let oid = "a".repeat(40);
        let data = format!("# v2 git bundle\n{oid} refs/heads/main\n\nPACK");
        let mut cur = Cursor::new(data.into_bytes());
        let header = read_header(&mut cur).expect("header");
        assert_eq!(header.version, BundleVersion::V2);
        assert_eq!(header.object_format, HashAlgo::Sha1);
        assert_eq!(header.prerequisites.len(), 0);
        assert_eq!(header.refs.len(), 1);
        assert_eq!(header.refs[0].0, "refs/heads/main");
    }

    #[test]
    fn bad_signature_yields_error() {
        let mut cur = Cursor::new(b"# v4 git bundle\n\nPACK");
        let err = read_header(&mut cur).unwrap_err();
        assert_eq!(err, BundleError::BadSignature);
    }

    #[test]
    fn unknown_capability() {
        let mut cur = Cursor::new(b"# v3 git bundle\n@unknown=yes\n\nPACK");
        let err = read_header(&mut cur).unwrap_err();
        assert!(matches!(err, BundleError::UnsupportedCapability(_)));
    }

    #[test]
    fn truncated_ref_line() {
        let oid = "a".repeat(40);
        let data = format!("# v2 git bundle\n{oid}\n\nPACK");
        let mut cur = Cursor::new(data.into_bytes());
        let err = read_header(&mut cur).unwrap_err();
        assert!(matches!(err, BundleError::MalformedLine(_)));
    }

    #[test]
    fn filter_spec_roundtrip_blob_none() {
        let f = FilterSpec::parse("blob:none").expect("parse");
        assert_eq!(f.to_capability_value(), "blob:none");
    }

    #[test]
    fn verify_report_missing_prerequisite_error() {
        let oid = ObjectId::from_hex(&"b".repeat(40)).expect("oid");
        let report = BundleVerifyReport {
            missing_prerequisites: vec![(oid, Some("subj".to_owned()))],
            prerequisites_not_connected: false,
        };
        let err = report.into_result().unwrap_err();
        assert!(matches!(err, BundleError::MissingPrerequisite { .. }));
    }

    fn write_bundle_file(bytes: &[u8]) -> tempfile::NamedTempFile {
        let mut file = tempfile::NamedTempFile::new().expect("tempfile");
        file.write_all(bytes).expect("write bundle");
        file.flush().expect("flush bundle");
        file
    }

    #[test]
    fn open_reports_pack_offset_after_header() {
        let oid = "a".repeat(40);
        let header = format!("# v2 git bundle\n{oid} refs/heads/main\n\n");
        let mut data = header.clone().into_bytes();
        data.extend_from_slice(b"PACK\0\0\0\x02\0\0\0\0");
        let file = write_bundle_file(&data);
        let bundle = Bundle::open(file.path()).expect("open");
        assert_eq!(bundle.pack_byte_offset(), header.len() as u64);
        assert_eq!(bundle.header().refs.len(), 1);
    }

    #[test]
    fn open_skips_blank_lines_before_pack() {
        let oid = "a".repeat(40);
        let header = format!("# v2 git bundle\n{oid} HEAD\n\n\r\n\n");
        let mut data = header.clone().into_bytes();
        data.extend_from_slice(b"PACK");
        let file = write_bundle_file(&data);
        let bundle = Bundle::open(file.path()).expect("open");
        assert_eq!(bundle.pack_byte_offset(), header.len() as u64);
    }

    #[test]
    fn open_handles_pack_magic_across_read_buffer_boundary() {
        let oid = "c".repeat(40);
        let signature = "# v2 git bundle\n";
        let ref_line = |i: usize| format!("{oid} refs/tags/t{i:06}\n");
        let line_len = ref_line(0).len();
        // Grow the header until the blank terminator lands two bytes before the first
        // buffer refill, so "PACK" straddles the boundary.
        let mut refs = 0usize;
        while signature.len() + (refs + 1) * line_len + 1 + 2 <= HEADER_READ_BUF {
            refs += 1;
        }
        let mut header = String::from(signature);
        for i in 0..refs {
            header.push_str(&ref_line(i));
        }
        header.push('\n');
        let pad = HEADER_READ_BUF - 2 - header.len();
        let mut data = header.clone().into_bytes();
        data.extend(std::iter::repeat_n(b'\n', pad));
        data.extend_from_slice(b"PACK\0\0\0\x02");
        assert_eq!(&data[HEADER_READ_BUF - 2..HEADER_READ_BUF + 2], b"PACK");
        let file = write_bundle_file(&data);
        let bundle = Bundle::open(file.path()).expect("open");
        assert_eq!(bundle.header().refs.len(), refs);
        assert_eq!(bundle.pack_byte_offset(), (HEADER_READ_BUF - 2) as u64);
    }

    #[test]
    fn open_without_pack_is_missing_pack() {
        let oid = "a".repeat(40);
        let truncated = format!("# v2 git bundle\n{oid} HEAD\n\n");
        let file = write_bundle_file(truncated.as_bytes());
        assert_eq!(
            Bundle::open(file.path()).unwrap_err(),
            BundleError::MissingPack
        );

        let short = format!("# v2 git bundle\n{oid} HEAD\n\nPA");
        let file = write_bundle_file(short.as_bytes());
        assert_eq!(
            Bundle::open(file.path()).unwrap_err(),
            BundleError::MissingPack
        );

        let garbage = format!("# v2 git bundle\n{oid} HEAD\n\nJUNK");
        let file = write_bundle_file(garbage.as_bytes());
        assert_eq!(
            Bundle::open(file.path()).unwrap_err(),
            BundleError::MissingPack
        );
    }
}