Skip to main content

grit_lib/serve/
receive_pack.rs

1//! Accepting pushes (`receive-pack`).
2
3use crate::error::ObjectError;
4use crate::error::RefError;
5use std::collections::{HashMap, HashSet};
6use std::io::{BufReader, BufWriter, Read, Write};
7use std::path::{Path, PathBuf};
8use std::sync::Arc;
9
10use super::connectivity::{PushedObjects, Unconnected};
11
12use super::{
13    check_object_format, for_each_advertised_ref, parse_oid, read_packet, ref_update_policy,
14    write_flush, write_line, Packet, Peel, ProtocolVersion, Result, ServeError, ServeOptions,
15};
16use crate::config::ConfigSet;
17use crate::fsck_msg_policy::{ObjectFsckPolicy, SkipListError};
18use crate::fsck_standalone::FsckError;
19use crate::hooks::{run_hook_in_git_dir_with_cancel, HookResult};
20use crate::index_pack::{install_pack_path_with_progress, IngestPackOptions};
21use crate::objects::odb::store::{MemoryStore, ObjectStore, WritableObjectStore};
22use crate::objects::odb::OdbBuilder;
23use crate::objects::pack_stream_scan::{PackStreamScanError, PackStreamScanner};
24use crate::objects::{ObjectId, ObjectKind};
25use crate::odb::Odb;
26use crate::odb::WriteOptions;
27use crate::parallel::{try_par_map, ParallelHashError, ParallelOp};
28use crate::progress::{CancelSignal, CancelToken};
29use crate::receive_pack::{max_input_size_from_config, should_use_unpack_objects};
30use crate::receive_quarantine::{MigratedPackKeepGuard, ReceiveQuarantine};
31use crate::repo::Repository;
32use crate::shallow::{add_shallow_boundaries, load_shallow_boundaries, PushShallowInfo};
33use crate::unpack_objects::{unpack_objects, UnpackOptions};
34
35/// Bytes read from the client per call while spooling a pushed pack.
36const SPOOL_CHUNK: usize = 256 * 1024;
37
38/// Where an incoming push pack is held until pre-receive succeeds.
39enum ReceivePackIngest {
40    Quarantine(ReceiveQuarantine),
41    MemoryOverlay {
42        overlay: Arc<MemoryStore>,
43        objects_dir: PathBuf,
44        read: Arc<dyn ObjectStore>,
45        git_dir: PathBuf,
46        received: HashSet<ObjectId>,
47    },
48}
49
50impl ReceivePackIngest {
51    fn create(repo: &Repository) -> crate::error::Result<Self> {
52        let local = repo.odb();
53        if local.uses_files_primary() {
54            return Ok(Self::Quarantine(ReceiveQuarantine::create(
55                local,
56                repo.git_dir(),
57            )?));
58        }
59        let overlay = Arc::new(MemoryStore::new(local.hash_algo()));
60        Ok(Self::MemoryOverlay {
61            overlay,
62            objects_dir: local.objects_dir().to_path_buf(),
63            read: local.read_store(),
64            git_dir: repo.git_dir().to_path_buf(),
65            received: HashSet::new(),
66        })
67    }
68
69    fn odb(&self) -> Odb {
70        match self {
71            Self::Quarantine(q) => q.odb(),
72            Self::MemoryOverlay {
73                overlay,
74                objects_dir,
75                read,
76                git_dir,
77                ..
78            } => {
79                let mut odb = OdbBuilder::new()
80                    .push_read_source(read.clone())
81                    .build(objects_dir, Some(overlay.clone()));
82                odb = odb.with_config_git_dir(git_dir.clone());
83                odb
84            }
85        }
86    }
87
88    fn spool_dir(&self) -> PathBuf {
89        match self {
90            Self::Quarantine(q) => q.odb().objects_dir().join("pack"),
91            Self::MemoryOverlay { .. } => std::env::temp_dir(),
92        }
93    }
94
95    fn files_quarantine(&self) -> Option<&ReceiveQuarantine> {
96        match self {
97            Self::Quarantine(q) => Some(q),
98            Self::MemoryOverlay { .. } => None,
99        }
100    }
101
102    fn set_received(&mut self, ids: HashSet<ObjectId>) {
103        if let Self::MemoryOverlay { received, .. } = self {
104            *received = ids;
105        }
106    }
107
108    fn migrate(&mut self, repo: &Repository) -> crate::error::Result<Vec<MigratedPackKeepGuard>> {
109        match self {
110            Self::Quarantine(q) => q.migrate(),
111            Self::MemoryOverlay {
112                overlay, received, ..
113            } => {
114                let primary = repo.odb().primary_writable()?;
115                for oid in received.iter() {
116                    if primary.contains(oid)? {
117                        continue;
118                    }
119                    let Some(obj) = overlay.read(oid)? else {
120                        return Err(crate::error::Error::Object(ObjectError::NotFound {
121                            name: oid.to_hex(),
122                        }));
123                    };
124                    primary.write(obj.kind, &obj.data, WriteOptions::default())?;
125                }
126                repo.odb().flush_for_publish()?;
127                Ok(Vec::new())
128            }
129        }
130    }
131}
132
133/// Capabilities offered in the receive-pack advertisement, before the
134/// per-repository ones.
135const CAPABILITIES: &[&str] = &[
136    "report-status",
137    "report-status-v2",
138    "delete-refs",
139    "side-band-64k",
140    "quiet",
141    "atomic",
142    "ofs-delta",
143];
144
145/// How [`ReceivePolicy`] treats updates to the branch checked out in a non-bare repository
146/// (`receive.denyCurrentBranch`).
147#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
148pub enum DenyCurrentBranch {
149    /// Refuse the update (`refuse`, `true`, or unset).
150    #[default]
151    Refuse,
152    /// Allow the ref move without updating the worktree (`warn`).
153    Warn,
154    /// Allow the ref move without updating the worktree (`ignore`, `false`, …).
155    Ignore,
156    /// Move the ref and check out the new commit in every worktree that has the branch checked
157    /// out (`updateInstead`).
158    UpdateInstead,
159}
160
161impl DenyCurrentBranch {
162    /// Whether [`super::ref_update_policy::validate_ref_update`] should refuse the update.
163    #[must_use]
164    pub fn refuses_checked_out_update(self) -> bool {
165        matches!(self, Self::Refuse)
166    }
167}
168
169/// Which pushed objects and ref updates the server refuses.
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub struct ReceivePolicy {
172    /// Refuse branch (`refs/heads/*`) updates that would discard commits
173    /// (`receive.denyNonFastForwards`). Other refs are not checked.
174    pub deny_non_fast_forwards: bool,
175    /// Refuse ref deletions (`receive.denyDeletes`).
176    pub deny_deletes: bool,
177    /// How to treat updates to the branch checked out in a non-bare repository
178    /// (`receive.denyCurrentBranch`).
179    pub deny_current_branch: DenyCurrentBranch,
180    /// Refuse to delete the branch `HEAD` points at, in a bare repository as
181    /// well as one with a working tree (`receive.denyDeleteCurrent`).
182    pub deny_delete_current: bool,
183    /// Check every pushed commit, tree and tag with this policy before any of
184    /// them enters the repository (`receive.fsckObjects`). One refused object
185    /// refuses the whole push with [`UnpackError::Rejected`]. `None` skips the
186    /// check.
187    pub fsck_objects: Option<ObjectFsckPolicy>,
188    /// Accept pushes from a shallow client that would make this repository
189    /// shallow at one of the client's boundaries (`receive.shallowUpdate`).
190    /// The new boundaries are added to the repository's `shallow` file.
191    /// When false, such updates are refused with
192    /// [`RefUpdateRefusal::ShallowUpdateNotAllowed`]; pushes whose history the
193    /// repository already holds below the client's boundaries are accepted
194    /// either way.
195    pub shallow_update: bool,
196}
197
198impl Default for ReceivePolicy {
199    fn default() -> Self {
200        Self {
201            deny_non_fast_forwards: false,
202            deny_deletes: false,
203            deny_current_branch: DenyCurrentBranch::Refuse,
204            deny_delete_current: true,
205            fsck_objects: None,
206            shallow_update: false,
207        }
208    }
209}
210
211impl ReceivePolicy {
212    /// Read the policy from the `receive.*` configuration keys.
213    ///
214    /// `receive.denyCurrentBranch` accepts Git's values: `refuse`, `true` or unset
215    /// refuse the update; `warn` and `ignore` / `false` allow the ref move without
216    /// touching the worktree; `updateInstead` moves the ref and runs push-to-checkout
217    /// (hook or built-in deploy) on worktrees that have the branch checked out.
218    /// `receive.denyDeleteCurrent` is the same, except that `updateInstead` refuses
219    /// deletion: there is no tree to deploy for a deleted branch.
220    ///
221    /// Pushed objects are checked when `receive.fsckObjects` is true, or when it
222    /// is unset and `transfer.fsckObjects` is true. A value that is not a
223    /// boolean turns the check on rather than silently leaving it off. The
224    /// check uses `receive.fsck.<msg-id>` overrides and `receive.fsck.skipList`
225    /// (see [`ObjectFsckPolicy::from_config`]).
226    ///
227    /// # Parameters
228    ///
229    /// - `config`: the repository's loaded configuration.
230    ///
231    /// # Returns
232    ///
233    /// The policy, with defaults for unset or unparseable keys.
234    #[must_use]
235    pub fn from_config(config: &ConfigSet) -> Self {
236        let flag = |key: &str| config.get_bool(key).ok().flatten().unwrap_or(false);
237        // Whether a `receive.deny*` key is set to a value that lets the update through.
238        let allows_delete = |key: &str| {
239            config.value(key).is_some_and(|v| {
240                let v = v.to_ascii_lowercase();
241                matches!(v.as_str(), "ignore" | "warn" | "false" | "no" | "off" | "0")
242            })
243        };
244        let deny_current_branch = match config.value("receive.denyCurrentBranch") {
245            None => DenyCurrentBranch::Refuse,
246            Some(v) => {
247                let v = v.to_ascii_lowercase();
248                if matches!(v.as_str(), "refuse" | "true" | "1" | "yes" | "on") {
249                    DenyCurrentBranch::Refuse
250                } else if v == "warn" {
251                    DenyCurrentBranch::Warn
252                } else if v == "updateinstead" {
253                    DenyCurrentBranch::UpdateInstead
254                } else if matches!(v.as_str(), "ignore" | "false" | "no" | "off" | "0") {
255                    DenyCurrentBranch::Ignore
256                } else {
257                    DenyCurrentBranch::Refuse
258                }
259            }
260        };
261        let explicit = |key: &str| config.get_bool(key).ok().flatten();
262        let fsck = explicit("receive.fsckObjects")
263            .or(explicit("transfer.fsckObjects"))
264            .unwrap_or(false);
265        Self {
266            deny_non_fast_forwards: flag("receive.denyNonFastForwards"),
267            deny_deletes: flag("receive.denyDeletes"),
268            deny_current_branch,
269            deny_delete_current: !allows_delete("receive.denyDeleteCurrent"),
270            fsck_objects: fsck.then(|| ObjectFsckPolicy::from_config(config, "receive.fsck")),
271            shallow_update: flag("receive.shallowUpdate"),
272        }
273    }
274}
275
276/// A pushed object refused by [`ReceivePolicy::fsck_objects`].
277#[derive(Debug, Clone, PartialEq, Eq)]
278pub struct RejectedObject {
279    /// The refused object.
280    pub oid: ObjectId,
281    /// Its type.
282    pub kind: ObjectKind,
283    /// The error-level problem found in it.
284    pub error: FsckError,
285}
286
287impl std::fmt::Display for RejectedObject {
288    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
289        write!(
290            f,
291            "{} {} refused: {}",
292            self.kind,
293            self.oid.to_hex(),
294            self.error.report_line()
295        )
296    }
297}
298
299/// One `<old> <new> <ref>` update requested by a pushing client.
300#[derive(Debug, Clone, PartialEq, Eq)]
301pub struct RefCommand {
302    /// Full name of the ref to change, such as `refs/heads/main`.
303    pub refname: String,
304    /// Value the client expects the ref to have now; `None` to create it.
305    pub old: Option<ObjectId>,
306    /// Value the client asks for; `None` to delete the ref.
307    pub new: Option<ObjectId>,
308}
309
310impl RefCommand {
311    fn refused(&self, reason: RefUpdateRefusal) -> RefUpdateResult {
312        RefUpdateResult {
313            refname: self.refname.clone(),
314            old: self.old,
315            new: self.new,
316            error: Some(reason),
317        }
318    }
319
320    fn accepted(&self) -> RefUpdateResult {
321        RefUpdateResult {
322            refname: self.refname.clone(),
323            old: self.old,
324            new: self.new,
325            error: None,
326        }
327    }
328}
329
330/// Why the server refused one ref update.
331///
332/// The [`Display`](std::fmt::Display) text is the reason sent to the client on
333/// the update's `ng <ref> <reason>` status line, which Git clients show next to
334/// the rejected ref.
335#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
336#[non_exhaustive]
337pub enum RefUpdateRefusal {
338    /// The ref name is outside `refs/` or not a valid ref name.
339    #[error("funny refname")]
340    InvalidRefName,
341    /// The ref is hidden by `receive.hideRefs` / `transfer.hideRefs`.
342    #[error("deny updating a hidden ref")]
343    HiddenRef,
344    /// The ref no longer has the value the client expected.
345    #[error("stale info")]
346    StaleInfo,
347    /// Deletions are refused (`receive.denyDeletes`).
348    #[error("deletion prohibited")]
349    DeletionProhibited,
350    /// The update would delete the branch `HEAD` points at
351    /// (`receive.denyDeleteCurrent`).
352    #[error("deletion of the current branch prohibited")]
353    CurrentBranchDeletion,
354    /// The update would move the checked-out branch
355    /// (`receive.denyCurrentBranch`).
356    #[error("branch is currently checked out")]
357    CurrentBranch,
358    /// Push-to-checkout could not refresh the index (`updateInstead` deploy).
359    #[error("Up-to-date check failed")]
360    PushCheckoutUpToDateFailed,
361    /// The worktree differs from the index (`updateInstead` deploy).
362    #[error("Working directory has unstaged changes")]
363    PushCheckoutUnstagedChanges,
364    /// The index differs from `HEAD` (`updateInstead` deploy).
365    #[error("Working directory has staged changes")]
366    PushCheckoutStagedChanges,
367    /// Push-to-checkout could not apply the new tree (`updateInstead` deploy).
368    #[error("Could not update working tree to new HEAD")]
369    PushCheckoutFailed,
370    /// The `push-to-checkout` hook exited non-zero.
371    #[error("push-to-checkout hook declined")]
372    PushCheckoutHookDeclined,
373    /// The new value is not in the repository or the pushed pack.
374    #[error("missing necessary objects")]
375    MissingObjects,
376    /// The new value is there, but an object its history reaches (below what the
377    /// repository's refs already reached) is in neither the push nor the
378    /// repository, and no promisor remote of the repository promises it. The new
379    /// value may have been in the repository before the push: objects a refused
380    /// ref leaves behind do not make its history complete. Git clients see the
381    /// same reason as for [`Self::MissingObjects`].
382    #[error("missing necessary objects")]
383    Unconnected {
384        /// The first missing object found.
385        missing: ObjectId,
386        /// The object that names it.
387        referrer: ObjectId,
388    },
389    /// The branch update would discard commits (`receive.denyNonFastForwards`).
390    #[error("non-fast-forward")]
391    NonFastForward,
392    /// A branch update under `receive.denyNonFastForwards` names a non-commit
393    /// as its old or new value.
394    #[error("bad ref")]
395    BadRef,
396    /// Under `receive.denyNonFastForwards`, the ancestry walk between the old
397    /// and new branch tips failed, so the update could not be shown to be a
398    /// fast-forward.
399    #[error("unable to verify fast-forward")]
400    FastForwardCheckFailed,
401    /// The pushed history ends at a boundary of the shallow client that this
402    /// repository does not hold the ancestry of, and
403    /// [`ReceivePolicy::shallow_update`] is off.
404    #[error("shallow update not allowed")]
405    ShallowUpdateNotAllowed,
406    /// The pushed pack could not be stored; see
407    /// [`ReceiveOutcome::unpack_error`].
408    #[error("unpacker error")]
409    UnpackerError,
410    /// Another update in an `atomic` push was refused, so this one was not
411    /// applied either.
412    #[error("atomic transaction failed")]
413    AtomicTransactionFailed,
414    /// The `pre-receive` hook script exited non-zero.
415    #[error("pre-receive hook declined")]
416    PreReceiveHookDeclined,
417    /// The `update` hook script exited non-zero for this ref.
418    #[error("hook declined")]
419    UpdateHookDeclined,
420    /// A [`ReceiveHook`] refused the update with this reason.
421    #[error("{0}")]
422    Policy(String),
423    /// The ref is a served repository's target branch, which changes only
424    /// through the server's own operations (snapshot checkouts' integrate and
425    /// slices), never by a push.
426    #[error("{target} is the target branch; it changes only through integrate")]
427    TargetBranch {
428        /// The target branch's name, without `refs/heads/`.
429        target: String,
430    },
431    /// A create collided with an existing ref under ASCII case folding.
432    #[error("reference already exists")]
433    ReferenceAlreadyExists,
434    /// Writing the ref failed after every check passed.
435    #[error("failed to update ref: {0}")]
436    RefStore(String),
437}
438
439/// The result of one requested ref update.
440#[derive(Debug, Clone, PartialEq, Eq)]
441pub struct RefUpdateResult {
442    /// Full name of the ref the client asked to change.
443    pub refname: String,
444    /// Value the client expected the ref to have; `None` for a create.
445    pub old: Option<ObjectId>,
446    /// Value the client asked for; `None` for a delete.
447    pub new: Option<ObjectId>,
448    /// Why the update was refused, or `None` when it was applied.
449    pub error: Option<RefUpdateRefusal>,
450}
451
452/// Why a pushed pack could not be stored.
453///
454/// The [`Display`](std::fmt::Display) text is sent to the client on the
455/// `unpack <reason>` status line. Every requested update is then refused with
456/// [`RefUpdateRefusal::UnpackerError`].
457#[derive(Debug, thiserror::Error)]
458#[non_exhaustive]
459pub enum UnpackError {
460    /// The client stopped sending before the pack was complete.
461    #[error("unexpected end of pack stream")]
462    Truncated,
463    /// The pack is larger than `receive.maxInputSize`.
464    #[error("pack exceeds maximum input size of {limit} bytes")]
465    TooLarge {
466        /// The configured limit in bytes.
467        limit: u64,
468    },
469    /// The stream is not a well-formed pack.
470    #[error(transparent)]
471    Malformed(#[from] PackStreamScanError),
472    /// The pack arrived but its objects could not be stored (bad checksum,
473    /// corrupt object, failed connectivity check, ...).
474    #[error("{0}")]
475    Ingest(crate::error::ErrorSummary),
476    /// A pushed object failed [`ReceivePolicy::fsck_objects`].
477    #[error("{0}")]
478    Rejected(RejectedObject),
479    /// The `receive.fsck.skipList` file could not be loaded, so no object
480    /// could be checked.
481    #[error(transparent)]
482    FsckSkipList(#[from] SkipListError),
483    /// Reading the client stream or writing the spooled pack failed.
484    #[error(transparent)]
485    Io(#[from] std::io::Error),
486    /// The session was stopped by a cancel signal while reading the pack.
487    #[error("cancelled")]
488    Cancelled,
489}
490
491impl From<crate::error::Error> for UnpackError {
492    fn from(err: crate::error::Error) -> Self {
493        match err {
494            crate::error::Error::Cancelled => Self::Cancelled,
495            other => Self::Ingest(crate::error::ErrorSummary::from(other)),
496        }
497    }
498}
499
500/// In-process checks an embedding server runs on every push.
501///
502/// These run in addition to [`ReceivePolicy`] and the repository's hook scripts,
503/// so a server can refuse updates from Rust code without installing a script.
504/// Both methods default to accepting; implement the ones you need. A closure
505/// `Fn(&RefCommand, &Odb) -> Result<(), RefUpdateRefusal>` implements the
506/// per-ref [`Self::update`] check, and `()` accepts everything.
507///
508/// Refusals are usually [`RefUpdateRefusal::Policy`] with a reason the client
509/// will see.
510///
511/// Push options from the client are passed in [`ReceiveHookContext`] to
512/// [`Self::pre_receive_ctx`] and [`Self::update_ctx`]; the default methods ignore
513/// them and call [`Self::pre_receive`] and [`Self::update`].
514pub trait ReceiveHook {
515    /// Inspect the whole push before any ref changes, like a `pre-receive` hook.
516    ///
517    /// Runs before the `pre-receive` script, while the pushed objects are still
518    /// in quarantine.
519    ///
520    /// # Parameters
521    ///
522    /// - `commands`: the updates that passed the built-in checks, in request
523    ///   order.
524    /// - `odb`: an object database that sees the repository's objects and the
525    ///   quarantined pushed ones.
526    ///
527    /// # Errors
528    ///
529    /// Returning a refusal refuses every update in `commands` with it.
530    fn pre_receive(
531        &self,
532        commands: &[RefCommand],
533        odb: &Odb,
534    ) -> std::result::Result<(), RefUpdateRefusal> {
535        let _ = (commands, odb);
536        Ok(())
537    }
538
539    /// Like [`Self::pre_receive`], with push options from the client request.
540    fn pre_receive_ctx(
541        &self,
542        commands: &[RefCommand],
543        odb: &Odb,
544        ctx: &ReceiveHookContext<'_>,
545    ) -> std::result::Result<(), RefUpdateRefusal> {
546        let _ = ctx;
547        self.pre_receive(commands, odb)
548    }
549
550    /// Inspect one update, like an `update` hook.
551    ///
552    /// Runs before the `update` script for the same ref, after the pushed
553    /// objects were moved into the repository.
554    ///
555    /// # Parameters
556    ///
557    /// - `command`: the update to check.
558    /// - `odb`: the repository's object database, including the pushed objects.
559    ///
560    /// # Errors
561    ///
562    /// Returning a refusal refuses this update only (or the whole push when the
563    /// client asked for an `atomic` push).
564    fn update(&self, command: &RefCommand, odb: &Odb) -> std::result::Result<(), RefUpdateRefusal> {
565        let _ = (command, odb);
566        Ok(())
567    }
568
569    /// Like [`Self::update`], with push options from the client request.
570    fn update_ctx(
571        &self,
572        command: &RefCommand,
573        odb: &Odb,
574        ctx: &ReceiveHookContext<'_>,
575    ) -> std::result::Result<(), RefUpdateRefusal> {
576        let _ = ctx;
577        self.update(command, odb)
578    }
579
580    /// What to tell the pushing client once every update has been decided,
581    /// as a hook script's output would: Git clients show each line as
582    /// `remote: <line>`.
583    ///
584    /// The text goes out on the progress side band just before the status
585    /// report, so only to clients that asked for a side band. The default
586    /// sends nothing.
587    ///
588    /// # Parameters
589    ///
590    /// - `updates`: every requested update with its result, in request order.
591    ///
592    /// # Returns
593    ///
594    /// The text, or `None` for nothing.
595    fn message(&self, updates: &[RefUpdateResult]) -> Option<String> {
596        let _ = updates;
597        None
598    }
599
600    /// Run after refs were updated and the `post-receive` script, like a
601    /// `post-receive` hook for in-process policy and audit.
602    ///
603    /// Ref updates and the client's status report are already fixed when this
604    /// runs. A refusal is recorded on [`ReceiveOutcome::post_receive_callback_error`]
605    /// and does not change them (same as a failing `post-receive` script).
606    fn post_receive(
607        &self,
608        commands: &[&RefCommand],
609        odb: &Odb,
610        ctx: &ReceiveHookContext<'_>,
611    ) -> std::result::Result<(), RefUpdateRefusal> {
612        let _ = (commands, odb, ctx);
613        Ok(())
614    }
615
616    /// Run after the `post-update` script for the same applied updates.
617    ///
618    /// Refusals are recorded on [`ReceiveOutcome::post_update_callback_error`]
619    /// without changing ref updates or the client report.
620    fn post_update(
621        &self,
622        commands: &[&RefCommand],
623        odb: &Odb,
624    ) -> std::result::Result<(), RefUpdateRefusal> {
625        let _ = (commands, odb);
626        Ok(())
627    }
628}
629
630/// Push options and other session context for [`ReceiveHook`].
631#[derive(Debug, Clone, Copy, Default)]
632pub struct ReceiveHookContext<'a> {
633    /// Values the client sent after the command list (`push-options` capability).
634    pub push_options: &'a [String],
635}
636
637impl ReceiveHook for () {}
638
639impl<F> ReceiveHook for F
640where
641    F: Fn(&RefCommand, &Odb) -> std::result::Result<(), RefUpdateRefusal>,
642{
643    fn update(&self, command: &RefCommand, odb: &Odb) -> std::result::Result<(), RefUpdateRefusal> {
644        self(command, odb)
645    }
646}
647
648/// One server-side hook invocation during receive-pack.
649#[derive(Debug, Clone, PartialEq, Eq)]
650pub struct HookRunRecord {
651    /// Hook name (`pre-receive`, `update`, `post-receive`, or `post-update`).
652    pub name: String,
653    /// Whether an executable hook ran (missing hooks are skipped).
654    pub ran: bool,
655    /// Exit code when [`Self::ran`] is true.
656    pub exit_code: Option<i32>,
657}
658
659/// What a push session did.
660#[derive(Debug, Default)]
661pub struct ReceiveOutcome {
662    /// Why the pushed pack could not be stored, when it could not.
663    pub unpack_error: Option<UnpackError>,
664    /// One entry per ref update the client requested, in request order.
665    pub updates: Vec<RefUpdateResult>,
666    /// Hook runs performed for this push, in execution order.
667    pub hooks: Vec<HookRunRecord>,
668    /// Push options delivered to hooks via `GIT_PUSH_OPTION_*`.
669    pub push_options: Vec<String>,
670    /// Decoded pack bytes read from the client (zero when no pack was sent).
671    pub bytes_received: u64,
672    /// Object count from the pack header (zero when no pack was sent).
673    pub objects_received: u32,
674    /// [`ReceiveHook::post_receive`] refused after refs were updated.
675    pub post_receive_callback_error: Option<RefUpdateRefusal>,
676    /// [`ReceiveHook::post_update`] refused after refs were updated.
677    pub post_update_callback_error: Option<RefUpdateRefusal>,
678}
679
680struct AppliedPush {
681    updates: Vec<RefUpdateResult>,
682    post_receive_callback_error: Option<RefUpdateRefusal>,
683    post_update_callback_error: Option<RefUpdateRefusal>,
684}
685
686fn applied_push(updates: Vec<RefUpdateResult>) -> AppliedPush {
687    AppliedPush {
688        updates,
689        post_receive_callback_error: None,
690        post_update_callback_error: None,
691    }
692}
693
694/// One push session served into a repository (the server side of `git-receive-pack`):
695/// advertise refs, read the ref updates and pack, store the objects, apply the updates,
696/// and report the result to the client.
697///
698/// Build it with [`ReceivePack::new`], optionally add an in-process [`ReceiveHook`]
699/// ([`ReceivePack::with_hook`]) and a [`CancelToken`] ([`ReceivePack::with_cancel`]),
700/// then [`ReceivePack::run`] it over the client's byte streams. Protocol v2 is not
701/// defined for pushes, so a v2 session is served as v0.
702///
703/// The pushed pack is streamed into a temp file inside a quarantine object
704/// directory, so memory use does not grow with the pack size. Its objects are
705/// then stored via [`unpack_objects`] for packs with fewer objects than
706/// `receive.unpackLimit` and installed as a pack with an index otherwise,
707/// matching Git receive-pack. On that unpack path, blobs larger than 1 MiB are
708/// inflated and written as loose objects in fixed-size chunks, so peak memory
709/// stays bounded rather than holding the full inflated payload. Updates are checked against `policy` and
710/// against the value the client expected each ref to have; with the `atomic`
711/// capability either every update is applied or none is. Server-side hooks run
712/// through the repository [`CommandRunner`](crate::command_runner::CommandRunner)
713/// (`pre-receive`, `update`, `post-receive`, `post-update`) with push options and
714/// quarantined object ingestion matching Git receive-pack. An in-process [`ReceiveHook`] runs just
715/// before the matching script: [`ReceiveHook::pre_receive`] before
716/// `pre-receive` and [`ReceiveHook::update`] before each `update`; its
717/// [`ReceiveHook::message`] reaches a client that asked for a side band just
718/// before the status report.
719///
720/// The pack is read only up to its trailing checksum, never to end of input:
721/// a push client keeps its side open until it has read the status report.
722///
723/// A shallow client announces its boundaries with `shallow <oid>` lines before
724/// the commands. Their parents are not required when the pushed objects are
725/// checked. An update whose history ends at such a boundary that this
726/// repository's refs do not already reach would make the repository shallow:
727/// it is refused with [`RefUpdateRefusal::ShallowUpdateNotAllowed`] unless
728/// [`ReceivePolicy::shallow_update`] is on, in which case the boundaries are
729/// added to the `shallow` file before the ref is written and hook scripts see
730/// the client's boundaries through `GIT_SHALLOW_FILE`.
731///
732/// # Returns
733///
734/// What was stored and which updates were applied. A pack that could not be
735/// stored ([`ReceiveOutcome::unpack_error`]) and refused updates
736/// ([`RefUpdateResult::error`]) are reported to the client and in the outcome;
737/// they are not errors.
738///
739/// # Errors
740///
741/// Returns [`ServeError::Protocol`] for a malformed command list, and I/O or
742/// repository errors from writing the response, creating the quarantine, or
743/// moving the received objects into the repository.
744#[must_use = "a ReceivePack does nothing until it is run"]
745pub struct ReceivePack<'a> {
746    repo: &'a Repository,
747    options: &'a ServeOptions,
748    policy: &'a ReceivePolicy,
749    hook: &'a dyn ReceiveHook,
750    cancel: &'a CancelToken,
751}
752
753impl<'a> ReceivePack<'a> {
754    /// Prepare a session receiving into `repo` with `options` (framing, protocol version,
755    /// agent string, hidden refs) and `policy` (which updates to refuse).
756    pub fn new(repo: &'a Repository, options: &'a ServeOptions, policy: &'a ReceivePolicy) -> Self {
757        Self {
758            repo,
759            options,
760            policy,
761            hook: &(),
762            cancel: CancelToken::never_ref(),
763        }
764    }
765
766    /// Run `hook`'s in-process checks alongside the repository's hook scripts.
767    pub fn with_hook(mut self, hook: &'a dyn ReceiveHook) -> Self {
768        self.hook = hook;
769        self
770    }
771
772    /// Stop without moving refs when `cancel` fires while reading the pack or before the
773    /// updates are applied.
774    pub fn with_cancel(mut self, cancel: &'a CancelToken) -> Self {
775        self.cancel = cancel;
776        self
777    }
778
779    /// Serve the session, reading the client's commands and pack from `input` and writing
780    /// the advertisement and status report to `output`.
781    ///
782    /// # Errors
783    ///
784    /// See [`ReceivePack`]; [`Error::Cancelled`](crate::error::Error::Cancelled) when the cancel token is already set
785    /// or fires before the ref updates are committed.
786    pub fn run(self, input: &mut dyn Read, output: &mut dyn Write) -> Result<ReceiveOutcome> {
787        receive_pack(
788            self.repo,
789            input,
790            output,
791            self.options,
792            self.policy,
793            self.hook,
794            self.cancel,
795        )
796    }
797}
798
799/// Serve one push session (see [`ReceivePack`]).
800pub(crate) fn receive_pack(
801    repo: &Repository,
802    input: &mut dyn Read,
803    output: &mut dyn Write,
804    opts: &ServeOptions,
805    policy: &ReceivePolicy,
806    hook: &dyn ReceiveHook,
807    cancel: &CancelToken,
808) -> Result<ReceiveOutcome> {
809    let stop = CancelSignal::new(cancel);
810    stop.poll()?;
811    if opts.advertise_refs || !opts.stateless_rpc {
812        if opts.protocol == ProtocolVersion::V1 {
813            write_line(output, "version 1")?;
814        }
815        write_advertisement(repo, output, opts)?;
816        output.flush()?;
817        if opts.advertise_refs {
818            return Ok(ReceiveOutcome::default());
819        }
820    }
821
822    let Some(request) = read_commands(repo, input)? else {
823        return Ok(ReceiveOutcome::default());
824    };
825
826    let cfg = repo
827        .config()
828        .snapshot()
829        .unwrap_or_else(|_| std::sync::Arc::new(ConfigSet::new()));
830    let max_input = max_input_size_from_config(cfg.as_ref());
831    let sideband = request.caps.contains("side-band-64k");
832
833    let needs_pack = request.commands.iter().any(|c| c.new.is_some());
834    let mut bytes_received = 0u64;
835    let mut objects_received = 0u32;
836    let mut ingest = if needs_pack {
837        Some(ReceivePackIngest::create(repo)?)
838    } else {
839        None
840    };
841
842    let receiver_grafts = if request.shallow.is_empty() {
843        HashSet::new()
844    } else {
845        load_shallow_boundaries(repo.git_dir())
846    };
847    let mut shallow_info =
848        PushShallowInfo::classify(&request.shallow, repo.odb(), &receiver_grafts)?;
849    let client_boundaries: HashSet<ObjectId> = request.shallow.iter().copied().collect();
850
851    // A partial repository (one offloaded to a large-object promisor, say) lacks
852    // the objects its promisor remotes have; a pushed tree may still name them.
853    let promises = if crate::promisor::repo_treats_promisor_packs(cfg.as_ref()) {
854        Some(super::promised_objects::PromisorObjects::new(repo)?)
855    } else {
856        None
857    };
858    // What the refs reached before the push: the connectivity walk stops there.
859    let existing = if needs_pack {
860        existing_ref_values(repo)?
861    } else {
862        HashSet::new()
863    };
864    // New tips that reach a missing object: refused below.
865    let mut unconnected: HashMap<ObjectId, Unconnected> = HashMap::new();
866    let unpack_error = match ingest.as_mut() {
867        Some(ing) => {
868            let odb = ing.odb();
869            let spool_dir = ing.spool_dir();
870            spool_push_pack(input, &odb, &spool_dir, max_input, &stop)
871                .and_then(|pack| {
872                    bytes_received = pack.bytes_received();
873                    objects_received = pack.object_count();
874                    ingest_spooled_pack(
875                        &odb,
876                        pack,
877                        cfg.as_ref(),
878                        &client_boundaries,
879                        promises.as_ref(),
880                    )
881                })
882                .and_then(|indexed| {
883                    if let Some(ids) = &indexed {
884                        ing.set_received(ids.clone());
885                    }
886                    if let Some(fsck) = &policy.fsck_objects {
887                        check_received_objects(repo, &odb, indexed.as_ref(), cfg.as_ref(), fsck)?;
888                    }
889                    // Every push, loose or packed (even empty): objects the
890                    // repository already had are not proof of a complete history.
891                    let received = match indexed {
892                        Some(received) => received,
893                        None if odb.uses_files_primary() => odb
894                            .enumerate_local_loose_objects()?
895                            .into_iter()
896                            .map(|(oid, _)| oid)
897                            .collect(),
898                        None => HashSet::new(),
899                    };
900                    unconnected = unconnected_tips(
901                        repo,
902                        &odb,
903                        &received,
904                        &existing,
905                        &request.commands,
906                        &client_boundaries,
907                        promises.as_ref(),
908                    )?;
909                    Ok(())
910                })
911                .err()
912        }
913        None => None,
914    };
915    if matches!(unpack_error, Some(UnpackError::Cancelled)) {
916        return Err(crate::error::Error::Cancelled);
917    }
918
919    let shallow = match ingest.as_ref() {
920        Some(ing) if unpack_error.is_none() => {
921            let lookup = ing.odb();
922            shallow_info.retain_received(&lookup)?;
923            ShallowPlan::new(
924                repo,
925                &lookup,
926                &request,
927                policy,
928                &shallow_info,
929                &receiver_grafts,
930            )?
931        }
932        _ => ShallowPlan::none(request.commands.len()),
933    };
934
935    let mut hook_records = Vec::new();
936    let mut post_receive_callback_error = None;
937    let mut post_update_callback_error = None;
938    let updates = if unpack_error.is_some() {
939        request
940            .commands
941            .iter()
942            .map(|c| c.refused(RefUpdateRefusal::UnpackerError))
943            .collect()
944    } else {
945        stop.poll()?;
946        let applied = apply_commands_with_hooks(
947            repo,
948            &request,
949            opts,
950            policy,
951            hook,
952            ingest.as_mut(),
953            &unconnected,
954            &shallow,
955            sideband.then_some(&mut *output),
956            &mut hook_records,
957            cancel,
958        )?;
959        post_receive_callback_error = applied.post_receive_callback_error;
960        post_update_callback_error = applied.post_update_callback_error;
961        applied.updates
962    };
963
964    if sideband {
965        if let Some(text) = hook.message(&updates) {
966            write_progress_text(output, &text)?;
967        }
968    }
969    if request.caps.contains("report-status") || request.caps.contains("report-status-v2") {
970        let report = build_report(unpack_error.as_ref(), &updates)?;
971        if sideband {
972            crate::pkt_line::write_sideband_channel1_64k(&mut &mut *output, &report)?;
973            write_flush(output)?;
974        } else {
975            output.write_all(&report)?;
976        }
977    }
978    output.flush()?;
979
980    Ok(ReceiveOutcome {
981        unpack_error,
982        updates,
983        hooks: hook_records,
984        push_options: request.push_options.clone(),
985        bytes_received,
986        objects_received,
987        post_receive_callback_error,
988        post_update_callback_error,
989    })
990}
991
992/// Send `text` on the progress side band (channel 2), one line per packet,
993/// as Git relays a hook script's output.
994fn write_progress_text(output: &mut dyn Write, text: &str) -> Result<()> {
995    for line in text.lines() {
996        let line = format!("{line}\n");
997        for chunk in line.as_bytes().chunks(65515) {
998            crate::pkt_line::write_sideband_packet(&mut *output, 2, chunk)
999                .map_err(ServeError::Io)?;
1000        }
1001    }
1002    Ok(())
1003}
1004
1005/// Write the refs the client may update, with capabilities on the first line.
1006fn write_advertisement(
1007    repo: &Repository,
1008    output: &mut dyn Write,
1009    opts: &ServeOptions,
1010) -> Result<()> {
1011    let mut caps: Vec<String> = CAPABILITIES.iter().map(|c| (*c).to_owned()).collect();
1012    let config = repo
1013        .config()
1014        .snapshot()
1015        .unwrap_or_else(|_| std::sync::Arc::new(ConfigSet::new()));
1016    if config
1017        .get_bool("receive.advertisepushoptions")
1018        .ok()
1019        .flatten()
1020        .unwrap_or(false)
1021    {
1022        caps.push("push-options".to_owned());
1023    }
1024    caps.push(format!("object-format={}", repo.odb().hash_algo().name()));
1025    if let Some(path) = &opts.push_bundle {
1026        caps.push(format!("{}={path}", super::PUSH_BUNDLE_CAPABILITY));
1027    }
1028    if !opts.agent.is_empty() {
1029        caps.push(format!("agent={}", opts.agent));
1030    }
1031    let caps = caps.join(" ");
1032
1033    let mut first_line = true;
1034    for_each_advertised_ref(repo, &opts.hidden_refs, &[], Peel::No, |r| {
1035        if first_line {
1036            write_line(output, &format!("{} {}\0{caps}", r.oid.to_hex(), r.name))?;
1037            first_line = false;
1038        } else {
1039            write_line(output, &format!("{} {}", r.oid.to_hex(), r.name))?;
1040        }
1041        Ok(std::ops::ControlFlow::Continue(()))
1042    })?;
1043    if first_line {
1044        let zero = ObjectId::null(repo.odb().hash_algo());
1045        write_line(
1046            output,
1047            &format!("{} capabilities^{{}}\0{caps}", zero.to_hex()),
1048        )?;
1049    }
1050    write_flush(output)
1051}
1052
1053/// The command list of a push.
1054struct PushRequest {
1055    commands: Vec<RefCommand>,
1056    caps: HashSet<String>,
1057    push_options: Vec<String>,
1058    /// The client's shallow boundaries, from `shallow <oid>` lines.
1059    shallow: Vec<ObjectId>,
1060}
1061
1062/// Read the command list up to its flush. Returns `None` when the client sent
1063/// no commands (nothing to push).
1064///
1065/// A shallow client sends a `shallow <oid>` line for each of its boundaries
1066/// before the commands; they are collected wherever they appear in the list.
1067fn read_commands(repo: &Repository, input: &mut dyn Read) -> Result<Option<PushRequest>> {
1068    let mut commands = Vec::new();
1069    let mut caps = HashSet::new();
1070    let mut shallow = Vec::new();
1071    loop {
1072        match read_packet(input)? {
1073            None | Some(Packet::Flush) => break,
1074            Some(Packet::Data(line)) => {
1075                if let Some(hex) = line.strip_prefix("shallow ") {
1076                    shallow.push(parse_oid(repo, hex)?);
1077                    continue;
1078                }
1079                let (line, line_caps) = line.split_once('\0').unwrap_or((line.as_str(), ""));
1080                if commands.is_empty() {
1081                    caps.extend(
1082                        line_caps
1083                            .split(' ')
1084                            .filter(|c| !c.is_empty())
1085                            .map(str::to_owned),
1086                    );
1087                }
1088                let mut parts = line.splitn(3, ' ');
1089                let (Some(old), Some(new), Some(refname)) =
1090                    (parts.next(), parts.next(), parts.next())
1091                else {
1092                    return Err(ServeError::Protocol {
1093                        detail: format!("malformed command '{line}'"),
1094                    }
1095                    .into());
1096                };
1097                let old = parse_oid(repo, old)?;
1098                let new = parse_oid(repo, new)?;
1099                commands.push(RefCommand {
1100                    old: (!old.is_zero()).then_some(old),
1101                    new: (!new.is_zero()).then_some(new),
1102                    refname: refname.to_owned(),
1103                });
1104            }
1105            Some(other) => {
1106                return Err(ServeError::Protocol {
1107                    detail: format!("unexpected packet in command list: {other:?}"),
1108                }
1109                .into())
1110            }
1111        }
1112    }
1113    if commands.is_empty() {
1114        return Ok(None);
1115    }
1116    let mut push_options = Vec::new();
1117    if caps.contains("push-options") {
1118        loop {
1119            match read_packet(input)? {
1120                None | Some(Packet::Flush) => break,
1121                Some(Packet::Data(line)) => {
1122                    push_options.push(line);
1123                }
1124                Some(other) => {
1125                    return Err(ServeError::Protocol {
1126                        detail: format!("unexpected packet in push options: {other:?}"),
1127                    }
1128                    .into())
1129                }
1130            }
1131        }
1132    }
1133    let format = caps.iter().find_map(|c| c.strip_prefix("object-format="));
1134    check_object_format(repo, format)?;
1135    Ok(Some(PushRequest {
1136        commands,
1137        caps,
1138        push_options,
1139        shallow,
1140    }))
1141}
1142
1143/// The shallow-file changes a push from a shallow client needs.
1144struct ShallowPlan {
1145    /// For each command, in request order, the boundaries accepting it would
1146    /// add to the repository's `shallow` file.
1147    roots: Vec<Vec<ObjectId>>,
1148    /// Under [`ReceivePolicy::shallow_update`], a shallow file listing the
1149    /// repository's boundaries and the client's, which hooks see as
1150    /// `GIT_SHALLOW_FILE` so they can walk the pushed history.
1151    hook_file: Option<tempfile::NamedTempFile>,
1152}
1153
1154impl ShallowPlan {
1155    /// No shallow-file changes for `commands` commands.
1156    fn none(commands: usize) -> Self {
1157        Self {
1158            roots: vec![Vec::new(); commands],
1159            hook_file: None,
1160        }
1161    }
1162
1163    /// Work out the new boundaries of every command once the pushed objects
1164    /// are in `odb`.
1165    fn new(
1166        repo: &Repository,
1167        odb: &Odb,
1168        request: &PushRequest,
1169        policy: &ReceivePolicy,
1170        info: &PushShallowInfo,
1171        receiver_grafts: &HashSet<ObjectId>,
1172    ) -> Result<Self> {
1173        if info.is_empty() {
1174            return Ok(Self::none(request.commands.len()));
1175        }
1176        let mut receiver_tips: HashSet<ObjectId> = HashSet::new();
1177        super::for_each_advertised_ref(repo, &[], &[], Peel::Yes, |r| {
1178            receiver_tips.insert(r.peeled.unwrap_or(r.oid));
1179            Ok(std::ops::ControlFlow::Continue(()))
1180        })?;
1181        receiver_tips.extend(crate::refs::resolve_ref_in(repo, "HEAD").ok());
1182        let graph = repo.odb().commit_graph_chain();
1183        let roots = request
1184            .commands
1185            .iter()
1186            .map(|c| match c.new {
1187                Some(new) => {
1188                    info.new_roots(odb, graph.as_deref(), new, receiver_grafts, &receiver_tips)
1189                }
1190                None => Ok(Vec::new()),
1191            })
1192            .collect::<crate::error::Result<_>>()?;
1193        let hook_file = if policy.shallow_update {
1194            Some(crate::shallow::temporary_push_shallow_file(
1195                repo.git_dir(),
1196                receiver_grafts,
1197                &request.shallow,
1198            )?)
1199        } else {
1200            None
1201        };
1202        Ok(Self { roots, hook_file })
1203    }
1204
1205    /// `GIT_SHALLOW_FILE` for hook scripts, when set.
1206    fn hook_env(&self) -> Result<Vec<(String, String)>> {
1207        let Some(file) = &self.hook_file else {
1208            return Ok(Vec::new());
1209        };
1210        crate::shallow::git_shallow_hook_env(file.path())
1211    }
1212}
1213
1214/// Validate, run hooks, migrate quarantine objects, and apply ref updates.
1215#[allow(clippy::too_many_arguments)]
1216fn apply_commands_with_hooks(
1217    repo: &Repository,
1218    request: &PushRequest,
1219    opts: &ServeOptions,
1220    policy: &ReceivePolicy,
1221    hook: &dyn ReceiveHook,
1222    ingest: Option<&mut ReceivePackIngest>,
1223    unconnected: &HashMap<ObjectId, Unconnected>,
1224    shallow: &ShallowPlan,
1225    mut sideband_out: Option<&mut dyn Write>,
1226    hook_records: &mut Vec<HookRunRecord>,
1227    cancel: &CancelToken,
1228) -> Result<AppliedPush> {
1229    let stop = CancelSignal::new(cancel);
1230    let lookup_odb = ingest.as_ref().map(|ing| ing.odb());
1231    let head_branch = ref_update_policy::head_branch(repo);
1232    let checked_out = ref_update_policy::checked_out_branch(repo);
1233    let branches = ref_update_policy::HeadBranches {
1234        head: head_branch.as_deref(),
1235        checked_out: checked_out.as_deref(),
1236    };
1237    let mut results: Vec<RefUpdateResult> = request
1238        .commands
1239        .iter()
1240        .zip(&shallow.roots)
1241        .map(|(c, roots)| {
1242            if !roots.is_empty() && !policy.shallow_update {
1243                return Ok(c.refused(RefUpdateRefusal::ShallowUpdateNotAllowed));
1244            }
1245            Ok(
1246                match ref_update_policy::validate_ref_update(
1247                    repo,
1248                    lookup_odb.as_ref(),
1249                    c,
1250                    policy,
1251                    &opts.hidden_refs,
1252                    branches,
1253                )? {
1254                    Some(reason) => c.refused(reason),
1255                    None => match c.new.and_then(|new| unconnected.get(&new)) {
1256                        Some(gap) => c.refused(RefUpdateRefusal::Unconnected {
1257                            missing: gap.oid,
1258                            referrer: gap.referrer,
1259                        }),
1260                        None => c.accepted(),
1261                    },
1262                },
1263            )
1264        })
1265        .collect::<Result<_>>()?;
1266
1267    let atomic = request.caps.contains("atomic");
1268    if atomic && results.iter().any(|r| r.error.is_some()) {
1269        refuse_remaining(&mut results, &RefUpdateRefusal::AtomicTransactionFailed);
1270        return Ok(applied_push(results));
1271    }
1272
1273    let accepted: Vec<RefCommand> = request
1274        .commands
1275        .iter()
1276        .zip(&results)
1277        .filter(|(_, r)| r.error.is_none())
1278        .map(|(c, _)| c.clone())
1279        .collect();
1280    let hook_ctx = ReceiveHookContext {
1281        push_options: &request.push_options,
1282    };
1283    if !accepted.is_empty() {
1284        if let Err(refusal) = hook.pre_receive_ctx(
1285            &accepted,
1286            lookup_odb.as_ref().unwrap_or(repo.odb()),
1287            &hook_ctx,
1288        ) {
1289            refuse_remaining(&mut results, &refusal);
1290            return Ok(applied_push(results));
1291        }
1292    }
1293
1294    let scripts = ReceiveHookScripts::new(repo, &request.push_options, shallow.hook_env()?, cancel);
1295    let all_cmds: Vec<&RefCommand> = request.commands.iter().collect();
1296    stop.poll()?;
1297    let pre_result = scripts.pre_receive(
1298        &all_cmds,
1299        ingest.as_ref().and_then(|i| i.files_quarantine()),
1300    )?;
1301    relay_hook_sideband(&mut sideband_out, &pre_result)?;
1302    hook_records.push(hook_record("pre-receive", &pre_result));
1303    if !pre_result.is_ok() {
1304        refuse_remaining(&mut results, &RefUpdateRefusal::PreReceiveHookDeclined);
1305        return Ok(applied_push(results));
1306    }
1307
1308    if results.iter().all(|r| r.error.is_some()) {
1309        return Ok(applied_push(results));
1310    }
1311
1312    let mut migrated_pack_keeps = Vec::new();
1313    if let Some(ing) = ingest {
1314        migrated_pack_keeps = ing.migrate(repo)?;
1315        repo.odb().invalidate_packs();
1316    }
1317
1318    for (cmd, result) in request.commands.iter().zip(results.iter_mut()) {
1319        if result.error.is_some() {
1320            continue;
1321        }
1322        if let Err(refusal) = hook.update_ctx(cmd, repo.odb(), &hook_ctx) {
1323            result.error = Some(refusal);
1324            continue;
1325        }
1326        stop.poll()?;
1327        let update_result = scripts.update(cmd)?;
1328        relay_hook_sideband(&mut sideband_out, &update_result)?;
1329        hook_records.push(hook_record("update", &update_result));
1330        if !update_result.is_ok() {
1331            result.error = Some(RefUpdateRefusal::UpdateHookDeclined);
1332        }
1333    }
1334
1335    for (cmd, result) in request.commands.iter().zip(results.iter_mut()) {
1336        if result.error.is_some() {
1337            continue;
1338        }
1339        match super::push_to_checkout::deploy_for_update_instead(
1340            repo,
1341            cmd,
1342            policy,
1343            &mut sideband_out,
1344        )? {
1345            None => {}
1346            Some(reason) => result.error = Some(reason),
1347        }
1348    }
1349
1350    if atomic && results.iter().any(|r| r.error.is_some()) {
1351        refuse_remaining(&mut results, &RefUpdateRefusal::AtomicTransactionFailed);
1352        return Ok(applied_push(results));
1353    }
1354
1355    let new_roots: HashSet<ObjectId> = shallow
1356        .roots
1357        .iter()
1358        .zip(&results)
1359        .filter(|(_, r)| r.error.is_none())
1360        .flat_map(|(roots, _)| roots.iter().copied())
1361        .collect();
1362    if let Err(e) = add_shallow_boundaries(repo.git_dir(), &new_roots) {
1363        let refusal = RefUpdateRefusal::RefStore(e.to_string());
1364        for (roots, result) in shallow.roots.iter().zip(results.iter_mut()) {
1365            if !roots.is_empty() && result.error.is_none() {
1366                result.error = Some(refusal.clone());
1367            }
1368        }
1369        if atomic {
1370            refuse_remaining(&mut results, &RefUpdateRefusal::AtomicTransactionFailed);
1371            return Ok(applied_push(results));
1372        }
1373    }
1374
1375    let pending: Vec<(usize, crate::refs::store::RefUpdate)> = request
1376        .commands
1377        .iter()
1378        .zip(results.iter())
1379        .enumerate()
1380        .filter(|(_, (_, r))| r.error.is_none())
1381        .map(|(idx, (cmd, _))| (idx, receive_command_store_update(cmd)))
1382        .collect();
1383
1384    let store = repo.ref_store();
1385    let ref_lock_wait = store.ref_lock_wait();
1386    if atomic {
1387        let batch: Vec<_> = pending.iter().map(|(_, u)| u.clone()).collect();
1388        if let Err(crate::error::Error::Ref(RefError::Store(e))) =
1389            crate::refs::commit_ref_updates_on_store_with_wait(store, &batch, || ref_lock_wait)
1390        {
1391            let refusal = ref_update_refusal_from_store(&e);
1392            for (idx, _) in pending {
1393                if results[idx].error.is_none() {
1394                    results[idx].error = Some(refusal.clone());
1395                }
1396            }
1397        }
1398    } else {
1399        for (idx, refusal) in commit_receive_ref_updates_on_store(store, pending, ref_lock_wait) {
1400            results[idx].error = Some(refusal);
1401        }
1402    }
1403
1404    let successful: Vec<&RefCommand> = request
1405        .commands
1406        .iter()
1407        .zip(results.iter())
1408        .filter(|(_, r)| r.error.is_none())
1409        .map(|(c, _)| c)
1410        .collect();
1411    let mut post_receive_callback_error = None;
1412    let mut post_update_callback_error = None;
1413    if !successful.is_empty() {
1414        stop.poll()?;
1415        let post_result = scripts.post_receive(&successful)?;
1416        relay_hook_sideband(&mut sideband_out, &post_result)?;
1417        hook_records.push(hook_record("post-receive", &post_result));
1418        post_receive_callback_error = hook
1419            .post_receive(
1420                &successful,
1421                repo.odb(),
1422                &ReceiveHookContext {
1423                    push_options: &request.push_options,
1424                },
1425            )
1426            .err();
1427
1428        stop.poll()?;
1429        let pu_result = scripts.post_update(&successful)?;
1430        relay_hook_sideband(&mut sideband_out, &pu_result)?;
1431        hook_records.push(hook_record("post-update", &pu_result));
1432        post_update_callback_error = hook.post_update(&successful, repo.odb()).err();
1433    }
1434
1435    drop(migrated_pack_keeps);
1436
1437    Ok(AppliedPush {
1438        updates: results,
1439        post_receive_callback_error,
1440        post_update_callback_error,
1441    })
1442}
1443
1444/// Refuse every update that has not been refused yet.
1445fn refuse_remaining(results: &mut [RefUpdateResult], reason: &RefUpdateRefusal) {
1446    for r in results.iter_mut().filter(|r| r.error.is_none()) {
1447        r.error = Some(reason.clone());
1448    }
1449}
1450
1451fn hook_record(name: &str, result: &HookResult) -> HookRunRecord {
1452    HookRunRecord {
1453        name: name.to_owned(),
1454        ran: result.was_executed(),
1455        exit_code: result.status(),
1456    }
1457}
1458
1459/// The receiving repository's hook scripts for one push, run with Git's arguments, stdin
1460/// and environment.
1461///
1462/// Every script runs from the repository directory with `GIT_DIR=.` (see
1463/// [`run_hook_in_git_dir`]) and its output captured. `pre-receive` and `post-receive` get the
1464/// push options as `GIT_PUSH_OPTION_COUNT` / `GIT_PUSH_OPTION_<n>`; `update` and
1465/// `post-update` do not, as in Git. Each `*_env` pair list also carries the extra
1466/// variables given to [`Self::new`] (such as `GIT_SHALLOW_FILE`).
1467pub(crate) struct ReceiveHookScripts<'a> {
1468    repo: &'a Repository,
1469    cancel: &'a CancelToken,
1470    /// Push options plus the extra variables, for `pre-receive` and `post-receive`.
1471    receive_env: Vec<(String, String)>,
1472    /// The extra variables alone, for `update` and `post-update`.
1473    update_env: Vec<(String, String)>,
1474}
1475
1476impl<'a> ReceiveHookScripts<'a> {
1477    /// Scripts of `repo` for a push that sent `push_options`, with `extra_env` set for every
1478    /// script.
1479    pub fn new(
1480        repo: &'a Repository,
1481        push_options: &[String],
1482        extra_env: Vec<(String, String)>,
1483        cancel: &'a CancelToken,
1484    ) -> Self {
1485        let mut receive_env = Vec::with_capacity(push_options.len() + 1 + extra_env.len());
1486        receive_env.push((
1487            "GIT_PUSH_OPTION_COUNT".to_owned(),
1488            push_options.len().to_string(),
1489        ));
1490        receive_env.extend(
1491            push_options
1492                .iter()
1493                .enumerate()
1494                .map(|(i, opt)| (format!("GIT_PUSH_OPTION_{i}"), opt.clone())),
1495        );
1496        receive_env.extend(extra_env.iter().cloned());
1497        Self {
1498            repo,
1499            cancel,
1500            receive_env,
1501            update_env: extra_env,
1502        }
1503    }
1504
1505    /// Run `pre-receive` over `commands` (one `<old> <new> <ref>` line each on stdin).
1506    ///
1507    /// With `quarantine`, the script also gets its variables so it can read the pushed
1508    /// objects before they enter the repository.
1509    pub(crate) fn pre_receive(
1510        &self,
1511        commands: &[&RefCommand],
1512        quarantine: Option<&ReceiveQuarantine>,
1513    ) -> crate::error::Result<HookResult> {
1514        let mut env = self.receive_env.clone();
1515        if let Some(q) = quarantine {
1516            env.extend(q.hook_env());
1517        }
1518        self.run_with_commands("pre-receive", commands, &env)
1519    }
1520
1521    /// Run `update` for one command, with `<ref> <old> <new>` as arguments.
1522    pub(crate) fn update(&self, cmd: &RefCommand) -> crate::error::Result<HookResult> {
1523        let algo = self.repo.odb().hash_algo();
1524        let hex = |oid: Option<ObjectId>| oid.unwrap_or_else(|| ObjectId::null(algo)).to_hex();
1525        let (old, new) = (hex(cmd.old), hex(cmd.new));
1526        let args = [cmd.refname.as_str(), old.as_str(), new.as_str()];
1527        self.run_git_dir_hook("update", &args, None, &pairs(&self.update_env))
1528    }
1529
1530    /// Run `post-receive` over the applied `commands`.
1531    pub(crate) fn post_receive(
1532        &self,
1533        commands: &[&RefCommand],
1534    ) -> crate::error::Result<HookResult> {
1535        self.run_with_commands("post-receive", commands, &self.receive_env)
1536    }
1537
1538    /// Run `post-update` with the names of the applied `commands` as arguments.
1539    pub(crate) fn post_update(&self, commands: &[&RefCommand]) -> crate::error::Result<HookResult> {
1540        let args: Vec<&str> = commands.iter().map(|c| c.refname.as_str()).collect();
1541        self.run_git_dir_hook("post-update", &args, None, &pairs(&self.update_env))
1542    }
1543
1544    fn run_git_dir_hook(
1545        &self,
1546        hook_name: &str,
1547        args: &[&str],
1548        stdin_data: Option<&[u8]>,
1549        env: &[(&str, &str)],
1550    ) -> crate::error::Result<HookResult> {
1551        match run_hook_in_git_dir_with_cancel(
1552            self.repo,
1553            hook_name,
1554            args,
1555            stdin_data,
1556            env,
1557            self.cancel,
1558        ) {
1559            Ok(result) => Ok(result),
1560            Err(crate::error::Error::Cancelled) => Err(crate::error::Error::Cancelled),
1561            Err(_) => Ok(hook_declined()),
1562        }
1563    }
1564
1565    fn run_with_commands(
1566        &self,
1567        hook_name: &str,
1568        commands: &[&RefCommand],
1569        env: &[(String, String)],
1570    ) -> crate::error::Result<HookResult> {
1571        let zero = ObjectId::null(self.repo.odb().hash_algo());
1572        let stdin: String = commands
1573            .iter()
1574            .map(|c| {
1575                format!(
1576                    "{} {} {}\n",
1577                    c.old.unwrap_or(zero).to_hex(),
1578                    c.new.unwrap_or(zero).to_hex(),
1579                    c.refname
1580                )
1581            })
1582            .collect();
1583        self.run_git_dir_hook(hook_name, &[], Some(stdin.as_bytes()), &pairs(env))
1584    }
1585}
1586
1587fn hook_declined() -> HookResult {
1588    HookResult::Failed {
1589        status: 1,
1590        output: crate::hooks::HookOutput::default(),
1591    }
1592}
1593
1594fn pairs(env: &[(String, String)]) -> Vec<(&str, &str)> {
1595    env.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect()
1596}
1597
1598fn receive_updates_eligible_for_initial_packed(updates: &[crate::refs::store::RefUpdate]) -> bool {
1599    use crate::refs::store::{Expected, RawRef};
1600    updates.iter().all(|update| {
1601        matches!(update.new_value, Some(RawRef::Direct(_)))
1602            && matches!(update.expected, Expected::Missing)
1603            && !update.flags.log_only
1604            && update.reflog.is_none()
1605    })
1606}
1607
1608/// Apply a non-atomic receive-pack ref batch in one store transaction when possible.
1609///
1610/// Plain creates use the initial packed-refs path; mixed batches use a regular transaction.
1611/// When a batch fails, fall back to a non-atomic batch (one reftable table write per phase)
1612/// so sibling updates still commit while individual refs can be rejected.
1613fn ref_update_refusal_from_store(err: &crate::refs::store::RefStoreError) -> RefUpdateRefusal {
1614    match err {
1615        crate::refs::store::RefStoreError::CreateExists { .. } => {
1616            RefUpdateRefusal::ReferenceAlreadyExists
1617        }
1618        other => RefUpdateRefusal::RefStore(other.to_string()),
1619    }
1620}
1621
1622fn commit_receive_ref_updates_on_store(
1623    store: &dyn crate::refs::store::RefStore,
1624    pending: Vec<(usize, crate::refs::store::RefUpdate)>,
1625    ref_lock_wait: crate::lockfile::LockWait,
1626) -> Vec<(usize, RefUpdateRefusal)> {
1627    if pending.is_empty() {
1628        return Vec::new();
1629    }
1630    let updates: Vec<_> = pending.iter().map(|(_, u)| u.clone()).collect();
1631    let batch_ok = if receive_updates_eligible_for_initial_packed(&updates) {
1632        crate::refs::commit_initial_ref_updates_on_store_with_wait(store, updates.clone(), || {
1633            ref_lock_wait
1634        })
1635    } else {
1636        crate::refs::commit_ref_updates_on_store_with_wait(store, &updates, || ref_lock_wait)
1637    };
1638    if batch_ok.is_ok() {
1639        return Vec::new();
1640    }
1641    let indices: Vec<_> = pending.iter().map(|(idx, _)| *idx).collect();
1642    match crate::refs::commit_ref_updates_non_atomic_on_store(store, updates) {
1643        Ok(outcomes) => indices
1644            .into_iter()
1645            .zip(outcomes)
1646            .filter_map(|(idx, outcome)| {
1647                outcome
1648                    .result
1649                    .err()
1650                    .map(|e| (idx, ref_update_refusal_from_store(&e)))
1651            })
1652            .collect(),
1653        Err(e) => indices
1654            .into_iter()
1655            .map(|idx| {
1656                (
1657                    idx,
1658                    match &e {
1659                        crate::error::Error::Ref(RefError::Store(store)) => {
1660                            ref_update_refusal_from_store(store)
1661                        }
1662                        other => RefUpdateRefusal::RefStore(other.to_string()),
1663                    },
1664                )
1665            })
1666            .collect(),
1667    }
1668}
1669
1670fn receive_command_store_update(cmd: &RefCommand) -> crate::refs::store::RefUpdate {
1671    let mut flags = crate::refs::store::RefUpdateFlags::default();
1672    if cmd.refname == "HEAD" {
1673        flags.no_deref = true;
1674    }
1675    crate::refs::store::RefUpdate {
1676        name: crate::refs::store::RefName::from_trusted(cmd.refname.clone()),
1677        new_value: cmd.new.map(crate::refs::store::RawRef::Direct),
1678        expected: match cmd.old {
1679            Some(oid) => crate::refs::store::Expected::Oid(oid),
1680            None => crate::refs::store::Expected::Missing,
1681        },
1682        reflog: None,
1683        flags,
1684    }
1685}
1686
1687/// Relay what a hook wrote (stdout, then stderr) to the client on sideband 2.
1688fn relay_hook_sideband(out: &mut Option<&mut dyn Write>, result: &HookResult) -> Result<()> {
1689    let (Some(w), Some(output)) = (out.as_mut(), result.output()) else {
1690        return Ok(());
1691    };
1692    for chunk in output
1693        .stdout
1694        .chunks(65515)
1695        .chain(output.stderr.chunks(65515))
1696    {
1697        crate::pkt_line::write_sideband_packet(*w, 2, chunk).map_err(ServeError::Io)?;
1698    }
1699    Ok(())
1700}
1701
1702/// Encode the status report as pkt-lines: the unpack result, one line per ref,
1703/// and a closing flush.
1704fn build_report(
1705    unpack_error: Option<&UnpackError>,
1706    updates: &[RefUpdateResult],
1707) -> Result<Vec<u8>> {
1708    let mut report = Vec::new();
1709    match unpack_error {
1710        None => write_line(&mut report, "unpack ok")?,
1711        Some(e) => write_line(&mut report, &format!("unpack {}", one_line(&e.to_string())))?,
1712    }
1713    for u in updates {
1714        match &u.error {
1715            None => write_line(&mut report, &format!("ok {}", u.refname))?,
1716            Some(reason) => write_line(
1717                &mut report,
1718                &format!("ng {} {}", u.refname, one_line(&reason.to_string())),
1719            )?,
1720        }
1721    }
1722    write_flush(&mut report)?;
1723    Ok(report)
1724}
1725
1726/// Collapse a message to a single line so it fits in one status pkt-line.
1727fn one_line(msg: &str) -> String {
1728    msg.lines().next().unwrap_or_default().trim().to_owned()
1729}
1730
1731/// A pushed pack written to a temp file in the quarantine's `pack/` directory.
1732struct SpooledPack {
1733    file: tempfile::NamedTempFile,
1734    /// The 12-byte pack header (signature, version, object count).
1735    header: [u8; 12],
1736    /// Pack bytes read from the client (including header and checksum).
1737    bytes_received: u64,
1738}
1739
1740impl SpooledPack {
1741    fn bytes_received(&self) -> u64 {
1742        self.bytes_received
1743    }
1744
1745    fn object_count(&self) -> u32 {
1746        u32::from_be_bytes([
1747            self.header[8],
1748            self.header[9],
1749            self.header[10],
1750            self.header[11],
1751        ])
1752    }
1753}
1754
1755/// Stream exactly one packfile from the push stream into a temp file.
1756///
1757/// The pack's end is found by scanning its entries as they arrive, and nothing
1758/// past the trailing checksum is requested: the push client keeps its write
1759/// half open until it has read the status report, so waiting for end of input
1760/// would hang (issue #1656). The checksum itself is verified when the pack is
1761/// stored.
1762fn spool_push_pack(
1763    input: &mut dyn Read,
1764    odb: &Odb,
1765    spool_dir: &Path,
1766    max_bytes: Option<u64>,
1767    stop: &CancelSignal<'_>,
1768) -> std::result::Result<SpooledPack, UnpackError> {
1769    let hash = odb.hash_algo();
1770    let mut file = tempfile::Builder::new()
1771        .prefix("tmp_pack_")
1772        .tempfile_in(spool_dir)?;
1773    let mut scanner = PackStreamScanner::new(hash);
1774    let mut header = [0u8; 12];
1775    let mut chunk = vec![0u8; SPOOL_CHUNK];
1776    let mut written = 0u64;
1777    let mut pack_len: Option<u64> = None;
1778    let mut out = BufWriter::with_capacity(SPOOL_CHUNK, file.as_file_mut());
1779    loop {
1780        stop.poll().map_err(|_| UnpackError::Cancelled)?;
1781        let want = match pack_len {
1782            Some(len) if written >= len => break,
1783            Some(len) => usize::try_from(len - written).map_or(chunk.len(), |n| n.min(chunk.len())),
1784            None => chunk.len(),
1785        };
1786        let n = read_some(input, &mut chunk[..want])?;
1787        if n == 0 {
1788            return Err(UnpackError::Truncated);
1789        }
1790        let mut data = &chunk[..n];
1791        if pack_len.is_none() {
1792            scanner.feed(data)?;
1793            if scanner.is_complete() {
1794                let len = scanner
1795                    .pack_body_byte_len()
1796                    .saturating_add(hash.len() as u64);
1797                let keep = usize::try_from(len.saturating_sub(written)).map_or(n, |k| k.min(n));
1798                data = &data[..keep];
1799                pack_len = Some(len);
1800            }
1801        }
1802        if let Some(limit) = max_bytes {
1803            let total = pack_len.unwrap_or(written.saturating_add(data.len() as u64));
1804            if total > limit {
1805                return Err(UnpackError::TooLarge { limit });
1806            }
1807        }
1808        if let Some(missing) = usize::try_from(written)
1809            .ok()
1810            .and_then(|start| header.get_mut(start..))
1811        {
1812            let take = missing.len().min(data.len());
1813            missing[..take].copy_from_slice(&data[..take]);
1814        }
1815        out.write_all(data)?;
1816        written += data.len() as u64;
1817    }
1818    out.flush()?;
1819    drop(out);
1820    Ok(SpooledPack {
1821        file,
1822        header,
1823        bytes_received: written,
1824    })
1825}
1826
1827/// `Read::read`, retrying when interrupted by a signal.
1828fn read_some(input: &mut dyn Read, buf: &mut [u8]) -> std::io::Result<usize> {
1829    loop {
1830        match input.read(buf) {
1831            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
1832            other => return other,
1833        }
1834    }
1835}
1836
1837/// Check the objects a push stored in the quarantine `odb` against `fsck`.
1838///
1839/// `indexed` lists the objects of an installed pack; `None` means the pack was
1840/// exploded into loose objects, which are then all the quarantine holds.
1841fn check_received_objects(
1842    repo: &Repository,
1843    odb: &Odb,
1844    indexed: Option<&HashSet<ObjectId>>,
1845    cfg: &ConfigSet,
1846    fsck: &ObjectFsckPolicy,
1847) -> std::result::Result<(), UnpackError> {
1848    let checker = fsck.checker(odb.hash_algo(), repo.git_dir(), repo.environment())?;
1849    let mut received: Vec<ObjectId> = match indexed {
1850        Some(ids) => ids.iter().copied().collect(),
1851        None => odb
1852            .enumerate_local_loose_objects()?
1853            .into_iter()
1854            .map(|(oid, _)| oid)
1855            .collect(),
1856    };
1857    received.sort_unstable();
1858    let parallel = repo
1859        .thread_policy()
1860        .try_resolve(ParallelOp::IndexPack, cfg)
1861        .map_err(crate::error::Error::from)?;
1862    let verdicts = try_par_map(&received, parallel, received.len(), |oid| {
1863        if odb.read_info(oid)?.kind == ObjectKind::Blob {
1864            return Ok(None);
1865        }
1866        let obj = odb.read(oid)?;
1867        Ok(checker
1868            .check(oid, obj.kind, &obj.data)
1869            .err()
1870            .map(|error| RejectedObject {
1871                oid: *oid,
1872                kind: obj.kind,
1873                error,
1874            }))
1875    })
1876    .map_err(|ParallelHashError::Task(e): ParallelHashError<crate::error::Error>| e)?;
1877    match verdicts.into_iter().flatten().next() {
1878        Some(rejected) => Err(UnpackError::Rejected(rejected)),
1879        None => Ok(()),
1880    }
1881}
1882
1883/// The new tips among `commands` that reach an object that is in neither the push
1884/// (`received`, read through the quarantine `odb`) nor `repo`, unless a promisor
1885/// remote promises it, with the first such object. The walk stops at what
1886/// `existing` (the values of `repo`'s refs and `HEAD` before the push) reaches
1887/// (see [`super::connectivity`]).
1888fn unconnected_tips(
1889    repo: &Repository,
1890    odb: &Odb,
1891    received: &HashSet<ObjectId>,
1892    existing: &HashSet<ObjectId>,
1893    commands: &[RefCommand],
1894    shallow_boundaries: &HashSet<ObjectId>,
1895    promises: Option<&super::promised_objects::PromisorObjects<'_>>,
1896) -> std::result::Result<HashMap<ObjectId, Unconnected>, UnpackError> {
1897    let mut tips: Vec<ObjectId> = commands.iter().filter_map(|c| c.new).collect();
1898    tips.sort_unstable();
1899    tips.dedup();
1900    if tips.is_empty() {
1901        return Ok(HashMap::new());
1902    }
1903    let receiver_grafts = load_shallow_boundaries(repo.git_dir());
1904    let graph = repo.odb().commit_graph_chain();
1905    let pushed = PushedObjects {
1906        odb,
1907        repo_odb: repo.odb(),
1908        graph: graph.as_deref(),
1909        received,
1910        existing,
1911        shallow_boundaries,
1912        receiver_grafts: &receiver_grafts,
1913        promises,
1914    };
1915    let gaps = pushed.unconnected_tips(&tips)?;
1916    Ok(tips
1917        .into_iter()
1918        .zip(gaps)
1919        .filter_map(|(tip, gap)| Some((tip, gap?)))
1920        .collect())
1921}
1922
1923/// The values of every ref of `repo` and of `HEAD`, with tags also peeled.
1924fn existing_ref_values(repo: &Repository) -> Result<HashSet<ObjectId>> {
1925    let mut existing = super::advertised_object_ids_from_repo(repo, &[], &[], super::Peel::Yes)?;
1926    existing.extend(crate::refs::resolve_ref_in(repo, "HEAD").ok());
1927    Ok(existing)
1928}
1929
1930/// Store a spooled push pack: explode small packs into loose objects, index and
1931/// install larger ones, per `receive.unpacklimit`.
1932///
1933/// Returns the ids of the installed pack's objects; `None` when the objects
1934/// were unpacked as loose files.
1935///
1936/// The loose path checks that every pushed object's references resolve,
1937/// except the parents of `shallow_boundaries` (the pushing client is shallow
1938/// there and does not have them) and, with `promises`, objects a promisor
1939/// remote of the repository has: Git's connectivity check skips those too, so a
1940/// push to a repository whose large blobs live on a promisor works.
1941fn ingest_spooled_pack(
1942    odb: &Odb,
1943    pack: SpooledPack,
1944    cfg: &ConfigSet,
1945    shallow_boundaries: &HashSet<ObjectId>,
1946    promises: Option<&super::promised_objects::PromisorObjects<'_>>,
1947) -> std::result::Result<Option<HashSet<ObjectId>>, UnpackError> {
1948    if !odb.uses_files_primary() {
1949        let mut reader = BufReader::with_capacity(SPOOL_CHUNK, pack.file.reopen()?);
1950        let opts = UnpackOptions {
1951            quiet: true,
1952            strict: true,
1953            shallow_boundaries: shallow_boundaries.clone(),
1954            ..UnpackOptions::default()
1955        };
1956        let ingested = match promises {
1957            Some(promises) => {
1958                let promised =
1959                    |oid: &ObjectId, referrer: &ObjectId| promises.is_promised(oid, Some(referrer));
1960                crate::unpack_objects::unpack_objects_with_promises(
1961                    &mut reader,
1962                    odb,
1963                    &opts,
1964                    &promised,
1965                )?;
1966                odb.flush_for_publish()?;
1967                let mut ids = HashSet::new();
1968                odb.for_each_object(&mut |oid| {
1969                    ids.insert(*oid);
1970                    std::ops::ControlFlow::Continue(())
1971                })?;
1972                ids
1973            }
1974            None => {
1975                let ingested = crate::unpack_objects::ingest_pack_into_odb(
1976                    &mut reader,
1977                    odb,
1978                    &opts,
1979                    None,
1980                    None,
1981                )?;
1982                ingested.object_ids.into_iter().collect()
1983            }
1984        };
1985        return Ok(Some(ingested));
1986    }
1987    if should_use_unpack_objects(&pack.header, cfg) {
1988        let mut reader = BufReader::with_capacity(SPOOL_CHUNK, pack.file.reopen()?);
1989        let opts = UnpackOptions {
1990            quiet: true,
1991            strict: true,
1992            shallow_boundaries: shallow_boundaries.clone(),
1993            ..UnpackOptions::default()
1994        };
1995        match promises {
1996            Some(promises) => {
1997                let promised =
1998                    |oid: &ObjectId, referrer: &ObjectId| promises.is_promised(oid, Some(referrer));
1999                crate::unpack_objects::unpack_objects_with_promises(
2000                    &mut reader,
2001                    odb,
2002                    &opts,
2003                    &promised,
2004                )?;
2005            }
2006            None => {
2007                unpack_objects(&mut reader, odb, &opts)?;
2008            }
2009        }
2010        return Ok(None);
2011    }
2012    let path = pack.file.into_temp_path();
2013    let installed = install_pack_path_with_progress(
2014        &path,
2015        odb,
2016        &IngestPackOptions {
2017            fix_thin: true,
2018            // Indexing hashes every object and the trailer is checked while
2019            // installing, so a second full read of the pack proves nothing.
2020            skip_post_index_verify: true,
2021            merged_config: Some(cfg.clone()),
2022            ..Default::default()
2023        },
2024        None,
2025        None,
2026    )?;
2027    Ok(Some(installed.object_ids))
2028}
2029
2030#[cfg(test)]
2031mod tests {
2032    use super::*;
2033
2034    #[test]
2035    fn report_lists_unpack_status_then_each_ref() {
2036        let oid = ObjectId::from_hex("1111111111111111111111111111111111111111")
2037            .unwrap_or_else(|_| ObjectId::zero());
2038        let updates = vec![
2039            RefUpdateResult {
2040                refname: "refs/heads/main".into(),
2041                old: None,
2042                new: Some(oid),
2043                error: None,
2044            },
2045            RefUpdateResult {
2046                refname: "refs/heads/topic".into(),
2047                old: Some(oid),
2048                new: None,
2049                error: Some(RefUpdateRefusal::DeletionProhibited),
2050            },
2051        ];
2052        let report = build_report(None, &updates).unwrap_or_default();
2053        assert_eq!(
2054            String::from_utf8_lossy(&report),
2055            "000eunpack ok\n0017ok refs/heads/main\n002cng refs/heads/topic deletion prohibited\n0000"
2056        );
2057    }
2058
2059    #[test]
2060    fn report_renders_typed_unpack_error_and_refusal() {
2061        let updates = vec![RefUpdateResult {
2062            refname: "refs/heads/main".into(),
2063            old: None,
2064            new: None,
2065            error: Some(RefUpdateRefusal::UnpackerError),
2066        }];
2067        let report = build_report(Some(&UnpackError::Truncated), &updates).unwrap_or_default();
2068        assert_eq!(
2069            String::from_utf8_lossy(&report),
2070            "0029unpack unexpected end of pack stream\n0026ng refs/heads/main unpacker error\n0000"
2071        );
2072    }
2073
2074    #[test]
2075    fn refusals_render_the_reasons_git_clients_expect() {
2076        let cases = [
2077            (RefUpdateRefusal::InvalidRefName, "funny refname"),
2078            (RefUpdateRefusal::HiddenRef, "deny updating a hidden ref"),
2079            (RefUpdateRefusal::StaleInfo, "stale info"),
2080            (RefUpdateRefusal::DeletionProhibited, "deletion prohibited"),
2081            (
2082                RefUpdateRefusal::CurrentBranchDeletion,
2083                "deletion of the current branch prohibited",
2084            ),
2085            (
2086                RefUpdateRefusal::CurrentBranch,
2087                "branch is currently checked out",
2088            ),
2089            (
2090                RefUpdateRefusal::MissingObjects,
2091                "missing necessary objects",
2092            ),
2093            (
2094                RefUpdateRefusal::Unconnected {
2095                    missing: ObjectId::zero(),
2096                    referrer: ObjectId::zero(),
2097                },
2098                "missing necessary objects",
2099            ),
2100            (RefUpdateRefusal::NonFastForward, "non-fast-forward"),
2101            (RefUpdateRefusal::BadRef, "bad ref"),
2102            (
2103                RefUpdateRefusal::FastForwardCheckFailed,
2104                "unable to verify fast-forward",
2105            ),
2106            (
2107                RefUpdateRefusal::ShallowUpdateNotAllowed,
2108                "shallow update not allowed",
2109            ),
2110            (RefUpdateRefusal::UnpackerError, "unpacker error"),
2111            (
2112                RefUpdateRefusal::AtomicTransactionFailed,
2113                "atomic transaction failed",
2114            ),
2115            (
2116                RefUpdateRefusal::PreReceiveHookDeclined,
2117                "pre-receive hook declined",
2118            ),
2119            (RefUpdateRefusal::UpdateHookDeclined, "hook declined"),
2120            (RefUpdateRefusal::Policy("frozen".into()), "frozen"),
2121            (
2122                RefUpdateRefusal::TargetBranch {
2123                    target: "main".into(),
2124                },
2125                "main is the target branch; it changes only through integrate",
2126            ),
2127            (
2128                RefUpdateRefusal::RefStore("locked".into()),
2129                "failed to update ref: locked",
2130            ),
2131            (
2132                RefUpdateRefusal::ReferenceAlreadyExists,
2133                "reference already exists",
2134            ),
2135        ];
2136        for (refusal, wire) in cases {
2137            assert_eq!(refusal.to_string(), wire);
2138        }
2139    }
2140
2141    /// Serves `data`, then fails any further read: reading past the pack would
2142    /// block forever on a real push connection.
2143    struct NoReadPastEnd<'a> {
2144        data: &'a [u8],
2145    }
2146
2147    impl Read for NoReadPastEnd<'_> {
2148        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
2149            if self.data.is_empty() {
2150                return Err(std::io::Error::other("read past the end of the pack"));
2151            }
2152            let n = buf.len().min(self.data.len());
2153            buf[..n].copy_from_slice(&self.data[..n]);
2154            self.data = &self.data[n..];
2155            Ok(n)
2156        }
2157    }
2158
2159    fn sample_pack() -> Vec<u8> {
2160        use grit_test_support::objects::{HashAlgo as FixtureAlgo, ObjectKind as PK, PackBuilder};
2161        let mut pb = PackBuilder::new(FixtureAlgo::Sha1);
2162        let mut seed = 0x2545_f491_u32;
2163        // Incompressible, so the pack spans several spool chunks.
2164        let big: Vec<u8> = (0..(SPOOL_CHUNK * 3))
2165            .map(|_| {
2166                seed ^= seed << 13;
2167                seed ^= seed >> 17;
2168                seed ^= seed << 5;
2169                seed as u8
2170            })
2171            .collect();
2172        pb.add_full(PK::Blob, &big);
2173        pb.add_full(PK::Blob, b"small\n");
2174        pb.build().bytes
2175    }
2176
2177    fn test_odb(dir: &std::path::Path) -> Odb {
2178        let objects = dir.join("objects");
2179        std::fs::create_dir_all(objects.join("pack")).unwrap_or_default();
2180        Odb::new(&objects)
2181    }
2182
2183    #[test]
2184    fn spool_stops_at_the_trailer_without_reading_further() {
2185        let tmp = tempfile::tempdir().unwrap_or_else(|e| panic!("{e}"));
2186        let odb = test_odb(tmp.path());
2187        let pack = sample_pack();
2188        let mut input = NoReadPastEnd { data: &pack };
2189        let spool = odb.objects_dir().join("pack");
2190        let spooled = spool_push_pack(&mut input, &odb, &spool, None, &CancelSignal::never())
2191            .unwrap_or_else(|e| panic!("{e}"));
2192        assert_eq!(std::fs::read(spooled.file.path()).unwrap_or_default(), pack);
2193        assert_eq!(&spooled.header[..], &pack[..12]);
2194    }
2195
2196    #[test]
2197    fn spool_keeps_only_the_pack_when_more_bytes_follow() {
2198        let tmp = tempfile::tempdir().unwrap_or_else(|e| panic!("{e}"));
2199        let odb = test_odb(tmp.path());
2200        let pack = sample_pack();
2201        let mut stream = pack.clone();
2202        stream.extend_from_slice(b"trailing junk");
2203        let spool = odb.objects_dir().join("pack");
2204        let spooled = spool_push_pack(
2205            &mut stream.as_slice(),
2206            &odb,
2207            &spool,
2208            None,
2209            &CancelSignal::never(),
2210        )
2211        .unwrap_or_else(|e| panic!("{e}"));
2212        assert_eq!(std::fs::read(spooled.file.path()).unwrap_or_default(), pack);
2213    }
2214
2215    #[test]
2216    fn spool_reports_typed_errors() {
2217        let tmp = tempfile::tempdir().unwrap_or_else(|e| panic!("{e}"));
2218        let odb = test_odb(tmp.path());
2219        let pack = sample_pack();
2220
2221        let truncated = &pack[..pack.len() - 5];
2222        let never = CancelSignal::never();
2223        let spool = odb.objects_dir().join("pack");
2224        assert!(matches!(
2225            spool_push_pack(&mut &truncated[..], &odb, &spool, None, &never),
2226            Err(UnpackError::Truncated)
2227        ));
2228
2229        let limit = (pack.len() - 1) as u64;
2230        assert!(matches!(
2231            spool_push_pack(&mut pack.as_slice(), &odb, &spool, Some(limit), &never),
2232            Err(UnpackError::TooLarge { limit: l }) if l == limit
2233        ));
2234        assert!(spool_push_pack(
2235            &mut pack.as_slice(),
2236            &odb,
2237            &spool,
2238            Some(pack.len() as u64),
2239            &never
2240        )
2241        .is_ok());
2242
2243        assert!(matches!(
2244            spool_push_pack(
2245                &mut &b"JUNK\0\0\0\x02\0\0\0\x01"[..],
2246                &odb,
2247                &spool,
2248                None,
2249                &never
2250            ),
2251            Err(UnpackError::Malformed(PackStreamScanError::BadSignature))
2252        ));
2253
2254        let leftovers: Vec<_> = std::fs::read_dir(odb.objects_dir().join("pack"))
2255            .map(|rd| rd.filter_map(|e| e.ok()).collect())
2256            .unwrap_or_default();
2257        assert!(
2258            leftovers.is_empty(),
2259            "failed spools must not leave temp files"
2260        );
2261    }
2262
2263    #[test]
2264    fn deny_current_branch_values() {
2265        let policy_for = |value: Option<&str>| {
2266            let mut config = ConfigSet::new();
2267            if let Some(v) = value {
2268                let _ = config.add_command_override("receive.denyCurrentBranch", v);
2269            }
2270            ReceivePolicy::from_config(&config).deny_current_branch
2271        };
2272        use DenyCurrentBranch as D;
2273        assert_eq!(policy_for(None), D::Refuse);
2274        assert_eq!(policy_for(Some("ignore")), D::Ignore);
2275        assert_eq!(policy_for(Some("updateInstead")), D::UpdateInstead);
2276        assert_eq!(policy_for(Some("refuse")), D::Refuse);
2277        assert_eq!(policy_for(Some("warn")), D::Warn);
2278    }
2279
2280    #[test]
2281    fn shallow_update_is_off_unless_configured() {
2282        assert!(!ReceivePolicy::default().shallow_update);
2283        let mut config = ConfigSet::new();
2284        let _ = config.add_command_override("receive.shallowUpdate", "true");
2285        assert!(ReceivePolicy::from_config(&config).shallow_update);
2286    }
2287
2288    #[test]
2289    fn deny_delete_current_values() {
2290        let policy_for = |value: Option<&str>| {
2291            let mut config = ConfigSet::new();
2292            if let Some(v) = value {
2293                let _ = config.add_command_override("receive.denyDeleteCurrent", v);
2294            }
2295            ReceivePolicy::from_config(&config).deny_delete_current
2296        };
2297        assert!(policy_for(None));
2298        assert!(!policy_for(Some("ignore")));
2299        assert!(!policy_for(Some("warn")));
2300        assert!(!policy_for(Some("false")));
2301        // There is no working tree to update for a deleted branch.
2302        assert!(policy_for(Some("updateInstead")));
2303        assert!(policy_for(Some("refuse")));
2304        assert!(ReceivePolicy::default().deny_delete_current);
2305    }
2306
2307    #[test]
2308    fn existing_ref_values_includes_injected_head() {
2309        use std::sync::Arc;
2310
2311        use crate::config::environment::RepositoryOptions;
2312        use crate::objects::ObjectKind;
2313        use crate::refs::store::{
2314            Expected, MemoryRefStore, RawRef, RefName, RefStore, RefTransaction, RefUpdate,
2315        };
2316        use crate::repo::{InitOptions, Repository};
2317
2318        let dir = tempfile::tempdir().expect("tempdir");
2319        Repository::init(
2320            dir.path(),
2321            &InitOptions::new()
2322                .with_bare(true)
2323                .with_initial_branch("master"),
2324        )
2325        .expect("init");
2326        let store = Arc::new(MemoryRefStore::new());
2327        let repo = Repository::open(
2328            dir.path(),
2329            &(RepositoryOptions::empty()
2330                .with_ref_store(store.clone())
2331                .with_initial_branch("master")),
2332        )
2333        .expect("open");
2334        let tree = repo.odb().write(ObjectKind::Tree, b"").expect("tree");
2335        let commit = repo
2336            .odb()
2337            .write(
2338                ObjectKind::Commit,
2339                format!(
2340                    "tree {tree}\nauthor T <t@e.com> 1 +0000\ncommitter T <t@e.com> 1 +0000\n\ninit\n"
2341                )
2342                .as_bytes(),
2343            )
2344            .expect("commit");
2345        store
2346            .prepare(
2347                RefTransaction::new()
2348                    .update(RefUpdate {
2349                        name: RefName::from_trusted("refs/heads/master"),
2350                        new_value: Some(RawRef::Direct(commit)),
2351                        expected: Expected::Missing,
2352                        reflog: None,
2353                        flags: Default::default(),
2354                    })
2355                    .expect("txn"),
2356            )
2357            .expect("prepare")
2358            .commit()
2359            .expect("commit");
2360
2361        let values = existing_ref_values(&repo).expect("values");
2362        assert!(values.contains(&commit));
2363    }
2364}