use std::path::{Path, PathBuf};
use time::OffsetDateTime;
use crate::json::policy_file::find_git_root;
pub(crate) fn build_meta() -> crate::json::JsonMeta {
let source_root = source_root();
#[cfg(feature = "hotpath-cloud-meta")]
if source_root.is_none()
&& crate::lib_on::cloud::enabled()
&& crate::lib_on::locations::any_relative_file().is_some()
{
eprintln!(
"hotpath: could not resolve the source checkout from the working directory; \
set HOTPATH_META_SOURCE_ROOT to the workspace path relative to the repo root \
(source links will be unavailable in this report)"
);
}
cfg_if::cfg_if! {
if #[cfg(feature = "hotpath-cloud-meta")] {
let ci = crate::lib_on::ci_info::detect();
let git_root = checkout_git_root();
let local = git_root
.as_deref()
.and_then(crate::lib_on::git_info::read_git_info_at);
let git = merge_git_info(local, ci.as_ref());
let benchmark = crate::lib_on::cloud::benchmark_name().ok();
let ci = ci.map(|ci| ci.ci);
let policy = checkout_policy(
policy_root(git_root).as_deref(),
benchmark.as_deref(),
ci.is_some(),
);
} else {
let git: Option<crate::json::JsonGitInfo> = None;
let ci: Option<crate::json::JsonCiInfo> = None;
let benchmark: Option<String> = None;
let policy: Option<crate::json::JsonPolicy> = None;
}
}
crate::json::JsonMeta {
rustc: env!("HOTPATH_META_RUSTC_VERSION").to_string(),
os: format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH),
created_at: now_whole_seconds(),
source_root,
git,
ci,
benchmark,
policy,
}
}
#[cfg(feature = "hotpath-cloud-meta")]
fn checkout_git_root() -> Option<PathBuf> {
if std::env::var("HOTPATH_META_SOURCE_ROOT").is_ok() {
find_git_root(&std::env::current_dir().ok()?)
} else {
Some(resolve_checkout()?.git_root)
}
}
#[cfg(feature = "hotpath-cloud-meta")]
fn policy_root(checkout_git_root: Option<PathBuf>) -> Option<PathBuf> {
checkout_git_root.or_else(|| find_git_root(&std::env::current_dir().ok()?))
}
#[cfg(feature = "hotpath-cloud-meta")]
fn checkout_policy(
git_root: Option<&Path>,
benchmark: Option<&str>,
in_ci: bool,
) -> Option<crate::json::JsonPolicy> {
use crate::json::policy_file::PolicyLookup;
match crate::json::policy_file::lookup(git_root, benchmark) {
PolicyLookup::NotFound => {
let uploading = crate::lib_on::cloud::enabled();
if git_root.is_none() && (uploading || in_ci) {
eprintln!(
"hotpath: no git repository found from the working directory, so no policy \
file was looked for."
);
}
if in_ci && !uploading {
eprintln!(
"hotpath: {}",
crate::lib_on::cloud::missing_policy_message(benchmark)
);
}
None
}
PolicyLookup::Found(policy) => Some(policy),
PolicyLookup::Unusable(unusable) => {
eprintln!("hotpath: {unusable} The report carries no policy.");
None
}
}
}
#[cfg(feature = "hotpath-cloud-meta")]
fn merge_git_info(
local: Option<crate::json::JsonGitInfo>,
ci: Option<&crate::lib_on::ci_info::CiContext>,
) -> Option<crate::json::JsonGitInfo> {
let Some(ci) = ci else {
return local;
};
let (sha, local_ref, repository) = match local {
Some(git) => (Some(git.sha), git.r#ref, git.repository),
None => (None, None, None),
};
let sha = sha.or_else(|| ci.sha.clone())?;
let describes_checkout = ci.sha.as_deref() == Some(sha.as_str());
Some(crate::json::JsonGitInfo {
r#ref: if describes_checkout {
ci.r#ref.clone().or(local_ref)
} else {
local_ref
},
base_sha: ci.base_sha.clone().filter(|base| base != &sha),
repository: repository.or_else(|| ci.repository.clone()),
sha,
})
}
fn source_root() -> Option<String> {
if let Ok(v) = std::env::var("HOTPATH_META_SOURCE_ROOT") {
return Some(v);
}
let checkout = resolve_checkout()?;
let rel = checkout
.workspace_root
.strip_prefix(&checkout.git_root)
.ok()?;
Some(rel.to_string_lossy().replace('\\', "/"))
}
struct ResolvedCheckout {
git_root: PathBuf,
workspace_root: PathBuf,
}
fn resolve_checkout() -> Option<ResolvedCheckout> {
let cwd = std::env::current_dir().ok()?;
let probe = crate::lib_on::locations::any_relative_file()?;
let workspace_root = cwd
.ancestors()
.find(|dir| dir.join(probe).is_file())
.map(Path::to_path_buf)?;
let git_root = find_git_root(&workspace_root)?;
Some(ResolvedCheckout {
git_root,
workspace_root,
})
}
fn now_whole_seconds() -> OffsetDateTime {
let now = OffsetDateTime::now_utc();
now.replace_nanosecond(0).unwrap_or(now)
}
#[cfg(test)]
mod tests {
use crate::lib_on::report_meta::now_whole_seconds;
use time::format_description::well_known::Rfc3339;
#[test]
fn created_at_is_utc_whole_seconds() {
let now = now_whole_seconds();
assert!(now.offset().is_utc());
let wire = now.format(&Rfc3339).unwrap();
assert!(wire.ends_with('Z') && !wire.contains('.'), "{wire}");
}
#[cfg(feature = "hotpath-cloud-meta")]
mod cloud {
use crate::json::{JsonCiInfo, JsonGitInfo, JsonMeta, JsonPolicy, JsonPullRequest};
use crate::lib_on::ci_info::CiContext;
use crate::lib_on::report_meta::merge_git_info;
use time::macros::datetime;
const LOCAL_SHA: &str = "1111111111111111111111111111111111111111";
const CI_SHA: &str = "2222222222222222222222222222222222222222";
const BASE_SHA: &str = "3333333333333333333333333333333333333333";
const HEAD_SHA: &str = "4444444444444444444444444444444444444444";
fn detached_local(sha: &str) -> JsonGitInfo {
JsonGitInfo {
sha: sha.to_string(),
r#ref: None,
base_sha: None,
repository: Some("pawurb/hotpath-rs".to_string()),
}
}
fn pull_request_ci(sha: &str) -> CiContext {
CiContext {
ci: JsonCiInfo {
provider: "github-actions".to_string(),
event: "pull_request".to_string(),
pull_request: Some(JsonPullRequest {
number: 42,
base_ref: "main".to_string(),
head_ref: "feature-x".to_string(),
head_sha: Some(HEAD_SHA.to_string()),
}),
..JsonCiInfo::default()
},
sha: Some(sha.to_string()),
r#ref: Some("refs/pull/42/merge".to_string()),
repository: Some("pawurb/other-name".to_string()),
base_sha: Some(BASE_SHA.to_string()),
}
}
#[test]
fn env_describes_the_commit_it_checked_out() {
let git = merge_git_info(
Some(detached_local(LOCAL_SHA)),
Some(&pull_request_ci(LOCAL_SHA)),
)
.expect("git info");
assert_eq!(git.sha, LOCAL_SHA);
assert_eq!(git.r#ref.as_deref(), Some("refs/pull/42/merge"));
assert_eq!(git.base_sha.as_deref(), Some(BASE_SHA));
assert_eq!(git.repository.as_deref(), Some("pawurb/hotpath-rs"));
}
#[test]
fn head_checkout_drops_the_env_ref_but_keeps_the_base() {
let git = merge_git_info(
Some(detached_local(LOCAL_SHA)),
Some(&pull_request_ci(CI_SHA)),
)
.expect("git info");
assert_eq!(git.sha, LOCAL_SHA);
assert_eq!(git.r#ref, None);
assert_eq!(git.base_sha.as_deref(), Some(BASE_SHA));
}
#[test]
fn base_checkout_drops_the_base() {
let git = merge_git_info(
Some(detached_local(BASE_SHA)),
Some(&pull_request_ci(CI_SHA)),
)
.expect("git info");
assert_eq!(git.sha, BASE_SHA);
assert_eq!(git.base_sha, None);
}
#[test]
fn checkout_fills_gaps_the_provider_leaves() {
let bare_ci = CiContext {
ci: JsonCiInfo {
provider: "github-actions".to_string(),
..JsonCiInfo::default()
},
sha: None,
r#ref: None,
repository: None,
base_sha: None,
};
let local = JsonGitInfo {
r#ref: Some("refs/heads/main".to_string()),
..detached_local(LOCAL_SHA)
};
let git = merge_git_info(Some(local), Some(&bare_ci)).expect("git info");
assert_eq!(git.sha, LOCAL_SHA);
assert_eq!(git.r#ref.as_deref(), Some("refs/heads/main"));
assert_eq!(git.repository.as_deref(), Some("pawurb/hotpath-rs"));
}
#[test]
fn ci_alone_still_yields_git_info() {
let git = merge_git_info(None, Some(&pull_request_ci(CI_SHA))).expect("git info");
assert_eq!(git.sha, CI_SHA);
assert_eq!(git.r#ref.as_deref(), Some("refs/pull/42/merge"));
assert_eq!(git.base_sha.as_deref(), Some(BASE_SHA));
assert_eq!(git.repository.as_deref(), Some("pawurb/other-name"));
assert!(merge_git_info(None, None).is_none());
}
#[test]
fn absent_fields_add_no_keys() {
let meta = JsonMeta {
rustc: "1.89.0".to_string(),
os: "macos-aarch64".to_string(),
created_at: datetime!(2026-08-27 10:15:42 UTC),
source_root: Some(String::new()),
git: Some(detached_local(LOCAL_SHA)),
ci: None,
benchmark: None,
policy: None,
};
let value: serde_json::Value = serde_json::to_value(&meta).unwrap();
let keys = |value: &serde_json::Value| -> Vec<String> {
let mut keys: Vec<String> = value.as_object().unwrap().keys().cloned().collect();
keys.sort();
keys
};
assert_eq!(
keys(&value),
["created_at", "git", "os", "rustc", "source_root"]
);
assert_eq!(keys(&value["git"]), ["repository", "sha"]);
}
#[test]
fn new_fields_round_trip() {
let meta = JsonMeta {
rustc: "1.89.0".to_string(),
os: "macos-aarch64".to_string(),
created_at: datetime!(2026-08-27 10:15:42 UTC),
source_root: Some(String::new()),
git: merge_git_info(
Some(detached_local(LOCAL_SHA)),
Some(&pull_request_ci(LOCAL_SHA)),
),
ci: Some(pull_request_ci(LOCAL_SHA).ci),
benchmark: Some("ci".to_string()),
policy: Some(JsonPolicy {
source: "# ours\n[functions.timing]\n".to_string(),
path: "hotpath/ci-policy.toml".to_string(),
}),
};
let sent = meta.policy.clone();
let json = serde_json::to_string(&meta).unwrap();
let back: JsonMeta = serde_json::from_str(&json).unwrap();
let git = back.git.expect("git info");
assert_eq!(git.sha, LOCAL_SHA);
assert_eq!(git.base_sha.as_deref(), Some(BASE_SHA));
assert_eq!(git.repository.as_deref(), Some("pawurb/hotpath-rs"));
let ci = back.ci.expect("ci info");
assert_eq!(ci.provider, "github-actions");
assert_eq!(ci.event, "pull_request");
let pr = ci.pull_request.expect("pull request info");
assert_eq!(pr.number, 42);
assert_eq!(pr.base_ref, "main");
assert_eq!(pr.head_ref, "feature-x");
assert_eq!(pr.head_sha.as_deref(), Some(HEAD_SHA));
assert_eq!(back.benchmark.as_deref(), Some("ci"));
assert_eq!(back.policy, sent);
}
#[test]
fn old_report_deserializes() {
let json = r#"{
"rustc": "1.89.0",
"os": "macos-aarch64",
"created_at": "2026-08-27T10:15:42Z",
"source_root": "",
"git": {"sha": "1111111111111111111111111111111111111111", "ref": "refs/heads/main"}
}"#;
let meta: JsonMeta = serde_json::from_str(json).unwrap();
let git = meta.git.expect("git info");
assert_eq!(git.base_sha, None);
assert_eq!(git.repository, None);
assert!(meta.ci.is_none());
assert!(meta.benchmark.is_none());
assert!(meta.policy.is_none());
}
}
}