clone: print advice on checking out detached HEAD

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 16, 2012 at 16:46 UTC 2857093ba15982d21ff0d5a9fd65294ac895cb67
4 files changed +20 -16
advice.c
+14
@@ -64,3 +64,17 @@ void NORETURN die_resolve_conflict(const char *me)
64 error_resolve_conflict(me);
65 die("Exiting because of an unresolved conflict.");
66 }
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
+1
@@ -14,5 +14,6 @@ int git_default_advice_config(const char *var, const char *value);
14 void advise(const char *advice, ...);
15 int error_resolve_conflict(const char *me);
16 extern void NORETURN die_resolve_conflict(const char *me);
17 +void detach_advice(const char *new_name);
18
19 #endif /* ADVICE_H */
builtin/checkout.c
+1 -15
@@ -514,20 +514,6 @@ static void report_tracking(struct branch_info *new)
514 strbuf_release(&sb);
515 }
516
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 -
517 static void update_refs_for_switch(struct checkout_opts *opts,
518 struct branch_info *old,
519 struct branch_info *new)
@@ -575,7 +561,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
561 REF_NODEREF, DIE_ON_ERR);
562 if (!opts->quiet) {
563 if (old->path && advice_detached_head)
578 - detach_advice(old->path, new->name);
564 + detach_advice(new->name);
565 describe_detached_head(_("HEAD is now at"), new->commit);
566 }
567 } else if (new->path) { /* Switch branches. */
builtin/clone.c
+4 -1
@@ -563,7 +563,10 @@ static int checkout(void)
563 "unable to checkout.\n"));
564 return 0;
565 }
566 - if (strcmp(head, "HEAD")) {
566 + if (!strcmp(head, "HEAD")) {
567 + if (advice_detached_head)
568 + detach_advice(sha1_to_hex(sha1));
569 + } else {
570 if (prefixcmp(head, "refs/heads/"))
571 die(_("HEAD not found below refs/heads!"));
572 }