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

Re: [PATCH v2] switch: mention the --detach option when dying due to lack of a branch

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Feb 24, 2022, 08:53 UTC
Message-ID
<220224.86sfs8abj6.gmgdl@evledraar.gmail.com>
In-Reply-To
<20220224064710.2252637-1-alexhenrie24@gmail.com>
On Wed, Feb 23 2022, Alex Henrie wrote:
> Users who are accustomed to doing `git checkout <tag>` assume that
> `git switch <tag>` will do the same thing. Inform them of the --detach
> option so they aren't left wondering why `git switch` doesn't work but
> `git checkout` does.

Thanks, this looks good to me! FWIW while testing this v2 I came up with this on top, which you may or may not want (some unrelated changes):

1. We had a to_free but didn't free it, now we do. Didn't matter with
   SANITIZE=leak, but FWIW valgrind with "valgrind --leak-check=full
   --show-leak-kinds=all" is marginally happier
2. Maybe s/code = /return / with a helper is easier to read, maybe not.
3. That #2 makes the code/advice wrapper simpler (also with the to_free)
4. Used test_cmp in the test to check the actual output we got, and added
   a missing test for the "tag" case.
   In any case s/test_i18ngrep/grep/ is the right thing nowadays for new code.
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 9244827ca02..9e4d03343fa 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1393,34 +1393,39 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
 	return status;
 }
 
-static void die_expecting_a_branch(const struct branch_info *branch_info)
+static int die_expecting_a_branch_message(const struct branch_info *branch_info, char **to_free)
 {
 	struct object_id oid;
-	char *to_free;
-	int code;
 
-	if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, &to_free, 0) == 1) {
-		const char *ref = to_free;
+	if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, to_free, 0) == 1) {
+		const char *ref = *to_free;
 
 		if (skip_prefix(ref, "refs/tags/", &ref))
-			code = die_message(_("a branch is expected, got tag '%s'"), ref);
+			return die_message(_("a branch is expected, got tag '%s'"), ref);
 		else if (skip_prefix(ref, "refs/remotes/", &ref))
-			code = die_message(_("a branch is expected, got remote branch '%s'"), ref);
+			return die_message(_("a branch is expected, got remote branch '%s'"), ref);
 		else
-			code = die_message(_("a branch is expected, got '%s'"), ref);
+			return die_message(_("a branch is expected, got '%s'"), ref);
 	}
 	else if (branch_info->commit)
-		code = die_message(_("a branch is expected, got commit '%s'"), branch_info->name);
+		return die_message(_("a branch is expected, got commit '%s'"), branch_info->name);
 	else
 		/*
 		 * This case should never happen because we already die() on
 		 * non-commit, but just in case.
 		 */
-		code = die_message(_("a branch is expected, got '%s'"), branch_info->name);
+		return die_message(_("a branch is expected, got '%s'"), branch_info->name);
+}
+
+static void die_expecting_a_branch(const struct branch_info *branch_info)
+{
+	char *to_free = NULL;
+	int code = die_expecting_a_branch_message(branch_info, &to_free);
 
 	if (advice_enabled(ADVICE_SUGGEST_DETACHING_HEAD))
 		advise(_("If you want to detach HEAD at the commit, try again with the --detach option."));
 
+	free(to_free);
 	exit(code);
 }
 
diff --git a/t/t2060-switch.sh b/t/t2060-switch.sh
index f54691bac9f..0708359ee24 100755
--- a/t/t2060-switch.sh
+++ b/t/t2060-switch.sh
@@ -32,15 +32,31 @@ test_expect_success 'switch and detach' '
 	test_must_fail git symbolic-ref HEAD
 '
 
-test_expect_success 'suggestion to detach' '
-	test_must_fail git switch main^{commit} 2>stderr &&
-	test_i18ngrep "try again with the --detach option" stderr
+test_expect_success 'suggestion to detach commit' '
+	test_must_fail git switch main^{commit} 2>actual &&
+	cat >expect <<-\EOF &&
+	fatal: a branch is expected, got commit '\''main^{commit}'\''
+	hint: If you want to detach HEAD at the commit, try again with the --detach option.
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'suggestion to detach tag' '
+	test_must_fail git switch first 2>actual &&
+	cat >expect <<-\EOF &&
+	fatal: a branch is expected, got tag '\''first'\''
+	hint: If you want to detach HEAD at the commit, try again with the --detach option.
+	EOF
+	test_cmp expect actual
 '
 
 test_expect_success 'suggestion to detach is suppressed with advice.suggestDetachingHead=false' '
 	test_config advice.suggestDetachingHead false &&
-	test_must_fail git switch main^{commit} 2>stderr &&
-	test_i18ngrep ! "try again with the --detach option" stderr
+	test_must_fail git switch main^{commit} 2>actual &&
+	cat >expect <<-\EOF &&
+	fatal: a branch is expected, got commit '\''main^{commit}'\''
+	EOF
+	test_cmp expect actual
 '
 
 test_expect_success 'switch and detach current branch' '
Previous: Alex HenrieNext: Junio C Hamano
Message 2 of 6 in “switch: mention the --detach option when dying due to lack of a branch”
  1. switch: mention the --detach option when dying due to lack of a branchAlex Henrie, Feb 24, 2022
  2. Ævar Arnfjörð BjarmasonFeb 24, 2022
  3. Junio C HamanoFeb 24, 2022
  4. Ævar Arnfjörð BjarmasonFeb 25, 2022
  5. Alex HenrieFeb 25, 2022
  6. Junio C HamanoFeb 25, 2022

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.