Pre-Commit Hook for Processing README.Rmd

When writing an R package I usually create a README.Rmd file that I render to I use {pkgdown} to then create documentation. I run the last step via CI, so once it’s set up I never need to think about it again.

The problem is that I regularly forget to process the README.Rmd file, which means that despite keeping that up to date, everything else lags behind.

What if I automated the process? I created a simple pre-commit hook which processes README.Rmd whenever I make a commit and automatically adds any changes to the commit.

To do this is simple: just create a file called pre-commit in the .git/hooks folder with the following content:


if [ README.Rmd -nt ] || [ ! -f ]
        R -e "rmarkdown::render('README.Rmd')"
        git add

Make it executable and… Voila! 🚀 Whenever you commit, the file will be updated if it’s older than the most recent change to README.Rmd. If doesn’t exist, then it will be created.