Skip to content

Commit 868e2fa

Browse files
committed
feat: preprocessor now has branch
- default main branch is selected - can specifiy branch in the preprocessor - branch can appear in the footer template too - added workflow example in README.md
1 parent ea24441 commit 868e2fa

6 files changed

Lines changed: 73 additions & 16 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mdbook-gitinfo"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2024"
55
authors = ["Seb Blair <s.blair@gre.ac.uk>"]
66
description = "An mdBook preprocessor to inject Git commit metadata into generated books"

README.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,25 @@ Add the following to your book.toml:
5353
```toml
5454
[preprocessor.gitinfo]
5555
enable = true
56-
template = "Date: {{date}}{{sep}}commit: {{hash}}"
56+
template = "Date: {{date}}{{sep}}branch: {{branch}}{{sep}}commit: {{hash}}"
5757
separator = ""
5858
font-size = "0.8em"
5959
date-format = "%Y-%m-%d"
6060
time-format = "%H:%M:%S"
61+
branch = "main" # default is main, therefore optional
62+
```
63+
64+
65+
### Example Output
66+
67+
With the above configuration, this footer will be injected:
68+
69+
```html
70+
<footer>
71+
<span class="gitinfo-footer" style="font-size:0.8em;...">
72+
Date: 2025-06-23 16:19:28 • branch: main • commit: 2160ec5
73+
</span>
74+
</footer>
6175
```
6276

6377
> [!NOTE]
@@ -101,16 +115,22 @@ You can use the following placeholders in the template string:
101115

102116
- `{{sep}}`: Separator (defaults to " • ")
103117

104-
## Example Output
105-
106-
With the above configuration, this footer will be injected:
107-
108-
```html
109-
<footer>
110-
<span class="gitinfo-footer" style="font-size:0.8em;...">
111-
Date: 2025-06-23 16:19:28 • commit: 2160ec5
112-
</span>
113-
</footer>
118+
## .github/workflow/...
119+
120+
In order for mdbook-gitinfo to reference the correct commit whilst using `actions/checkout@v4`, set `fetch-depth` as `0`:
121+
122+
```yml
123+
...
124+
jobs:
125+
deploy:
126+
runs-on: ubuntu-22.04
127+
concurrency:
128+
group: ${{ github.workflow }}-${{ github.ref }}
129+
steps:
130+
- uses: actions/checkout@v4
131+
with:
132+
fetch-depth: 0
133+
...
114134
```
115135

116136
## Compatibility

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ pub struct GitInfoConfig {
3939
/// Default: `"%H:%M:%S"`.
4040
#[serde(rename = "time-format")]
4141
pub time_format: Option<String>,
42+
43+
/// Set the branch from where to get the commit history from
44+
/// Default: "main"
45+
pub branch: Option<String>,
4246
}
4347

4448
/// Load and deserialize the `[preprocessor.gitinfo]` table from `book.toml`.

src/git.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ where
6262
}
6363
}
6464

65+
/// Verify the branch exists
66+
///
67+
/// # Arguments
68+
/// * `branch`: &str representing the branches name
69+
/// * `dir` - The path to the Git repository root or working directory to execute from.
70+
///
71+
/// # Returns
72+
/// * bool: true or false with respect to whether a branch exists locally or not.
73+
///
74+
/// # Example
75+
///
76+
/// ```no_run
77+
/// use std::path::Path;
78+
/// use mdbook_gitinfo::git::verify_branch;
79+
///
80+
/// if !git::verify_branch(&branch, &ctx.root) {
81+
/// eprintln!("Warning: Branch '{}' not found, falling back to 'main'", branch);
82+
/// branch = "main".to_string();
83+
/// }
84+
pub fn verify_branch(branch: &str, dir: &Path) -> bool {
85+
get_git_output(["rev-parse", "--verify", branch], dir).is_ok()
86+
}
87+
6588
#[cfg(test)]
6689
mod tests {
6790
use super::*;

src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::{io, process};
1818
/// The `GitInfo` struct implements the `Preprocessor` trait for injecting Git metadata.
1919
///
2020
/// It reads settings from `[preprocessor.gitinfo]` in `book.toml`, including optional fields like
21-
/// `template`, `font-size`, `separator`, `date-format`, and `time-format`.
21+
/// `template`, `font-size`, `separator`, `date-format`, `time-format` and `branch`.
2222
pub struct GitInfo;
2323

2424
impl GitInfo {
@@ -45,6 +45,13 @@ impl Preprocessor for GitInfo {
4545
let separator = cfg.separator.unwrap_or_else(|| " • ".to_string());
4646
let date_format = cfg.date_format.unwrap_or_else(|| "%Y-%m-%d".to_string());
4747
let time_format = cfg.time_format.unwrap_or_else(|| "%H:%M:%S".to_string());
48+
let mut branch = cfg.branch.unwrap_or_else(|| "main".to_string());
49+
50+
// Verify the branch exists
51+
if !git::verify_branch(&branch, &ctx.root) {
52+
eprintln!("Warning: Branch '{}' not found, falling back to 'main'", branch);
53+
branch = "main".to_string();
54+
}
4855

4956
let content_dir = ctx.config.book.src.clone();
5057

@@ -57,12 +64,12 @@ impl Preprocessor for GitInfo {
5764

5865
// Configurable rendering options
5966
let short_hash = git::get_git_output(
60-
["log", "-1", "--format=%h", "--", &path_str],
67+
["log", "-1", "--format=%h", &format!("{branch}"), "--", &path_str],
6168
&ctx.root,
6269
)
6370
.unwrap_or_default();
6471
let long_hash = git::get_git_output(
65-
["log", "-1", "--format=%H", "--", &path_str],
72+
["log", "-1", "--format=%H", &format!("{branch}"), "--", &path_str],
6673
&ctx.root,
6774
)
6875
.unwrap_or_default();
@@ -103,6 +110,7 @@ impl Preprocessor for GitInfo {
103110
&tag,
104111
&formatted_date,
105112
&separator,
113+
&branch,
106114
);
107115

108116
// Inline style for visibility control
@@ -198,13 +206,15 @@ pub fn render_template(
198206
tag: &str,
199207
date: &str,
200208
sep: &str,
209+
branch: &str,
201210
) -> String {
202211
template
203212
.replace("{{hash}}", hash)
204213
.replace("{{long}}", long_hash)
205214
.replace("{{tag}}", tag)
206215
.replace("{{date}}", date)
207216
.replace("{{sep}}", sep)
217+
.replace("{{branch}}", branch)
208218
}
209219

210220
fn decorate_chapters<F>(item: &mut BookItem, decorate: &F)

0 commit comments

Comments
 (0)