fix(lib.sh): avoid cd side-effect in git_commit/git_dirty and safe JSON in write_metadata_json

This commit is contained in:
yy-fighting 2026-07-22 03:44:18 +00:00
parent b6dab8e139
commit 489c5a5e61

View File

@ -63,17 +63,18 @@ health_check() {
# -----------------------------------------------------------------------------
git_commit() {
cd "$ROOT_DIR" || return 1
git rev-parse --short HEAD 2>/dev/null || echo "unknown"
( cd "$ROOT_DIR" && git rev-parse --short HEAD 2>/dev/null || echo "unknown" )
}
git_dirty() {
cd "$ROOT_DIR" || return 1
(
cd "$ROOT_DIR" || exit 1
if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then
echo "dirty"
else
echo "clean"
fi
)
}
# -----------------------------------------------------------------------------
@ -96,28 +97,34 @@ write_metadata_json() {
mkdir -p "$(dirname "$output_path")"
cat > "$output_path" <<EOF
{
"${PYTHON:-python3}" - "$output_path" "$experiment" "$run_id" "$model" "$backend" "$engine" "$hardware" "$accelerator" "$chip" "$script" "$env_path" "$description" "$(date --iso-8601=seconds)" "$(git_commit)" "$(git_dirty)" <<'PY'
import json, sys
(out, experiment, run_id, model, backend, engine, hardware, accelerator,
chip, script, env_path, description, timestamp, git_commit,
git_dirty) = sys.argv[1:16]
data = {
"metadata": {
"experiment": "${experiment}",
"run_id": "${run_id}",
"timestamp": "$(date --iso-8601=seconds)",
"model": "${model}",
"backend": "${backend}",
"engine": "${engine}",
"hardware": "${hardware}",
"accelerator": "${accelerator}",
"chip": "${chip}",
"script": "${script}",
"env": "${env_path}",
"git_commit": "$(git_commit)",
"git_dirty": "$(git_dirty)",
"description": "${description}"
"experiment": experiment,
"run_id": run_id,
"timestamp": timestamp,
"model": model,
"backend": backend,
"engine": engine,
"hardware": hardware,
"accelerator": accelerator,
"chip": chip,
"script": script,
"env": env_path,
"git_commit": git_commit,
"git_dirty": git_dirty,
"description": description,
},
"config": {},
"scenarios": []
"scenarios": [],
}
EOF
with open(out, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
PY
}
# -----------------------------------------------------------------------------