Skip to content

Commit 533b8a6

Browse files
committed
feat(R): check README.Rmd changes are rendered
Adds the githook generated by usethis::use_readme_rmd() to pre-commits, ensuring any changes made to README.Rmd are properly synchronised to the rendered README.md file.
1 parent e145473 commit 533b8a6

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@
129129
minimum_pre_commit_version: 0.15.0
130130
description: 'Run lintr against R code'
131131

132+
- id: r-readme
133+
name: 'Generated README.Rmd'
134+
entry: run-r-readme.sh
135+
language: 'script'
136+
files: README\.Rmd$
137+
pass_filenames: false
138+
minimum_pre_commit_version: 0.15.0
139+
description: 'Check README.md in-sync with README.Rmd'
140+
132141
- id: buf-lint
133142
name: 'Lint protobuf files'
134143
entry: buf-lint.sh

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Hooks:
1919
* `rust-doc`: runs `cargo doc` against the workspace - great for linting [intra-doc links]
2020
* `r-stylr`: runs [`stylr`] to format R code
2121
* `r-lintr`: static analysis of R code with [`lintr`]
22+
* `r-readme`: ensure README.Rmd changes are rendered to README.md
2223
* `buf-lint`: runs [`buf`] lints against protobuf files
2324
* `buf-breaking`: protobuf breaking change detection using [`buf`]
2425

@@ -87,6 +88,8 @@ repos:
8788

8889
- id: r-lintr
8990
stages: [commit, push]
91+
92+
- id: r-readme
9093
stages: [commit, push]
9194

9295
- id: buf-lint

run-r-readme.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
README=($(git diff --cached --name-only | grep -Ei '^README\.[R]?md$'))
6+
7+
if [[ ${#README[@]} == 0 ]]; then
8+
exit 0
9+
fi
10+
11+
if [[ README.Rmd -nt README.md ]]; then
12+
echo -e "README.md is out of date; please re-knit README.Rmd"
13+
exit 1
14+
elif [[ ${#README[@]} -lt 2 ]]; then
15+
echo -e "README.Rmd and README.md should be both staged"
16+
exit 1
17+
fi

0 commit comments

Comments
 (0)