git-zero-server 0.1.0

Allocation-free no_std Git smart/dumb HTTP serving and receive-pack primitives
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
//! Stateless v0 negotiation. Parse the complete bounded body before output.
use super::extensions;
use git_zero_core::{
    Error, ObjectFormat, ObjectId, ObjectKind, Repository, Result,
    io::Write,
    pktline::{self, Packet},
};

pub(crate) fn capabilities(selection: bool) -> &'static [u8] {
    if selection {
        git_zero_core::repository::UPLOAD_CAPABILITIES
    } else {
        b" multi_ack multi_ack_detailed side-band side-band-64k no-progress"
    }
}

#[derive(Clone, Copy, Default)]
struct Capabilities(u16);

const CAPABILITY_FLAGS: &[(&[u8], u16)] = &[
    (b"multi_ack", 1),
    (b"multi_ack_detailed", 2),
    (b"side-band", 4),
    (b"side-band-64k", 8),
    (b"no-progress", 16),
    (b"shallow", 64),
    (b"filter", 128),
    (b"deepen-relative", 256),
    (b"ofs-delta", 512),
    (b"include-tag", 1024),
    (b"deepen-since", 2048),
    (b"deepen-not", 4096),
];

fn capability_flag(capability: &[u8], format: ObjectFormat) -> Result<u16> {
    if capability == format.capability() {
        return Ok(32);
    }
    if matches!(
        capability,
        b"object-format=sha1" | b"object-format=sha256" | b""
    ) {
        return Err(Error::InvalidRequest);
    }
    CAPABILITY_FLAGS
        .iter()
        .find_map(|&(name, flag)| (name == capability).then_some(flag))
        .ok_or(Error::Unsupported)
}

impl Capabilities {
    fn parse(bytes: &[u8], format: ObjectFormat) -> Result<Self> {
        let mut seen = 0_u16;
        // libgit2 can leave a trailing separator; empty fields select nothing.
        for capability in bytes
            .split(|&byte| byte == b' ')
            .filter(|part| !part.is_empty())
        {
            let flag = capability_flag(capability, format)?;
            if seen & flag != 0 {
                return Err(Error::InvalidRequest);
            }
            seen |= flag;
        }
        if seen & 12 == 12 {
            return Err(Error::InvalidRequest);
        }
        Ok(Self(seen))
    }

    fn want(&mut self, value: &[u8], first: bool, format: ObjectFormat) -> Result<ObjectId> {
        let mut hex = value;
        if let Some(split) = value.iter().position(|&byte| byte == b' ') {
            if !first {
                return Err(Error::InvalidRequest);
            }
            *self = Self::parse(&value[split + 1..], format)?;
            hex = &value[..split];
        }
        id(hex, format)
    }

    fn ack(self) -> u8 {
        // Clients can select both advertised modes; detailed takes precedence.
        if self.0 & 2 != 0 {
            2
        } else {
            (self.0 & 1) as u8
        }
    }
    fn band(self) -> u8 {
        ((self.0 >> 2) & 3) as u8
    }
}

pub(crate) struct UploadRequest<'a> {
    pub done: bool,
    capabilities: Capabilities,
    haves: &'a [u8],
    v2: bool,
    #[cfg(feature = "read")]
    shallow_body: &'a [u8],
    pub extensions: extensions::Extensions,
    #[cfg(feature = "read")]
    include_tags: bool,
}

pub(crate) fn id(bytes: &[u8], format: ObjectFormat) -> Result<ObjectId> {
    let id = ObjectId::from_hex(bytes)?;
    if id.format() != format || id.is_zero() {
        return Err(Error::InvalidRequest);
    }
    Ok(id)
}

fn request_line(line: &[u8]) -> &[u8] {
    line.strip_suffix(b"\n").unwrap_or(line)
}

fn unexpected_command(line: &[u8]) -> Error {
    if line.starts_with(b"want ") || line.starts_with(b"have ") || line == b"done" {
        Error::InvalidRequest
    } else {
        Error::Unsupported
    }
}

fn parse_wants(
    bytes: &mut &[u8],
    format: ObjectFormat,
    mut want: impl FnMut(ObjectId) -> Result<()>,
    extensions: &mut extensions::Extensions,
) -> Result<Capabilities> {
    let mut wanted = false;
    let mut extended = false;
    let mut capabilities = Capabilities::default();
    while let Some(packet) = pktline::read(bytes)? {
        let Packet::Data(line) = packet else {
            if packet != Packet::Flush {
                return Err(Error::InvalidRequest);
            }
            // Shallow commands use the server's advertisement; Git does not
            // echo `shallow` in its want capabilities. Filter is negotiated.
            extensions.options.deepen_relative |= capabilities.0 & 256 != 0;
            extensions.require_filter_capability(capabilities.0 & 128 != 0)?;
            return wanted.then_some(capabilities).ok_or(Error::InvalidRequest);
        };
        let line = request_line(line);
        if let Some(value) = line.strip_prefix(b"want ") {
            if extended {
                return Err(Error::InvalidRequest);
            }
            let id = capabilities.want(value, !wanted, format)?;
            if !extensions.revisions_enabled && capabilities.0 & 6144 != 0 {
                return Err(Error::Unsupported);
            }
            want(id)?;
            wanted = true;
        } else if wanted {
            extensions.argument(line, format)?;
            extended = true;
        } else {
            return Err(unexpected_command(line));
        }
    }
    Err(Error::InvalidRequest)
}

fn parse_haves(mut bytes: &[u8], format: ObjectFormat) -> Result<bool> {
    let empty = bytes.is_empty();
    while let Some(packet) = pktline::read(&mut bytes)? {
        let Packet::Data(line) = packet else {
            return bytes
                .is_empty()
                .then_some(false)
                .ok_or(Error::InvalidRequest);
        };
        let line = request_line(line);
        if line == b"done" && bytes.is_empty() {
            return Ok(true);
        }
        let value = line
            .strip_prefix(b"have ")
            .ok_or_else(|| unexpected_command(line))?;
        id(value, format)?;
    }
    empty.then_some(false).ok_or(Error::InvalidRequest)
}

impl<'a> UploadRequest<'a> {
    pub fn parse(
        bytes: &'a [u8],
        format: ObjectFormat,
        version: crate::route::Version,
        want: impl FnMut(ObjectId) -> Result<()>,
    ) -> Result<Self> {
        Self::parse_mode(bytes, format, version, false, want)
    }

    fn parse_mode(
        mut bytes: &'a [u8],
        format: ObjectFormat,
        version: crate::route::Version,
        revisions_enabled: bool,
        want: impl FnMut(ObjectId) -> Result<()>,
    ) -> Result<Self> {
        let mut extensions = extensions::Extensions::new(revisions_enabled);
        #[cfg(feature = "read")]
        let shallow_body = bytes;
        if version == crate::route::Version::V2 {
            let command = crate::v2::Command::parse(bytes, format)?.ok_or(Error::InvalidRequest)?;
            if command.name != b"fetch" {
                return Err(Error::InvalidRequest);
            }
            let fetch = crate::v2::Fetch::parse(command.args, format, want, &mut extensions)?;
            extensions.validate()?;
            return Ok(Self {
                done: fetch.done,
                capabilities: Capabilities(u16::from(fetch.ofs_delta) * 512),
                haves: command.args,
                v2: true,
                #[cfg(feature = "read")]
                shallow_body: command.args,
                extensions,
                #[cfg(feature = "read")]
                include_tags: fetch.include_tags,
            });
        }
        let capabilities = parse_wants(&mut bytes, format, want, &mut extensions)?;
        extensions.validate()?;
        Ok(Self {
            done: parse_haves(bytes, format)?,
            capabilities,
            haves: bytes,
            v2: false,
            #[cfg(feature = "read")]
            shallow_body,
            extensions,
            #[cfg(feature = "read")]
            include_tags: capabilities.0 & 1024 != 0,
        })
    }

    #[cfg(feature = "read")]
    pub fn ofs_delta(&self) -> bool {
        self.capabilities.0 & 512 != 0
    }

    pub fn require_complete(&self) -> Result<()> {
        // A caller-supplied complete pack cannot promise follow-tag selection.
        // Only the graph-backed API advertises include-tag.
        if self.extensions.active() || self.capabilities.0 & (192 | 1024 | 6144) != 0 {
            return Err(Error::Unsupported);
        }
        Ok(())
    }

    #[cfg(feature = "read")]
    pub(crate) fn exclusion_ids<'r>(
        &'r self,
        repository: &'r impl Repository,
    ) -> impl Iterator<Item = Result<ObjectId>> + 'r {
        packet_values(self.shallow_body, b"deepen-not ")
            .map(move |name| exclusion_ref(repository, name))
    }

    #[cfg(feature = "read")]
    pub(crate) fn revision_info(
        &self,
        format: ObjectFormat,
        changes: impl Iterator<Item = git_zero_core::ShallowChange>,
        output: &mut impl Write,
    ) -> Result<()> {
        let mut updates = ShallowUpdates::new(self, format);
        if updates.enabled {
            for change in changes {
                let (id, unshallow) = match change {
                    git_zero_core::ShallowChange::Shallow(id) => (id, false),
                    git_zero_core::ShallowChange::Unshallow(id) => (id, true),
                };
                updates.change(id, unshallow, output)?;
            }
        }
        updates.finish(output)
    }

    #[cfg(feature = "read")]
    pub fn shallow_info(
        &self,
        format: ObjectFormat,
        objects: &[Option<git_zero_core::ValidationMetadata>],
        output: &mut impl Write,
    ) -> Result<()> {
        self.revision_info(
            format,
            objects.iter().flatten().filter_map(shallow_change),
            output,
        )
    }

    #[cfg(feature = "read")]
    pub(crate) fn parse_selected<R: Repository + ?Sized>(
        repository: &git_zero_core::ValidatedRepository<'_, '_, R>,
        body: &'a [u8],
        version: crate::route::Version,
        wants: &mut [ObjectId],
        options: crate::WantOptions,
        revisions_enabled: bool,
    ) -> Result<(Self, usize)> {
        let mut count = 0;
        let mut remaining = options.max_want_lines;
        let upload = Self::parse_mode(
            body,
            repository.object_format(),
            version,
            revisions_enabled,
            |id| {
                remaining = remaining.checked_sub(1).ok_or(Error::LimitExceeded)?;
                let allowed = match options.policy {
                    crate::WantPolicy::AdvertisedOnly => super::advertised(repository, id),
                    crate::WantPolicy::AllVisibleReachable => repository.find_index(id).is_some(),
                };
                if !allowed {
                    return Err(Error::InvalidRequest);
                }
                if !wants[..count].contains(&id) {
                    *wants.get_mut(count).ok_or(Error::BufferTooSmall)? = id;
                    count += 1;
                }
                Ok(())
            },
        )?;
        Ok((upload, count))
    }

    /// Revisit validated packets without retaining an array of have IDs. Only
    /// present commits are acknowledged or used to exclude complete history.
    pub fn common<'r>(
        &'r self,
        repository: &'r impl Repository,
    ) -> impl Iterator<Item = Result<ObjectId>> + 'r {
        packet_values(self.haves, b"have ")
            .filter_map(|hex| ObjectId::from_hex(hex).ok())
            .filter_map(move |id| common_commit(repository, id).transpose())
    }

    #[cfg(feature = "read")]
    pub fn shallow(&self) -> impl Iterator<Item = Result<ObjectId>> + '_ {
        packet_values(self.shallow_body, b"shallow ").map(ObjectId::from_hex)
    }

    #[cfg(feature = "read")]
    pub fn validate_shallow(&self, repository: &impl Repository) -> Result<()> {
        for id in self.shallow() {
            if common_commit(repository, id?)?.is_none() {
                return Err(Error::InvalidRequest);
            }
        }
        Ok(())
    }

    #[cfg(feature = "read")]
    pub fn include_tags(&self) -> bool {
        self.include_tags
    }

    pub fn acknowledge(&self, repository: &impl Repository, output: &mut impl Write) -> Result<()> {
        let mut acknowledgments = Acknowledgments::new(self);
        acknowledgments.start(output)?;
        for id in self.common(repository) {
            acknowledgments.common(id?, output)?;
        }
        acknowledgments.finish(output)
    }

    pub fn output<'w, W: Write>(&self, output: &'w mut W) -> PackOutput<'w, W> {
        PackOutput {
            output,
            band: self.band(),
            buffer: [0; 1024],
            length: 0,
        }
    }

    pub(crate) fn band(&self) -> u8 {
        // V2 always frames its packfile section on sideband channel 1.
        if self.v2 { 2 } else { self.capabilities.band() }
    }
}

#[cfg(feature = "read")]
fn exclusion_matches(full: &[u8], name: &[u8]) -> bool {
    [
        (b"".as_slice(), b"".as_slice()),
        (b"refs/", b""),
        (b"refs/tags/", b""),
        (b"refs/heads/", b""),
        (b"refs/remotes/", b""),
        (b"refs/remotes/", b"/HEAD"),
    ]
    .iter()
    .any(|&(prefix, suffix)| {
        full.strip_prefix(prefix)
            .and_then(|rest| rest.strip_suffix(suffix))
            == Some(name)
    })
}

#[cfg(feature = "read")]
fn exclusion_ref(repository: &impl Repository, name: &[u8]) -> Result<ObjectId> {
    let mut found = if name == b"HEAD" {
        repository.head_state().id()
    } else {
        None
    };
    for index in 0..repository.reference_count() {
        let full = repository
            .reference_name(index)
            .ok_or(Error::InvalidReference)?
            .as_bytes();
        if !exclusion_matches(full, name) {
            continue;
        }
        if found.is_some() {
            return Err(Error::InvalidRequest);
        }
        // Validation already authenticated symbolic aliases and their final ID.
        found = Some(
            repository
                .reference_id(index)
                .ok_or(Error::InvalidReference)?,
        );
    }
    found.ok_or(Error::InvalidRequest)
}

#[cfg(feature = "read")]
fn shallow_change(
    object: &git_zero_core::ValidationMetadata,
) -> Option<git_zero_core::ShallowChange> {
    if object.is_shallow() {
        Some(git_zero_core::ShallowChange::Shallow(object.id))
    } else if object.is_unshallow() {
        Some(git_zero_core::ShallowChange::Unshallow(object.id))
    } else {
        None
    }
}

// The request parser already checked packet framing and ID syntax. These
// helpers keep packet filtering separate from fallible repository metadata.
fn packet_values<'a>(mut bytes: &'a [u8], prefix: &'static [u8]) -> impl Iterator<Item = &'a [u8]> {
    core::iter::from_fn(move || pktline::read(&mut bytes).ok().flatten()).filter_map(
        move |packet| {
            let Packet::Data(line) = packet else {
                return None;
            };
            request_line(line).strip_prefix(prefix)
        },
    )
}

pub(crate) fn common_commit(
    repository: &impl Repository,
    id: ObjectId,
) -> Result<Option<ObjectId>> {
    let Some(index) = repository.find_index(id) else {
        return Ok(None);
    };
    Ok((repository.object_info(index)?.kind == ObjectKind::Commit).then_some(id))
}

/// Complete-pack sink passed to host pack writers. It forwards raw pack bytes
/// or frames channel 1 according to the client's negotiated sideband mode.
/// Uses a fixed 1 KiB buffer; packet sizes stay within both protocol limits.
/// The server flushes the final packet and terminator after the writer succeeds.
pub struct PackOutput<'a, W: Write> {
    output: &'a mut W,
    band: u8,
    buffer: [u8; 1024],
    length: usize,
}

impl<W: Write> PackOutput<'_, W> {
    fn flush(&mut self) -> Result<()> {
        if self.length != 0 {
            pktline::write_parts(self.output, &[b"\x01", &self.buffer[..self.length]])?;
            self.length = 0;
        }
        Ok(())
    }

    pub(crate) fn finish(&mut self) -> Result<()> {
        if self.band != 0 {
            self.flush()?;
            self.output.write_all(b"0000")?;
        }
        Ok(())
    }
}

impl<W: Write> Write for PackOutput<'_, W> {
    fn write_all(&mut self, mut bytes: &[u8]) -> Result<()> {
        if self.band == 0 {
            return self.output.write_all(bytes);
        }
        let capacity = if self.band == 1 {
            995
        } else {
            self.buffer.len()
        };
        while !bytes.is_empty() {
            let count = bytes.len().min(capacity - self.length);
            self.buffer[self.length..self.length + count].copy_from_slice(&bytes[..count]);
            self.length += count;
            bytes = &bytes[count..];
            if self.length == capacity {
                self.flush()?;
            }
        }
        Ok(())
    }
}

/// Write a terminal v0 ERR packet as a replacement for a failed buffered upload
/// response. Discard the failed response first. No backend details are exposed.
/// Hosts own the HTTP status; never append this to a partial raw pack.
pub fn write_upload_error(error: Error, output: &mut impl Write) -> Result<()> {
    let message: &[u8] = match error {
        Error::BufferTooSmall | Error::LimitExceeded => b"request exceeds resource limits",
        Error::Backend => b"repository unavailable",
        Error::Unsupported => b"unsupported upload request",
        _ => b"invalid upload request",
    };
    pktline::write_parts(output, &[b"ERR ", message, b"\n"])
}

/// Stream host-computed shallow changes before acknowledgments/pack framing.
/// The host must validate commit IDs and may only unshallow a client boundary.
#[derive(Clone, Copy, Eq, PartialEq)]
enum Phase {
    Initial,
    Started,
    Finished,
}

/// A v0/v1 depth probe ends with a flush even without changes; v2 emits a
/// section only for changes in a final response. Errors are terminal.
#[cfg(feature = "read")]
pub struct ShallowUpdates {
    format: ObjectFormat,
    v2: bool,
    enabled: bool,
    phase: Phase,
    failure: Option<Error>,
}
#[cfg(feature = "read")]
impl ShallowUpdates {
    pub(crate) fn new(request: &UploadRequest<'_>, format: ObjectFormat) -> Self {
        Self {
            format,
            v2: request.v2,
            enabled: request.extensions.history() && (!request.v2 || request.done),
            phase: Phase::Initial,
            failure: None,
        }
    }
    /// Whether this protocol round carries depth-boundary announcements.
    pub const fn is_enabled(&self) -> bool {
        self.enabled
    }
    pub fn change(&mut self, id: ObjectId, unshallow: bool, output: &mut impl Write) -> Result<()> {
        let result = self.write_change(id, unshallow, output);
        self.record(result)
    }
    fn write_change(
        &mut self,
        id: ObjectId,
        unshallow: bool,
        output: &mut impl Write,
    ) -> Result<()> {
        self.ready()?;
        if self.phase == Phase::Finished || !self.enabled {
            return Err(Error::InvalidRequest);
        }
        if id.is_zero() || id.format() != self.format {
            return Err(Error::InvalidObjectId);
        }
        if self.phase == Phase::Initial && self.v2 {
            pktline::write(output, b"shallow-info\n")?;
        }
        self.phase = Phase::Started;
        let marker: &[u8] = if unshallow {
            b"unshallow "
        } else {
            b"shallow "
        };
        pktline::write_parts(output, &[marker, id.to_hex().as_bytes(), b"\n"])
    }
    pub fn finish(&mut self, output: &mut impl Write) -> Result<()> {
        let result = self.write_finish(output);
        self.record(result)
    }
    fn write_finish(&mut self, output: &mut impl Write) -> Result<()> {
        self.ready()?;
        if self.phase == Phase::Finished {
            return Err(Error::InvalidRequest);
        }
        let started = self.phase == Phase::Started;
        self.phase = Phase::Finished;
        if self.enabled && (!self.v2 || started) {
            output.write_all(if self.v2 { b"0001" } else { b"0000" })?;
        }
        Ok(())
    }
    fn ready(&self) -> Result<()> {
        self.failure.map_or(Ok(()), Err)
    }
    fn record(&mut self, result: Result<()>) -> Result<()> {
        if let Err(error) = result {
            self.failure.get_or_insert(error);
        }
        result
    }
}

/// Feed only host-verified common commit IDs. Sinks may change between calls.
/// The host must not acknowledge private/unreachable IDs or claim an early cut.
pub struct Acknowledgments {
    v2: bool,
    done: bool,
    mode: u8,
    suppress: bool,
    last: Option<ObjectId>,
    phase: Phase,
}
impl Acknowledgments {
    pub(crate) fn new(request: &UploadRequest<'_>) -> Self {
        Self {
            v2: request.v2,
            done: request.done,
            mode: request.capabilities.ack(),
            suppress: !request.v2 && request.extensions.history() && request.haves.is_empty(),
            last: None,
            phase: Phase::Initial,
        }
    }
    pub fn start(&mut self, output: &mut impl Write) -> Result<()> {
        if self.phase != Phase::Initial {
            return Err(Error::InvalidRequest);
        }
        self.phase = Phase::Started;
        if self.v2 {
            pktline::write(
                output,
                if self.done {
                    b"packfile\n"
                } else {
                    b"acknowledgments\n"
                },
            )?;
        }
        Ok(())
    }
    pub fn common(&mut self, id: ObjectId, output: &mut impl Write) -> Result<()> {
        if self.phase != Phase::Started || id.is_zero() {
            return Err(Error::InvalidRequest);
        }
        if self.suppress
            || (self.v2 && self.done)
            || (!self.v2 && self.mode == 0 && self.last.is_some())
        {
            return Ok(());
        }
        let suffix: &[u8] = if self.v2 || self.mode == 0 {
            b"\n"
        } else if self.mode == 1 {
            b" continue\n"
        } else {
            b" common\n"
        };
        pktline::write_parts(output, &[b"ACK ", id.to_hex().as_bytes(), suffix])?;
        self.last = Some(id);
        Ok(())
    }
    pub fn finish(&mut self, output: &mut impl Write) -> Result<()> {
        if self.phase != Phase::Started {
            return Err(Error::InvalidRequest);
        }
        self.phase = Phase::Finished;
        if self.v2 {
            if !self.done {
                if self.last.is_none() {
                    pktline::write(output, b"NAK\n")?;
                }
                output.write_all(b"0000")?;
            }
        } else if !self.suppress && !(self.mode == 0 && self.last.is_some()) {
            if let Some(id) = self.last.filter(|_| self.done) {
                pktline::write_parts(output, &[b"ACK ", id.to_hex().as_bytes(), b"\n"])?;
            } else {
                pktline::write(output, b"NAK\n")?;
            }
        }
        Ok(())
    }
}