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

[PATCH v4 10/10] clone: print advice on checking out detached HEAD

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 13, 2012, 07:22 UTC
Message-ID
<1326439322-15648-11-git-send-email-pclouds@gmail.com>
In-Reply-To
<1326189427-20800-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 advice.c           |   14 ++++++++++++++
 advice.h           |    1 +
 builtin/checkout.c |   16 +---------------
 builtin/clone.c    |    5 ++++-
 4 files changed, 20 insertions(+), 16 deletions(-)
diff --git a/advice.c b/advice.c
index e02e632..3e1a145 100644
--- a/advice.c
+++ b/advice.c
@@ -64,3 +64,17 @@ void NORETURN die_resolve_conflict(const char *me)
 	error_resolve_conflict(me);
 	die("Exiting because of an unresolved conflict.");
 }
+
+void detach_advice(const char *new_name)
+{
+	const char fmt[] =
+	"Note: checking out '%s'.\n\n"
+	"You are in 'detached HEAD' state. You can look around, make experimental\n"
+	"changes and commit them, and you can discard any commits you make in this\n"
+	"state without impacting any branches by performing another checkout.\n\n"
+	"If you want to create a new branch to retain commits you create, you may\n"
+	"do so (now or later) by using -b with the checkout command again. Example:\n\n"
+	"  git checkout -b new_branch_name\n\n";
+
+	fprintf(stderr, fmt, new_name);
+}
diff --git a/advice.h b/advice.h
index e5d0af7..7bda45b 100644
--- a/advice.h
+++ b/advice.h
@@ -14,5 +14,6 @@ int git_default_advice_config(const char *var, const char *value);
 void advise(const char *advice, ...);
 int error_resolve_conflict(const char *me);
 extern void NORETURN die_resolve_conflict(const char *me);
+void detach_advice(const char *new_name);
 
 #endif /* ADVICE_H */
diff --git a/builtin/checkout.c b/builtin/checkout.c
index f1984d9..5bf96ba 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -514,20 +514,6 @@ static void report_tracking(struct branch_info *new)
 	strbuf_release(&sb);
 }
 
-static void detach_advice(const char *old_path, const char *new_name)
-{
-	const char fmt[] =
-	"Note: checking out '%s'.\n\n"
-	"You are in 'detached HEAD' state. You can look around, make experimental\n"
-	"changes and commit them, and you can discard any commits you make in this\n"
-	"state without impacting any branches by performing another checkout.\n\n"
-	"If you want to create a new branch to retain commits you create, you may\n"
-	"do so (now or later) by using -b with the checkout command again. Example:\n\n"
-	"  git checkout -b new_branch_name\n\n";
-
-	fprintf(stderr, fmt, new_name);
-}
-
 static void update_refs_for_switch(struct checkout_opts *opts,
 				   struct branch_info *old,
 				   struct branch_info *new)
@@ -575,7 +561,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
 			   REF_NODEREF, DIE_ON_ERR);
 		if (!opts->quiet) {
 			if (old->path && advice_detached_head)
-				detach_advice(old->path, new->name);
+				detach_advice(new->name);
 			describe_detached_head(_("HEAD is now at"), new->commit);
 		}
 	} else if (new->path) {	/* Switch branches. */
diff --git a/builtin/clone.c b/builtin/clone.c
index a3c8f78..bfdafaa 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -506,7 +506,10 @@ static int checkout(void)
 			  "unable to checkout.\n"));
 		return 0;
 	}
-	if (strcmp(head, "HEAD")) {
+	if (!strcmp(head, "HEAD")) {
+		if (advice_detached_head)
+			detach_advice(sha1_to_hex(sha1));
+	} else {
 		if (prefixcmp(head, "refs/heads/"))
 			die(_("HEAD not found below refs/heads!"));
 	}
-- 
1.7.3.1.256.g2539c.dirty
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 49 of 68 in “clone: allow detached checkout when --branch takes a tag”
  1. clone: allow detached checkout when --branch takes a tagNguyễn Thái Ngọc Duy, Jan 5, 2012
  2. Jeff KingJan 5, 2012
  3. Nguyen Thai Ngoc DuyJan 6, 2012
  4. Jeff KingJan 6, 2012
  5. Junio C HamanoJan 5, 2012
  6. Nguyen Thai Ngoc DuyJan 6, 2012
  7. 1/6 t5601: add &&Nguyễn Thái Ngọc Duy, Jan 8, 2012
  8. 2/6 clone: write detached HEAD in bare repositoriesNguyễn Thái Ngọc Duy, Jan 8, 2012
  9. 3/6 clone: factor out checkout codeNguyễn Thái Ngọc Duy, Jan 8, 2012
  10. Junio C HamanoJan 10, 2012
  11. Nguyen Thai Ngoc DuyJan 10, 2012
  12. Junio C HamanoJan 10, 2012
  13. Nguyen Thai Ngoc DuyJan 10, 2012
  14. 4/6 clone: --branch=<branch> always means refs/heads/<branch>Nguyễn Thái Ngọc Duy, Jan 8, 2012
  15. Junio C HamanoJan 10, 2012
  16. 5/6 clone: allow --branch to take a tagNguyễn Thái Ngọc Duy, Jan 8, 2012
  17. 6/6 clone: print advice on checking out detached HEADNguyễn Thái Ngọc Duy, Jan 8, 2012
  18. Junio C HamanoJan 10, 2012
  19. Nguyen Thai Ngoc DuyJan 10, 2012
  20. Junio C HamanoJan 10, 2012
  21. Nguyen Thai Ngoc DuyJan 10, 2012
  22. 00/10 nd/clone-detachedNguyễn Thái Ngọc Duy, Jan 10, 2012
  23. 00/10 nd/clone-detachedNguyễn Thái Ngọc Duy, Jan 13, 2012
  24. Junio C HamanoJan 13, 2012
  25. Nguyen Thai Ngoc DuyJan 14, 2012
  26. Junio C HamanoJan 14, 2012
  27. Nguyen Thai Ngoc DuyJan 14, 2012
  28. Junio C HamanoJan 15, 2012
  29. 00/10 nd/clone-detached rebase onto nd/clone-single-branchNguyễn Thái Ngọc Duy, Jan 16, 2012
  30. 01/10 t5601: add missing && cascadeNguyễn Thái Ngọc Duy, Jan 16, 2012
  31. 02/10 clone: write detached HEAD in bare repositoriesNguyễn Thái Ngọc Duy, Jan 16, 2012
  32. 03/10 clone: factor out checkout codeNguyễn Thái Ngọc Duy, Jan 16, 2012
  33. 04/10 clone: factor out HEAD update codeNguyễn Thái Ngọc Duy, Jan 16, 2012
  34. 05/10 clone: factor out remote ref writingNguyễn Thái Ngọc Duy, Jan 16, 2012
  35. 06/10 clone: delay cloning until after remote HEAD checkingNguyễn Thái Ngọc Duy, Jan 16, 2012
  36. 07/10 clone: --branch=<branch> always means refs/heads/<branch>Nguyễn Thái Ngọc Duy, Jan 16, 2012
  37. 08/10 clone: refuse to clone if --branch points to bogus refNguyễn Thái Ngọc Duy, Jan 16, 2012
  38. 09/10 clone: allow --branch to take a tagNguyễn Thái Ngọc Duy, Jan 16, 2012
  39. 10/10 clone: print advice on checking out detached HEADNguyễn Thái Ngọc Duy, Jan 16, 2012
  40. 01/10 t5601: add missing && cascadeNguyễn Thái Ngọc Duy, Jan 13, 2012
  41. 02/10 clone: write detached HEAD in bare repositoriesNguyễn Thái Ngọc Duy, Jan 13, 2012
  42. 03/10 clone: factor out checkout codeNguyễn Thái Ngọc Duy, Jan 13, 2012
  43. 04/10 clone: factor out HEAD update codeNguyễn Thái Ngọc Duy, Jan 13, 2012
  44. 05/10 clone: factor out remote ref writingNguyễn Thái Ngọc Duy, Jan 13, 2012
  45. 06/10 clone: delay cloning until after remote HEAD checkingNguyễn Thái Ngọc Duy, Jan 13, 2012
  46. 07/10 clone: --branch=<branch> always means refs/heads/<branch>Nguyễn Thái Ngọc Duy, Jan 13, 2012
  47. 08/10 clone: refuse to clone if --branch points to bogus refNguyễn Thái Ngọc Duy, Jan 13, 2012
  48. 09/10 clone: allow --branch to take a tagNguyễn Thái Ngọc Duy, Jan 13, 2012
  49. 10/10 clone: print advice on checking out detached HEADNguyễn Thái Ngọc Duy, Jan 13, 2012
  50. 01/10 t5601: add missing && cascadeNguyễn Thái Ngọc Duy, Jan 10, 2012
  51. 02/10 clone: write detached HEAD in bare repositoriesNguyễn Thái Ngọc Duy, Jan 10, 2012
  52. 03/10 clone: factor out checkout codeNguyễn Thái Ngọc Duy, Jan 10, 2012
  53. 04/10 clone: factor out HEAD update codeNguyễn Thái Ngọc Duy, Jan 10, 2012
  54. 05/10 clone: factor out remote ref writingNguyễn Thái Ngọc Duy, Jan 10, 2012
  55. 06/10 clone: delay cloning until after remote HEAD checkingNguyễn Thái Ngọc Duy, Jan 10, 2012
  56. Junio C HamanoJan 11, 2012
  57. Nguyen Thai Ngoc DuyJan 12, 2012
  58. Junio C HamanoJan 24, 2012
  59. Junio C HamanoJan 24, 2012
  60. Junio C HamanoJan 24, 2012
  61. Nguyen Thai Ngoc DuyJan 24, 2012
  62. 07/10 clone: --branch=<branch> always means refs/heads/<branch>Nguyễn Thái Ngọc Duy, Jan 10, 2012
  63. Junio C HamanoJan 11, 2012
  64. Nguyen Thai Ngoc DuyJan 12, 2012
  65. 08/10 clone: refuse to clone if --branch points to bogus refNguyễn Thái Ngọc Duy, Jan 10, 2012
  66. 09/10 clone: allow --branch to take a tagNguyễn Thái Ngọc Duy, Jan 10, 2012
  67. Junio C HamanoJan 11, 2012
  68. 10/10 clone: print advice on checking out detached HEADNguyễn Thái Ngọc Duy, Jan 10, 2012

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.