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

[PATCH v4 6/9] checkout: pass the "num_matches" up to callers

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
May 31, 2018, 19:52 UTC
Message-ID
<20180531195252.29173-7-avarab@gmail.com>
In-Reply-To
<20180531195252.29173-1-avarab@gmail.com>

Pass the previously added "num_matches" struct value up to the callers of unique_tracking_name(). This will allow callers to optionally print better error messages in a later change.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/checkout.c | 16 +++++++++++-----
 builtin/worktree.c |  4 ++--
 checkout.c         |  5 ++++-
 checkout.h         |  3 ++-
 4 files changed, 19 insertions(+), 9 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2e1d2376d2..ec7cf93b4a 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -239,7 +239,8 @@ static int checkout_merged(int pos, const struct checkout *state)
 }
 
 static int checkout_paths(const struct checkout_opts *opts,
-			  const char *revision)
+			  const char *revision,
+			  int *dwim_remotes_matched)
 {
 	int pos;
 	struct checkout state = CHECKOUT_INIT;
@@ -878,7 +879,8 @@ static int parse_branchname_arg(int argc, const char **argv,
 				int dwim_new_local_branch_ok,
 				struct branch_info *new_branch_info,
 				struct checkout_opts *opts,
-				struct object_id *rev)
+				struct object_id *rev,
+				int *dwim_remotes_matched)
 {
 	struct tree **source_tree = &opts->source_tree;
 	const char **new_branch = &opts->new_branch;
@@ -972,7 +974,8 @@ static int parse_branchname_arg(int argc, const char **argv,
 			recover_with_dwim = 0;
 
 		if (recover_with_dwim) {
-			const char *remote = unique_tracking_name(arg, rev);
+			const char *remote = unique_tracking_name(arg, rev,
+								  dwim_remotes_matched);
 			if (remote) {
 				*new_branch = arg;
 				arg = remote;
@@ -1109,6 +1112,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	struct branch_info new_branch_info;
 	char *conflict_style = NULL;
 	int dwim_new_local_branch = 1;
+	int dwim_remotes_matched = 0;
 	struct option options[] = {
 		OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
 		OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -1219,7 +1223,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 			opts.track == BRANCH_TRACK_UNSPECIFIED &&
 			!opts.new_branch;
 		int n = parse_branchname_arg(argc, argv, dwim_ok,
-					     &new_branch_info, &opts, &rev);
+					     &new_branch_info, &opts, &rev,
+					     &dwim_remotes_matched);
 		argv += n;
 		argc -= n;
 	}
@@ -1262,7 +1267,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 
 	UNLEAK(opts);
 	if (opts.patch_mode || opts.pathspec.nr)
-		return checkout_paths(&opts, new_branch_info.name);
+		return checkout_paths(&opts, new_branch_info.name,
+				      &dwim_remotes_matched);
 	else
 		return checkout_branch(&opts, &new_branch_info);
 }
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 5c7d2bb180..a763dbdccb 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -412,7 +412,7 @@ static const char *dwim_branch(const char *path, const char **new_branch)
 	if (guess_remote) {
 		struct object_id oid;
 		const char *remote =
-			unique_tracking_name(*new_branch, &oid);
+			unique_tracking_name(*new_branch, &oid, NULL);
 		return remote;
 	}
 	return NULL;
@@ -484,7 +484,7 @@ static int add(int ac, const char **av, const char *prefix)
 
 		commit = lookup_commit_reference_by_name(branch);
 		if (!commit) {
-			remote = unique_tracking_name(branch, &oid);
+			remote = unique_tracking_name(branch, &oid, NULL);
 			if (remote) {
 				new_branch = branch;
 				branch = remote;
diff --git a/checkout.c b/checkout.c
index 7ce5306bc7..c578782baa 100644
--- a/checkout.c
+++ b/checkout.c
@@ -23,12 +23,15 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
 	return 0;
 }
 
-const char *unique_tracking_name(const char *name, struct object_id *oid)
+const char *unique_tracking_name(const char *name, struct object_id *oid,
+				 int *dwim_remotes_matched)
 {
 	struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
 	cb_data.src_ref = xstrfmt("refs/heads/%s", name);
 	cb_data.dst_oid = oid;
 	for_each_remote(check_tracking_name, &cb_data);
+	if (dwim_remotes_matched)
+		*dwim_remotes_matched = cb_data.num_matches;
 	free(cb_data.src_ref);
 	if (cb_data.num_matches == 1)
 		return cb_data.dst_ref;
diff --git a/checkout.h b/checkout.h
index 2decb9b820..4e518c801a 100644
--- a/checkout.h
+++ b/checkout.h
@@ -18,6 +18,7 @@ struct tracking_name_data {
  * exists, NULL otherwise.
  */
 extern const char *unique_tracking_name(const char *name,
-					struct object_id *oid);
+					struct object_id *oid,
+					int *dwim_remotes_matched);
 
 #endif /* CHECKOUT_H */
-- 
2.17.0.290.gded63e768a
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 31 of 95 in “checkout & worktree: introduce a core.DWIMRemote setting”
  1. checkout & worktree: introduce a core.DWIMRemote settingÆvar Arnfjörð Bjarmason, May 2, 2018
  2. Duy NguyenMay 2, 2018
  3. Eric SunshineMay 2, 2018
  4. Duy NguyenMay 2, 2018
  5. Ævar Arnfjörð BjarmasonMay 2, 2018
  6. checkout & worktree: introduce checkout.implicitRemoteÆvar Arnfjörð Bjarmason, May 3, 2018
  7. Duy NguyenMay 3, 2018
  8. Ævar Arnfjörð BjarmasonMay 4, 2018
  9. Duy NguyenMay 4, 2018
  10. Ævar Arnfjörð BjarmasonMay 4, 2018
  11. Eric SunshineMay 4, 2018
  12. checkout & worktree: introduce checkout.implicitRemoteÆvar Arnfjörð Bjarmason, May 24, 2018
  13. Junio C HamanoMay 25, 2018
  14. Duy NguyenMay 25, 2018
  15. Ævar Arnfjörð BjarmasonMay 25, 2018
  16. Duy NguyenMay 26, 2018
  17. Ævar Arnfjörð BjarmasonMay 31, 2018
  18. 0/9 ambiguous checkout UI & checkout.defaultRemoteÆvar Arnfjörð Bjarmason, May 31, 2018
  19. 1/9 checkout tests: index should be clean after dwim checkoutÆvar Arnfjörð Bjarmason, May 31, 2018
  20. Junio C HamanoJun 1, 2018
  21. Ævar Arnfjörð BjarmasonJun 1, 2018
  22. 2/9 checkout.h: wrap the arguments to unique_tracking_name()Ævar Arnfjörð Bjarmason, May 31, 2018
  23. 3/9 checkout.[ch]: move struct declaration into *.hÆvar Arnfjörð Bjarmason, May 31, 2018
  24. Thomas GummererMay 31, 2018
  25. Junio C HamanoJun 1, 2018
  26. Ævar Arnfjörð BjarmasonJun 1, 2018
  27. 4/9 checkout.[ch]: introduce an *_INIT macroÆvar Arnfjörð Bjarmason, May 31, 2018
  28. Junio C HamanoJun 1, 2018
  29. 5/9 checkout.[ch]: change "unique" member to "num_matches"Ævar Arnfjörð Bjarmason, May 31, 2018
  30. 7/9 builtin/checkout.c: use "ret" variable for returnÆvar Arnfjörð Bjarmason, May 31, 2018
  31. 6/9 checkout: pass the "num_matches" up to callersÆvar Arnfjörð Bjarmason, May 31, 2018
  32. 9/9 checkout & worktree: introduce checkout.defaultRemoteÆvar Arnfjörð Bjarmason, May 31, 2018
  33. Stefan BellerMay 31, 2018
  34. Eric SunshineJun 1, 2018
  35. Ævar Arnfjörð BjarmasonJun 1, 2018
  36. Thomas GummererMay 31, 2018
  37. Junio C HamanoJun 1, 2018
  38. 8/9 checkout: add advice for ambiguous "checkout <branch>"Ævar Arnfjörð Bjarmason, May 31, 2018
  39. Junio C HamanoJun 1, 2018
  40. Junio C HamanoJun 1, 2018
  41. Ævar Arnfjörð BjarmasonJun 1, 2018
  42. Junio C HamanoJun 4, 2018
  43. Ævar Arnfjörð BjarmasonJun 1, 2018
  44. Eric SunshineJun 1, 2018
  45. Ævar Arnfjörð BjarmasonJun 1, 2018
  46. 0/8 ambiguous checkout UI & checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 1, 2018
  47. 1/8 checkout tests: index should be clean after dwim checkoutÆvar Arnfjörð Bjarmason, Jun 1, 2018
  48. 2/8 checkout.h: wrap the arguments to unique_tracking_name()Ævar Arnfjörð Bjarmason, Jun 1, 2018
  49. 3/8 checkout.[ch]: introduce an *_INIT macroÆvar Arnfjörð Bjarmason, Jun 1, 2018
  50. Eric SunshineJun 1, 2018
  51. 4/8 checkout.[ch]: change "unique" member to "num_matches"Ævar Arnfjörð Bjarmason, Jun 1, 2018
  52. Eric SunshineJun 1, 2018
  53. 6/8 builtin/checkout.c: use "ret" variable for returnÆvar Arnfjörð Bjarmason, Jun 1, 2018
  54. 5/8 checkout: pass the "num_matches" up to callersÆvar Arnfjörð Bjarmason, Jun 1, 2018
  55. 8/8 checkout & worktree: introduce checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 1, 2018
  56. 7/8 checkout: add advice for ambiguous "checkout <branch>"Ævar Arnfjörð Bjarmason, Jun 1, 2018
  57. Eric SunshineJun 1, 2018
  58. 0/8 ambiguous checkout UI & checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 2, 2018
  59. 1/8 checkout tests: index should be clean after dwim checkoutÆvar Arnfjörð Bjarmason, Jun 2, 2018
  60. 2/8 checkout.h: wrap the arguments to unique_tracking_name()Ævar Arnfjörð Bjarmason, Jun 2, 2018
  61. 3/8 checkout.c: introduce an *_INIT macroÆvar Arnfjörð Bjarmason, Jun 2, 2018
  62. 4/8 checkout.c]: change "unique" member to "num_matches"Ævar Arnfjörð Bjarmason, Jun 2, 2018
  63. 6/8 builtin/checkout.c: use "ret" variable for returnÆvar Arnfjörð Bjarmason, Jun 2, 2018
  64. 5/8 checkout: pass the "num_matches" up to callersÆvar Arnfjörð Bjarmason, Jun 2, 2018
  65. 7/8 checkout: add advice for ambiguous "checkout <branch>"Ævar Arnfjörð Bjarmason, Jun 2, 2018
  66. 8/8 checkout & worktree: introduce checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 2, 2018
  67. Eric SunshineJun 3, 2018
  68. 0/8 ambiguous checkout UI & checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 5, 2018
  69. 1/8 checkout tests: index should be clean after dwim checkoutÆvar Arnfjörð Bjarmason, Jun 5, 2018
  70. SZEDER GáborJun 5, 2018
  71. tests: make use of the test_must_be_empty functionÆvar Arnfjörð Bjarmason, Jul 27, 2018
  72. Junio C HamanoJul 27, 2018
  73. SZEDER GáborJul 31, 2018
  74. t6018-rev-list-glob: fix 'empty stdin' testSZEDER Gábor, Aug 22, 2018
  75. Eric SunshineAug 22, 2018
  76. SZEDER GáborAug 22, 2018
  77. Eric SunshineAug 22, 2018
  78. Junio C HamanoAug 22, 2018
  79. Junio C HamanoAug 22, 2018
  80. Jeff KingAug 22, 2018
  81. rev-list: make empty --stdin not an errorJeff King, Aug 22, 2018
  82. Junio C HamanoAug 22, 2018
  83. Jeff KingAug 22, 2018
  84. Junio C HamanoAug 22, 2018
  85. Jeff KingAug 22, 2018
  86. Junio C HamanoAug 22, 2018
  87. 3/8 checkout.c: introduce an *_INIT macroÆvar Arnfjörð Bjarmason, Jun 5, 2018
  88. 2/8 checkout.h: wrap the arguments to unique_tracking_name()Ævar Arnfjörð Bjarmason, Jun 5, 2018
  89. 4/8 checkout.c: change "unique" member to "num_matches"Ævar Arnfjörð Bjarmason, Jun 5, 2018
  90. 6/8 builtin/checkout.c: use "ret" variable for returnÆvar Arnfjörð Bjarmason, Jun 5, 2018
  91. 7/8 checkout: add advice for ambiguous "checkout <branch>"Ævar Arnfjörð Bjarmason, Jun 5, 2018
  92. 8/8 checkout & worktree: introduce checkout.defaultRemoteÆvar Arnfjörð Bjarmason, Jun 5, 2018
  93. 5/8 checkout: pass the "num_matches" up to callersÆvar Arnfjörð Bjarmason, Jun 5, 2018
  94. Eric SunshineMay 4, 2018
  95. Eric SunshineMay 4, 2018

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.