git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] advice: enhance `detach_advice()` to `detach_advice_if_enabled()`

From
Rubén Justo <rjusto@gmail.com>
Date
Dec 8, 2024, 08:12 UTC
Message-ID
<47cf2e55-364c-49d2-a364-4f1276196071@gmail.com>
In-Reply-To
<0e139151-7162-42b3-afae-248c28bf4c4b@gmail.com>

We have the `detachedHead` advice since 13be3e31f1 ("Reword 'detached HEAD' notification", 2010-01-29).

This advice is shown to the user in the `detach_advice()` function, and its only two clients verify beforehand if the advice is desired, in order to call the function accordingly.

The `advise_if_enabled()` API encapsulates some functionality that we can take advantage of:

 - Checks if the advice is desired, using `advice_enabled()`.
 - Automatically adds help, when needed, on how to disable the
   advice: "Turn off this advice by ...".
 - Displays the message consistently with other advise messages,
   prefixing each line with 'hint:'.

Let's simplify the logic for the clients of `detach_advice()` by eliminating their need to decide whether to show the advice, bringing that decision into `detach_advice()`. Also, let's make the it use `advice_if_enabled()` to ensure consistency with other advice messages.

To better reflect the changes in the function let's rename it to `detach_advice_if_enabled()`.

Finally, note that we have two tests in t7201 related to this advice: "checkout to detach HEAD (with advice declined)" and "checkout a detach HEAD". They are unaffected by the change we're doing here, so it is not necessary to adjust them in this step.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 advice.c           | 8 +++-----
 advice.h           | 2 +-
 builtin/checkout.c | 5 ++---
 builtin/clone.c    | 3 +--
 4 files changed, 7 insertions(+), 11 deletions(-)
diff --git a/advice.c b/advice.c
index 6b879d805c..399ae58437 100644
--- a/advice.c
+++ b/advice.c
@@ -272,7 +272,7 @@ void advise_on_updating_sparse_paths(struct string_list *pathspec_list)
 			    "* Disable or modify the sparsity rules."));
 }
 
-void detach_advice(const char *new_name)
+void detach_advice_if_enabled(const char *new_name)
 {
 	const char *fmt =
 	_("Note: switching to '%s'.\n"
@@ -288,11 +288,9 @@ void detach_advice(const char *new_name)
 	"\n"
 	"Or undo this operation with:\n"
 	"\n"
-	"  git switch -\n"
-	"\n"
-	"Turn off this advice by setting config variable advice.detachedHead to false\n\n");
+	"  git switch -\n");
 
-	fprintf(stderr, fmt, new_name);
+	advise_if_enabled(ADVICE_DETACHED_HEAD, fmt, new_name);
 }
 
 void advise_on_moving_dirty_path(struct string_list *pathspec_list)
diff --git a/advice.h b/advice.h
index d7466bc0ef..739c5e4987 100644
--- a/advice.h
+++ b/advice.h
@@ -79,7 +79,7 @@ void NORETURN die_resolve_conflict(const char *me);
 void NORETURN die_conclude_merge(void);
 void NORETURN die_ff_impossible(void);
 void advise_on_updating_sparse_paths(struct string_list *pathspec_list);
-void detach_advice(const char *new_name);
+void detach_advice_if_enabled(const char *new_name);
 void advise_on_moving_dirty_path(struct string_list *pathspec_list);
 
 #endif /* ADVICE_H */
diff --git a/builtin/checkout.c b/builtin/checkout.c
index c449558e66..e1366556d7 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1009,9 +1009,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
 				NULL,
 				REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR);
 		if (!opts->quiet) {
-			if (old_branch_info->path &&
-			    advice_enabled(ADVICE_DETACHED_HEAD) && !opts->force_detach)
-				detach_advice(new_branch_info->name);
+			if (old_branch_info->path && !opts->force_detach)
+				detach_advice_if_enabled(new_branch_info->name);
 			describe_detached_head(_("HEAD is now at"), new_branch_info->commit);
 		}
 	} else if (new_branch_info->path) {	/* Switch branches. */
diff --git a/builtin/clone.c b/builtin/clone.c
index 21721db28a..d0c5e89a2a 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -752,8 +752,7 @@ static int checkout(int submodule_progress, int filter_submodules,
 		return 0;
 	}
 	if (!strcmp(head, "HEAD")) {
-		if (advice_enabled(ADVICE_DETACHED_HEAD))
-			detach_advice(oid_to_hex(&oid));
+		detach_advice_if_enabled(oid_to_hex(&oid));
 		FREE_AND_NULL(head);
 	} else {
 		if (!starts_with(head, "refs/heads/"))
-- 
2.47.1.407.gf6b6eee3e5
Previous: Rubén JustoNext: Rubén Justo
Message 8 of 16 in “advice: suggest using subcommand "git config set"”
  1. advice: suggest using subcommand "git config set"Bence Ferdinandy, Dec 4, 2024
  2. Justin ToblerDec 4, 2024
  3. Bence FerdinandyDec 5, 2024
  4. Patrick SteinhardtDec 5, 2024
  5. advice: suggest using subcommand "git config set"Bence Ferdinandy, Dec 5, 2024
  6. Patrick SteinhardtDec 6, 2024
  7. Rubén JustoDec 8, 2024
  8. 1/3 advice: enhance `detach_advice()` to `detach_advice_if_enabled()`Rubén Justo, Dec 8, 2024
  9. 2/3 commit: use `advise_if_enabled()` in `read_graft_file()`Rubén Justo, Dec 8, 2024
  10. 3/3 object-name: advice to avoid refs that resemble hashesRubén Justo, Dec 8, 2024
  11. Bence FerdinandyDec 9, 2024
  12. Bence FerdinandyDec 9, 2024
  13. Rubén JustoDec 9, 2024
  14. Bence FerdinandyDec 11, 2024
  15. Rubén JustoDec 11, 2024
  16. Junio C HamanoDec 6, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.