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>
Alex Riesen committed
May 12, 2008 at 23:41 UTC
64c0d71ce91696dfe5beb4b51e3233e56c857290
1 file changed
+16
-19
config.c
+16
-19
@@ -627,11 +627,9 @@ static int store_aux(const char* key, const char* value)
627
case KEY_SEEN:
628
if (matches(key, value)) {
629
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);
631
} else if (store.seen >= MAX_MATCHES) {
634
- fprintf(stderr, "Too many matches\n");
632
+ error("too many matches for %s", key);
633
return 1;
634
}
635
@@ -681,9 +679,9 @@ static int store_aux(const char* key, const char* value)
679
return 0;
680
}
681
684
-static int write_error(void)
682
+static int write_error(const char *filename)
683
{
686
- fprintf(stderr, "Failed to write new configuration file\n");
684
+ error("failed to write new configuration file %s", filename);
685
686
/* Same error code as "failed to rename". */
687
return 4;
@@ -842,7 +840,7 @@ int git_config_set_multivar(const char* key, const char* value,
840
*/
841
842
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);
844
ret = 2;
845
goto out_free;
846
}
@@ -862,14 +860,14 @@ int git_config_set_multivar(const char* key, const char* value,
860
/* Leave the extended basename untouched.. */
861
if (!dot || i > store.baselen) {
862
if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
865
- fprintf(stderr, "invalid key: %s\n", key);
863
+ error("invalid key: %s", key);
864
free(store.key);
865
ret = 1;
866
goto out_free;
867
}
868
c = tolower(c);
869
} else if (c == '\n') {
872
- fprintf(stderr, "invalid key (newline): %s\n", key);
870
+ error("invalid key (newline): %s", key);
871
free(store.key);
872
ret = 1;
873
goto out_free;
@@ -885,7 +883,7 @@ int git_config_set_multivar(const char* key, const char* value,
883
lock = xcalloc(sizeof(struct lock_file), 1);
884
fd = hold_lock_file_for_update(lock, config_filename, 0);
885
if (fd < 0) {
888
- fprintf(stderr, "could not lock config file\n");
886
+ error("could not lock config file %s", config_filename);
887
free(store.key);
888
ret = -1;
889
goto out_free;
@@ -932,8 +930,7 @@ int git_config_set_multivar(const char* key, const char* value,
930
store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
931
if (regcomp(store.value_regex, value_regex,
932
REG_EXTENDED)) {
935
- fprintf(stderr, "Invalid pattern: %s\n",
936
- value_regex);
933
+ error("invalid pattern: %s", value_regex);
934
free(store.value_regex);
935
ret = 6;
936
goto out_free;
@@ -951,7 +948,7 @@ int git_config_set_multivar(const char* key, const char* value,
948
* existing config file.
949
*/
950
if (git_config_from_file(store_aux, config_filename)) {
954
- fprintf(stderr, "invalid config file\n");
951
+ error("invalid config file %s", config_filename);
952
free(store.key);
953
if (store.value_regex != NULL) {
954
regfree(store.value_regex);
@@ -1030,7 +1027,7 @@ int git_config_set_multivar(const char* key, const char* value,
1027
}
1028
1029
if (commit_lock_file(lock) < 0) {
1033
- fprintf(stderr, "Cannot commit config file!\n");
1030
+ error("could not commit config file %s", config_filename);
1031
ret = 4;
1032
goto out_free;
1033
}
@@ -1051,7 +1048,7 @@ out_free:
1048
return ret;
1049
1050
write_err_out:
1054
- ret = write_error();
1051
+ ret = write_error(lock->filename);
1052
goto out_free;
1053
1054
}
@@ -1101,7 +1098,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
1098
config_filename = xstrdup(config_filename);
1099
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
1100
if (out_fd < 0) {
1104
- ret = error("Could not lock config file!");
1101
+ ret = error("could not lock config file %s", config_filename);
1102
goto out;
1103
}
1104
@@ -1125,7 +1122,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
1122
}
1123
store.baselen = strlen(new_name);
1124
if (!store_write_section(out_fd, new_name)) {
1128
- ret = write_error();
1125
+ ret = write_error(lock->filename);
1126
goto out;
1127
}
1128
continue;
@@ -1136,14 +1133,14 @@ int git_config_rename_section(const char *old_name, const char *new_name)
1133
continue;
1134
length = strlen(buf);
1135
if (write_in_full(out_fd, buf, length) != length) {
1139
- ret = write_error();
1136
+ ret = write_error(lock->filename);
1137
goto out;
1138
}
1139
}
1140
fclose(config_file);
1141
unlock_and_out:
1142
if (commit_lock_file(lock) < 0)
1146
- ret = error("Cannot commit config file!");
1143
+ ret = error("could not commit config file %s", config_filename);
1144
out:
1145
free(config_filename);
1146
return ret;