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)
library(plyr)
library(daff)
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"
f1 = readLines(file1)
f2 = readLines(file2)
diff_msg = differ(file1, file2)
cat(diff_msg)
differ_print = function(file1, file2, ...){
diff_msg = differ(file1, file2)
ss = strsplit(diff_msg, "\n")[[1]]
ss = ss[-(1:4)]
ss = grep("^( |\\+|\\-)", ss, value=TRUE)
nc = nchar(ss)
first = substr(ss, 1, 1)
first = plyr::revalue(first, c("+" = "+++",
"-" = "---"))
start = paste("@@,diff", file1, file2)
ss = c(start, paste0(first, ",", substr(ss, 2, nc)))
ss = paste(ss, collapse = "\n")
tfile = tempfile(fileext = ".csv")
writeLines(ss, con = tfile)
patch <- read_diff(tfile)
render_diff(patch, ...)
return(diff_msg)
}
diff_msg = differ_print(file1, file2)
diff_msg = differ_print(file1, file2, pretty = TRUE)
library(gistr)
diff_gist = function(file1, file2, ...) {
diff_msg = differ(file1, file2)
ss = strsplit(diff_msg, "\n")[[1]]
ind = grep("^@@", ss)
start = paste("## difference between", file1, file2)
ss = ss[seq(ind + 1, length(ss))]
ss = gsub("^ ", "", ss)
ss = c(start, "```diff", ss, "```")
ss = paste(ss, collapse = "\n")
tfile = tempfile(fileext = ".md")
writeLines(ss, con = tfile)
res = gist_create(files = tfile, ...)
return(list(diff_msg = diff_msg, gist = res))
}
res = diff_gist(file1, file2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment