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

[PATCH v3 3/4] worktree: factor out dwim_branch function

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Mar 17, 2018, 22:08 UTC
Message-ID
<20180317220830.30963-4-t.gummerer@gmail.com>
In-Reply-To
<20180317220830.30963-1-t.gummerer@gmail.com>

Factor out a dwim_branch function, which takes care of the dwim'ery in 'git worktree add <path>'. It's not too much code currently, but we're adding a new kind of dwim in a subsequent patch, at which point it makes more sense to have it as a separate function.

Factor it out now to reduce the patch noise in the next patch.
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 builtin/worktree.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 2266c132f9..52049b447a 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -367,6 +367,20 @@ static int add_worktree(const char *path, const char *refname,
 	return ret;
 }
 
+static const char *dwim_branch(const char *path, struct add_opts *opts)
+{
+	int n;
+	const char *s = worktree_basename(path, &n);
+	opts->new_branch = xstrndup(s, n);
+	if (guess_remote) {
+		struct object_id oid;
+		const char *remote =
+			unique_tracking_name(opts->new_branch, &oid);
+		return remote;
+	}
+	return NULL;
+}
+
 static int add(int ac, const char **av, const char *prefix)
 {
 	struct add_opts opts;
@@ -419,16 +433,9 @@ static int add(int ac, const char **av, const char *prefix)
 	}
 
 	if (ac < 2 && !opts.new_branch && !opts.detach) {
-		int n;
-		const char *s = worktree_basename(path, &n);
-		opts.new_branch = xstrndup(s, n);
-		if (guess_remote) {
-			struct object_id oid;
-			const char *remote =
-				unique_tracking_name(opts.new_branch, &oid);
-			if (remote)
-				branch = remote;
-		}
+		const char *dwim_branchname = dwim_branch(path, &opts);
+		if (dwim_branchname)
+			branch = dwim_branchname;
 	}
 
 	if (ac == 2 && !opts.new_branch && !opts.detach) {
-- 
2.17.0.rc0.231.g781580f06
Previous: Thomas GummererNext: Thomas Gummerer
Message 24 of 112 in “worktree: teach "add" to check out existing branches”
  1. worktree: teach "add" to check out existing branchesThomas Gummerer, Jan 21, 2018
  2. Robert P. J. DayJan 21, 2018
  3. Duy NguyenJan 22, 2018
  4. Thomas GummererJan 22, 2018
  5. 0/3 worktree: teach "add" to check out existing branchesThomas Gummerer, Feb 4, 2018
  6. 1/3 worktree: improve message when creating a new worktreeThomas Gummerer, Feb 4, 2018
  7. Duy NguyenFeb 5, 2018
  8. Thomas GummererFeb 5, 2018
  9. Junio C HamanoFeb 5, 2018
  10. Eric SunshineFeb 7, 2018
  11. Thomas GummererFeb 9, 2018
  12. Duy NguyenFeb 9, 2018
  13. Duy NguyenFeb 10, 2018
  14. 2/3 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Feb 4, 2018
  15. 3/3 worktree: teach "add" to check out existing branchesThomas Gummerer, Feb 4, 2018
  16. Duy NguyenFeb 5, 2018
  17. Junio C HamanoFeb 5, 2018
  18. Thomas GummererFeb 5, 2018
  19. Duy NguyenFeb 6, 2018
  20. Thomas GummererFeb 9, 2018
  21. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 17, 2018
  22. 1/4 worktree: improve message when creating a new worktreeThomas Gummerer, Mar 17, 2018
  23. 2/4 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Mar 17, 2018
  24. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Mar 17, 2018
  25. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 17, 2018
  26. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 17, 2018
  27. 1/4 worktree: improve message when creating a new worktreeThomas Gummerer, Mar 17, 2018
  28. Duy NguyenMar 19, 2018
  29. Junio C HamanoMar 19, 2018
  30. Eric SunshineMar 20, 2018
  31. Thomas GummererMar 24, 2018
  32. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Mar 17, 2018
  33. 2/4 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Mar 17, 2018
  34. Eric SunshineMar 20, 2018
  35. Eric SunshineMar 20, 2018
  36. Eric SunshineMar 20, 2018
  37. Thomas GummererMar 24, 2018
  38. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 17, 2018
  39. Eric SunshineMar 20, 2018
  40. Thomas GummererMar 24, 2018
  41. 0/6 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 25, 2018
  42. 1/6 worktree: improve message when creating a new worktreeThomas Gummerer, Mar 25, 2018
  43. 2/6 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Mar 25, 2018
  44. Eric SunshineMar 27, 2018
  45. Thomas GummererMar 30, 2018
  46. 3/6 worktree: remove force_new_branch from struct add_optsThomas Gummerer, Mar 25, 2018
  47. Eric SunshineMar 27, 2018
  48. Thomas GummererMar 30, 2018
  49. 4/6 worktree: factor out dwim_branch functionThomas Gummerer, Mar 25, 2018
  50. Eric SunshineMar 27, 2018
  51. 5/6 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 25, 2018
  52. Eric SunshineMar 27, 2018
  53. Thomas GummererMar 30, 2018
  54. 6/6 t2025: rename now outdated branch nameThomas Gummerer, Mar 25, 2018
  55. Eric SunshineMar 27, 2018
  56. Thomas GummererMar 30, 2018
  57. 0/6 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 31, 2018
  58. 1/6 worktree: remove extra members from struct add_optsThomas Gummerer, Mar 31, 2018
  59. 3/6 worktree: improve message when creating a new worktreeThomas Gummerer, Mar 31, 2018
  60. Eric SunshineApr 8, 2018
  61. 2/6 reset: introduce show-new-head-line optionThomas Gummerer, Mar 31, 2018
  62. Junio C HamanoApr 2, 2018
  63. Thomas GummererApr 2, 2018
  64. Thomas GummererApr 2, 2018
  65. Junio C HamanoApr 2, 2018
  66. Thomas GummererApr 2, 2018
  67. 6/6 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 31, 2018
  68. 5/6 fixup! worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 1, 2018
  69. Eric SunshineApr 9, 2018
  70. Thomas GummererApr 9, 2018
  71. Eric SunshineApr 9, 2018
  72. Eric SunshineApr 8, 2018
  73. Thomas GummererApr 8, 2018
  74. 4/6 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Mar 31, 2018
  75. 5/6 worktree: factor out dwim_branch functionThomas Gummerer, Mar 31, 2018
  76. Eric SunshineApr 8, 2018
  77. Thomas GummererApr 8, 2018
  78. Eric SunshineApr 9, 2018
  79. Thomas GummererApr 9, 2018
  80. Thomas GummererApr 9, 2018
  81. Eric SunshineApr 9, 2018
  82. Thomas GummererApr 11, 2018
  83. Eric SunshineApr 11, 2018
  84. Thomas GummererApr 11, 2018
  85. Eric SunshineApr 11, 2018
  86. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 15, 2018
  87. 1/4 worktree: remove extra members from struct add_optsThomas Gummerer, Apr 15, 2018
  88. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Apr 15, 2018
  89. 2/4 worktree: improve message when creating a new worktreeThomas Gummerer, Apr 15, 2018
  90. Junio C HamanoApr 16, 2018
  91. Thomas GummererApr 23, 2018
  92. Eric SunshineApr 23, 2018
  93. Thomas GummererApr 23, 2018
  94. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 15, 2018
  95. Eric SunshineApr 23, 2018
  96. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 23, 2018
  97. 1/4 worktree: remove extra members from struct add_optsThomas Gummerer, Apr 23, 2018
  98. Eric SunshineApr 24, 2018
  99. 2/4 worktree: improve message when creating a new worktreeThomas Gummerer, Apr 23, 2018
  100. Eric SunshineApr 24, 2018
  101. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Apr 23, 2018
  102. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 23, 2018
  103. Eric SunshineApr 24, 2018
  104. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 24, 2018
  105. 1/4 worktree: remove extra members from struct add_optsThomas Gummerer, Apr 24, 2018
  106. 2/4 worktree: improve message when creating a new worktreeThomas Gummerer, Apr 24, 2018
  107. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Apr 24, 2018
  108. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 24, 2018
  109. Eric SunshineApr 27, 2018
  110. Thomas GummererApr 28, 2018
  111. Junio C HamanoApr 30, 2018
  112. Junio C HamanoMar 18, 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.