summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2017-06-13 13:26:59 -0700
committerJunio C Hamano <gitster@pobox.com>2017-06-13 13:26:59 -0700
commit7a190a215dbcf782c33ed531104389de8504e29d (patch)
tree05b00d7adb3abb89de530834599a5daa04a6d026
parent146b0ab1a528b54803adbf8b6b72239a8d68a65c (diff)
parent3d7dd2d3b6ccc8903a37cffe3a2f39cf1be21c86 (diff)
downloadgit-7a190a215dbcf782c33ed531104389de8504e29d.tar.gz
Merge branch 'jk/bug-to-abort' into maint
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
-rw-r--r--builtin/config.c3
-rw-r--r--environment.c2
-rw-r--r--git-compat-util.h9
-rwxr-xr-xt/t1300-repo-config.sh6
-rw-r--r--usage.c32
5 files changed, 51 insertions, 1 deletions
diff --git a/builtin/config.c b/builtin/config.c
index 3a554ad50c..7f6c25d4d9 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -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;
diff --git a/environment.c b/environment.c
index ff6e4f06e9..1f0bda5afa 100644
--- a/environment.c
+++ b/environment.c
@@ -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);
diff --git a/git-compat-util.h b/git-compat-util.h
index bd04564a69..4575b3890b 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1064,6 +1064,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
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index afcca0d52c..13b7851f7c 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -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
diff --git a/usage.c b/usage.c
index ad6d2910fb..1f63e033e9 100644
--- a/usage.c
+++ b/usage.c
@@ -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