gert is a simple git client based on ‘libgit2’ (libgit2.org):
libgit2 is a portable, pure C implementation of the Git core methods provided as a re-entrant linkable library with a solid API, allowing you to write native speed custom Git applications in any language which supports C bindings.
What this means for R users is that we can work with local and remote Git repositories from the comfort of R!
User-friendly authentication is a high priority for gert, which supports both SSH and HTTPS remotes on all platforms. User credentials are shared with command line Git through the git-credential store and ssh keys stored on disk or ssh-agent.
Let’s attach gert.
library(gert)
#> Error in library(gert): there is no package called 'gert'
Before you can do anything with Git, you must first configure your user name and email. When you attach gert, it actually reveals whether you’ve already done this and, above, you can see that we have.
But what if you have not already configured your user name and email? Do this with git_config_global_set()
:
git_config_global_set("user.name", "Jerry Johnson")
git_config_global_set("user.email", "jerry@gmail.com")
We can verify our success (and see all global options) with git_config_global()
.
git_config_global()
#> Error in git_config_global(): could not find function "git_config_global"
This is equivalent to these commands in command line Git:
git config --global user.name 'Jerry Johnson'
git config --global user.email 'jerry@gmail.com'
git config --global --list
To inspect and change local Git config, i.e. options specific to one repository, use git_config()
and git_config_set()
.
gert::git_init()
is essentially git init
; it’s how we create a new local repository. You provide the path to the repository you want to create.
(path <- file.path(tempdir(), "aaa", "bbb", "repo_ccc"))
#> [1] "/tmp/RtmpzmXNWd/aaa/bbb/repo_ccc"
dir.exists(path)
#> [1] FALSE
(r <- git_init(path))
#> Error in git_init(path): could not find function "git_init"
dir.exists(path)
#> [1] FALSE
Note that all non-existing parts of the path are created: aaa
, bbb
, and repo_ccc
(the actual git repository).
git_find()
finds a git repository at or above the path you provide and errors otherwise.
git_find(r)
#> Error in git_find(r): could not find function "git_find"
dir.create(file.path(r, "child_dir"))
#> Error in eval(expr, envir, enclos): object 'r' not found
git_find(file.path(r, "child_dir"))
#> Error in git_find(file.path(r, "child_dir")): could not find function "git_find"
git_find(file.path(tempdir(), "aaa", "bbb"))
#> Error in git_find(file.path(tempdir(), "aaa", "bbb")): could not find function "git_find"
git_init()
can also create a repository in a pre-existing directory, as long as it is empty.
r2 <- file.path(tempdir(), "repo_ddd")
dir.create(r2)
git_init(r2)
#> Error in git_init(r2): could not find function "git_init"
Cleanup.
unlink(r, recursive = TRUE)
#> Error in eval(expr, envir, enclos): object 'r' not found
unlink(r2, recursive = TRUE)