44# ' Get or set Git options, as `git config` does on the command line. **Global**
55# ' settings affect all of a user's Git operations (`git config --global`),
66# ' whereas **local** settings are scoped to a specific repository (`git config
7- # ' --local`). When both exist, local options always win. Four functions address
8- # ' the four possible combinations of getting vs setting and global vs. local.
7+ # ' --local`). When both exist, local options always win.
98# '
109# ' ```{r echo = FALSE, results = "asis"}
1110# ' dat <- data.frame(
12- # ' local = c("`git_config()`", "`git_config_set()`"),
13- # ' global = c("`git_config_global()`", "`git_config_global_set()`"),
14- # ' row.names = c("get", "set")
11+ # ' local = c("`git_config()`", "`git_config_get()`", "`git_config_local_get()`", "` git_config_set()`"),
12+ # ' global = c("`git_config_global()`", "`git_config_get()`", "`git_config_global_get()`", "` git_config_global_set()`"),
13+ # ' row.names = c("get all", "get one (local+global)", "get one (local or global only) ", "set")
1514# ' )
1615# ' knitr::kable(dat, col.names = paste0("**", colnames(dat), "**"))
1716# ' ```
2120# ' option is determined from global or local config.
2221# ' * `git_config_global()`: a `data.frame`, as for `git_config()`, except only
2322# ' for global Git options.
23+ # ' * `git_config_get()`: the value of the named option considering both local and
24+ # ' global config (local wins), or `NULL` if unset.
25+ # ' * `git_config_local_get()`: as for `git_config_get()`, but restricted to
26+ # ' local (repository-level) config only.
27+ # ' * `git_config_global_get()`: as for `git_config_get()`, but for global config only.
2428# ' * `git_config_set()`, `git_config_global_set()`: The previous value(s) of
2529# ' `name` in local or global config, respectively. If this option was
2630# ' previously unset, returns `NULL`. Returns invisibly.
3640# '
3741# ' previous <- git_config_set("aaa.bbb", "ccc", repo = r)
3842# ' previous
39- # ' cfg <- git_config(repo = r)
40- # ' subset(cfg, level == "local")
41- # ' cfg$value[cfg$name == "aaa.bbb"]
43+ # ' git_config_local_get("aaa.bbb", repo = r)
4244# '
4345# ' previous <- git_config_set("aaa.bbb", NULL, repo = r)
4446# ' previous
45- # ' cfg <- git_config(repo = r)
46- # ' subset(cfg, level == "local")
47- # ' cfg$value[cfg$name == "aaa.bbb"]
47+ # ' git_config_local_get("aaa.bbb", repo = r)
48+ # '
49+ # ' # Get a single named option (returns NULL if unset)
50+ # ' git_config_get("aaa.bbb", repo = r)
51+ # ' git_config_set("aaa.bbb", "ccc", repo = r)
52+ # ' git_config_get("aaa.bbb", repo = r)
4853# '
4954# ' unlink(r, recursive = TRUE)
5055# '
5358# ' git_config_global_set("user.name", "Your Name")
5459# ' git_config_global_set("user.email", "your@email.com")
5560# ' git_config_global()
61+ # '
62+ # ' # Get a single global option (returns NULL if unset)
63+ # ' git_config_global_get("user.name")
64+ # ' git_config_global_get("gert.nonexistent")
5665# ' }
5766# ' @export
5867# ' @family git
@@ -70,10 +79,41 @@ git_config_global <- function() {
7079 .Call(R_git_config_list , NULL )
7180}
7281
82+ # ' @export
83+ # ' @rdname git_config
84+ # ' @param name Name of the option to get or set
85+ git_config_get <- function (name , repo = ' .' ) {
86+ cfg <- git_config(repo = repo )
87+ if (! name %in% cfg $ name ) {
88+ return (NULL )
89+ }
90+ cfg $ value [cfg $ name == name ]
91+ }
92+
93+ # ' @export
94+ # ' @rdname git_config
95+ git_config_local_get <- function (name , repo = ' .' ) {
96+ cfg <- git_config(repo = repo )
97+ cfg <- cfg [cfg $ level == " local" , ]
98+ if (! name %in% cfg $ name ) {
99+ return (NULL )
100+ }
101+ cfg $ value [cfg $ name == name ]
102+ }
103+
104+ # ' @export
105+ # ' @rdname git_config
106+ git_config_global_get <- function (name ) {
107+ cfg <- git_config_global()
108+ if (! name %in% cfg $ name ) {
109+ return (NULL )
110+ }
111+ cfg $ value [cfg $ name == name ]
112+ }
113+
73114# ' @export
74115# ' @rdname git_config
75116# ' @useDynLib gert R_git_config_set
76- # ' @param name Name of the option to set
77117# ' @param value Value to set. Must be a string, logical, number or `NULL` (to
78118# ' unset).
79119# ' @param add if `TRUE`, append a new entry for `name` instead of replacing
@@ -85,8 +125,7 @@ git_config_set <- function(name, value, add = FALSE, repo = '.') {
85125 }
86126 repo <- git_open(repo )
87127 name <- as.character(name )
88- orig_cfg <- git_config(repo = repo )
89- out <- orig_cfg $ value [orig_cfg $ name == name & orig_cfg $ level == " local" ]
128+ out <- git_config_local_get(name , repo = repo )
90129 .Call(R_git_config_set , repo , name , value , add )
91130 if (length(out ) > 0 ) {
92131 invisible (out )
@@ -98,8 +137,7 @@ git_config_set <- function(name, value, add = FALSE, repo = '.') {
98137# ' @export
99138# ' @rdname git_config
100139git_config_global_set <- function (name , value , add = FALSE ) {
101- orig_cfg <- git_config_global()
102- out <- orig_cfg $ value [orig_cfg $ name == name ]
140+ out <- git_config_global_get(name )
103141 .Call(R_git_config_set , NULL , name , value , add )
104142 if (length(out ) > 0 ) {
105143 invisible (out )
@@ -141,9 +179,8 @@ configure_global_user <- function() {
141179}
142180
143181global_user_is_configured <- function () {
144- cfg <- git_config_global()
145- user_name_exists <- any(cfg $ name == " user.name" )
146- user_email_exists <- any(cfg $ name == " user.email" )
182+ user_name_exists <- ! is.null(git_config_global_get(" user.name" ))
183+ user_email_exists <- ! is.null(git_config_global_get(" user.email" ))
147184 user_name_exists && user_email_exists
148185}
149186
@@ -161,14 +198,10 @@ global_user_is_configured <- function() {
161198# ' `FALSE` otherwise.
162199# '
163200# ' @export
164- # ' @examples
201+ # ' @examplesIf interactive()
165202# ' user_is_configured()
166203user_is_configured <- function (repo = " ." ) {
167- cfg <- tryCatch(
168- git_config(repo ),
169- error = function (e ) git_config_global()
170- )
171- user_name_exists <- any(cfg $ name == " user.name" )
172- user_email_exists <- any(cfg $ name == " user.email" )
204+ user_name_exists <- ! is.null(git_config_get(" user.name" , repo = repo ))
205+ user_email_exists <- ! is.null(git_config_get(" user.email" , repo = repo ))
173206 user_name_exists && user_email_exists
174207}
0 commit comments