Skip to content

Commit ec7d35d

Browse files
authored
Accomodate out-of-repo builds (#523)
1 parent e9562d7 commit ec7d35d

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

preproc/src/main.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
use std::collections::HashMap;
1111
use std::io;
12+
use std::path::Path;
1213
use std::process;
1314

14-
use anyhow::Context;
15+
use anyhow::{anyhow, Context};
1516
use clap::{App, Arg, ArgMatches, SubCommand};
1617
use mdbook::book::Book;
1718
use mdbook::errors::Error;
@@ -97,7 +98,9 @@ impl Preprocessor for Pandocs {
9798
renderer != "not-supported"
9899
}
99100

100-
fn run(&self, _: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
101+
fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
102+
let config = ctx.config.get_preprocessor("pandocs");
103+
101104
let mut sections = HashMap::new();
102105
for item in book.iter() {
103106
if let BookItem::Chapter(ref chapter) = item {
@@ -130,11 +133,20 @@ impl Preprocessor for Pandocs {
130133
abort_if_err!(self.process_admonitions(chapter));
131134

132135
if chapter.name == "Foreword" {
133-
let commit = abort_if_err!(Commit::rev_parse("HEAD"));
134-
chapter.content.push_str(&format!(
135-
"<small>This document version was produced from git commit [`{}`](https://github.com/gbdev/pandocs/tree/{}) ({}). </small>",
136-
commit.short_hash(), commit.hash(), commit.timestamp(),
137-
));
136+
// If the `.git` directory exists, we're very likely on a dev machine,
137+
// so it's safe to assume the command is installed.
138+
if Path::new(".git").exists() {
139+
let commit = abort_if_err!(Commit::rev_parse("HEAD"));
140+
chapter.content.push_str(&format!(
141+
"<small>This document version was produced from git commit [`{}`](https://github.com/gbdev/pandocs/tree/{}) ({}).</small>",
142+
commit.short_hash(), commit.hash(), commit.timestamp(),
143+
));
144+
} else if matches!(config.and_then(|config| config.get("out-of-repo")).and_then(|value| value.as_bool()), Some(true)) {
145+
// OK, just don't add anything.
146+
} else {
147+
res = Err(anyhow!("Git metadata is missing, but out-of-repo builds are not enabled!\n\tYou can enable them by setting `preprocessor.pandocs.out-of-repo` to `true`.\n\t(Consider using an environment variable for this:\n\t https://rust-lang.github.io/mdBook/format/configuration/environment-variables.html)"));
148+
return;
149+
}
138150
}
139151
}
140152
});

0 commit comments

Comments
 (0)