Skip to content

Commit 1c05177

Browse files
committed
Simplify things
1 parent e1709e9 commit 1c05177

7 files changed

Lines changed: 25 additions & 67 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ VignetteBuilder:
3232
knitr
3333
Encoding: UTF-8
3434
Roxygen: list(markdown = TRUE)
35-
RoxygenNote: 7.3.3.9000
3635
SystemRequirements: libgit2 (>= 1.0): libgit2-devel (rpm) or libgit2-dev (deb)
3736
Language: en-US
37+
Config/roxygen2/version: 8.0.0

R/config.R

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -151,38 +151,16 @@ git_config_global_set <- function(name, value, add = FALSE) {
151151
#' @export
152152
#' @rdname git_config
153153
#' @useDynLib gert R_git_config_unset
154-
#' @param pattern Regular expression matching values to unset. Note: the regular
155-
#' expressions engine used depends on the libgit2 installation; you can check
156-
#' this using `libgit2_config()$regex_backend`
157-
#' (`"`\code{\Sexpr[stage=render,results=rd]{gert::libgit2_config()$regex_backend}}`"`
158-
#' for this installation).
159-
#' @param fixed If `TRUE`, only unset values that match `pattern` entirely and
160-
#' as-is.
161-
git_config_unset <- function(name, pattern, fixed = FALSE, repo = '.')
162-
{
163-
git_config_unset_impl(
164-
name, pattern, fixed, git_open(repo),
165-
function() git_config_local_get(name, repo = repo)
166-
)
154+
#' @param pattern optional regular expression, for matching values to unset in
155+
#' case of multiple values
156+
git_config_unset <- function(name, pattern = NULL, repo = '.'){
157+
.Call(R_git_config_unset, git_open(repo), name, pattern)
167158
}
168159

169160
#' @export
170161
#' @rdname git_config
171-
git_config_global_unset <- function(name, pattern, fixed = FALSE)
172-
{
173-
git_config_unset_impl(
174-
name, pattern, fixed, NULL,
175-
function() git_config_global_get(name)
176-
)
177-
}
178-
179-
git_config_unset_impl <- function(name, pattern, fixed, repo, get_val_cb) {
180-
if (fixed) pattern <- fixed_regex(pattern)
181-
prev <- (get_val_cb)()
182-
.Call(R_git_config_unset, repo, name, pattern)
183-
post <- (get_val_cb)()
184-
out <- setdiff(prev, post)
185-
invisible(out)
162+
git_config_global_unset <- function(name, pattern = NULL){
163+
.Call(R_git_config_unset, NULL, name, pattern)
186164
}
187165

188166
#' Show libgit2 version and capabilities
@@ -200,15 +178,6 @@ libgit2_config <- function() {
200178
res
201179
}
202180

203-
# helper used in git_config_unset()
204-
fixed_regex <- function(string) {
205-
metachars <- c(".", "\\", "|", "(", ")", "[", "]", "{", "}", "^", "$", "*", "+", "?")
206-
for (metachar in metachars) {
207-
string <- gsub(metachar, paste0("\\", metachar), string, fixed = TRUE)
208-
}
209-
paste0("^", string, "$")
210-
}
211-
212181
# helpers used in tests
213182
configure_local_user <- function(repo = ".") {
214183
git_config_set('user.name', "Jerry Johnson", repo = repo)

man/git_config.Rd

Lines changed: 4 additions & 10 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,17 @@ SEXP R_git_config_set(SEXP ptr, SEXP name, SEXP value, SEXP add){
108108
SEXP R_git_config_unset(SEXP ptr, SEXP name, SEXP pattern){
109109
git_config *cfg = NULL;
110110
const char *cname = CHAR(STRING_ELT(name, 0));
111-
const char *cpattern = CHAR(STRING_ELT(pattern, 0));
112111
if(Rf_isNull(ptr)) {
113112
bail_if(git_config_open_default(&cfg), "git_config_open_default");
114113
} else {
115114
bail_if(git_repository_config(&cfg, get_git_repository(ptr)),"git_repository_config");
116115
}
117-
bail_if(git_config_delete_multivar(cfg, cname, cpattern), "git_config_delete_multivar");
116+
if(Rf_length(pattern)){
117+
const char *cpattern = CHAR(STRING_ELT(pattern, 0));
118+
bail_if(git_config_delete_multivar(cfg, cname, cpattern), "git_config_delete_multivar");
119+
} else {
120+
bail_if(git_config_delete_entry(cfg, cname), "git_config_delete_entry");
121+
}
118122
git_config_free(cfg);
119123
return R_NilValue;
120124
}

src/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ git_strarray *files_to_array(SEXP files);
2323

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

26-
#define AT_LEAST_LIBGIT2(x,y) (LIBGIT2_VER_MAJOR > x || (LIBGIT2_VER_MAJOR >= x && LIBGIT2_VER_MINOR >= y))
26+
#define AT_LEAST_LIBGIT2(x,y) (LIBGIT2_VER_MAJOR > x || (LIBGIT2_VER_MAJOR == x && LIBGIT2_VER_MINOR >= y))
2727

2828
/* Workaround for API change in 1.8.0 and 1.8.1 only: https://github.com/libgit2/libgit2/issues/6793 */
2929
#if LIBGIT2_VER_MAJOR == 1 && LIBGIT2_VER_MINOR == 8 && LIBGIT2_VER_REVISION < 2

src/version.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ SEXP R_libgit2_config(void){
2525
SEXP ssh = PROTECT(Rf_ScalarLogical(features & GIT_FEATURE_SSH));
2626
SEXP https = PROTECT(Rf_ScalarLogical(features & GIT_FEATURE_HTTPS));
2727
SEXP threads = PROTECT(Rf_ScalarLogical(features & GIT_FEATURE_THREADS));
28-
const char* c_regex_backend;
29-
#if AT_LEAST_LIBGIT2(1, 9)
30-
c_regex_backend = git_libgit2_feature_backend(GIT_FEATURE_REGEX);
31-
#else
32-
c_regex_backend = "unknown";
33-
#endif
34-
SEXP regex_backend = PROTECT(safe_string(c_regex_backend));
3528
git_buf buf = {0};
3629
git_config_find_global(&buf);
3730
SEXP config_global = PROTECT(safe_string(buf.ptr));
@@ -42,10 +35,9 @@ SEXP R_libgit2_config(void){
4235
git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf);
4336
SEXP config_search_path = PROTECT(safe_string(buf.ptr));
4437
git_buf_free(&buf);
45-
SEXP out = build_list(8, "version", version, "ssh", ssh, "https", https, "threads", threads,
46-
"regex_backend", regex_backend,
38+
SEXP out = build_list(7, "version", version, "ssh", ssh, "https", https, "threads", threads,
4739
"config.global", config_global, "config.system", config_system,
4840
"config.home", config_search_path);
49-
UNPROTECT(8);
41+
UNPROTECT(7);
5042
return out;
5143
}

tests/testthat/test-config.R

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ test_that("git_config_unset() works for multivar options", {
4444
on.exit(unlink(repo, recursive = TRUE))
4545

4646
git_config_set("aaa.bbb", "ccc", add = TRUE, repo = repo)
47-
git_config_set("aaa.bbb", "cccc", add = TRUE, repo = repo)
47+
git_config_set("aaa.bbb", "ccccc", add = TRUE, repo = repo)
4848
git_config_set("aaa.bbb", "ddd", add = TRUE, repo = repo)
49+
git_config_set("aaa.bbb", "eee", add = TRUE, repo = repo)
4950

50-
removed <- git_config_unset("aaa.bbb", "ccc", fixed = TRUE, repo = repo)
51-
expect_equal(removed, "ccc")
52-
expect_equal(git_config_get("aaa.bbb", repo = repo), c("cccc", "ddd"))
51+
git_config_unset("aaa.bbb", "ccc", repo = repo)
52+
expect_equal(git_config_get("aaa.bbb", repo = repo), c("ddd", "eee"))
5353

54-
wildcard_removed <- git_config_unset("aaa.bbb", ".*", repo = repo)
55-
expect_equal(wildcard_removed, c("cccc", "ddd"))
54+
git_config_unset("aaa.bbb", repo = repo)
5655
expect_null(git_config_get("aaa.bbb", repo = repo))
5756
})

0 commit comments

Comments
 (0)