Skip to content

Commit 7a39741

Browse files
Michael J Grubergitster
authored andcommitted
config: define and document exit codes
The return codes of git_config_set() and friends are magic numbers right in the source. #define them in cache.h where the functions are declared, and use the constants in the source. Also, mention the resulting exit codes of "git config" in its man page (and complete the list). Signed-off-by: Michael J Gruber <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b602ed7 commit 7a39741

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

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
-------

cache.h

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

1015+
/* git_config_parse_key() returns these negated: */
1016+
#define CONFIG_INVALID_KEY 1
1017+
#define CONFIG_NO_SECTION_OR_NAME 2
1018+
/* git_config_set(), git_config_set_multivar() return the above or these: */
1019+
#define CONFIG_NO_LOCK -1
1020+
#define CONFIG_INVALID_FILE 3
1021+
#define CONFIG_NO_WRITE 4
1022+
#define CONFIG_NOTHING_SET 5
1023+
#define CONFIG_INVALID_PATTERN 6
1024+
10151025
typedef int (*config_fn_t)(const char *, const char *, void *);
10161026
extern int git_default_config(const char *, const char *, void *);
10171027
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)