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 > > > ## Initialize a temporary repository > path <- tempfile(pattern = "git2r-") > dir.create(path) > dir.create(file.path(path, "subfolder")) > repo <- init(path) > > ## Create a user > config(repo, user.name = "Alice", user.email = "alice@example.org") > > ## Create three files and commit > writeLines("First file", file.path(path, "example-1.txt")) > writeLines("Second file", file.path(path, "subfolder/example-2.txt")) > writeLines("Third file", file.path(path, "example-3.txt")) > add(repo, c("example-1.txt", "subfolder/example-2.txt", "example-3.txt")) > commit(repo, "Commit message") [c96710a] 2024-10-20: Commit message > > ## Traverse tree entries and its subtrees. > ## Various approaches that give identical result. > stopifnot(identical(ls_tree(tree = tree(last_commit(path))), + ls_tree(tree = tree(last_commit(repo))))) > stopifnot(identical(ls_tree(repo = path), ls_tree(repo = repo))) > > ## ls_tree(repo = repo) should match `git ls-tree -lr HEAD` > ls_tree_result <- ls_tree(repo = repo) > stopifnot(identical(ls_tree_result$name, + c("example-1.txt", "example-3.txt", "example-2.txt"))) > > # Argument `tree` can be a 'character that identifies a tree in the repository' > ls_tree(tree = tree(last_commit(path))$sha, repo = repo) mode type sha path name 1 100644 blob 4ef78b159f921a9b82457084dd837d4bbd874e50 example-1.txt 2 100644 blob 1d778cd00040ab2ecd483e3a9013b2d5cca5764d example-3.txt 3 100644 blob 85ded04863858cb7e99523989aca2384922e9f03 subfolder/ example-2.txt len 1 12 2 12 3 13 > > ## Skip content in subfolder > ls_tree_toplevel <- ls_tree(repo = repo, recursive = FALSE) > stopifnot(nrow(ls_tree_toplevel) == 3) > stopifnot(identical(ls_tree_toplevel$name, + c("example-1.txt", "example-3.txt", "subfolder"))) > > ## Start in subfolder > ls_tree_subfolder <- ls_tree(tree = "HEAD:subfolder", repo = repo) > stopifnot(nrow(ls_tree_subfolder) == 1) > stopifnot(identical(ls_tree_subfolder$name, "example-2.txt")) > > ## Cleanup > unlink(path, recursive = TRUE) > > proc.time() user system elapsed 0.26 0.18 0.43