Skip to content

Commit a1db7e4

Browse files
committed
mdbook: add build info, like we do on Pan Docs (#164)
1 parent 2db4f6d commit a1db7e4

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

preproc/src/git.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* This Source Code Form is subject to the
3+
* terms of the Mozilla Public License, v.
4+
* 2.0. If a copy of the MPL was not
5+
* distributed with this file, You can
6+
* obtain one at
7+
* http://mozilla.org/MPL/2.0/.
8+
*/
9+
10+
use std::process::{Command, Stdio};
11+
12+
use anyhow::Error;
13+
14+
#[derive(Debug)]
15+
pub struct Commit {
16+
info: String,
17+
splits: [usize; 2],
18+
}
19+
20+
impl Commit {
21+
pub fn rev_parse(what: &str) -> Result<Self, Error> {
22+
let output = Command::new("git")
23+
.args([
24+
"--git-dir=.git",
25+
"show",
26+
"-s",
27+
"--format=%H%x00%h%x00%ci",
28+
what,
29+
])
30+
.stderr(Stdio::inherit())
31+
.stdin(Stdio::null())
32+
.output()
33+
.expect("Failed to get commit info");
34+
if !output.status.success() {
35+
return Err(Error::msg(format!(
36+
"Git exited with status {} while getting commit info",
37+
output.status
38+
)));
39+
}
40+
let mut info = String::from_utf8(output.stdout).expect("Commit info is not valid UTF-8??");
41+
let trimmed_len = info.trim_end().len();
42+
info.truncate(trimmed_len);
43+
44+
let first_split = info
45+
.find('\0')
46+
.expect("Failed to split hash and short hash");
47+
let second_split = info[first_split + 1..]
48+
.find('\0')
49+
.expect("Failed to split short hash and timestamp")
50+
+ first_split
51+
+ 1;
52+
53+
Ok(Self {
54+
info,
55+
splits: [first_split, second_split],
56+
})
57+
}
58+
59+
pub fn hash(&self) -> &str {
60+
&self.info[..self.splits[0]]
61+
}
62+
63+
pub fn short_hash(&self) -> &str {
64+
&self.info[self.splits[0] + 1..self.splits[1]]
65+
}
66+
67+
pub fn timestamp(&self) -> &str {
68+
&self.info[self.splits[1] + 1..]
69+
}
70+
}

preproc/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use std::io;
1414
use std::process;
1515

1616
mod admonitions;
17+
mod git;
18+
mod links;
1719
mod preproc;
1820
use preproc::GbAsmTut;
19-
mod links;
2021

2122
pub fn make_app() -> App<'static, 'static> {
2223
App::new("pandocs-preproc")

preproc/src/preproc.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* http://mozilla.org/MPL/2.0/.
88
*/
99

10+
use crate::git::Commit;
1011
use crate::links;
1112
use anyhow::Result;
1213
use mdbook::book::{Book, BookItem};
@@ -33,6 +34,12 @@ impl Preprocessor for GbAsmTut {
3334
fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
3435
let src_dir = ctx.root.join(&ctx.config.book.src);
3536

37+
let commit = if ctx.root.join(".git").exists() {
38+
Some(Commit::rev_parse("HEAD")?)
39+
} else {
40+
None
41+
};
42+
3643
let mut res = Ok(());
3744
book.for_each_mut(|section: &mut BookItem| {
3845
if res.is_err() {
@@ -50,6 +57,17 @@ impl Preprocessor for GbAsmTut {
5057
if let Err(err) = self.process_admonitions(ch) {
5158
res = Err(err);
5259
}
60+
61+
if ch.name == "Home" {
62+
if let Some(ref commit) = commit {
63+
ch.content.push_str(&format!(
64+
"\n\n\n <small>This document version was produced from git commit [`{}`](https://github.com/gbdev/gb-asm-tutorial/tree/{}) ({}).</small>\n",
65+
commit.short_hash(),
66+
commit.hash(),
67+
commit.timestamp(),
68+
));
69+
}
70+
}
5371
}
5472
}
5573
});

0 commit comments

Comments
 (0)