Skip to content

Commit 2857093

Browse files
pcloudsgitster
authored andcommitted
clone: print advice on checking out detached HEAD
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5a7d5b6 commit 2857093

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

advice.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,17 @@ void NORETURN die_resolve_conflict(const char *me)
6464
error_resolve_conflict(me);
6565
die("Exiting because of an unresolved conflict.");
6666
}
67+
68+
void detach_advice(const char *new_name)
69+
{
70+
const char fmt[] =
71+
"Note: checking out '%s'.\n\n"
72+
"You are in 'detached HEAD' state. You can look around, make experimental\n"
73+
"changes and commit them, and you can discard any commits you make in this\n"
74+
"state without impacting any branches by performing another checkout.\n\n"
75+
"If you want to create a new branch to retain commits you create, you may\n"
76+
"do so (now or later) by using -b with the checkout command again. Example:\n\n"
77+
" git checkout -b new_branch_name\n\n";
78+
79+
fprintf(stderr, fmt, new_name);
80+
}

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ int git_default_advice_config(const char *var, const char *value);
1414
void advise(const char *advice, ...);
1515
int error_resolve_conflict(const char *me);
1616
extern void NORETURN die_resolve_conflict(const char *me);
17+
void detach_advice(const char *new_name);
1718

1819
#endif /* ADVICE_H */

builtin/checkout.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -514,20 +514,6 @@ static void report_tracking(struct branch_info *new)
514514
strbuf_release(&sb);
515515
}
516516

517-
static void detach_advice(const char *old_path, const char *new_name)
518-
{
519-
const char fmt[] =
520-
"Note: checking out '%s'.\n\n"
521-
"You are in 'detached HEAD' state. You can look around, make experimental\n"
522-
"changes and commit them, and you can discard any commits you make in this\n"
523-
"state without impacting any branches by performing another checkout.\n\n"
524-
"If you want to create a new branch to retain commits you create, you may\n"
525-
"do so (now or later) by using -b with the checkout command again. Example:\n\n"
526-
" git checkout -b new_branch_name\n\n";
527-
528-
fprintf(stderr, fmt, new_name);
529-
}
530-
531517
static void update_refs_for_switch(struct checkout_opts *opts,
532518
struct branch_info *old,
533519
struct branch_info *new)
@@ -575,7 +561,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
575561
REF_NODEREF, DIE_ON_ERR);
576562
if (!opts->quiet) {
577563
if (old->path && advice_detached_head)
578-
detach_advice(old->path, new->name);
564+
detach_advice(new->name);
579565
describe_detached_head(_("HEAD is now at"), new->commit);
580566
}
581567
} else if (new->path) { /* Switch branches. */

builtin/clone.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,10 @@ static int checkout(void)
563563
"unable to checkout.\n"));
564564
return 0;
565565
}
566-
if (strcmp(head, "HEAD")) {
566+
if (!strcmp(head, "HEAD")) {
567+
if (advice_detached_head)
568+
detach_advice(sha1_to_hex(sha1));
569+
} else {
567570
if (prefixcmp(head, "refs/heads/"))
568571
die(_("HEAD not found below refs/heads!"));
569572
}

0 commit comments

Comments
 (0)