Merge branch 'jk/bug-to-abort'

Introduce the BUG() macro to improve die("BUG: ...").

* jk/bug-to-abort:
  usage: add NORETURN to BUG() function definitions
  config: complain about --local outside of a git repo
  setup_git_env: convert die("BUG") to BUG()
  usage.c: add BUG() function
This commit is contained in:
Junio C Hamano 2017-05-29 12:34:45 +09:00
commit 220c6a7080
5 changed files with 51 additions and 1 deletions

View File

@ -496,6 +496,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
usage_with_options(builtin_config_usage, builtin_config_options);
}
if (use_local_config && nongit)
die(_("--local can only be used inside a git repository"));
if (given_config_source.file &&
!strcmp(given_config_source.file, "-")) {
given_config_source.file = NULL;

View File

@ -169,7 +169,7 @@ static void setup_git_env(void)
git_dir = getenv(GIT_DIR_ENVIRONMENT);
if (!git_dir) {
if (!startup_info->have_repository)
die("BUG: setup_git_env called without repository");
BUG("setup_git_env called without repository");
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
}
gitfile = read_gitfile(git_dir);

View File

@ -1069,6 +1069,15 @@ static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
#define HAVE_VARIADIC_MACROS 1
#endif
#ifdef HAVE_VARIADIC_MACROS
__attribute__((format (printf, 3, 4))) NORETURN
void BUG_fl(const char *file, int line, const char *fmt, ...);
#define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
#else
__attribute__((format (printf, 1, 2))) NORETURN
void BUG(const char *fmt, ...);
#endif
/*
* Preserves errno, prints a message, but gives no warning for ENOENT.
* Returns 0 on success, which includes trying to unlink an object that does

View File

@ -1539,4 +1539,10 @@ test_expect_success !MINGW '--show-origin blob ref' '
test_cmp expect output
'
test_expect_success '--local requires a repo' '
# we expect 128 to ensure that we do not simply
# fail to find anything and return code "1"
test_expect_code 128 nongit git config --local foo.bar
'
test_done

32
usage.c
View File

@ -201,3 +201,35 @@ void warning(const char *warn, ...)
warn_routine(warn, params);
va_end(params);
}
static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
{
char prefix[256];
/* truncation via snprintf is OK here */
if (file)
snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
else
snprintf(prefix, sizeof(prefix), "BUG: ");
vreportf(prefix, fmt, params);
abort();
}
#ifdef HAVE_VARIADIC_MACROS
NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
BUG_vfl(file, line, fmt, ap);
va_end(ap);
}
#else
NORETURN void BUG(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
BUG_vfl(NULL, 0, fmt, ap);
va_end(ap);
}
#endif