@@ -4,6 +4,7 @@ use crate::common::{
44use std:: error:: Error ;
55use std:: fs;
66use std:: path:: Path ;
7+ use std:: process:: Command ;
78use std:: str;
89use 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