Skip to content

Instantly share code, notes, and snippets.

@jennybc
Forked from muschellij2/Differ.R
Last active June 10, 2020 21:13
Show Gist options
  • Save jennybc/471bb4b54644c42cbfa6 to your computer and use it in GitHub Desktop.
Save jennybc/471bb4b54644c42cbfa6 to your computer and use it in GitHub Desktop.
Get the Diff of 2 file solely with R functions
rm(list=ls())
library(git2r)
differ = function(file1, file2){
path <- tempfile(pattern="git2r-")
dir.create(path)
repo <- init(path)
suppressWarnings({
f1 = readLines(file1)
})
suppressWarnings({
f2 = readLines(file2)
})
## Create a file, add, commit
base = "test.txt"
tfile = file.path(path, base)
writeLines(f1, tfile)
add(repo, base)
commit(repo, "Added First File")
## Change the file
writeLines(f2, tfile)
diff_1 <- diff(repo, as_char = TRUE)
diff_1 = gsub("a/test.txt", file1, diff_1, fixed = TRUE)
diff_1 = gsub("b/test.txt", file2, diff_1, fixed = TRUE)
# summary(diff_1)
# cat(diff(repo, as_char=TRUE))
return(diff_1)
}
file1 = "~/Desktop/difftest.txt"
file2 = "~/Desktop/difftest2.txt"
diff_msg = differ(file1, file2)
cat(diff_msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment