R Under development (unstable) (2024-10-19 r87247 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ## git2r, R bindings to the libgit2 library. > ## Copyright (C) 2013-2023 The git2r contributors > ## > ## This program is free software; you can redistribute it and/or modify > ## it under the terms of the GNU General Public License, version 2, > ## as published by the Free Software Foundation. > ## > ## git2r is distributed in the hope that it will be useful, > ## but WITHOUT ANY WARRANTY; without even the implied warranty of > ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > ## GNU General Public License for more details. > ## > ## You should have received a copy of the GNU General Public License along > ## with this program; if not, write to the Free Software Foundation, Inc., > ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. > > library("git2r") > > ## For debugging > sessionInfo() R Under development (unstable) (2024-10-19 r87247 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows Server 2022 x64 (build 20348) Matrix products: default locale: [1] LC_COLLATE=C LC_CTYPE=German_Germany.utf8 [3] LC_MONETARY=C LC_NUMERIC=C [5] LC_TIME=C time zone: Europe/Berlin tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] git2r_0.35.0 loaded via a namespace (and not attached): [1] compiler_4.5.0 > libgit2_version() $major [1] 1 $minor [1] 7 $rev [1] 2 > libgit2_features() $threads [1] TRUE $https [1] TRUE $ssh [1] FALSE > > > ## Create a directory in tempdir > path <- tempfile(pattern = "git2r-") > dir.create(path) > > ## Initialize a repository > repo <- init(path) > config(repo, user.name = "Alice", user.email = "alice@example.org") > > ## Create a file, add, commit > writeLines("Hello world!", file.path(path, "test.txt")) > add(repo, "test.txt") > commit(repo, "Commit message") [7202881] 2024-10-20: Commit message > > ## Change the file, diff between index and workdir > writeLines("Hello again!\nHere is a second line\nAnd a third", + file.path(path, "test.txt")) > diff_1 <- diff(repo) > diff(repo, as_char = TRUE) [1] "diff --git a/test.txt b/test.txt\nindex dfd6895..0055243 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1 +1,3 @@\n-Hello world!\r\n+Hello again!\r\n+Here is a second line\r\n+And a third\r\n" > diff(repo, as_char = TRUE, filename = file.path(path, "test.diff")) NULL > > stopifnot(identical(diff_1$old, "index")) > stopifnot(identical(diff_1$new, "workdir")) > stopifnot(identical(length(diff_1$files), 1L)) > stopifnot(identical(diff_1$files[[1]]$old_file, "test.txt")) > stopifnot(identical(diff_1$files[[1]]$new_file, "test.txt")) > stopifnot(identical(length(diff_1$files[[1]]$hunks), 1L)) > stopifnot(identical(length(diff_1$files[[1]]$hunks[[1]]$lines), 4L)) > ## TODO: check actual diff > > ## Diff between index and HEAD is empty > diff_2 <- diff(repo, index = TRUE) > diff(repo, as_char = TRUE) [1] "diff --git a/test.txt b/test.txt\nindex dfd6895..0055243 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1 +1,3 @@\n-Hello world!\r\n+Hello again!\r\n+Here is a second line\r\n+And a third\r\n" > diff(repo, as_char = TRUE, filename = file.path(path, "test.diff")) NULL > > stopifnot(identical(diff_2$old, "HEAD")) > stopifnot(identical(diff_2$new, "index")) > stopifnot(identical(diff_2$files, list())) > > ## Diff between tree and working dir, same as diff_1 > diff_3 <- diff(tree(commits(repo)[[1]])) > diff(repo, as_char = TRUE) [1] "diff --git a/test.txt b/test.txt\nindex dfd6895..0055243 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1 +1,3 @@\n-Hello world!\r\n+Hello again!\r\n+Here is a second line\r\n+And a third\r\n" > diff(repo, as_char = TRUE, filename = file.path(path, "test.diff")) NULL > > stopifnot(identical(diff_3$old, tree(commits(repo)[[1]]))) > stopifnot(identical(diff_3$new, "workdir")) > stopifnot(identical(diff_3$files, diff_1$files)) > stopifnot(identical(print(diff_3), diff_3)) Old: tree: 1021ec8da99758dbb8dfc8edb2a12a2c5bcc39dd mode type sha name 1 100644 blob dfd68952a9ba420d898d4e3a666256e8f3a20602 test.txt New: workdir > > ## Add changes, diff between index and HEAD is the same as diff_1 > add(repo, "test.txt") > diff_4 <- diff(repo, index = TRUE) > diff(repo, as_char = TRUE) [1] "" > diff(repo, as_char = TRUE, filename = file.path(path, "test.diff")) NULL > > stopifnot(identical(diff_4$old, "HEAD")) > stopifnot(identical(diff_4$new, "index")) > stopifnot(identical(diff_4$files, diff_1$files)) > > ## Diff between tree and index > diff_5 <- diff(tree(commits(repo)[[1]]), index = TRUE) > diff(repo, as_char = TRUE) [1] "" > diff(repo, as_char = TRUE, filename = file.path(path, "test.diff")) NULL > > stopifnot(identical(diff_5$old, tree(commits(repo)[[1]]))) > stopifnot(identical(diff_5$new, "index")) > stopifnot(identical(diff_5$files, diff_1$files)) > > ## Diff between two trees > commit(repo, "Second commit") [522ebec] 2024-10-20: Second commit > tree_1 <- tree(commits(repo)[[2]]) > tree_2 <- tree(commits(repo)[[1]]) > diff_6 <- diff(tree_1, tree_2) > diff(repo, as_char = TRUE) [1] "" > diff(repo, as_char = TRUE, filename = file.path(path, "test.diff")) NULL > > stopifnot(identical(diff_6$old, tree_1)) > stopifnot(identical(diff_6$new, tree_2)) > stopifnot(identical(diff_6$files, diff_1$files)) > > ## Length of a diff > stopifnot(identical(length(diff_1), 1L)) > stopifnot(identical(length(diff_2), 0L)) > stopifnot(identical(length(diff_3), 1L)) > stopifnot(identical(length(diff_4), 1L)) > stopifnot(identical(length(diff_5), 1L)) > stopifnot(identical(length(diff_6), 1L)) > > ## Binary files > set.seed(42) > writeBin(as.raw((sample(0:255, 1000, replace = TRUE))), + con = file.path(path, "test.bin")) > add(repo, "test.bin") > diff_7 <- diff(repo, index = TRUE) > diff(repo, as_char = TRUE) [1] "" > diff(repo, as_char = TRUE, filename = file.path(path, "test.diff")) NULL > > stopifnot(any(grepl("binary file", capture.output(summary(diff_7))))) > > ## TODO: errors > ## Check non-logical index argument > res <- tools::assertError( + .Call(git2r:::git2r_diff, NULL, NULL, NULL, "FALSE", + NULL, 3L, 0L, "a", "b", NULL, NULL, NULL)) > stopifnot(length(grep(paste0("Error in 'git2r_diff': 'index' must be logical ", + "vector of length one with non NA value\n"), + res[[1]]$message)) > 0) > > ## Check various combinations of diff arguments > res <- tools::assertError( + .Call(git2r:::git2r_diff, NULL, NULL, + tree(commits(repo)[[1]]), + FALSE, NULL, 3L, 0L, "a", "b", NULL, NULL, NULL)) > stopifnot(length(grep("Error in 'git2r_diff': Invalid diff parameters", + res[[1]]$message)) > 0) > > res <- tools::assertError( + .Call(git2r:::git2r_diff, NULL, NULL, + tree(commits(repo)[[1]]), + TRUE, NULL, 3L, 0L, "a", "b", NULL, NULL, NULL)) > stopifnot(length(grep("Error in 'git2r_diff': Invalid diff parameters", + res[[1]]$message)) > 0) > > res <- tools::assertError( + .Call(git2r:::git2r_diff, repo, tree(commits(repo)[[1]]), + NULL, FALSE, NULL, 3L, 0L, "a", "b", NULL, NULL, NULL)) > stopifnot(length(grep("Error in 'git2r_diff': Invalid diff parameters", + res[[1]]$message)) > 0) > > res <- tools::assertError( + .Call(git2r:::git2r_diff, repo, tree(commits(repo)[[1]]), + NULL, TRUE, NULL, 3L, 0L, "a", "b", NULL, NULL, NULL)) > stopifnot(length(grep("Error in 'git2r_diff': Invalid diff parameters", + res[[1]]$message)) > 0) > > res <- tools::assertError( + .Call(git2r:::git2r_diff, repo, tree(commits(repo)[[1]]), + tree(commits(repo)[[2]]), FALSE, NULL, 3L, 0L, "a", + "b", NULL, NULL, NULL)) > stopifnot(length(grep("Error in 'git2r_diff': Invalid diff parameters", + res[[1]]$message)) > 0) > > res <- tools::assertError( + .Call(git2r:::git2r_diff, repo, tree(commits(repo)[[1]]), + tree(commits(repo)[[2]]), TRUE, NULL, 3L, 0L, "a", + "b", NULL, NULL, NULL)) > stopifnot(length(grep("Error in 'git2r_diff': Invalid diff parameters", + res[[1]]$message)) > 0) > > ## TODO: printing > > ## Cleanup > unlink(path, recursive = TRUE) > > proc.time() user system elapsed 0.42 0.29 0.64