branch: make the advice to force-deleting a conditional one

The error message we show when the user tries to delete a not fully merged branch describes the error and gives a hint to the user: error: the branch 'foo' is not fully merged. If you are sure you want to delete it, run 'git branch -D foo'. Let's move the hint part so that it is displayed using the advice machinery: error: the branch 'foo' is not fully merged hint: If you are sure you want to delete it, run 'git branch -D foo' hint: Disable this message with "git config advice.forceDeleteBranch false" Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Jan 11, 2024 at 13:40 UTC bec9bb4b3918d2b3c7b91bbb116a667d5d6d298d
4 files changed +10 -3
Documentation/config/advice.txt
+3
@@ -45,6 +45,9 @@ advice.*::
45 Advice shown when linkgit:git-fetch[1] takes a long time
46 to calculate forced updates after ref updates, or to warn
47 that the check is disabled.
48 + forceDeleteBranch::
49 + Advice shown when a user tries to delete a not fully merged
50 + branch without the force option set.
51 ignoredHook::
52 Advice shown if a hook is ignored because the hook is not
53 set as executable.
advice.c
+1
@@ -47,6 +47,7 @@ static struct {
47 [ADVICE_DETACHED_HEAD] = { "detachedHead", 1 },
48 [ADVICE_DIVERGING] = { "diverging", 1 },
49 [ADVICE_FETCH_SHOW_FORCED_UPDATES] = { "fetchShowForcedUpdates", 1 },
50 + [ADVICE_FORCE_DELETE_BRANCH] = { "forceDeleteBranch", 1 },
51 [ADVICE_GRAFT_FILE_DEPRECATED] = { "graftFileDeprecated", 1 },
52 [ADVICE_IGNORED_HOOK] = { "ignoredHook", 1 },
53 [ADVICE_IMPLICIT_IDENTITY] = { "implicitIdentity", 1 },
advice.h
+1
@@ -21,6 +21,7 @@ enum advice_type {
21 ADVICE_DETACHED_HEAD,
22 ADVICE_DIVERGING,
23 ADVICE_FETCH_SHOW_FORCED_UPDATES,
24 + ADVICE_FORCE_DELETE_BRANCH,
25 ADVICE_GRAFT_FILE_DEPRECATED,
26 ADVICE_IGNORED_HOOK,
27 ADVICE_IMPLICIT_IDENTITY,
builtin/branch.c
+5 -3
@@ -24,6 +24,7 @@
24 #include "ref-filter.h"
25 #include "worktree.h"
26 #include "help.h"
27 +#include "advice.h"
28 #include "commit-reach.h"
29
30 static const char * const builtin_branch_usage[] = {
@@ -190,9 +191,10 @@ static int check_branch_commit(const char *branchname, const char *refname,
191 return -1;
192 }
193 if (!force && !branch_merged(kinds, branchname, rev, head_rev)) {
193 - error(_("the branch '%s' is not fully merged.\n"
194 - "If you are sure you want to delete it, "
195 - "run 'git branch -D %s'"), branchname, branchname);
194 + error(_("the branch '%s' is not fully merged"), branchname);
195 + advise_if_enabled(ADVICE_FORCE_DELETE_BRANCH,
196 + _("If you are sure you want to delete it, "
197 + "run 'git branch -D %s'"), branchname);
198 return -1;
199 }
200 return 0;