Skip to content

Commit 6bb696c

Browse files
committed
Merge branch 'mg/config-symbolic-constants'
* mg/config-symbolic-constants: config: Give error message when not changing a multivar config: define and document exit codes
2 parents 91810ab + 5a2df36 commit 6bb696c

4 files changed

Lines changed: 38 additions & 21 deletions

File tree

Documentation/git-config.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ The default is to assume the config file of the current repository,
5050
.git/config unless defined otherwise with GIT_DIR and GIT_CONFIG
5151
(see <<FILES>>).
5252

53-
This command will fail if:
54-
55-
. The config file is invalid,
56-
. Can not write to the config file,
57-
. no section was provided,
58-
. the section or key is invalid,
59-
. you try to unset an option which does not exist,
60-
. you try to unset/set an option for which multiple lines match, or
61-
. you use '--global' option without $HOME being properly set.
62-
53+
This command will fail (with exit code ret) if:
54+
55+
. The config file is invalid (ret=3),
56+
. can not write to the config file (ret=4),
57+
. no section or name was provided (ret=2),
58+
. the section or key is invalid (ret=1),
59+
. you try to unset an option which does not exist (ret=5),
60+
. you try to unset/set an option for which multiple lines match (ret=5),
61+
. you try to use an invalid regexp (ret=6), or
62+
. you use '--global' option without $HOME being properly set (ret=128).
63+
64+
On success, the command returns the exit code 0.
6365

6466
OPTIONS
6567
-------

builtin/config.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
436436
NULL, NULL);
437437
}
438438
else if (actions == ACTION_SET) {
439+
int ret;
439440
check_argc(argc, 2, 2);
440441
value = normalize_value(argv[0], argv[1]);
441-
return git_config_set(argv[0], value);
442+
ret = git_config_set(argv[0], value);
443+
if (ret == CONFIG_NOTHING_SET)
444+
error("cannot overwrite multiple values with a single value\n"
445+
" Use a regexp, --add or --set-all to change %s.", argv[0]);
446+
return ret;
442447
}
443448
else if (actions == ACTION_SET_ALL) {
444449
check_argc(argc, 2, 3);

cache.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,16 @@ extern const char *packed_object_info_detail(struct packed_git *, off_t, unsigne
10231023
/* Dumb servers support */
10241024
extern int update_server_info(int);
10251025

1026+
/* git_config_parse_key() returns these negated: */
1027+
#define CONFIG_INVALID_KEY 1
1028+
#define CONFIG_NO_SECTION_OR_NAME 2
1029+
/* git_config_set(), git_config_set_multivar() return the above or these: */
1030+
#define CONFIG_NO_LOCK -1
1031+
#define CONFIG_INVALID_FILE 3
1032+
#define CONFIG_NO_WRITE 4
1033+
#define CONFIG_NOTHING_SET 5
1034+
#define CONFIG_INVALID_PATTERN 6
1035+
10261036
typedef int (*config_fn_t)(const char *, const char *, void *);
10271037
extern int git_default_config(const char *, const char *, void *);
10281038
extern int git_config_from_file(config_fn_t fn, const char *, void *);

config.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,12 +1123,12 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
11231123

11241124
if (last_dot == NULL || last_dot == key) {
11251125
error("key does not contain a section: %s", key);
1126-
return -2;
1126+
return -CONFIG_NO_SECTION_OR_NAME;
11271127
}
11281128

11291129
if (!last_dot[1]) {
11301130
error("key does not contain variable name: %s", key);
1131-
return -2;
1131+
return -CONFIG_NO_SECTION_OR_NAME;
11321132
}
11331133

11341134
baselen = last_dot - key;
@@ -1165,7 +1165,7 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
11651165

11661166
out_free_ret_1:
11671167
free(*store_key);
1168-
return -1;
1168+
return -CONFIG_INVALID_KEY;
11691169
}
11701170

11711171
/*
@@ -1221,7 +1221,7 @@ int git_config_set_multivar(const char *key, const char *value,
12211221
if (fd < 0) {
12221222
error("could not lock config file %s: %s", config_filename, strerror(errno));
12231223
free(store.key);
1224-
ret = -1;
1224+
ret = CONFIG_NO_LOCK;
12251225
goto out_free;
12261226
}
12271227

@@ -1235,12 +1235,12 @@ int git_config_set_multivar(const char *key, const char *value,
12351235
if ( ENOENT != errno ) {
12361236
error("opening %s: %s", config_filename,
12371237
strerror(errno));
1238-
ret = 3; /* same as "invalid config file" */
1238+
ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
12391239
goto out_free;
12401240
}
12411241
/* if nothing to unset, error out */
12421242
if (value == NULL) {
1243-
ret = 5;
1243+
ret = CONFIG_NOTHING_SET;
12441244
goto out_free;
12451245
}
12461246

@@ -1268,7 +1268,7 @@ int git_config_set_multivar(const char *key, const char *value,
12681268
REG_EXTENDED)) {
12691269
error("invalid pattern: %s", value_regex);
12701270
free(store.value_regex);
1271-
ret = 6;
1271+
ret = CONFIG_INVALID_PATTERN;
12721272
goto out_free;
12731273
}
12741274
}
@@ -1290,7 +1290,7 @@ int git_config_set_multivar(const char *key, const char *value,
12901290
regfree(store.value_regex);
12911291
free(store.value_regex);
12921292
}
1293-
ret = 3;
1293+
ret = CONFIG_INVALID_FILE;
12941294
goto out_free;
12951295
}
12961296

@@ -1303,7 +1303,7 @@ int git_config_set_multivar(const char *key, const char *value,
13031303
/* if nothing to unset, or too many matches, error out */
13041304
if ((store.seen == 0 && value == NULL) ||
13051305
(store.seen > 1 && multi_replace == 0)) {
1306-
ret = 5;
1306+
ret = CONFIG_NOTHING_SET;
13071307
goto out_free;
13081308
}
13091309

@@ -1364,7 +1364,7 @@ int git_config_set_multivar(const char *key, const char *value,
13641364

13651365
if (commit_lock_file(lock) < 0) {
13661366
error("could not commit config file %s", config_filename);
1367-
ret = 4;
1367+
ret = CONFIG_NO_WRITE;
13681368
goto out_free;
13691369
}
13701370

0 commit comments

Comments
 (0)