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

[PATCH/RFC] builtin-checkout: suggest creating local branch when appropriate to do so

From
Jay Soffian <jaysoffian@gmail.com>
Date
Oct 5, 2009, 20:46 UTC
Message-ID
<1254775583-49452-1-git-send-email-jaysoffian@gmail.com>

A user who has just cloned a remote repository and wishes to then work on a branch other than master may not realize they first need to create the local branch. e.g.:

$ git clone git://git.kernel.org/pub/scm/git/git.git $ cd git $ git checkout next error: pathspec 'next' did not match any file(s) known to git.

This commit teaches git to make a suggestion to the user:
$ git clone git://git.kernel.org/pub/scm/git/git.git
$ cd git
$ git checkout next
error: pathspec 'next' did not match any file(s) known to git.
To create a local branch from the same named remote branch, use
  git checkout -b next origin/next
Motivated by http://article.gmane.org/gmane.comp.version-control.git/129528
Signed-off-by: Jay Soffian <jaysoffian@gmail.com>
---
 builtin-checkout.c |   43 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 41 insertions(+), 2 deletions(-)

I dunno, this seems like a lot of code just to make a suggestion to the user. Is it worth it?

Also, I initially was going to use for_each_remote_ref and compare every remote ref name to see if it tail matched what the user gave us, but it was easier to use for_each_remote and build up the remote ref name and then check for its existence. Not sure if either approach is preferable.

Thoughts/comments?
diff --git a/builtin-checkout.c b/builtin-checkout.c
index d050c37..7f2e215 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -145,6 +145,38 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
 	mm->size = size;
 }
 
+struct suggest_new_branch_name_data {
+	const char *name, *found;
+	int matches;
+};
+
+static int suggest_new_branch_name_compare(struct remote *remote, void *priv)
+{
+	struct suggest_new_branch_name_data *data = priv;
+	unsigned char sha1[20];
+	struct strbuf buf = STRBUF_INIT;
+	strbuf_addf(&buf, "refs/remotes/%s/%s", remote->name, data->name);
+	if (resolve_ref(buf.buf, sha1, 1, NULL)) {
+		data->matches++;
+		if (data->found)
+			strbuf_release(&buf);
+		else
+			data->found = strbuf_detach(&buf, NULL);
+	}
+	return 0;
+}
+
+static void suggest_new_branch_name(const char *name)
+{
+	struct suggest_new_branch_name_data data;
+	data.name = name;
+	data.found = NULL;
+	data.matches = 0;
+	for_each_remote(suggest_new_branch_name_compare, &data);
+	if (data.matches == 1)
+		fprintf(stderr, "To create a local branch from the same named remote branch, use\n  git checkout -b %s %s\n", name, prettify_refname(data.found));
+}
+
 static int checkout_merged(int pos, struct checkout *state)
 {
 	struct cache_entry *ce = active_cache[pos];
@@ -231,8 +263,13 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
 		match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
 	}
 
-	if (report_path_error(ps_matched, pathspec, 0))
+	if (report_path_error(ps_matched, pathspec, 0)) {
+		for (pos = 0; pathspec[pos]; pos++)
+			;
+		if (pos == 1)
+			suggest_new_branch_name(pathspec[0]);
 		return 1;
+	}
 
 	/* Any unmerged paths? */
 	for (pos = 0; pos < active_nr; pos++) {
@@ -675,8 +712,10 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 			arg = "@{-1}";
 
 		if (get_sha1(arg, rev)) {
-			if (has_dash_dash)          /* case (1) */
+			if (has_dash_dash) {         /* case (1) */
+				suggest_new_branch_name(arg);
 				die("invalid reference: %s", arg);
+			}
 			goto no_reference;          /* case (3 -> 2) */
 		}
 
-- 
1.6.4.2
Next: Sverre Rabbelier
Message 1 of 91 in “builtin-checkout: suggest creating local branch when appropriate to do so”
  1. builtin-checkout: suggest creating local branch when appropriate to do soJay Soffian, Oct 5, 2009
  2. Sverre RabbelierOct 5, 2009
  3. Johannes SchindelinOct 5, 2009
  4. Sverre RabbelierOct 5, 2009
  5. Jay SoffianOct 5, 2009
  6. Jay SoffianOct 5, 2009
  7. Johannes SchindelinOct 5, 2009
  8. Jeff KingOct 5, 2009
  9. Thomas RastOct 6, 2009
  10. Johannes SchindelinOct 6, 2009
  11. Junio C HamanoOct 6, 2009
  12. Johannes SchindelinOct 6, 2009
  13. Junio C HamanoOct 6, 2009
  14. Johannes SchindelinOct 6, 2009
  15. Matthieu MoyOct 6, 2009
  16. Mikael MagnussonOct 6, 2009
  17. Johannes SchindelinOct 6, 2009
  18. Junio C HamanoOct 18, 2009
  19. 1/3 check_filename(): make verify_filename() callable without dyingJunio C Hamano, Oct 18, 2009
  20. 2/3 DWIM "git checkout frotz" to "git checkout -b frotz origin/frotz"Junio C Hamano, Oct 18, 2009
  21. Nanako ShiraishiOct 18, 2009
  22. Björn SteinbrinkOct 18, 2009
  23. Nanako ShiraishiOct 18, 2009
  24. Junio C HamanoOct 18, 2009
  25. Björn SteinbrinkOct 19, 2009
  26. 3/3 git checkout --nodwimJunio C Hamano, Oct 18, 2009
  27. Alex RiesenOct 18, 2009
  28. Junio C HamanoOct 18, 2009
  29. Use "--no-" prefix to switch off some of checkout dwimmeryAlex Riesen, Oct 18, 2009
  30. Junio C HamanoOct 18, 2009
  31. Alex RiesenOct 19, 2009
  32. Alex RiesenOct 19, 2009
  33. Junio C HamanoOct 19, 2009
  34. Alex RiesenOct 19, 2009
  35. Junio C HamanoOct 19, 2009
  36. Avery PennarunOct 21, 2009
  37. Nanako ShiraishiOct 21, 2009
  38. Junio C HamanoOct 21, 2009
  39. git checkout --no-guessJunio C Hamano, Oct 21, 2009
  40. Avery PennarunOct 21, 2009
  41. Jay SoffianOct 26, 2009
  42. Avery PennarunOct 26, 2009
  43. Johannes SchindelinOct 22, 2009
  44. Erik Faye-LundOct 22, 2009
  45. Michael J GruberOct 23, 2009
  46. Junio C HamanoOct 24, 2009
  47. David RoundyOct 24, 2009
  48. Junio C HamanoOct 24, 2009
  49. Johannes SchindelinOct 26, 2009
  50. Avery PennarunOct 26, 2009
  51. Jeff KingOct 26, 2009
  52. Avery PennarunOct 26, 2009
  53. Jeff KingOct 26, 2009
  54. Avery PennarunOct 26, 2009
  55. Jeff KingOct 5, 2009
  56. Eugene SajineOct 6, 2009
  57. Junio C HamanoOct 6, 2009
  58. Johannes SchindelinOct 12, 2009
  59. Björn SteinbrinkOct 12, 2009
  60. Thomas RastOct 12, 2009
  61. Junio C HamanoOct 12, 2009
  62. Thomas RastOct 13, 2009
  63. Junio C HamanoOct 13, 2009
  64. Junio C HamanoOct 13, 2009
  65. Thomas RastOct 13, 2009
  66. Junio C HamanoOct 13, 2009
  67. Johannes SchindelinOct 13, 2009
  68. Junio C HamanoOct 13, 2009
  69. Jeff KingOct 13, 2009
  70. Johannes SchindelinOct 13, 2009
  71. Jay SoffianOct 14, 2009
  72. Junio C HamanoOct 14, 2009
  73. Jay SoffianOct 14, 2009
  74. Junio C HamanoOct 14, 2009
  75. Uri OkrentOct 25, 2009
  76. Jeff KingOct 14, 2009
  77. Thomas RastOct 14, 2009
  78. Jakub NarebskiOct 14, 2009
  79. Johannes SixtOct 13, 2009
  80. Daniel BarkalowOct 13, 2009
  81. Junio C HamanoOct 13, 2009
  82. Daniel BarkalowOct 13, 2009
  83. Jeff KingOct 13, 2009
  84. Junio C HamanoOct 13, 2009
  85. Johannes SchindelinOct 13, 2009
  86. Thomas RastOct 14, 2009
  87. Johannes SchindelinOct 16, 2009
  88. Thomas RastOct 16, 2009
  89. Uri OkrentOct 25, 2009
  90. Junio C HamanoOct 26, 2009
  91. Björn SteinbrinkOct 13, 2009

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.