1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-18 01:56:15 +02:00

Merge branch 'ab/usage-die-message'

Code clean-up to hide vreportf() from public API.

* ab/usage-die-message:
  config API: use get_error_routine(), not vreportf()
  usage.c + gc: add and use a die_message_errno()
  gc: return from cmd_gc(), don't call exit()
  usage.c API users: use die_message() for error() + exit 128
  usage.c API users: use die_message() for "fatal :" + exit 128
  usage.c: add a die_message() routine
This commit is contained in:
Junio C Hamano 2022-01-10 11:52:53 -08:00
commit 4b51386bbf
9 changed files with 71 additions and 34 deletions

View File

@ -401,16 +401,18 @@ static void dump_marks(void);
static NORETURN void die_nicely(const char *err, va_list params)
{
va_list cp;
static int zombie;
char message[2 * PATH_MAX];
report_fn die_message_fn = get_die_message_routine();
vsnprintf(message, sizeof(message), err, params);
fputs("fatal: ", stderr);
fputs(message, stderr);
fputc('\n', stderr);
va_copy(cp, params);
die_message_fn(err, params);
if (!zombie) {
char message[2 * PATH_MAX];
zombie = 1;
vsnprintf(message, sizeof(message), err, cp);
write_crash_report(message);
end_packfile();
unkeep_all_packs();

View File

@ -470,7 +470,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
/*
* Returns 0 if there was no previous error and gc can proceed, 1 if
* gc should not proceed due to an error in the last run. Prints a
* message and returns -1 if an error occurred while reading gc.log
* message and returns with a non-[01] status code if an error occurred
* while reading gc.log
*/
static int report_last_gc_error(void)
{
@ -484,7 +485,7 @@ static int report_last_gc_error(void)
if (errno == ENOENT)
goto done;
ret = error_errno(_("cannot stat '%s'"), gc_log_path);
ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
goto done;
}
@ -493,7 +494,7 @@ static int report_last_gc_error(void)
len = strbuf_read_file(&sb, gc_log_path, 0);
if (len < 0)
ret = error_errno(_("cannot read '%s'"), gc_log_path);
ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
else if (len > 0) {
/*
* A previous gc failed. Report the error, and don't
@ -611,12 +612,13 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
}
if (detach_auto) {
int ret = report_last_gc_error();
if (ret < 0)
/* an I/O error occurred, already reported */
exit(128);
if (ret == 1)
/* Last gc --auto failed. Skip this one. */
return 0;
else if (ret)
/* an I/O error occurred, already reported */
return ret;
if (lock_repo_for_gc(force, &pid))
return 0;

View File

@ -200,11 +200,12 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
static void write_note_data(struct note_data *d, struct object_id *oid)
{
if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) {
error(_("unable to write note object"));
int status = die_message(_("unable to write note object"));
if (d->edit_path)
error(_("the note contents have been left in %s"),
d->edit_path);
exit(128);
die_message(_("the note contents have been left in %s"),
d->edit_path);
exit(status);
}
}

View File

@ -2555,11 +2555,12 @@ void git_die_config(const char *key, const char *err, ...)
{
const struct string_list *values;
struct key_value_info *kv_info;
report_fn error_fn = get_error_routine();
if (err) {
va_list params;
va_start(params, err);
vreportf("error: ", err, params);
error_fn(err, params);
va_end(params);
}
values = git_config_get_value_multi(key);

View File

@ -498,11 +498,12 @@ static inline int git_has_dir_sep(const char *path)
struct strbuf;
/* General helper functions */
void vreportf(const char *prefix, const char *err, va_list params);
NORETURN void usage(const char *err);
NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
int die_message(const char *err, ...) __attribute__((format (printf, 1, 2)));
int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
@ -537,6 +538,7 @@ static inline int const_error(void)
typedef void (*report_fn)(const char *, va_list params);
void set_die_routine(NORETURN_PTR report_fn routine);
report_fn get_die_message_routine(void);
void set_error_routine(report_fn routine);
report_fn get_error_routine(void);
void set_warn_routine(report_fn routine);

View File

@ -659,8 +659,9 @@ static NORETURN void die_webcgi(const char *err, va_list params)
{
if (dead <= 1) {
struct strbuf hdr = STRBUF_INIT;
report_fn die_message_fn = get_die_message_routine();
vreportf("fatal: ", err, params);
die_message_fn(err, params);
http_status(&hdr, 500, "Internal Server Error");
hdr_nocache(&hdr);

View File

@ -1076,6 +1076,6 @@ void NORETURN usage_msg_opt(const char *msg,
const char * const *usagestr,
const struct option *options)
{
fprintf(stderr, "fatal: %s\n\n", msg);
die_message("%s\n", msg); /* The extra \n is intentional */
usage_with_options(usagestr, options);
}

View File

@ -340,15 +340,6 @@ static void child_close_pair(int fd[2])
child_close(fd[1]);
}
/*
* parent will make it look like the child spewed a fatal error and died
* this is needed to prevent changes to t0061.
*/
static void fake_fatal(const char *err, va_list params)
{
vreportf("fatal: ", err, params);
}
static void child_error_fn(const char *err, va_list params)
{
const char msg[] = "error() should not be called in child\n";
@ -372,9 +363,10 @@ static void NORETURN child_die_fn(const char *err, va_list params)
static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
{
static void (*old_errfn)(const char *err, va_list params);
report_fn die_message_routine = get_die_message_routine();
old_errfn = get_error_routine();
set_error_routine(fake_fatal);
set_error_routine(die_message_routine);
errno = cerr->syserr;
switch (cerr->err) {
@ -1072,7 +1064,9 @@ static void *run_thread(void *data)
static NORETURN void die_async(const char *err, va_list params)
{
vreportf("fatal: ", err, params);
report_fn die_message_fn = get_die_message_routine();
die_message_fn(err, params);
if (in_async()) {
struct async *async = pthread_getspecific(async_key);

42
usage.c
View File

@ -6,7 +6,7 @@
#include "git-compat-util.h"
#include "cache.h"
void vreportf(const char *prefix, const char *err, va_list params)
static void vreportf(const char *prefix, const char *err, va_list params)
{
char msg[4096];
char *p, *pend = msg + sizeof(msg);
@ -55,6 +55,12 @@ static NORETURN void usage_builtin(const char *err, va_list params)
exit(129);
}
static void die_message_builtin(const char *err, va_list params)
{
trace2_cmd_error_va(err, params);
vreportf("fatal: ", err, params);
}
/*
* We call trace2_cmd_error_va() in the below functions first and
* expect it to va_copy 'params' before using it (because an 'ap' can
@ -62,10 +68,9 @@ static NORETURN void usage_builtin(const char *err, va_list params)
*/
static NORETURN void die_builtin(const char *err, va_list params)
{
trace2_cmd_error_va(err, params);
vreportf("fatal: ", err, params);
report_fn die_message_fn = get_die_message_routine();
die_message_fn(err, params);
exit(128);
}
@ -109,6 +114,7 @@ static int die_is_recursing_builtin(void)
* (ugh), so keep things static. */
static NORETURN_PTR report_fn usage_routine = usage_builtin;
static NORETURN_PTR report_fn die_routine = die_builtin;
static report_fn die_message_routine = die_message_builtin;
static report_fn error_routine = error_builtin;
static report_fn warn_routine = warn_builtin;
static int (*die_is_recursing)(void) = die_is_recursing_builtin;
@ -118,6 +124,11 @@ void set_die_routine(NORETURN_PTR report_fn routine)
die_routine = routine;
}
report_fn get_die_message_routine(void)
{
return die_message_routine;
}
void set_error_routine(report_fn routine)
{
error_routine = routine;
@ -211,6 +222,29 @@ void NORETURN die_errno(const char *fmt, ...)
va_end(params);
}
#undef die_message
int die_message(const char *err, ...)
{
va_list params;
va_start(params, err);
die_message_routine(err, params);
va_end(params);
return 128;
}
#undef die_message_errno
int die_message_errno(const char *fmt, ...)
{
char buf[1024];
va_list params;
va_start(params, fmt);
die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
va_end(params);
return 128;
}
#undef error_errno
int error_errno(const char *fmt, ...)
{