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
//! Accepting pushes (`receive-pack`).

use std::collections::HashSet;
use std::io::{Read, Write};

use super::{
    advertised_refs, check_object_format, head_info, parse_oid, read_packet, write_flush,
    write_line, Packet, ProtocolVersion, Result, ServeError, ServeOptions,
};
use crate::config::ConfigSet;
use crate::hooks::{run_hook_in_git_dir, HookResult};
use crate::index_pack::{ingest_received_pack, IngestPackOptions};
use crate::objects::ObjectId;
use crate::receive_pack::{max_input_size_from_config, should_use_unpack_objects};
use crate::receive_quarantine::ReceiveQuarantine;
use crate::repo::Repository;
use crate::unpack_objects::{unpack_objects, UnpackOptions};

/// Capabilities offered in the receive-pack advertisement, before the
/// per-repository ones.
const CAPABILITIES: &[&str] = &[
    "report-status",
    "report-status-v2",
    "delete-refs",
    "side-band-64k",
    "quiet",
    "atomic",
    "ofs-delta",
];

/// Which ref updates the server refuses.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReceivePolicy {
    /// Refuse updates that would discard commits (`receive.denyNonFastForwards`).
    pub deny_non_fast_forwards: bool,
    /// Refuse ref deletions (`receive.denyDeletes`).
    pub deny_deletes: bool,
    /// In a repository with a working tree, refuse to update or delete the
    /// branch `HEAD` points at (`receive.denyCurrentBranch`).
    pub deny_current_branch: bool,
}

impl Default for ReceivePolicy {
    fn default() -> Self {
        Self {
            deny_non_fast_forwards: false,
            deny_deletes: false,
            deny_current_branch: true,
        }
    }
}

impl ReceivePolicy {
    /// Read the policy from the `receive.*` configuration keys.
    ///
    /// `receive.denyCurrentBranch` accepts Git's values: `refuse`, `true` or
    /// unset refuse the update; `ignore`, `warn`, `false` and `updateInstead`
    /// allow it (this server does not update the working tree).
    ///
    /// # Parameters
    ///
    /// - `config`: the repository's loaded configuration.
    ///
    /// # Returns
    ///
    /// The policy, with defaults for unset or unparseable keys.
    #[must_use]
    pub fn from_config(config: &ConfigSet) -> Self {
        let flag = |key: &str| config.get_bool(key).and_then(|r| r.ok()).unwrap_or(false);
        let deny_current_branch = match config.get("receive.denyCurrentBranch") {
            None => true,
            Some(v) => !matches!(
                v.to_ascii_lowercase().as_str(),
                "ignore" | "warn" | "false" | "no" | "off" | "0" | "updateinstead"
            ),
        };
        Self {
            deny_non_fast_forwards: flag("receive.denyNonFastForwards"),
            deny_deletes: flag("receive.denyDeletes"),
            deny_current_branch,
        }
    }
}

/// The result of one requested ref update.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RefUpdateResult {
    /// Full name of the ref the client asked to change.
    pub refname: String,
    /// Value the client expected the ref to have; `None` for a create.
    pub old: Option<ObjectId>,
    /// Value the client asked for; `None` for a delete.
    pub new: Option<ObjectId>,
    /// Why the update was refused, or `None` when it was applied.
    pub error: Option<String>,
}

/// One server-side hook invocation during receive-pack.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HookRunRecord {
    /// Hook name (`pre-receive`, `update`, `post-receive`, or `post-update`).
    pub name: String,
    /// Whether an executable hook ran (missing hooks are skipped).
    pub ran: bool,
    /// Exit code when [`Self::ran`] is true.
    pub exit_code: Option<i32>,
}

/// What a push session did.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ReceiveOutcome {
    /// Why the pushed pack could not be stored, when it could not.
    pub unpack_error: Option<String>,
    /// One entry per ref update the client requested, in request order.
    pub updates: Vec<RefUpdateResult>,
    /// Hook runs performed for this push, in execution order.
    pub hooks: Vec<HookRunRecord>,
    /// Push options delivered to hooks via `GIT_PUSH_OPTION_*`.
    pub push_options: Vec<String>,
}

/// Serve one push session: advertise refs, read the ref updates and pack, store
/// the objects, apply the updates, and report the result to the client.
///
/// Objects from the pushed pack are stored via [`unpack_objects`] for small
/// packs (below `receive.unpacklimit`) and [`crate::index_pack::ingest_received_pack`]
/// otherwise, matching Git receive-pack. Updates are checked against `policy` and
/// against the value the client expected each ref to have; with the `atomic`
/// capability either every update is applied or none is. Server-side hooks run
/// through the repository [`CommandRunner`](crate::command_runner::CommandRunner)
/// (`pre-receive`, `update`, `post-receive`, `post-update`) with push options and
/// quarantined object ingestion matching Git receive-pack.
///
/// # Parameters
///
/// - `repo`: the repository receiving the push.
/// - `input`: the client's command list and pack.
/// - `output`: where the advertisement and status report are written.
/// - `opts`: framing, protocol version, agent string, and hidden refs. Protocol
///   v2 is not defined for pushes, so it is served as v0.
/// - `policy`: which updates to refuse.
///
/// # Returns
///
/// What was stored and which updates were applied. Refused updates are reported
/// to the client and in the outcome; they are not errors.
///
/// # Errors
///
/// Returns [`ServeError::Protocol`] for a malformed command list and I/O errors
/// from the streams.
pub fn receive_pack(
    repo: &Repository,
    input: &mut dyn Read,
    output: &mut dyn Write,
    opts: &ServeOptions,
    policy: &ReceivePolicy,
) -> Result<ReceiveOutcome> {
    if opts.advertise_refs || !opts.stateless_rpc {
        if opts.protocol == ProtocolVersion::V1 {
            write_line(output, "version 1")?;
        }
        write_advertisement(repo, output, opts)?;
        output.flush()?;
        if opts.advertise_refs {
            return Ok(ReceiveOutcome::default());
        }
    }

    let Some(request) = read_commands(repo, input)? else {
        return Ok(ReceiveOutcome::default());
    };

    let cfg = repo
        .config()
        .unwrap_or_else(|_| std::sync::Arc::new(ConfigSet::new()));
    let max_input = max_input_size_from_config(cfg.as_ref());
    let sideband = request.caps.contains("side-band-64k");

    let needs_pack = request.commands.iter().any(|c| c.new.is_some());
    let mut quarantine = if needs_pack {
        Some(ReceiveQuarantine::create(repo.odb(), repo.git_dir())?)
    } else {
        None
    };

    let unpack_error = if needs_pack {
        let Some(q) = quarantine.as_ref() else {
            return Err(ServeError::Protocol("pack push requires quarantine".into()));
        };
        match read_push_pack(input, max_input) {
            Ok(pack) => ingest_pushed_pack(&q.odb(), &pack, cfg.as_ref(), max_input)
                .err()
                .map(|e| e.to_string()),
            Err(msg) => Some(msg),
        }
    } else {
        None
    };

    let mut hook_records = Vec::new();
    let updates = if unpack_error.is_some() {
        request
            .commands
            .iter()
            .map(|c| c.refused("unpacker error"))
            .collect()
    } else {
        apply_commands_with_hooks(
            repo,
            &request,
            opts,
            policy,
            quarantine.as_mut(),
            sideband.then_some(&mut *output),
            &mut hook_records,
        )?
    };

    if request.caps.contains("report-status") || request.caps.contains("report-status-v2") {
        let report = build_report(unpack_error.as_deref(), &updates)?;
        if sideband {
            crate::pkt_line::write_sideband_channel1_64k(&mut &mut *output, &report)?;
            write_flush(output)?;
        } else {
            output.write_all(&report)?;
        }
    }
    output.flush()?;

    Ok(ReceiveOutcome {
        unpack_error,
        updates,
        hooks: hook_records,
        push_options: request.push_options.clone(),
    })
}

/// Write the refs the client may update, with capabilities on the first line.
fn write_advertisement(
    repo: &Repository,
    output: &mut dyn Write,
    opts: &ServeOptions,
) -> Result<()> {
    let mut caps: Vec<String> = CAPABILITIES.iter().map(|c| (*c).to_owned()).collect();
    let config = repo
        .config()
        .unwrap_or_else(|_| std::sync::Arc::new(ConfigSet::new()));
    if config
        .get_bool("receive.advertisepushoptions")
        .and_then(|r| r.ok())
        .unwrap_or(false)
    {
        caps.push("push-options".to_owned());
    }
    caps.push(format!("object-format={}", repo.odb().hash_algo().name()));
    if !opts.agent.is_empty() {
        caps.push(format!("agent={}", opts.agent));
    }
    let caps = caps.join(" ");

    let refs = advertised_refs(repo, &opts.hidden_refs)?;
    if refs.is_empty() {
        let zero = ObjectId::null(repo.odb().hash_algo());
        write_line(
            output,
            &format!("{} capabilities^{{}}\0{caps}", zero.to_hex()),
        )?;
    }
    for (i, r) in refs.iter().enumerate() {
        if i == 0 {
            write_line(output, &format!("{} {}\0{caps}", r.oid.to_hex(), r.name))?;
        } else {
            write_line(output, &format!("{} {}", r.oid.to_hex(), r.name))?;
        }
    }
    write_flush(output)
}

/// One `<old> <new> <ref>` line from the client.
struct Command {
    old: Option<ObjectId>,
    new: Option<ObjectId>,
    refname: String,
}

impl Command {
    fn refused(&self, reason: &str) -> RefUpdateResult {
        RefUpdateResult {
            refname: self.refname.clone(),
            old: self.old,
            new: self.new,
            error: Some(reason.to_owned()),
        }
    }

    fn accepted(&self) -> RefUpdateResult {
        RefUpdateResult {
            refname: self.refname.clone(),
            old: self.old,
            new: self.new,
            error: None,
        }
    }
}

/// The command list of a push.
struct PushRequest {
    commands: Vec<Command>,
    caps: HashSet<String>,
    push_options: Vec<String>,
}

/// Read the command list up to its flush. Returns `None` when the client sent
/// no commands (nothing to push).
fn read_commands(repo: &Repository, input: &mut dyn Read) -> Result<Option<PushRequest>> {
    let mut commands = Vec::new();
    let mut caps = HashSet::new();
    loop {
        match read_packet(input)? {
            None | Some(Packet::Flush) => break,
            Some(Packet::Data(line)) => {
                let (line, line_caps) = line.split_once('\0').unwrap_or((line.as_str(), ""));
                if commands.is_empty() {
                    caps.extend(
                        line_caps
                            .split(' ')
                            .filter(|c| !c.is_empty())
                            .map(str::to_owned),
                    );
                }
                let mut parts = line.splitn(3, ' ');
                let (Some(old), Some(new), Some(refname)) =
                    (parts.next(), parts.next(), parts.next())
                else {
                    return Err(ServeError::Protocol(format!("malformed command '{line}'")));
                };
                let old = parse_oid(repo, old)?;
                let new = parse_oid(repo, new)?;
                commands.push(Command {
                    old: (!old.is_zero()).then_some(old),
                    new: (!new.is_zero()).then_some(new),
                    refname: refname.to_owned(),
                });
            }
            Some(other) => {
                return Err(ServeError::Protocol(format!(
                    "unexpected packet in command list: {other:?}"
                )))
            }
        }
    }
    if commands.is_empty() {
        return Ok(None);
    }
    let mut push_options = Vec::new();
    if caps.contains("push-options") {
        loop {
            match read_packet(input)? {
                None | Some(Packet::Flush) => break,
                Some(Packet::Data(line)) => {
                    push_options.push(line);
                }
                Some(other) => {
                    return Err(ServeError::Protocol(format!(
                        "unexpected packet in push options: {other:?}"
                    )))
                }
            }
        }
    }
    let format = caps.iter().find_map(|c| c.strip_prefix("object-format="));
    check_object_format(repo, format)?;
    Ok(Some(PushRequest {
        commands,
        caps,
        push_options,
    }))
}

/// Validate, run hooks, migrate quarantine objects, and apply ref updates.
fn apply_commands_with_hooks(
    repo: &Repository,
    request: &PushRequest,
    opts: &ServeOptions,
    policy: &ReceivePolicy,
    quarantine: Option<&mut ReceiveQuarantine>,
    mut sideband_out: Option<&mut dyn Write>,
    hook_records: &mut Vec<HookRunRecord>,
) -> Result<Vec<RefUpdateResult>> {
    let lookup_odb = quarantine.as_ref().map(|q| q.odb());
    let current_branch = if repo.is_bare() {
        None
    } else {
        head_info(repo).target
    };
    let mut results: Vec<RefUpdateResult> = request
        .commands
        .iter()
        .map(|c| {
            match check_command(
                repo,
                lookup_odb.as_ref(),
                c,
                opts,
                policy,
                current_branch.as_deref(),
            ) {
                Some(reason) => c.refused(reason),
                None => c.accepted(),
            }
        })
        .collect();

    if request.caps.contains("atomic") && results.iter().any(|r| r.error.is_some()) {
        for r in results.iter_mut().filter(|r| r.error.is_none()) {
            r.error = Some("atomic transaction failed".to_owned());
        }
        return Ok(results);
    }

    let algo = repo.odb().hash_algo();
    let push_option_env = push_option_env_owned(&request.push_options);
    let mut pre_hook_env_owned = push_option_env.clone();
    if let Some(q) = quarantine.as_ref() {
        pre_hook_env_owned.extend(q.hook_env());
    }
    let pre_hook_env: Vec<(&str, &str)> = pre_hook_env_owned
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect();
    let update_hook_env: &[(&str, &str)] = &[];
    let post_receive_env_owned = push_option_env;
    let post_receive_env: Vec<(&str, &str)> = post_receive_env_owned
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect();

    let all_cmds: Vec<&Command> = request.commands.iter().collect();
    let pre_stdin = commands_stdin(&all_cmds, algo);
    let (pre_result, pre_out) = run_hook_in_git_dir(
        repo,
        "pre-receive",
        &[],
        Some(pre_stdin.as_bytes()),
        &pre_hook_env,
    );
    relay_hook_sideband(&mut sideband_out, &pre_out)?;
    hook_records.push(hook_record("pre-receive", &pre_result));
    if matches!(pre_result, HookResult::Failed(_)) {
        for r in results.iter_mut().filter(|r| r.error.is_none()) {
            r.error = Some("pre-receive hook declined".to_owned());
        }
        return Ok(results);
    }

    if results.iter().all(|r| r.error.is_some()) {
        return Ok(results);
    }

    if let Some(q) = quarantine {
        q.migrate().map_err(ServeError::Repository)?;
        repo.odb().invalidate_packs();
    }

    for (cmd, result) in request.commands.iter().zip(results.iter_mut()) {
        if result.error.is_some() {
            continue;
        }
        let zero = null_oid_hex(algo);
        let old_hex = cmd.old.map(|o| o.to_hex()).unwrap_or_else(|| zero.clone());
        let new_hex = cmd.new.map(|o| o.to_hex()).unwrap_or(zero.clone());
        let args = [cmd.refname.as_str(), old_hex.as_str(), new_hex.as_str()];
        let (update_result, update_out) =
            run_hook_in_git_dir(repo, "update", &args, None, update_hook_env);
        relay_hook_sideband(&mut sideband_out, &update_out)?;
        hook_records.push(hook_record("update", &update_result));
        if matches!(update_result, HookResult::Failed(_)) {
            result.error = Some("hook declined".to_owned());
        }
    }

    if request.caps.contains("atomic") && results.iter().any(|r| r.error.is_some()) {
        for r in results.iter_mut().filter(|r| r.error.is_none()) {
            r.error = Some("atomic transaction failed".to_owned());
        }
        return Ok(results);
    }

    let pending: Vec<(usize, crate::refs::store::RefUpdate)> = request
        .commands
        .iter()
        .zip(results.iter())
        .enumerate()
        .filter(|(_, (_, r))| r.error.is_none())
        .map(|(idx, (cmd, _))| (idx, receive_command_store_update(cmd)))
        .collect();

    if request.caps.contains("atomic") {
        let batch: Vec<_> = pending.iter().map(|(_, u)| u.clone()).collect();
        if let Err(e) = crate::refs::commit_ref_store_batch(repo.git_dir(), &batch) {
            let msg = format!("failed to update ref: {e}");
            for (idx, _) in pending {
                if results[idx].error.is_none() {
                    results[idx].error = Some(msg.clone());
                }
            }
        }
    } else {
        for (idx, update) in pending {
            if let Err(e) = crate::refs::commit_store_update(repo.git_dir(), update) {
                results[idx].error = Some(format!("failed to update ref: {e}"));
            }
        }
    }

    let successful: Vec<&Command> = request
        .commands
        .iter()
        .zip(results.iter())
        .filter(|(_, r)| r.error.is_none())
        .map(|(c, _)| c)
        .collect();
    if !successful.is_empty() {
        let post_stdin = commands_stdin(&successful, algo);
        let (post_result, post_out) = run_hook_in_git_dir(
            repo,
            "post-receive",
            &[],
            Some(post_stdin.as_bytes()),
            &post_receive_env,
        );
        relay_hook_sideband(&mut sideband_out, &post_out)?;
        hook_records.push(hook_record("post-receive", &post_result));

        let mut post_update_args: Vec<&str> = Vec::new();
        for result in results.iter().filter(|r| r.error.is_none()) {
            post_update_args.push(result.refname.as_str());
        }
        let (pu_result, pu_out) = run_hook_in_git_dir(
            repo,
            "post-update",
            &post_update_args,
            None,
            update_hook_env,
        );
        relay_hook_sideband(&mut sideband_out, &pu_out)?;
        hook_records.push(hook_record("post-update", &pu_result));
    }

    Ok(results)
}

fn hook_record(name: &str, result: &HookResult) -> HookRunRecord {
    HookRunRecord {
        name: name.to_owned(),
        ran: result.was_executed(),
        exit_code: match result {
            HookResult::Failed(code) => Some(*code),
            HookResult::Success => Some(0),
            HookResult::NotFound => None,
        },
    }
}

fn push_option_env_owned(options: &[String]) -> Vec<(String, String)> {
    let mut pairs = vec![(
        "GIT_PUSH_OPTION_COUNT".to_owned(),
        options.len().to_string(),
    )];
    for (i, opt) in options.iter().enumerate() {
        pairs.push((format!("GIT_PUSH_OPTION_{i}"), opt.clone()));
    }
    pairs
}

fn commands_stdin(commands: &[&Command], algo: crate::objects::HashAlgo) -> String {
    let zero = null_oid_hex(algo);
    commands
        .iter()
        .map(|c| {
            format!(
                "{} {} {}\n",
                c.old.map(|o| o.to_hex()).unwrap_or_else(|| zero.clone()),
                c.new.map(|o| o.to_hex()).unwrap_or_else(|| zero.clone()),
                c.refname
            )
        })
        .collect()
}

fn null_oid_hex(algo: crate::objects::HashAlgo) -> String {
    ObjectId::null(algo).to_hex()
}

fn receive_command_store_update(cmd: &Command) -> crate::refs::store::RefUpdate {
    let mut flags = crate::refs::store::RefUpdateFlags::default();
    if cmd.refname == "HEAD" {
        flags.no_deref = true;
    }
    crate::refs::store::RefUpdate {
        name: cmd.refname.clone(),
        new_value: cmd.new.map(crate::refs::store::RawRef::Direct),
        expected: match cmd.old {
            Some(oid) => crate::refs::store::Expected::Oid(oid),
            None => crate::refs::store::Expected::Missing,
        },
        reflog: None,
        flags,
    }
}

fn relay_hook_sideband(out: &mut Option<&mut dyn Write>, captured: &[u8]) -> Result<()> {
    let Some(w) = out.as_mut() else {
        return Ok(());
    };
    if captured.is_empty() {
        return Ok(());
    }
    for chunk in captured.chunks(65515) {
        crate::pkt_line::write_sideband_packet(*w, 2, chunk).map_err(ServeError::Io)?;
    }
    Ok(())
}

/// Decide whether one command may be applied. Returns the refusal reason.
fn check_command(
    repo: &Repository,
    lookup: Option<&crate::odb::Odb>,
    cmd: &Command,
    opts: &ServeOptions,
    policy: &ReceivePolicy,
    current_branch: Option<&str>,
) -> Option<&'static str> {
    if !cmd.refname.starts_with("refs/") || !crate::refs::is_valid_storable_ref_name(&cmd.refname) {
        return Some("funny refname");
    }
    if crate::hide_refs::ref_is_hidden(&cmd.refname, &cmd.refname, &opts.hidden_refs) {
        return Some("deny updating a hidden ref");
    }
    let current = crate::refs::resolve_ref(repo.git_dir(), &cmd.refname).ok();
    if current != cmd.old {
        return Some("stale info");
    }
    let is_current_branch = current_branch == Some(cmd.refname.as_str());

    let Some(new) = cmd.new else {
        if policy.deny_deletes {
            return Some("deletion prohibited");
        }
        if is_current_branch && policy.deny_current_branch {
            return Some("deletion of the current branch prohibited");
        }
        return None;
    };
    let odb = lookup.unwrap_or(repo.odb());
    if !odb.exists(&new) {
        return Some("missing necessary objects");
    }
    if is_current_branch && policy.deny_current_branch {
        return Some("branch is currently checked out");
    }
    if let (Some(old), true) = (current, policy.deny_non_fast_forwards) {
        let fast_forward = crate::merge_base::is_ancestor(repo, old, new).unwrap_or(false);
        if !fast_forward {
            return Some("non-fast-forward");
        }
    }
    None
}

/// Encode the status report as pkt-lines: the unpack result, one line per ref,
/// and a closing flush.
fn build_report(unpack_error: Option<&str>, updates: &[RefUpdateResult]) -> Result<Vec<u8>> {
    let mut report = Vec::new();
    match unpack_error {
        None => write_line(&mut report, "unpack ok")?,
        Some(e) => write_line(&mut report, &format!("unpack {}", one_line(e)))?,
    }
    for u in updates {
        match &u.error {
            None => write_line(&mut report, &format!("ok {}", u.refname))?,
            Some(reason) => write_line(
                &mut report,
                &format!("ng {} {}", u.refname, one_line(reason)),
            )?,
        }
    }
    write_flush(&mut report)?;
    Ok(report)
}

/// Collapse a message to a single line so it fits in one status pkt-line.
fn one_line(msg: &str) -> String {
    msg.lines().next().unwrap_or_default().trim().to_owned()
}

/// Read the pack bytes that follow the ref-update command list on the wire.
fn read_push_pack(
    input: &mut dyn Read,
    max_bytes: Option<u64>,
) -> std::result::Result<Vec<u8>, String> {
    let mut buf = Vec::new();
    let mut chunk = [0u8; 64 * 1024];
    loop {
        let n = input.read(&mut chunk).map_err(|e| e.to_string())?;
        if n == 0 {
            break;
        }
        if let Some(limit) = max_bytes {
            let next = buf.len().saturating_add(n);
            if next as u64 > limit {
                return Err("pack exceeds maximum input size".to_owned());
            }
        }
        buf.extend_from_slice(&chunk[..n]);
    }
    Ok(buf)
}

/// Store a pushed pack using unpack-objects or index-pack, per Git limits.
fn ingest_pushed_pack(
    odb: &crate::odb::Odb,
    pack: &[u8],
    cfg: &ConfigSet,
    max_input: Option<u64>,
) -> Result<()> {
    if should_use_unpack_objects(pack, cfg) {
        let mut cursor = std::io::Cursor::new(pack);
        unpack_objects(
            &mut cursor,
            odb,
            &UnpackOptions {
                quiet: true,
                strict: true,
                max_input_bytes: max_input,
                ..UnpackOptions::default()
            },
        )?;
    } else {
        ingest_received_pack(
            pack.to_vec(),
            odb,
            &IngestPackOptions {
                fix_thin: true,
                ..Default::default()
            },
        )?;
    }
    Ok(())
}

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

    #[test]
    fn report_lists_unpack_status_then_each_ref() {
        let oid = ObjectId::from_hex("1111111111111111111111111111111111111111")
            .unwrap_or_else(|_| ObjectId::zero());
        let updates = vec![
            RefUpdateResult {
                refname: "refs/heads/main".into(),
                old: None,
                new: Some(oid),
                error: None,
            },
            RefUpdateResult {
                refname: "refs/heads/topic".into(),
                old: Some(oid),
                new: None,
                error: Some("deletion prohibited".into()),
            },
        ];
        let report = build_report(None, &updates).unwrap_or_default();
        assert_eq!(
            String::from_utf8_lossy(&report),
            "000eunpack ok\n0017ok refs/heads/main\n002cng refs/heads/topic deletion prohibited\n0000"
        );
    }

    #[test]
    fn deny_current_branch_values() {
        let policy_for = |value: Option<&str>| {
            let mut config = ConfigSet::new();
            if let Some(v) = value {
                let _ = config.add_command_override("receive.denyCurrentBranch", v);
            }
            ReceivePolicy::from_config(&config).deny_current_branch
        };
        assert!(policy_for(None));
        assert!(!policy_for(Some("ignore")));
        assert!(!policy_for(Some("updateInstead")));
        assert!(policy_for(Some("refuse")));
    }
}