Skip to main content

git_policy_cli/
empty_tree.rs

1//! What:
2//!  The object name of the empty tree under each object format Git supports,
3//!  known without asking Git.
4//! Why:
5//!  The invocation capture needs the empty tree as the baseline of an unborn branch.
6//!  Git hashes the framed bytes `tree 0` followed by a NUL byte,
7//!  so the name depends on the object format alone and never on the repository's contents.
8//!  Spawning `git hash-object -t tree --stdin` for a constant cost a process on every commit.
9//!
10//! In TS you'd write (pseudocode):
11//! ```ts
12//! const EMPTY_TREE = { sha1: '4b825d...', sha256: '6ef19b...' } as const;
13//! ```
14
15/// The empty tree under SHA-1,
16///  the object format of nearly every repository.
17pub const SHA1_EMPTY_TREE: &str = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
18
19/// The empty tree under SHA-256.
20pub const SHA256_EMPTY_TREE: &str = "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321";
21
22/// What:
23///  The empty tree's object name for an object format as `git rev-parse --show-object-format` spells it,
24///  or `None` for a format this table does not know.
25/// Why:
26///   `None` sends the caller to ask Git,
27///  so a future object format is answered by Git and never guessed here.
28///
29/// In TS you'd write (pseudocode):
30/// ```ts
31/// function emptyTreeName(objectFormat: string): string | undefined;
32/// ```
33pub fn empty_tree_name(object_format: &str) -> Option<&'static str> {
34    return match object_format {
35        "sha1" => Some(SHA1_EMPTY_TREE),
36        "sha256" => Some(SHA256_EMPTY_TREE),
37        _ => None,
38    };
39}
40
41/// Empty-tree controls stay out of the release executable.
42#[cfg(test)]
43#[path = "empty_tree_tests.rs"]
44mod tests;