Skip to content

Commit 3194823

Browse files
committed
Print commit hash to console in wasm demo
1 parent b1558e0 commit 3194823

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

crates/aoc_wasm/web/web.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Aoc} from "./aoc.mjs";
22

3+
console.log("Commit", "${GIT_COMMIT}");
4+
35
const MODULE_PATHS = [
46
"./aoc-simd128.wasm",
57
"./aoc.wasm",

crates/xtask/src/cmd/web.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::common::{
44
use std::error::Error;
55
use std::fs;
66
use std::path::Path;
7+
use std::process::Command;
78
use std::str;
89
use utils::md5;
910

@@ -23,7 +24,14 @@ pub fn main(args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
2324
run_cargo(&["clean", "--doc"], &[])?;
2425
run_cargo(&["doc", "--no-deps"], &[])?;
2526

26-
let mut rewrites = vec![];
27+
let mut rewrites = vec![(
28+
"\"${GIT_COMMIT}\"".to_string(),
29+
get_commit_commit().unwrap_or_else(|e| {
30+
println!("failed to get git commit: {e}");
31+
"\"unknown\"".to_string()
32+
}),
33+
)];
34+
2735
for (env, extra_args, name) in [
2836
(&[][..], &[][..], "aoc.wasm"),
2937
(
@@ -123,3 +131,29 @@ fn add_rewrite(rewrites: &mut Vec<(String, String)>, path: &Path) -> Result<(),
123131

124132
Ok(())
125133
}
134+
135+
fn get_commit_commit() -> Result<String, Box<dyn Error>> {
136+
let hash = {
137+
let output = Command::new("git").args(["rev-parse", "HEAD"]).output()?;
138+
if !output.status.success() {
139+
return Err(format!("'git rev-parse HEAD' exited with {}", output.status).into());
140+
}
141+
String::from_utf8(output.stdout)?
142+
};
143+
144+
let modified = {
145+
let output = Command::new("git")
146+
.args(["status", "--porcelain"])
147+
.output()?;
148+
if !output.status.success() {
149+
return Err(format!("'git status' exited with {}", output.status).into());
150+
}
151+
!output.stdout.is_empty()
152+
};
153+
154+
if modified {
155+
Ok(format!("\"{} (modified)\"", hash.trim()))
156+
} else {
157+
Ok(format!("\"{}\"", hash.trim()))
158+
}
159+
}

0 commit comments

Comments
 (0)