TL;DR

  • Create a Shields.io badge for your GitHub README.md to track the number of commits since a specific commit

  • In my case, I set the base/reference point to the last commit to the repo of my blog’s theme which I added as a git submodule1 to the themes/ folder of my blog2

  • Using R Markdown, I have my badge automatically update to the current status of my submodule and rendered out to my README.md

  • Finally, I wrap the badge in a link to GitHub Commit Compare to quickly see all the recent changes to the theme before I pull them

How it works

  • NOTE: you can set up the baseline for this README workflow with the help of usethis::use_readme_rmd()

Getting the git ref

get_theme_sha <- function(theme_dir = NULL) {
  submodule <- git2r::repository(
    path = here::here("themes", theme_dir)
  )

  sha <- submodule |>
    git2r::repository_head() |>
    git2r::sha()
  return(sha)
}

get_theme_sha(theme_dir = "PaperMod")
[1] "71ce72b1bfb868b406c369c958f8682c63940e01"

Creating the badge

commit_badge <- function(user = NULL, repo = NULL, sha = NULL) {
  glue::glue("[![GitHub commits since tagged version](https://img.shields.io/github/commits-since/{user}/{repo}/{sha}?style=flat&logo=github&label=Commits%20since%20last%20theme%20submodule%20pull)](https://github.com/{user}/{repo}/compare/{sha}...master)")
}

badge <- commit_badge(
  user = "adityatelange",
  repo = "hugo-PaperMod",
  sha = get_theme_sha(theme_dir = "PaperMod")
)

badge
[![GitHub commits since tagged version](https://img.shields.io/github/commits-since/adityatelange/hugo-PaperMod/71ce72b1bfb868b406c369c958f8682c63940e01?style=flat&logo=github&label=Commits%20since%20last%20theme%20submodule%20pull)](https://github.com/adityatelange/hugo-PaperMod/compare/71ce72b1bfb868b406c369c958f8682c63940e01...master)

To include the badge in your Markdown file, use inline code:

`r badge`

`r commit_badge(user = "adityatelange", repo = "hugo-PaperMod",
                sha = get_theme_sha(theme_dir = "PaperMod"))`

Also works with knitr::include_graphics()


Appendix: The shield.io badge specs

🔗 https://shields.io/badges/git-hub-commits-since-tagged-version


  1. Learn more about submodules here: https://git-scm.com/book/en/v2/Git-Tools-Submodules ↩︎

  2. This blog is based on {blogdown} and Hugo, which uses a very specific directory structure↩︎