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 <git@drmicha.warpmail.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael J Gruber committed
May 17, 2011 at 17:38 UTC
7a39741999a5216257b1fbcc847cf0c62c114088
3 files changed
+32
-20
Documentation/git-config.txt
+12
-10
@@ -50,16 +50,18 @@ The default is to assume the config file of the current repository,
50
.git/config unless defined otherwise with GIT_DIR and GIT_CONFIG
51
(see <<FILES>>).
52
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.
65
66
OPTIONS
67
-------
cache.h
+10
@@ -1012,6 +1012,16 @@ extern const char *packed_object_info_detail(struct packed_git *, off_t, unsigne
1012
/* Dumb servers support */
1013
extern int update_server_info(int);
1014
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
+
1025
typedef int (*config_fn_t)(const char *, const char *, void *);
1026
extern int git_default_config(const char *, const char *, void *);
1027
extern int git_config_from_file(config_fn_t fn, const char *, void *);
config.c
+10
-10
@@ -1123,12 +1123,12 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
1123
1124
if (last_dot == NULL || last_dot == key) {
1125
error("key does not contain a section: %s", key);
1126
- return -2;
1126
+ return -CONFIG_NO_SECTION_OR_NAME;
1127
}
1128
1129
if (!last_dot[1]) {
1130
error("key does not contain variable name: %s", key);
1131
- return -2;
1131
+ return -CONFIG_NO_SECTION_OR_NAME;
1132
}
1133
1134
baselen = last_dot - key;
@@ -1165,7 +1165,7 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
1165
1166
out_free_ret_1:
1167
free(*store_key);
1168
- return -1;
1168
+ return -CONFIG_INVALID_KEY;
1169
}
1170
1171
/*
@@ -1221,7 +1221,7 @@ int git_config_set_multivar(const char *key, const char *value,
1221
if (fd < 0) {
1222
error("could not lock config file %s: %s", config_filename, strerror(errno));
1223
free(store.key);
1224
- ret = -1;
1224
+ ret = CONFIG_NO_LOCK;
1225
goto out_free;
1226
}
1227
@@ -1235,12 +1235,12 @@ int git_config_set_multivar(const char *key, const char *value,
1235
if ( ENOENT != errno ) {
1236
error("opening %s: %s", config_filename,
1237
strerror(errno));
1238
- ret = 3; /* same as "invalid config file" */
1238
+ ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
1239
goto out_free;
1240
}
1241
/* if nothing to unset, error out */
1242
if (value == NULL) {
1243
- ret = 5;
1243
+ ret = CONFIG_NOTHING_SET;
1244
goto out_free;
1245
}
1246
@@ -1268,7 +1268,7 @@ int git_config_set_multivar(const char *key, const char *value,
1268
REG_EXTENDED)) {
1269
error("invalid pattern: %s", value_regex);
1270
free(store.value_regex);
1271
- ret = 6;
1271
+ ret = CONFIG_INVALID_PATTERN;
1272
goto out_free;
1273
}
1274
}
@@ -1290,7 +1290,7 @@ int git_config_set_multivar(const char *key, const char *value,
1290
regfree(store.value_regex);
1291
free(store.value_regex);
1292
}
1293
- ret = 3;
1293
+ ret = CONFIG_INVALID_FILE;
1294
goto out_free;
1295
}
1296
@@ -1303,7 +1303,7 @@ int git_config_set_multivar(const char *key, const char *value,
1303
/* if nothing to unset, or too many matches, error out */
1304
if ((store.seen == 0 && value == NULL) ||
1305
(store.seen > 1 && multi_replace == 0)) {
1306
- ret = 5;
1306
+ ret = CONFIG_NOTHING_SET;
1307
goto out_free;
1308
}
1309
@@ -1364,7 +1364,7 @@ int git_config_set_multivar(const char *key, const char *value,
1364
1365
if (commit_lock_file(lock) < 0) {
1366
error("could not commit config file %s", config_filename);
1367
- ret = 4;
1367
+ ret = CONFIG_NO_WRITE;
1368
goto out_free;
1369
}
1370