Skip to content

Commit 5d82b97

Browse files
committed
Add unset argument to git_config_set()
1 parent a3c55b2 commit 5d82b97

5 files changed

Lines changed: 70 additions & 45 deletions

File tree

R/config.R

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,19 @@ git_config_global_get <- function(name) {
119119
#' @param add if `TRUE`, append a new entry for `name` instead of replacing
120120
#' existing one(s). Equivalent to `git config --add`. Only supported for
121121
#' string values.
122-
git_config_set <- function(name, value, add = FALSE, repo = '.') {
123-
if (!is.logical(add) || length(add) != 1) {
124-
stop("Argument add must be a logical of length 1.", call. = FALSE)
125-
}
122+
#' @param unset if `TRUE`, delete all entries for `name` matching the regular
123+
#' expression `value`, equivalent `git config --unset`. If `TRUE` and
124+
#' `value == NULL`, delete all entries for `name`, equivalent to
125+
#' `git config --unset-all`.
126+
git_config_set <- function(
127+
name, value,
128+
add = FALSE, unset = FALSE,
129+
repo = '.'
130+
) {
126131
repo <- git_open(repo)
127132
name <- as.character(name)
128133
out <- git_config_local_get(name, repo = repo)
129-
.Call(R_git_config_set, repo, name, value, add)
134+
git_config_set_impl(name, value, repo, add, unset)
130135
if (length(out) > 0) {
131136
invisible(out)
132137
} else {
@@ -136,16 +141,30 @@ git_config_set <- function(name, value, add = FALSE, repo = '.') {
136141

137142
#' @export
138143
#' @rdname git_config
139-
git_config_global_set <- function(name, value, add = FALSE) {
144+
git_config_global_set <- function(name, value, add = FALSE, unset = FALSE) {
140145
out <- git_config_global_get(name)
141-
.Call(R_git_config_set, NULL, name, value, add)
146+
git_config_set_impl(name, value, repo = NULL, add, unset)
142147
if (length(out) > 0) {
143148
invisible(out)
144149
} else {
145150
invisible(NULL)
146151
}
147152
}
148153

154+
git_config_set_impl <- function(name, value, repo, add, unset) {
155+
if (!is.logical(add) || length(add) != 1) {
156+
stop("Argument add must be a logical of length 1.", call. = FALSE)
157+
}
158+
if (!is.logical(unset) || length(unset) != 1) {
159+
stop("Argument unset must be a logical of length 1.", call. = FALSE)
160+
}
161+
if (add && unset) {
162+
stop("Arguments 'add' and 'unset' cannot both be TRUE.", call. = FALSE)
163+
}
164+
name <- as.character(name)
165+
.Call(R_git_config_set, repo, name, value, add, unset)
166+
}
167+
149168
#' Show libgit2 version and capabilities
150169
#'
151170
#' `libgit2_config()` reveals which version of libgit2 gert is using and which

man/git_config.Rd

Lines changed: 17 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ SEXP R_git_config_list(SEXP ptr){
6767
return out;
6868
}
6969

70-
SEXP R_git_config_set(SEXP ptr, SEXP name, SEXP value, SEXP add){
70+
SEXP R_git_config_set(SEXP ptr, SEXP name, SEXP value, SEXP add, SEXP unset){
7171
git_config *cfg = NULL;
7272
const char *cname = CHAR(STRING_ELT(name, 0));
7373
if(Rf_isNull(ptr)) {
@@ -79,6 +79,11 @@ SEXP R_git_config_set(SEXP ptr, SEXP name, SEXP value, SEXP add){
7979
if(TYPEOF(value) != STRSXP)
8080
Rf_error("add = TRUE only supported for string values");
8181
bail_if(git_config_set_multivar(cfg, cname, "^$", CHAR(STRING_ELT(value, 0))), "git_config_set_multivar");
82+
} else if(Rf_asLogical(unset)) {
83+
if(TYPEOF(value) != STRSXP)
84+
Rf_error("unset = TRUE only supported for string values");
85+
const char *cvalue = Rf_isNull(value) ? "^.*$" : CHAR(STRING_ELT(value, 0));
86+
bail_if(git_config_delete_multivar(cfg, cname, cvalue), "git_config_delete_multivar");
8287
} else {
8388
switch(TYPEOF(value)){
8489
case STRSXP:

src/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern SEXP R_git_commit_info(SEXP, SEXP);
3030
extern SEXP R_git_commit_log(SEXP, SEXP, SEXP, SEXP, SEXP);
3131
extern SEXP R_git_commit_stats(SEXP, SEXP);
3232
extern SEXP R_git_config_list(SEXP);
33-
extern SEXP R_git_config_set(SEXP, SEXP, SEXP, SEXP);
33+
extern SEXP R_git_config_set(SEXP, SEXP, SEXP, SEXP, SEXP);
3434
extern SEXP R_git_conflict_list(SEXP);
3535
extern SEXP R_git_create_branch(SEXP, SEXP, SEXP, SEXP, SEXP);
3636
extern SEXP R_git_delete_branch(SEXP, SEXP);
@@ -115,7 +115,7 @@ static const R_CallMethodDef CallEntries[] = {
115115
{"R_git_commit_log", (DL_FUNC) &R_git_commit_log, 5},
116116
{"R_git_commit_stats", (DL_FUNC) &R_git_commit_stats, 2},
117117
{"R_git_config_list", (DL_FUNC) &R_git_config_list, 1},
118-
{"R_git_config_set", (DL_FUNC) &R_git_config_set, 4},
118+
{"R_git_config_set", (DL_FUNC) &R_git_config_set, 5},
119119
{"R_git_conflict_list", (DL_FUNC) &R_git_conflict_list, 1},
120120
{"R_git_create_branch", (DL_FUNC) &R_git_create_branch, 5},
121121
{"R_git_delete_branch", (DL_FUNC) &R_git_delete_branch, 2},

tests/testthat/test-config.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,22 @@ test_that("local, custom config roundtrip", {
3838
expect_equal(orig, "ccc")
3939
expect_null(git_config_get("aaa.bbb", repo = repo))
4040
})
41+
42+
test_that("multivar, local, custom config roundtrip", {
43+
repo <- git_init(tempfile("gert-tests-config-multivar"))
44+
on.exit(unlink(repo, recursive = TRUE))
45+
46+
git_config_set("aaa.bbb", "ccc", repo = repo, add = TRUE)
47+
git_config_set("aaa.bbb", "ddd", repo = repo, add = TRUE)
48+
git_config_set("aaa.bbb", "eee", repo = repo, add = TRUE)
49+
cfg <- git_config(repo)
50+
expect_equal(cfg$value[cfg$name == "aaa.bbb"], c("ccc", "ddd", "eee"))
51+
52+
git_config_set("aaa.bbb", "ddd", repo = repo, unset = TRUE)
53+
cfg <- git_config(repo)
54+
expect_equal(cfg$value[cfg$name == "aaa.bbb"], c("ccc", "eee"))
55+
56+
git_config_set("aaa.bbb", "^[a-z]{3}$", repo = repo, unset = TRUE)
57+
cfg <- git_config(repo)
58+
expect_equal(cfg$value[cfg$name == "aaa.bbb"], character())
59+
})

0 commit comments

Comments
 (0)