From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: Alex Henrie <alexhenrie24@gmail.com>
Cc: git@vger.kernel.org, gitster@pobox.com, pclouds@gmail.com
Subject: Re: [PATCH v2] switch: mention the --detach option when dying due to lack of a branch
Date: Thu, 24 Feb 2022 09:53:09 +0100 [thread overview]
Message-ID: <220224.86sfs8abj6.gmgdl@evledraar.gmail.com> (raw)
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' '
next prev parent reply other threads:[~2022-02-24 8:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-24 6:47 [PATCH v2] switch: mention the --detach option when dying due to lack of a branch Alex Henrie
2022-02-24 8:53 ` Ævar Arnfjörð Bjarmason [this message]
2022-02-24 19:02 ` Junio C Hamano
2022-02-25 11:57 ` Ævar Arnfjörð Bjarmason
2022-02-25 17:20 ` Alex Henrie
2022-02-25 17:26 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=220224.86sfs8abj6.gmgdl@evledraar.gmail.com \
--to=avarab@gmail.com \
--cc=alexhenrie24@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).