Skip to main content

git_policy_cli/
shadow_skeleton.rs

1//! What:
2//!  The files and directories of an empty bare repository on the files ref backend, created in-process.
3//! Why:
4//!  The shadow repository used to be made by `git init --bare --template=`, a Git start whose whole effect is these
5//!  few entries; `create_shadow_repository` overwrites the `config` it writes and every later shadow step sets the
6//!  `HEAD` it writes, so nothing else of Git's work is used.
7//!  Creating them here removes one process start from every commit (`init`; the `config` read that follows it is the
8//!  wrapper's own and stays).
9//!  A torn creation leaves a directory that is not a repository; recovery removes the shadow tree as a whole
10//!  (`binary_recovery_tearing_shadow_tests.rs`), exactly as it does for a torn `git init`.
11//!
12//! In TS you'd write (pseudocode):
13//! ```ts
14//! for (const directory of SKELETON_DIRECTORIES) await mkdir(join(shadow, directory), { recursive: true });
15//! await writeFile(join(shadow, 'HEAD'), PLACEHOLDER_HEAD);
16//! ```
17
18/// `Path` is a borrowed filesystem path.
19use std::path::Path;
20
21/// Directories a bare repository has, relative to the repository, parents before children.
22///  The empty name is the repository directory itself.
23pub const SKELETON_DIRECTORIES: [&str; 7] =
24    ["", "objects", "objects/info", "objects/pack", "refs", "refs/heads", "refs/tags"];
25
26/// The `HEAD` a new repository starts with:
27///  a branch that does not exist yet.
28///  The shadow's ref steps point `HEAD` at the transaction's own target before any Git command reads it.
29pub const PLACEHOLDER_HEAD: &str = "ref: refs/heads/main\n";
30
31/// What:
32///  Creates the directories and the `HEAD` of an empty bare repository on the files ref backend.
33/// Why:
34///   A directory another run of the same transaction already made is not an error, as with `git init`.
35///
36/// In TS you'd write (pseudocode):
37/// ```ts
38/// await createShadowSkeleton(shadowPath);
39/// ```
40pub fn create_shadow_skeleton(shadow: &Path) -> std::io::Result<()> {
41    for relative in SKELETON_DIRECTORIES {
42        match std::fs::create_dir(shadow.join(relative)) {
43            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
44            other => other?,
45        }
46    }
47    return std::fs::write(shadow.join("HEAD"), PLACEHOLDER_HEAD);
48}
49
50/// Skeleton controls stay out of the release executable.
51#[cfg(test)]
52#[path = "shadow_skeleton_tests.rs"]
53mod tests;