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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 25, 2020 at 22:12 UTC 504ee1290e38fb1ff0d76f940b124e21ab57a99f
5 files changed +45 -26
builtin/branch.c
+2 -2
@@ -829,10 +829,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
829 die(_("Branch '%s' has no upstream information"), branch->name);
830
831 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);
833 strbuf_reset(&buf);
834 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);
836 strbuf_release(&buf);
837 } else if (argc > 0 && argc <= 2) {
838 if (filter.kind != FILTER_REFS_BRANCHES)
builtin/config.c
+4 -2
@@ -844,7 +844,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
844 value = normalize_value(argv[0], argv[1]);
845 UNLEAK(value);
846 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);
849 }
850 else if (actions == ACTION_GET) {
851 check_argc(argc, 1, 2);
@@ -880,7 +881,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
881 check_write();
882 check_argc(argc, 1, 2);
883 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);
886 }
887 else if (actions == ACTION_RENAME_SECTION) {
888 int ret;
builtin/remote.c
+5 -3
@@ -712,7 +712,7 @@ static int mv(int argc, const char **argv)
712
713 strbuf_reset(&buf);
714 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);
716 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
717 for (i = 0; i < oldremote->fetch.raw_nr; i++) {
718 char *ptr;
@@ -1491,7 +1491,8 @@ static int update(int argc, const char **argv)
1491
1492 static int remove_all_fetch_refspecs(const char *key)
1493 {
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);
1496 }
1497
1498 static void add_branches(struct remote *remote, const char **branches,
@@ -1686,7 +1687,8 @@ static int set_url(int argc, const char **argv)
1687 if (!delete_mode)
1688 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1689 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);
1692 out:
1693 strbuf_release(&name_buf);
1694 return 0;
config.c
+12 -12
@@ -2729,9 +2729,9 @@ void git_config_set(const char *key, const char *value)
2729 * if value_regex!=NULL, disregard key/value pairs where value does not match.
2730 * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
2731 * (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.
2735 *
2736 * Returns 0 on success.
2737 *
@@ -2752,7 +2752,7 @@ void git_config_set(const char *key, const char *value)
2752 int git_config_set_multivar_in_file_gently(const char *config_filename,
2753 const char *key, const char *value,
2754 const char *value_regex,
2755 - int multi_replace)
2755 + unsigned flags)
2756 {
2757 int fd = -1, in_fd = -1;
2758 int ret;
@@ -2769,7 +2769,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2769 if (ret)
2770 goto out_free;
2771
2772 - store.multi_replace = multi_replace;
2772 + store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0;
2773
2774 if (!config_filename)
2775 config_filename = filename_buf = git_pathdup("config");
@@ -2858,7 +2858,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2858
2859 /* if nothing to unset, or too many matches, error out */
2860 if ((store.seen_nr == 0 && value == NULL) ||
2861 - (store.seen_nr > 1 && multi_replace == 0)) {
2861 + (store.seen_nr > 1 && !store.multi_replace)) {
2862 ret = CONFIG_NOTHING_SET;
2863 goto out_free;
2864 }
@@ -2997,10 +2997,10 @@ write_err_out:
2997
2998 void git_config_set_multivar_in_file(const char *config_filename,
2999 const char *key, const char *value,
3000 - const char *value_regex, int multi_replace)
3000 + const char *value_regex, unsigned flags)
3001 {
3002 if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
3003 - value_regex, multi_replace))
3003 + value_regex, flags))
3004 return;
3005 if (value)
3006 die(_("could not set '%s' to '%s'"), key, value);
@@ -3009,17 +3009,17 @@ void git_config_set_multivar_in_file(const char *config_filename,
3009 }
3010
3011 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)
3013 {
3014 return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex,
3015 - multi_replace);
3015 + flags);
3016 }
3017
3018 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)
3020 {
3021 git_config_set_multivar_in_file(NULL, key, value, value_regex,
3022 - multi_replace);
3022 + flags);
3023 }
3024
3025 static int section_name_match (const char *buf, const char *name)
config.h
+22 -7
@@ -256,9 +256,22 @@ void git_config_set(const char *, const char *);
256
257 int git_config_parse_key(const char *, char **, size_t *);
258 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);
275
276 /**
277 * takes four parameters:
@@ -276,13 +289,15 @@ int git_config_set_multivar_in_file_gently(const char *, const char *, const cha
289 * - the value regex, as a string. It will disregard key/value pairs where value
290 * does not match.
291 *
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.
293 *
294 * It returns 0 on success.
295 */
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);
301
302 /**
303 * rename or remove sections in the config file