Skip to content

Commit

Permalink
config: define and document exit codes
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Michael J Gruber authored and Junio C Hamano committed May 18, 2011
1 parent b602ed7 commit 7a39741
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
22 changes: 12 additions & 10 deletions Documentation/git-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ The default is to assume the config file of the current repository,
.git/config unless defined otherwise with GIT_DIR and GIT_CONFIG
(see <<FILES>>).

This command will fail if:

. The config file is invalid,
. Can not write to the config file,
. no section was provided,
. the section or key is invalid,
. you try to unset an option which does not exist,
. you try to unset/set an option for which multiple lines match, or
. you use '--global' option without $HOME being properly set.

This command will fail (with exit code ret) if:

. The config file is invalid (ret=3),
. can not write to the config file (ret=4),
. no section or name was provided (ret=2),
. the section or key is invalid (ret=1),
. you try to unset an option which does not exist (ret=5),
. you try to unset/set an option for which multiple lines match (ret=5),
. you try to use an invalid regexp (ret=6), or
. you use '--global' option without $HOME being properly set (ret=128).

On success, the command returns the exit code 0.

OPTIONS
-------
Expand Down
10 changes: 10 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,16 @@ extern const char *packed_object_info_detail(struct packed_git *, off_t, unsigne
/* Dumb servers support */
extern int update_server_info(int);

/* git_config_parse_key() returns these negated: */
#define CONFIG_INVALID_KEY 1
#define CONFIG_NO_SECTION_OR_NAME 2
/* git_config_set(), git_config_set_multivar() return the above or these: */
#define CONFIG_NO_LOCK -1
#define CONFIG_INVALID_FILE 3
#define CONFIG_NO_WRITE 4
#define CONFIG_NOTHING_SET 5
#define CONFIG_INVALID_PATTERN 6

typedef int (*config_fn_t)(const char *, const char *, void *);
extern int git_default_config(const char *, const char *, void *);
extern int git_config_from_file(config_fn_t fn, const char *, void *);
Expand Down
20 changes: 10 additions & 10 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1123,12 +1123,12 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)

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

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

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

out_free_ret_1:
free(*store_key);
return -1;
return -CONFIG_INVALID_KEY;
}

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

Expand All @@ -1235,12 +1235,12 @@ int git_config_set_multivar(const char *key, const char *value,
if ( ENOENT != errno ) {
error("opening %s: %s", config_filename,
strerror(errno));
ret = 3; /* same as "invalid config file" */
ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
goto out_free;
}
/* if nothing to unset, error out */
if (value == NULL) {
ret = 5;
ret = CONFIG_NOTHING_SET;
goto out_free;
}

Expand Down Expand Up @@ -1268,7 +1268,7 @@ int git_config_set_multivar(const char *key, const char *value,
REG_EXTENDED)) {
error("invalid pattern: %s", value_regex);
free(store.value_regex);
ret = 6;
ret = CONFIG_INVALID_PATTERN;
goto out_free;
}
}
Expand All @@ -1290,7 +1290,7 @@ int git_config_set_multivar(const char *key, const char *value,
regfree(store.value_regex);
free(store.value_regex);
}
ret = 3;
ret = CONFIG_INVALID_FILE;
goto out_free;
}

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

Expand Down Expand Up @@ -1364,7 +1364,7 @@ int git_config_set_multivar(const char *key, const char *value,

if (commit_lock_file(lock) < 0) {
error("could not commit config file %s", config_filename);
ret = 4;
ret = CONFIG_NO_WRITE;
goto out_free;
}

Expand Down

0 comments on commit 7a39741

Please sign in to comment.