cli: add a CLI_ASSERT macro

`GIT_ASSERT` is not necessarily useful for the CLI; we need something
that prints to stderr and exits.
This commit is contained in:
Edward Thomson
2023-12-25 21:31:56 +00:00
parent 0239d68bb2
commit 59abaf51e1
5 changed files with 21 additions and 8 deletions

View File

@@ -70,7 +70,7 @@ static int fmt_date(git_str *out, git_time_t time, int offset)
time_t t;
struct tm gmt;
GIT_ASSERT_ARG(out);
CLI_ASSERT(out);
t = (time_t)(time + offset * 60);

View File

@@ -91,7 +91,7 @@ static int print_odb(git_object *object, display_t display)
ret = cli_error_os();
break;
default:
GIT_ASSERT(0);
CLI_ASSERT(!"invalid display type");
}
done:

View File

@@ -48,4 +48,17 @@ GIT_INLINE(int) cli_error_git(void)
#define cli_error_os() (perror(PROGRAM_NAME), CLI_EXIT_OS)
#if (defined(_DEBUG) || defined(GIT_ASSERT_HARD)) && GIT_ASSERT_HARD != 0
# include <assert.h>
# define CLI_ASSERT(expr) assert(expr)
#else
# define CLI_ASSERT(expr) do { \
if (!(expr)) { \
fprintf(stderr, "%s: assertion failed: '%s'\n", \
PROGRAM_NAME, #expr); \
exit(255); \
} \
} while(0)
#endif
#endif /* CLI_error_h__ */

View File

@@ -14,7 +14,7 @@
static int print_spec_args(git_str *out, const cli_opt_spec *spec)
{
GIT_ASSERT(!is_switch_or_value(spec));
CLI_ASSERT(!is_switch_or_value(spec));
if (spec->type == CLI_OPT_TYPE_ARG)
return git_str_printf(out, "<%s>", spec->value_name);
@@ -23,13 +23,13 @@ static int print_spec_args(git_str *out, const cli_opt_spec *spec)
if (spec->type == CLI_OPT_TYPE_LITERAL)
return git_str_printf(out, "--");
GIT_ASSERT(!"unknown option spec type");
CLI_ASSERT(!"unknown option spec type");
return -1;
}
GIT_INLINE(int) print_spec_alias(git_str *out, const cli_opt_spec *spec)
{
GIT_ASSERT(is_switch_or_value(spec) && spec->alias);
CLI_ASSERT(is_switch_or_value(spec) && spec->alias);
if (spec->type == CLI_OPT_TYPE_VALUE &&
!(spec->usage & CLI_OPT_USAGE_VALUE_OPTIONAL))
@@ -42,7 +42,7 @@ GIT_INLINE(int) print_spec_alias(git_str *out, const cli_opt_spec *spec)
GIT_INLINE(int) print_spec_name(git_str *out, const cli_opt_spec *spec)
{
GIT_ASSERT(is_switch_or_value(spec) && spec->name);
CLI_ASSERT(is_switch_or_value(spec) && spec->name);
if (spec->type == CLI_OPT_TYPE_VALUE &&
!(spec->usage & CLI_OPT_USAGE_VALUE_OPTIONAL))

View File

@@ -302,7 +302,7 @@ int cli_progress_fetch_transfer(const git_indexer_progress *stats, void *payload
default:
/* should not be reached */
GIT_ASSERT(!"unexpected progress state");
CLI_ASSERT(!"unexpected progress state");
}
return error;
@@ -337,7 +337,7 @@ int cli_progress_indexer(
default:
/* should not be reached */
GIT_ASSERT(!"unexpected progress state");
CLI_ASSERT(!"unexpected progress state");
}
return error;