Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ VignetteBuilder:
knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3.9000
SystemRequirements: libgit2 (>= 1.0): libgit2-devel (rpm) or libgit2-dev (deb)
Language: en-US
Config/roxygen2/version: 8.0.0
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export(git_config_get)
export(git_config_global)
export(git_config_global_get)
export(git_config_global_set)
export(git_config_global_unset)
export(git_config_local_get)
export(git_config_set)
export(git_config_unset)
export(git_conflicts)
export(git_diff)
export(git_diff_patch)
Expand Down Expand Up @@ -128,6 +130,7 @@ useDynLib(gert,R_git_commit_log)
useDynLib(gert,R_git_commit_stats)
useDynLib(gert,R_git_config_list)
useDynLib(gert,R_git_config_set)
useDynLib(gert,R_git_config_unset)
useDynLib(gert,R_git_conflict_list)
useDynLib(gert,R_git_create_branch)
useDynLib(gert,R_git_delete_branch)
Expand Down
31 changes: 24 additions & 7 deletions R/config.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#' Get or set Git configuration
#'
#' @description
#' Get or set Git options, as `git config` does on the command line. **Global**
#' settings affect all of a user's Git operations (`git config --global`),
#' whereas **local** settings are scoped to a specific repository (`git config
#' --local`). When both exist, local options always win.
#' Get, set, or unset Git options, as `git config` does on the command line.
#' **Global** settings affect all of a user's Git operations
#' (`git config --global`), whereas **local** settings are scoped to a specific
#' repository (`git config --local`). When both exist, local options always win.
#'
#' ```{r echo = FALSE, results = "asis"}
#' dat <- data.frame(
#' local = c("`git_config()`", "`git_config_get()`", "`git_config_local_get()`", "`git_config_set()`"),
#' global = c("`git_config_global()`", "`git_config_get()`", "`git_config_global_get()`", "`git_config_global_set()`"),
#' row.names = c("get all", "get one (local+global)", "get one (local or global only)", "set")
#' local = c("`git_config()`", "`git_config_get()`", "`git_config_local_get()`", "`git_config_set()`", "`git_config_unset()`"),
#' global = c("`git_config_global()`", "`git_config_get()`", "`git_config_global_get()`", "`git_config_global_set()`", "`git_config_global_unset()`"),
#' row.names = c("get all", "get one (local+global)", "get one (local or global only)", "set", "unset")
#' )
#' knitr::kable(dat, col.names = paste0("**", colnames(dat), "**"))
#' ```
Expand All @@ -28,6 +28,8 @@
#' * `git_config_set()`, `git_config_global_set()`: The previous value(s) of
#' `name` in local or global config, respectively. If this option was
#' previously unset, returns `NULL`. Returns invisibly.
#' * `git_config_unset()`, `git_config_global_unset()`: The previous value(s) of
#' `name` that were unset. Returns invisibly.
#'
#' @note All entries in the `name` column are automatically normalised to
#' lowercase (see
Expand Down Expand Up @@ -146,6 +148,21 @@ git_config_global_set <- function(name, value, add = FALSE) {
}
}

#' @export
#' @rdname git_config
#' @useDynLib gert R_git_config_unset
#' @param pattern optional regular expression, for matching values to unset in
#' case of multiple values
git_config_unset <- function(name, pattern = NULL, repo = '.'){
.Call(R_git_config_unset, git_open(repo), name, pattern)
}

#' @export
#' @rdname git_config
git_config_global_unset <- function(name, pattern = NULL){
.Call(R_git_config_unset, NULL, name, pattern)
}

#' Show libgit2 version and capabilities
#'
#' `libgit2_config()` reveals which version of libgit2 gert is using and which
Expand Down
19 changes: 16 additions & 3 deletions man/git_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,21 @@ SEXP R_git_config_set(SEXP ptr, SEXP name, SEXP value, SEXP add){
git_config_free(cfg);
return R_NilValue;
}

SEXP R_git_config_unset(SEXP ptr, SEXP name, SEXP pattern){
git_config *cfg = NULL;
const char *cname = CHAR(STRING_ELT(name, 0));
if(Rf_isNull(ptr)) {
bail_if(git_config_open_default(&cfg), "git_config_open_default");
} else {
bail_if(git_repository_config(&cfg, get_git_repository(ptr)),"git_repository_config");
}
if(Rf_length(pattern)){
const char *cpattern = CHAR(STRING_ELT(pattern, 0));
bail_if(git_config_delete_multivar(cfg, cname, cpattern), "git_config_delete_multivar");
} else {
bail_if(git_config_delete_entry(cfg, cname), "git_config_delete_entry");
}
git_config_free(cfg);
return R_NilValue;
}
2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern SEXP R_git_commit_log(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP R_git_commit_stats(SEXP, SEXP);
extern SEXP R_git_config_list(SEXP);
extern SEXP R_git_config_set(SEXP, SEXP, SEXP, SEXP);
extern SEXP R_git_config_unset(SEXP, SEXP, SEXP);
extern SEXP R_git_conflict_list(SEXP);
extern SEXP R_git_create_branch(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP R_git_delete_branch(SEXP, SEXP);
Expand Down Expand Up @@ -116,6 +117,7 @@ static const R_CallMethodDef CallEntries[] = {
{"R_git_commit_stats", (DL_FUNC) &R_git_commit_stats, 2},
{"R_git_config_list", (DL_FUNC) &R_git_config_list, 1},
{"R_git_config_set", (DL_FUNC) &R_git_config_set, 4},
{"R_git_config_unset", (DL_FUNC) &R_git_config_unset, 3},
{"R_git_conflict_list", (DL_FUNC) &R_git_conflict_list, 1},
{"R_git_create_branch", (DL_FUNC) &R_git_create_branch, 5},
{"R_git_delete_branch", (DL_FUNC) &R_git_delete_branch, 2},
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ git_strarray *files_to_array(SEXP files);

#define build_tibble(...) list_to_tibble(build_list( __VA_ARGS__))

#define AT_LEAST_LIBGIT2(x,y) (LIBGIT2_VER_MAJOR > x || LIBGIT2_VER_MINOR >= y)
#define AT_LEAST_LIBGIT2(x,y) (LIBGIT2_VER_MAJOR > x || (LIBGIT2_VER_MAJOR == x && LIBGIT2_VER_MINOR >= y))

/* Workaround for API change in 1.8.0 and 1.8.1 only: https://github.com/libgit2/libgit2/issues/6793 */
#if LIBGIT2_VER_MAJOR == 1 && LIBGIT2_VER_MINOR == 8 && LIBGIT2_VER_REVISION < 2
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ test_that("local, custom config roundtrip", {
expect_equal(orig, "ccc")
expect_null(git_config_get("aaa.bbb", repo = repo))
})

test_that("git_config_unset() works for multivar options", {
repo <- git_init(tempfile("gert-tests-config"))
on.exit(unlink(repo, recursive = TRUE))

git_config_set("aaa.bbb", "ccc", add = TRUE, repo = repo)
git_config_set("aaa.bbb", "ccccc", add = TRUE, repo = repo)
git_config_set("aaa.bbb", "ddd", add = TRUE, repo = repo)

git_config_unset("aaa.bbb", "ccc", repo = repo)
expect_equal(git_config_get("aaa.bbb", repo = repo), "ddd")

git_config_unset("aaa.bbb", repo = repo)
expect_null(git_config_get("aaa.bbb", repo = repo))
})
Loading