Skip to content

Commit 504ee12

Browse files
derrickstoleegitster
authored andcommitted
config: convert multi_replace to flags
We will extend the flexibility of the config API. Before doing so, let's take an existing 'int multi_replace' parameter and replace it with a new 'unsigned flags' parameter that can take multiple options as a bit field. Update all callers that specified multi_replace to now specify the CONFIG_FLAGS_MULTI_REPLACE flag. To add more clarity, extend the documentation of git_config_set_multivar_in_file() including a clear labeling of its arguments. Other config API methods in config.h require only a change of the final parameter from 'int' to 'unsigned'. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent faefdd6 commit 504ee12

File tree

5 files changed

+45
-26
lines changed

5 files changed

+45
-26
lines changed

builtin/branch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
829829
die(_("Branch '%s' has no upstream information"), branch->name);
830830

831831
strbuf_addf(&buf, "branch.%s.remote", branch->name);
832-
git_config_set_multivar(buf.buf, NULL, NULL, 1);
832+
git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
833833
strbuf_reset(&buf);
834834
strbuf_addf(&buf, "branch.%s.merge", branch->name);
835-
git_config_set_multivar(buf.buf, NULL, NULL, 1);
835+
git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
836836
strbuf_release(&buf);
837837
} else if (argc > 0 && argc <= 2) {
838838
if (filter.kind != FILTER_REFS_BRANCHES)

builtin/config.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
844844
value = normalize_value(argv[0], argv[1]);
845845
UNLEAK(value);
846846
return git_config_set_multivar_in_file_gently(given_config_source.file,
847-
argv[0], value, argv[2], 1);
847+
argv[0], value, argv[2],
848+
CONFIG_FLAGS_MULTI_REPLACE);
848849
}
849850
else if (actions == ACTION_GET) {
850851
check_argc(argc, 1, 2);
@@ -880,7 +881,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
880881
check_write();
881882
check_argc(argc, 1, 2);
882883
return git_config_set_multivar_in_file_gently(given_config_source.file,
883-
argv[0], NULL, argv[1], 1);
884+
argv[0], NULL, argv[1],
885+
CONFIG_FLAGS_MULTI_REPLACE);
884886
}
885887
else if (actions == ACTION_RENAME_SECTION) {
886888
int ret;

builtin/remote.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ static int mv(int argc, const char **argv)
712712

713713
strbuf_reset(&buf);
714714
strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
715-
git_config_set_multivar(buf.buf, NULL, NULL, 1);
715+
git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
716716
strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
717717
for (i = 0; i < oldremote->fetch.raw_nr; i++) {
718718
char *ptr;
@@ -1491,7 +1491,8 @@ static int update(int argc, const char **argv)
14911491

14921492
static int remove_all_fetch_refspecs(const char *key)
14931493
{
1494-
return git_config_set_multivar_gently(key, NULL, NULL, 1);
1494+
return git_config_set_multivar_gently(key, NULL, NULL,
1495+
CONFIG_FLAGS_MULTI_REPLACE);
14951496
}
14961497

14971498
static void add_branches(struct remote *remote, const char **branches,
@@ -1686,7 +1687,8 @@ static int set_url(int argc, const char **argv)
16861687
if (!delete_mode)
16871688
git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
16881689
else
1689-
git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1690+
git_config_set_multivar(name_buf.buf, NULL, oldurl,
1691+
CONFIG_FLAGS_MULTI_REPLACE);
16901692
out:
16911693
strbuf_release(&name_buf);
16921694
return 0;

config.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,9 +2729,9 @@ void git_config_set(const char *key, const char *value)
27292729
* if value_regex!=NULL, disregard key/value pairs where value does not match.
27302730
* if value_regex==CONFIG_REGEX_NONE, do not match any existing values
27312731
* (only add a new one)
2732-
* if multi_replace==0, nothing, or only one matching key/value is replaced,
2733-
* else all matching key/values (regardless how many) are removed,
2734-
* before the new pair is written.
2732+
* if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching
2733+
* key/values are removed before a single new pair is written. If the
2734+
* flag is not present, then replace only the first match.
27352735
*
27362736
* Returns 0 on success.
27372737
*
@@ -2752,7 +2752,7 @@ void git_config_set(const char *key, const char *value)
27522752
int git_config_set_multivar_in_file_gently(const char *config_filename,
27532753
const char *key, const char *value,
27542754
const char *value_regex,
2755-
int multi_replace)
2755+
unsigned flags)
27562756
{
27572757
int fd = -1, in_fd = -1;
27582758
int ret;
@@ -2769,7 +2769,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
27692769
if (ret)
27702770
goto out_free;
27712771

2772-
store.multi_replace = multi_replace;
2772+
store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0;
27732773

27742774
if (!config_filename)
27752775
config_filename = filename_buf = git_pathdup("config");
@@ -2858,7 +2858,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
28582858

28592859
/* if nothing to unset, or too many matches, error out */
28602860
if ((store.seen_nr == 0 && value == NULL) ||
2861-
(store.seen_nr > 1 && multi_replace == 0)) {
2861+
(store.seen_nr > 1 && !store.multi_replace)) {
28622862
ret = CONFIG_NOTHING_SET;
28632863
goto out_free;
28642864
}
@@ -2997,10 +2997,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
29972997

29982998
void git_config_set_multivar_in_file(const char *config_filename,
29992999
const char *key, const char *value,
3000-
const char *value_regex, int multi_replace)
3000+
const char *value_regex, unsigned flags)
30013001
{
30023002
if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
3003-
value_regex, multi_replace))
3003+
value_regex, flags))
30043004
return;
30053005
if (value)
30063006
die(_("could not set '%s' to '%s'"), key, value);
@@ -3009,17 +3009,17 @@ void git_config_set_multivar_in_file(const char *config_filename,
30093009
}
30103010

30113011
int git_config_set_multivar_gently(const char *key, const char *value,
3012-
const char *value_regex, int multi_replace)
3012+
const char *value_regex, unsigned flags)
30133013
{
30143014
return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex,
3015-
multi_replace);
3015+
flags);
30163016
}
30173017

30183018
void git_config_set_multivar(const char *key, const char *value,
3019-
const char *value_regex, int multi_replace)
3019+
const char *value_regex, unsigned flags)
30203020
{
30213021
git_config_set_multivar_in_file(NULL, key, value, value_regex,
3022-
multi_replace);
3022+
flags);
30233023
}
30243024

30253025
static int section_name_match (const char *buf, const char *name)

config.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,22 @@ void git_config_set(const char *, const char *);
256256

257257
int git_config_parse_key(const char *, char **, size_t *);
258258
int git_config_key_is_valid(const char *key);
259-
int git_config_set_multivar_gently(const char *, const char *, const char *, int);
260-
void git_config_set_multivar(const char *, const char *, const char *, int);
261-
int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, int);
259+
260+
/*
261+
* The following macros specify flag bits that alter the behavior
262+
* of the git_config_set_multivar*() methods.
263+
*/
264+
265+
/*
266+
* When CONFIG_FLAGS_MULTI_REPLACE is specified, all matching key/values
267+
* are removed before a single new pair is written. If the flag is not
268+
* present, then set operations replace only the first match.
269+
*/
270+
#define CONFIG_FLAGS_MULTI_REPLACE (1 << 0)
271+
272+
int git_config_set_multivar_gently(const char *, const char *, const char *, unsigned);
273+
void git_config_set_multivar(const char *, const char *, const char *, unsigned);
274+
int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, unsigned);
262275

263276
/**
264277
* takes four parameters:
@@ -276,13 +289,15 @@ int git_config_set_multivar_in_file_gently(const char *, const char *, const cha
276289
* - the value regex, as a string. It will disregard key/value pairs where value
277290
* does not match.
278291
*
279-
* - a multi_replace value, as an int. If value is equal to zero, nothing or only
280-
* one matching key/value is replaced, else all matching key/values (regardless
281-
* how many) are removed, before the new pair is written.
292+
* - a flags value with bits corresponding to the CONFIG_FLAG_* macros.
282293
*
283294
* It returns 0 on success.
284295
*/
285-
void git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
296+
void git_config_set_multivar_in_file(const char *config_filename,
297+
const char *key,
298+
const char *value,
299+
const char *value_regex,
300+
unsigned flags);
286301

287302
/**
288303
* rename or remove sections in the config file

0 commit comments

Comments
 (0)