]> git.ipfire.org Git - thirdparty/git.git/commit - git-compat-util.h
usage.c: add a non-fatal bug() function to go with BUG()
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Thu, 2 Jun 2022 12:25:33 +0000 (14:25 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 2 Jun 2022 19:51:35 +0000 (12:51 -0700)
commit0cc05b044fd690f37565262a5b09f60c203c5218
tree30682a4f70be5dc2665554dd0f840d6df4ba0e99
parent19d75948ef7abe9066efa4462108827d70565ae9
usage.c: add a non-fatal bug() function to go with BUG()

Add a bug() function to use in cases where we'd like to indicate a
runtime BUG(), but would like to defer the BUG() call because we're
possibly accumulating more bug() callers to exhaustively indicate what
went wrong.

We already have this sort of facility in various parts of the
codebase, just in the form of ad-hoc re-inventions of the
functionality that this new API provides. E.g. this will be used to
replace optbug() in parse-options.c, and the 'error("BUG:[...]' we do
in a loop in builtin/receive-pack.c.

Unlike the code this replaces we'll log to trace2 with this new bug()
function (as with other usage.c functions, including BUG()), we'll
also be able to avoid calls to xstrfmt() in some cases, as the bug()
function itself accepts variadic sprintf()-like arguments.

Any caller to bug() can follow up such calls with BUG_if_bug(),
which will BUG() out (i.e. abort()) if there were any preceding calls
to bug(), callers can also decide not to call BUG_if_bug() and leave
the resulting BUG() invocation until exit() time. There are currently
no bug() API users that don't call BUG_if_bug() themselves after a
for-loop, but allowing for not calling BUG_if_bug() keeps the API
flexible. As the tests and documentation here show we'll catch missing
BUG_if_bug() invocations in our exit() wrapper.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/technical/api-error-handling.txt
Documentation/technical/api-trace2.txt
common-main.c
git-compat-util.h
t/helper/test-trace2.c
t/t0210-trace2-normal.sh
usage.c