qua 0.74.0

Structure-aware query CLI for the Quarb engine
//! A dev build knows the commit it was made at, and the root word
//! `version` says it (`qua version`): two dev builds of one version
//! are told apart by asking them, and one made of a tree with
//! changes carries a `+` after the hash. A release build says none,
//! and neither does a build outside a repository (a crate from the
//! registry).

use std::path::Path;
use std::process::Command;

fn git(dir: &str, args: &[&str]) -> Option<String> {
    let out = Command::new("git")
        .arg("-C")
        .arg(dir)
        .args(args)
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8(out.stdout).ok()?.trim().to_string();
    (!text.is_empty()).then_some(text)
}

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    let version = std::env::var("CARGO_PKG_VERSION").unwrap_or_default();
    let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
    if !version.ends_with("-dev") {
        return;
    }
    let Some(commit) = git(&dir, &["rev-parse", "--short=8", "HEAD"]) else {
        return;
    };
    // Run again when the head moves: its log gains a line at every
    // commit and every checkout (a work tree has its own). A file
    // that is not there would make every build run this again.
    for file in ["logs/HEAD", "HEAD"] {
        let Some(path) = git(&dir, &["rev-parse", "--git-path", file]) else {
            continue;
        };
        let path = Path::new(&dir).join(path);
        if path.is_file() {
            println!("cargo:rerun-if-changed={}", path.display());
            break;
        }
    }
    // A tree with changes is not its commit, and the mark says so
    // (`commit 21d8c909+`): a build of sources not yet committed
    // answered the parent's hash as if it were made of it. The mark
    // is what this script saw when it last ran, which is when the
    // head moved; a change made after that is not seen.
    let changed = git(&dir, &["status", "--porcelain", "--untracked-files=no"]).is_some();
    let commit = if changed {
        format!("{commit}+")
    } else {
        commit
    };
    println!("cargo:rustc-env=QUARB_BUILD_COMMIT={commit}");
}