Skip to content

Commit 061fd57

Browse files
committed
Merge branch 'ah/advice-switch-requires-detach-to-detach'
The error message given by "git switch HEAD~4" has been clarified to suggest the "--detach" option that is required. * ah/advice-switch-requires-detach-to-detach: switch: mention the --detach option when dying due to lack of a branch
2 parents 20d34c0 + 808213b commit 061fd57

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

Documentation/config/advice.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ advice.*::
8585
linkgit:git-switch[1] or linkgit:git-checkout[1]
8686
to move to the detach HEAD state, to instruct how to
8787
create a local branch after the fact.
88+
suggestDetachingHead::
89+
Advice shown when linkgit:git-switch[1] refuses to detach HEAD
90+
without the explicit `--detach` option.
8891
checkoutAmbiguousRemoteBranchName::
8992
Advice shown when the argument to
9093
linkgit:git-checkout[1] and linkgit:git-switch[1]

advice.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static struct {
4242
[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName", 1 },
4343
[ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge", 1 },
4444
[ADVICE_DETACHED_HEAD] = { "detachedHead", 1 },
45+
[ADVICE_SUGGEST_DETACHING_HEAD] = { "suggestDetachingHead", 1 },
4546
[ADVICE_FETCH_SHOW_FORCED_UPDATES] = { "fetchShowForcedUpdates", 1 },
4647
[ADVICE_GRAFT_FILE_DEPRECATED] = { "graftFileDeprecated", 1 },
4748
[ADVICE_IGNORED_HOOK] = { "ignoredHook", 1 },

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct string_list;
2020
ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
2121
ADVICE_COMMIT_BEFORE_MERGE,
2222
ADVICE_DETACHED_HEAD,
23+
ADVICE_SUGGEST_DETACHING_HEAD,
2324
ADVICE_FETCH_SHOW_FORCED_UPDATES,
2425
ADVICE_GRAFT_FILE_DEPRECATED,
2526
ADVICE_IGNORED_HOOK,

builtin/checkout.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,23 +1397,31 @@ static void die_expecting_a_branch(const struct branch_info *branch_info)
13971397
{
13981398
struct object_id oid;
13991399
char *to_free;
1400+
int code;
14001401

14011402
if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, &to_free, 0) == 1) {
14021403
const char *ref = to_free;
14031404

14041405
if (skip_prefix(ref, "refs/tags/", &ref))
1405-
die(_("a branch is expected, got tag '%s'"), ref);
1406-
if (skip_prefix(ref, "refs/remotes/", &ref))
1407-
die(_("a branch is expected, got remote branch '%s'"), ref);
1408-
die(_("a branch is expected, got '%s'"), ref);
1406+
code = die_message(_("a branch is expected, got tag '%s'"), ref);
1407+
else if (skip_prefix(ref, "refs/remotes/", &ref))
1408+
code = die_message(_("a branch is expected, got remote branch '%s'"), ref);
1409+
else
1410+
code = die_message(_("a branch is expected, got '%s'"), ref);
14091411
}
1410-
if (branch_info->commit)
1411-
die(_("a branch is expected, got commit '%s'"), branch_info->name);
1412-
/*
1413-
* This case should never happen because we already die() on
1414-
* non-commit, but just in case.
1415-
*/
1416-
die(_("a branch is expected, got '%s'"), branch_info->name);
1412+
else if (branch_info->commit)
1413+
code = die_message(_("a branch is expected, got commit '%s'"), branch_info->name);
1414+
else
1415+
/*
1416+
* This case should never happen because we already die() on
1417+
* non-commit, but just in case.
1418+
*/
1419+
code = die_message(_("a branch is expected, got '%s'"), branch_info->name);
1420+
1421+
if (advice_enabled(ADVICE_SUGGEST_DETACHING_HEAD))
1422+
advise(_("If you want to detach HEAD at the commit, try again with the --detach option."));
1423+
1424+
exit(code);
14171425
}
14181426

14191427
static void die_if_some_operation_in_progress(void)

t/t2060-switch.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ test_expect_success 'switch and detach' '
3232
test_must_fail git symbolic-ref HEAD
3333
'
3434

35+
test_expect_success 'suggestion to detach' '
36+
test_must_fail git switch main^{commit} 2>stderr &&
37+
grep "try again with the --detach option" stderr
38+
'
39+
40+
test_expect_success 'suggestion to detach is suppressed with advice.suggestDetachingHead=false' '
41+
test_config advice.suggestDetachingHead false &&
42+
test_must_fail git switch main^{commit} 2>stderr &&
43+
! grep "try again with the --detach option" stderr
44+
'
45+
3546
test_expect_success 'switch and detach current branch' '
3647
test_when_finished git switch main &&
3748
git switch main &&

0 commit comments

Comments
 (0)