Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Last active February 8, 2024 21:17
Show Gist options
  • Save benmarwick/85e853b258a653f0b44d2df293fba222 to your computer and use it in GitHub Desktop.
Save benmarwick/85e853b258a653f0b44d2df293fba222 to your computer and use it in GitHub Desktop.
GitHub doesn't show rich diffs for Rmd files. That can make collaborative writing tough. Here's how to see rich diffs of two commits of a single R Markdown document on a GitHub repo or local Git repo
# another method
# remotes::install_github("ropenscilabs/reviewer")
browseURL(reviewer::diff_rmd("analysis/paper/paper.qmd",
# this gets the sha of the previous commit
git2r::commits(n=2)[[2]]$sha)$raw)
# How to see rich diffs of two commits of a single R Markdown document in a local Git repo
# https://github.com/lorenzwalthert/gitsum
library("gitsum")
library("tidyverse")
# To browse the commits locally, open an RStudio
# project that is using Git version control, then ...
# Set the path within the project to the Rmd file
# in the project that we want to compare versions of
file_path <- "analysis/paper/paper.Rmd"
# Make a data frame that stores the git commit history,
# one row per commit
init_gitsum()
tbl_out <- parse_log_detailed() %>%
select(date, author_name, message, hash)
# View this dataframe to pick the specific commits to compare
View(tbl_out)
# choose two commits to compare, here I've got the most recent
# one (the last row of the table), and the one immediately before that
i <- nrow(tbl_out)
j <- nrow(tbl_out) - 2
# consrtruct the terminal commants using the SHA of the commits we want to
# compare, and the file path
git_show_str_1 <- str_glue('git show ', tbl_out[i, ]$hash, ":", file_path)
git_show_str_2 <- str_glue('git show ', tbl_out[j, ]$hash, ":", file_path)
# get the contents of the file at each commit and
# store in a vector
earlier_commit <- system(git_show_str_2, intern = TRUE)
later_commit <- system(git_show_str_1, intern = TRUE)
# compute differences and visualise
diffobj::diffChr(earlier_commit,
later_commit,
mode = "sidebyside")
# another option: https://ropenscilabs.github.io/reviewer/articles/reviewer.html
# How to see rich diffs of two commits of a single R Markdown document on a GitHub repo (on the master branch only)
# Edit this to use the repo that has the file you want to inspect
gh_account_and_repo <- "LiYingWang/kwl.pottery"
# Edit this to use the single file that you want to inspect
# start the path here from the top level of the github repo
gh_file_path <- "analysis/paper/paper.Rmd"
# Run this next line to open a web page to see the commit history
# of this file so we can copy the SHA ids to use in the next step.
# Look for the little clipboard icon to click on to copy the full SHA,
# and past the SHAs in the next step below
browseURL(file.path(
"https://github.com",
gh_account_and_repo,
"commits/master",
gh_file_path
))
# Edit these to get the full SHA for the commits you want to compare, get
# the full SHA from the GitHub webpage that was opened in the previous step
commit_1_sha <- "8249ffc832acfb4839dfb9e5f838d45b82ca0063" # a recent commit
commit_2_sha <- "a05cad1b678a31c9def1860cbceeba4cbfb98577" # an earlier commit
# Don't change this
gh_raw <- "https://raw.githubusercontent.com"
# run these lines...
later_commit <- file.path(gh_raw, gh_account_and_repo, commit_1_sha, gh_file_path)
earlier_commit <- file.path(gh_raw, gh_account_and_repo, commit_2_sha, gh_file_path)
# run these lines to view the rich diff:
diffobj::diffFile(earlier_commit,
later_commit,
mode = "sidebyside")
@benmarwick
Copy link
Author

Here's an example of the output from this process, it pops up in the RStudio viewer, and can be popped out into a browser tab:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment