Skip to content

Commit 64c0d71

Browse files
raalkmlgitster
authored andcommitted
Improve reporting of errors in config file routines
Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent eafa29b commit 64c0d71

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

config.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,9 @@ static int store_aux(const char* key, const char* value)
627627
case KEY_SEEN:
628628
if (matches(key, value)) {
629629
if (store.seen == 1 && store.multi_replace == 0) {
630-
fprintf(stderr,
631-
"Warning: %s has multiple values\n",
632-
key);
630+
warning("%s has multiple values", key);
633631
} else if (store.seen >= MAX_MATCHES) {
634-
fprintf(stderr, "Too many matches\n");
632+
error("too many matches for %s", key);
635633
return 1;
636634
}
637635

@@ -681,9 +679,9 @@ static int store_aux(const char* key, const char* value)
681679
return 0;
682680
}
683681

684-
static int write_error(void)
682+
static int write_error(const char *filename)
685683
{
686-
fprintf(stderr, "Failed to write new configuration file\n");
684+
error("failed to write new configuration file %s", filename);
687685

688686
/* Same error code as "failed to rename". */
689687
return 4;
@@ -842,7 +840,7 @@ int git_config_set_multivar(const char* key, const char* value,
842840
*/
843841

844842
if (last_dot == NULL) {
845-
fprintf(stderr, "key does not contain a section: %s\n", key);
843+
error("key does not contain a section: %s", key);
846844
ret = 2;
847845
goto out_free;
848846
}
@@ -862,14 +860,14 @@ int git_config_set_multivar(const char* key, const char* value,
862860
/* Leave the extended basename untouched.. */
863861
if (!dot || i > store.baselen) {
864862
if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
865-
fprintf(stderr, "invalid key: %s\n", key);
863+
error("invalid key: %s", key);
866864
free(store.key);
867865
ret = 1;
868866
goto out_free;
869867
}
870868
c = tolower(c);
871869
} else if (c == '\n') {
872-
fprintf(stderr, "invalid key (newline): %s\n", key);
870+
error("invalid key (newline): %s", key);
873871
free(store.key);
874872
ret = 1;
875873
goto out_free;
@@ -885,7 +883,7 @@ int git_config_set_multivar(const char* key, const char* value,
885883
lock = xcalloc(sizeof(struct lock_file), 1);
886884
fd = hold_lock_file_for_update(lock, config_filename, 0);
887885
if (fd < 0) {
888-
fprintf(stderr, "could not lock config file\n");
886+
error("could not lock config file %s", config_filename);
889887
free(store.key);
890888
ret = -1;
891889
goto out_free;
@@ -932,8 +930,7 @@ int git_config_set_multivar(const char* key, const char* value,
932930
store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
933931
if (regcomp(store.value_regex, value_regex,
934932
REG_EXTENDED)) {
935-
fprintf(stderr, "Invalid pattern: %s\n",
936-
value_regex);
933+
error("invalid pattern: %s", value_regex);
937934
free(store.value_regex);
938935
ret = 6;
939936
goto out_free;
@@ -951,7 +948,7 @@ int git_config_set_multivar(const char* key, const char* value,
951948
* existing config file.
952949
*/
953950
if (git_config_from_file(store_aux, config_filename)) {
954-
fprintf(stderr, "invalid config file\n");
951+
error("invalid config file %s", config_filename);
955952
free(store.key);
956953
if (store.value_regex != NULL) {
957954
regfree(store.value_regex);
@@ -1030,7 +1027,7 @@ int git_config_set_multivar(const char* key, const char* value,
10301027
}
10311028

10321029
if (commit_lock_file(lock) < 0) {
1033-
fprintf(stderr, "Cannot commit config file!\n");
1030+
error("could not commit config file %s", config_filename);
10341031
ret = 4;
10351032
goto out_free;
10361033
}
@@ -1051,7 +1048,7 @@ int git_config_set_multivar(const char* key, const char* value,
10511048
return ret;
10521049

10531050
write_err_out:
1054-
ret = write_error();
1051+
ret = write_error(lock->filename);
10551052
goto out_free;
10561053

10571054
}
@@ -1101,7 +1098,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
11011098
config_filename = xstrdup(config_filename);
11021099
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
11031100
if (out_fd < 0) {
1104-
ret = error("Could not lock config file!");
1101+
ret = error("could not lock config file %s", config_filename);
11051102
goto out;
11061103
}
11071104

@@ -1125,7 +1122,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
11251122
}
11261123
store.baselen = strlen(new_name);
11271124
if (!store_write_section(out_fd, new_name)) {
1128-
ret = write_error();
1125+
ret = write_error(lock->filename);
11291126
goto out;
11301127
}
11311128
continue;
@@ -1136,14 +1133,14 @@ int git_config_rename_section(const char *old_name, const char *new_name)
11361133
continue;
11371134
length = strlen(buf);
11381135
if (write_in_full(out_fd, buf, length) != length) {
1139-
ret = write_error();
1136+
ret = write_error(lock->filename);
11401137
goto out;
11411138
}
11421139
}
11431140
fclose(config_file);
11441141
unlock_and_out:
11451142
if (commit_lock_file(lock) < 0)
1146-
ret = error("Cannot commit config file!");
1143+
ret = error("could not commit config file %s", config_filename);
11471144
out:
11481145
free(config_filename);
11491146
return ret;

0 commit comments

Comments
 (0)