An mdBook preprocessor that injects Git metadata (e.g., commit hash, date, tag) into your rendered book. This is useful for displaying build provenance or version information.
Features
- Injects the latest Git commit information for each/per chapter/subchapter.
- Fully configurable output format using template variables.
- Supports date and time formatting.
- Renders a styled footer below each chapter with Git metadata.
- Supports the
htmlrenderer.
Installation
From package manager:
Clone this repository and install it using Cargo:
Make sure the binary (mdbook-gitinfo) is in your PATH.
Configuration
Add the following to your book.toml:
[]
= true
= "Date: {{date}}{{sep}}branch: {{branch}}{{sep}}commit: {{hash}}"
= " • "
= "0.8em"
= "%Y-%m-%d"
= "%H:%M:%S"
= "main" # default is main, therefore optional
Example Output
With the above configuration, this footer will be injected:
Date: 2025-06-23 16:19:28 • branch: main • commit: 2160ec5
[!NOTE] date and time formatting use chrono format specifiers
Format Meaning Example Format Meaning Example %YYear with century 2025%HHour (00-23) 14%yYear without century (00-99) 25%IHour (01-12) 02%mMonth number (01-12) 06%pAM/PM PM%bAbbreviated month name Jun%MMinute (00-59) 05%BFull month name June%SSecond (00-60, leap-sec aware) 09%dDay of month (01-31) 24%fMicroseconds (000000-999999) 123456%eDay of month, space-padded 24%z+hhmm timezone offset +0100%aAbbreviated weekday Mon%:z+hh:mm timezone offset +01:00%AFull weekday name Monday%ZTime zone name BST%jDay of year (001–366) 176
Template Variables
You can use the following placeholders in the template string:
-
{{hash}}: Short commit hash (git rev-parse --short HEAD) -
{{long}}: Full commit hash -
{{tag}}: Git tag (fromgit describe) -
{{date}}: Timestamp of the latest commit, formatted -
{{sep}}: Separator (defaults to " • ")
.github/workflow/...
In order for mdbook-gitinfo to reference the correct commit whilst using actions/checkout@v4, set fetch-depth as 0:
...
jobs:
deploy:
runs-on: ubuntu-22.04
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
...
Compatibility
-
mdbook-gitinfois tested with mdbook 0.4.x. -
Only the html renderer is supported.