githttp-fs 1.15.4

A git-backed content management database served over HTTP
// githttp-fs
//
// Git-based Content Management System
// Copyright: 2026, Valerian Saliou <valerian@valeriansaliou.name>
// License: Mozilla Public License v2.0 (MPL v2.0)

//! The per-directory file-dates index: its reserved path and its stored
//! format.
//!
//! A file's creation and last-change dates are facts of its commit history,
//! but git keeps no index from a path to the commits that touched it, so
//! deriving them means walking history — a cost that grows with the
//! repository rather than with the request, and that no bound makes O(1).
//! They are therefore **recorded when they happen**: every commit this server
//! makes stamps the dates of the files it changed into the `.dates.json` of
//! their directory, in that same commit, and a read is one small blob read.
//!
//! It mirrors the file-order index (`order.rs`) wherever it can — what the
//! two share lives in `reserved.rs` — for the same
//! reasons: stored **per directory**, keyed by **leaf name**, so a commit
//! touches one small file per directory it changed, and a folder move carries
//! its indexes along with its subtree. What differs is ownership. The order
//! index is the caller's data, written through its own route and announced
//! by its own events; the dates index is the server's bookkeeping, written
//! only as a side effect of commits, never addressable, never announced, and
//! left out of every view of a commit (see `GitDates::stamp` in `git.rs`).
//!
//! Two rules keep it honest:
//!
//! - **Absence means unknown.** A file with no entry — one last written
//!   before the index existed, or committed by anything other than this
//!   server — reads as `null` dates, never as a guessed value. Likewise an
//!   entry's `created_at` is `null` when the file already existed, unindexed,
//!   at the time its first indexed change was recorded.
//! - **Malformed means absent.** Only this server writes the file, so a
//!   broken one can only come from a hand-edited commit, and that must not
//!   turn a read into an error.

use std::collections::BTreeMap;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::reserved;

/// Reserved leaf name of the dates index. Dot-prefixed like the order index,
/// and filtered out of every `/files` route regardless of
/// `include_hidden_files`.
pub const DATES_FILE_NAME: &str = ".dates.json";

/// One file's recorded dates. `created_at` is `None` when the file's creation
/// predates the index (see the module documentation).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct DatesEntry {
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
    #[serde(default)]
    pub updated_at: Option<DateTime<Utc>>,
}

/// A directory's index: leaf name to dates. A `BTreeMap` so the stored
/// document is name-sorted, which keeps the blob deterministic and its git
/// diffs minimal.
pub type DatesIndex = BTreeMap<String, DatesEntry>;

/// The stored document, deserialising side. An object rather than a bare
/// map so other server-recorded metadata can join it without a format
/// version bump.
#[derive(Deserialize)]
struct DatesDocument {
    files: DatesIndex,
}

/// The stored document, serialising side — borrows the index rather than
/// cloning it on every commit.
#[derive(Serialize)]
struct DatesDocumentRef<'a> {
    files: &'a DatesIndex,
}

/// Repo-root-relative path of the index for `directory` (`""` being the
/// repository root).
pub fn dates_file_path(directory: &str) -> String {
    reserved::index_file_path(directory, DATES_FILE_NAME)
}

/// Whether `path` names a dates index, at any depth.
pub fn is_dates_file(path: &str) -> bool {
    reserved::split_parent(path).1 == DATES_FILE_NAME
}

/// Serialises an index into the stored document, pretty-printed with a
/// trailing newline like the order index.
pub fn serialize(index: &DatesIndex) -> String {
    let mut document = serde_json::to_string_pretty(&DatesDocumentRef { files: index })
        .unwrap_or_else(|_err| "{\n  \"files\": {}\n}".to_string());

    document.push('\n');

    document
}

/// Parses a stored document, or `None` when it cannot be read as one.
pub fn parse(content: &[u8]) -> Option<DatesIndex> {
    serde_json::from_slice::<DatesDocument>(content)
        .ok()
        .map(|document| document.files)
}