1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
## git2r, R bindings to the libgit2 library.
## Copyright (C) 2013-2019 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.
##' Config
##'
##' Config file management. To display the configuration variables,
##' call method \code{config} without the \code{user.name},
##' \code{user.email} or \code{...} options.
##'
##' There are two ways git2r can find the local repository when
##' writing local options (1) Use the \code{repo} argument. (2) If the
##' \code{repo} argument is \code{NULL} but the current working
##' directory is inside the local repository, then \code{git2r} uses
##' that repository.
##' @param repo The \code{repository}. Default is NULL.
##' @param global Write option(s) to global configuration
##' file. Default is FALSE.
##' @param user.name The user name. Use NULL to delete the entry
##' @param user.email The e-mail address. Use NULL to delete the entry
##' @param ... Additional options to write or delete from the
##' configuration.
##' @return S3 class \code{git_config}. When writing options, the
##' configuration is returned invisible.
##' @export
##' @examples \dontrun{
##' ## Initialize a temporary repository
##' path <- tempfile(pattern = "git2r-")
##' dir.create(path)
##' repo <- init(path)
##'
##' ## Set user name and email.
##' config(repo, user.name = "Alice", user.email = "alice@@example.org")
##'
##' ## Display configuration
##' config(repo)
##'
##' ## Delete user email.
##' config(repo, user.email = NULL)
##'
##' ## Display configuration
##' config(repo)
##' }
config <- function(repo = NULL, global = FALSE, user.name, user.email, ...) {
if (is.null(repo)) {
repo <- discover_repository(getwd())
if (!is.null(repo))
repo <- repository(repo)
}
## Check that 'global' is either TRUE or FALSE
stopifnot(any(identical(global, TRUE), identical(global, FALSE)))
variables <- list(...)
if (!missing(user.name))
variables <- c(variables, list(user.name = user.name))
if (!missing(user.email))
variables <- c(variables, list(user.email = user.email))
if (length(variables)) {
for (i in seq_len(length(variables))) {
if (!is.null(variables[[i]])) {
if (!is.character(variables[[i]])) {
stop("'", names(variables)[i],
"' must be a character vector")
}
}
}
if (isTRUE(global)) {
repo <- NULL
if (.Platform$OS.type == "windows") {
## Ensure that git2r writes the config file to the
## root of the user's home directory by first creating
## an empty file. Otherwise it may be written to the
## user's Documents/ directory. Only create the empty
## file if the user has specified configuration
## options to set and no global config file exists.
if (is.na(git_config_files()[["path"]][3])) {
if (length(variables) > 0) {
file.create(file.path(home_dir(), ".gitconfig"))
}
}
}
} else if (is.null(repo)) {
stop("Unable to locate local repository")
}
.Call(git2r_config_set, repo, variables)
}
cfg <- .Call(git2r_config_get, repo)
## Sort the variables within levels by name
cfg <- structure(lapply(cfg, function(x) x[order(names(x))]),
class = "git_config")
if (length(variables)) {
invisible(cfg)
} else {
return(cfg)
}
}
##' @export
print.git_config <- function(x, ...) {
lapply(names(x), function(level) {
cat(sprintf("%s:\n", level))
lapply(names(x[[level]]), function(entry) {
cat(sprintf(" %s=%s\n", entry, x[[level]][[entry]][1]))
})
})
invisible(x)
}
##' Locate the path to configuration files
##'
##' Potential configuration files:
##' \describe{
##' \item{system}{
##' Locate the path to the system configuration file. If
##' '/etc/gitconfig' doesn't exist, it will look for
##' '\%PROGRAMFILES\%'.
##' }
##' \item{xdg}{
##' Locate the path to the global xdg compatible configuration
##' file. The xdg compatible configuration file is usually located
##' in '$HOME/.config/git/config'. This method will try to guess
##' the full path to that file, if the file exists.
##' }
##' \item{global}{
##' The user or global configuration file is usually located in
##' '$HOME/.gitconfig'. This method will try to guess the full
##' path to that file, if the file exists.
##' }
##' \item{local}{
##' Locate the path to the repository specific configuration file,
##' if the file exists.
##' }
##' }
##' @template repo-param
##' @return a \code{data.frame} with one row per potential
##' configuration file where \code{NA} means not found.
##' @export
git_config_files <- function(repo = ".") {
## Lookup repository
if (inherits(repo, "git_repository")) {
repo <- repo$path
} else if (is.null(repo)) {
repo <- discover_repository(getwd())
} else if (is.character(repo) && (length(repo) == 1) &&
!is.na(repo) && isTRUE(file.info(repo)$isdir)) {
repo <- discover_repository(repo)
} else {
repo <- NULL
}
## Find local configuration file
if (is.null(repo)) {
path <- NA_character_
} else {
path <- file.path(normalizePath(repo), "config")
if (!isTRUE(!file.info(path)$isdir))
path <- NA_character_
}
data.frame(file = c("system", "xdg", "global", "local"),
path = c(.Call(git2r_config_find_file, "system"),
.Call(git2r_config_find_file, "xdg"),
.Call(git2r_config_find_file, "global"),
path),
stringsAsFactors = FALSE)
}
|