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

[PATCH 2/3] clone: factor out unborn head setup into its own function

From
Jeff King <peff@peff.net>
Date
Jul 6, 2022, 08:00 UTC
Message-ID
<YsVBLlpqqlRcrzv/@coredump.intra.peff.net>
In-Reply-To
<YsVAfudoUA5YkQWH@coredump.intra.peff.net>

This commit pulls the handling of an empty repository's unborn head into its own function. There's a similar case to handle in a non-empty repository (i.e., where just HEAD is empty, but it has other branches). The code isn't too long, but there's enough subtle policy logic that we wouldn't want to cut-and-paste it.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/clone.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
diff --git a/builtin/clone.c b/builtin/clone.c
index f596cedcf1..b7d3962c12 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -866,6 +866,26 @@ static int path_exists(const char *path)
 	return !stat(path, &sb);
 }
 
+static void setup_unborn_head(const char *target, const char *reflog_msg)
+{
+	const char *branch;
+	const char *ref;
+	char *ref_free = NULL;
+
+	if (target && skip_prefix(target, "refs/heads/", &branch)) {
+		ref = target;
+		create_symref("HEAD", ref, reflog_msg);
+	} else {
+		branch = git_default_branch_name(0);
+		ref_free = xstrfmt("refs/heads/%s", branch);
+		ref = ref_free;
+	}
+
+	if (!option_bare)
+		install_branch_config(0, branch, remote_name, ref);
+	free(ref_free);
+}
+
 int cmd_clone(int argc, const char **argv, const char *prefix)
 {
 	int is_bundle = 0, is_local;
@@ -1283,10 +1303,6 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 			our_head_points_at = remote_head_points_at;
 	}
 	else {
-		const char *branch;
-		const char *ref;
-		char *ref_free = NULL;
-
 		if (option_branch)
 			die(_("Remote branch %s not found in upstream %s"),
 					option_branch, remote_name);
@@ -1297,20 +1313,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		remote_head = NULL;
 		option_no_checkout = 1;
 
-		if (transport_ls_refs_options.unborn_head_target &&
-		    skip_prefix(transport_ls_refs_options.unborn_head_target,
-				"refs/heads/", &branch)) {
-			ref = transport_ls_refs_options.unborn_head_target;
-			create_symref("HEAD", ref, reflog_msg.buf);
-		} else {
-			branch = git_default_branch_name(0);
-			ref_free = xstrfmt("refs/heads/%s", branch);
-			ref = ref_free;
-		}
-
-		if (!option_bare)
-			install_branch_config(0, branch, remote_name, ref);
-		free(ref_free);
+		setup_unborn_head(transport_ls_refs_options.unborn_head_target,
+				  reflog_msg.buf);
 	}
 
 	write_refspec_config(src_ref_prefix, our_head_points_at,
-- 
2.37.0.408.g2817302ee7
Previous: Jeff KingNext: Jeff King
Message 3 of 21 in “cloning unborn HEAD when other branches are present”
  1. 0/3 cloning unborn HEAD when other branches are presentJeff King, Jul 6, 2022
  2. 1/3 clone: drop extra newline from warning messageJeff King, Jul 6, 2022
  3. 2/3 clone: factor out unborn head setup into its own functionJeff King, Jul 6, 2022
  4. 3/3 clone: propagate empty remote HEAD even with other branchesJeff King, Jul 6, 2022
  5. Junio C HamanoJul 6, 2022
  6. Junio C HamanoJul 6, 2022
  7. Jeff KingJul 7, 2022
  8. Junio C HamanoJul 7, 2022
  9. 0/3 cloning unborn HEAD when other branches are presentJeff King, Jul 7, 2022
  10. 1/3 clone: drop extra newline from warning messageJeff King, Jul 7, 2022
  11. 2/3 clone: propagate empty remote HEAD even with other branchesJeff King, Jul 7, 2022
  12. Junio C HamanoJul 8, 2022
  13. Jeff KingJul 8, 2022
  14. 3/3 clone: use remote branch if it matches default HEADJeff King, Jul 7, 2022
  15. Junio C HamanoJul 8, 2022
  16. Jeff KingJul 8, 2022
  17. Junio C HamanoJul 8, 2022
  18. 4/3 clone: move unborn head creation to update_head()Jeff King, Jul 11, 2022
  19. Junio C HamanoJul 11, 2022
  20. Jeff KingJul 7, 2022
  21. Jonathan TanJul 6, 2022

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.