diff --git a/Cargo.toml b/Cargo.toml index 98dfa56..42bdb34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,6 @@ tempfile = "3.12.0" tokio = { version = "1.40.0", features = ["full"] } tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } + +[build-dependencies] +color-eyre = "0.6.3" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..7fa10c1 --- /dev/null +++ b/build.rs @@ -0,0 +1,35 @@ +fn main() { + // Always rerun. + + let version = if let Ok(commit) = try_get_commit() { + match has_no_changes() { + Ok(true) => commit, + Ok(false) => format!("{commit} (*)"), + Err(_) => format!("{commit} (?)"), + } + } else { + "unknown".into() + }; + + println!("cargo:rustc-env=GIT_COMMIT={version}"); +} + +fn try_get_commit() -> color_eyre::Result { + let stdout = std::process::Command::new("git") + .arg("rev-parse") + .arg("HEAD") + .output()? + .stdout; + + let stdout = String::from_utf8(stdout)?; + + Ok(stdout.trim()[0..8].to_owned()) +} + +fn has_no_changes() -> color_eyre::Result { + Ok(std::process::Command::new("git") + .args(["diff", "--no-ext-diff", "--quiet", "--exit-code"]) + .output()? + .status + .success()) +} diff --git a/src/main.rs b/src/main.rs index b4921af..853a8f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ mod web; use color_eyre::{eyre::WrapErr, Result}; use db::Db; +const VERSION: &str = env!("GIT_COMMIT"); + #[tokio::main] async fn main() -> Result<()> { tracing_subscriber::fmt().init(); diff --git a/src/web.rs b/src/web.rs index 6b3df9a..ed32751 100644 --- a/src/web.rs +++ b/src/web.rs @@ -25,14 +25,14 @@ pub async fn webserver(db: Db) -> Result<()> { .route("/trigger-build", post(trigger_build)) .with_state(AppState { db }); - info!("Serving website on port 3000"); + info!("Serving website on port 3000 (commit {})", crate::VERSION); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.wrap_err("failed to serve") } async fn root() -> impl IntoResponse { - Html(include_str!("../static/index.html")) + Html(include_str!("../static/index.html").replace("{{version}}", crate::VERSION)) } #[derive(Deserialize)] @@ -52,6 +52,7 @@ async fn build(State(state): State, Query(query): Query) - .replace("{{nightly}}", &query.nightly) .replace("{{target}}", &query.target) .replace("{{stderr}}", &build.stderr) + .replace("{{version}}", crate::VERSION) .replace("{{status}}", &build.status.to_string()); Html(page).into_response() diff --git a/static/build.html b/static/build.html index 520038d..8504e91 100644 --- a/static/build.html +++ b/static/build.html @@ -3,7 +3,7 @@ - Build + Build {{nightly}} {{target}}