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

[PATCH 3/8] builtin/clone: refactor handling of "--reference{,-if-able}"

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 25, 2026, 14:11 UTC
Message-ID
<20260825-pks-odb-write-alternates-at-creation-time-v1-3-911513ba95c3@pks.im>
In-Reply-To
<20260825-pks-odb-write-alternates-at-creation-time-v1-0-911513ba95c3@pks.im>

Users can pass "--reference{,-if-able}" to git-clone(1) to instruct it to set up alternates for the newly created repository. This allows it to reuse objects from the source repository so that in the best case we don't have to clone all objects over.

Those options are handled by the confusingly named `setup_reference()` function -- without the above context, one might rightfully believe that it was about refs, not about alternates. The function itself is rather simple: we loop through all provided alternate paths and then, if such an alternate is valid, we write it to the object database.

In subsequent commits we're about to consolidate the complete setup of alternates into this function, and furthermore we'll refactor the setup of the object database to handle doing this for us instead of writing the alterantes into it one by one.

Prepare for this refactoring by collecting the alternates into a strvec. Rename the function to `collect_alternates()` to clarify its scope.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/clone.c | 44 ++++++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 16 deletions(-)
diff --git a/builtin/clone.c b/builtin/clone.c
index 8c990ce0cc..8eae3ac7d9 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -154,24 +154,26 @@ static char *get_repo_path(const char *repo, int *is_bundle)
 	return canon;
 }
 
-static int add_one_reference(struct string_list_item *item, void *cb_data)
+struct add_one_alternate_data {
+	struct strvec *alternates;
+	int required;
+};
+
+static int add_one_alternate(struct string_list_item *item, void *cb_data)
 {
+	struct add_one_alternate_data *data = cb_data;
 	struct strbuf err = STRBUF_INIT;
-	int *required = cb_data;
 	char *ref_git = compute_alternate_path(item->string, &err);
 
 	if (!ref_git) {
-		if (*required)
+		if (data->required)
 			die("%s", err.buf);
 		else
 			fprintf(stderr,
 				_("info: Could not add alternate for '%s': %s\n"),
 				item->string, err.buf);
 	} else {
-		struct strbuf sb = STRBUF_INIT;
-		strbuf_addf(&sb, "%s/objects", ref_git);
-		odb_add_to_alternates_file(the_repository->objects, sb.buf);
-		strbuf_release(&sb);
+		strvec_pushf(data->alternates, "%s/objects", ref_git);
 	}
 
 	strbuf_release(&err);
@@ -218,14 +220,20 @@ static void copy_alternates(struct strbuf *src, const char *src_repo)
 	fclose(in);
 }
 
-static void setup_reference(void)
+static void collect_alternates(struct strvec *alternates)
 {
-	int required = 1;
-	for_each_string_list(&option_required_reference,
-			     add_one_reference, &required);
-	required = 0;
-	for_each_string_list(&option_optional_reference,
-			     add_one_reference, &required);
+	if (option_required_reference.nr || option_optional_reference.nr) {
+		struct add_one_alternate_data data = {
+			.alternates = alternates,
+			.required = 1,
+		};
+
+		for_each_string_list(&option_required_reference,
+				     add_one_alternate, &data);
+		data.required = 0;
+		for_each_string_list(&option_optional_reference,
+				     add_one_alternate, &data);
+	}
 }
 
 static void mkdir_if_missing(const char *pathname, mode_t mode)
@@ -999,6 +1007,7 @@ int cmd_clone(int argc,
 			   N_("uri"), N_("a URI for downloading bundles before fetching from origin remote")),
 		OPT_END()
 	};
+	struct strvec alternates = STRVEC_INIT;
 
 	const char * const builtin_clone_usage[] = {
 		N_("git clone [<options>] [--] <repo> [<dir>]"),
@@ -1343,8 +1352,10 @@ int cmd_clone(int argc,
 		warning(_("--local is ignored"));
 
 	create_object_database(the_repository);
-	if (option_required_reference.nr || option_optional_reference.nr)
-		setup_reference();
+	collect_alternates(&alternates);
+
+	for (size_t i = 0; i < alternates.nr; i++)
+		odb_add_to_alternates_file(the_repository->objects, alternates.v[i]);
 
 	transport = transport_get(remote, path ? path : remote->url.v[0]);
 	transport_set_verbosity(transport, option_verbosity, option_progress);
@@ -1641,6 +1652,7 @@ int cmd_clone(int argc,
 	string_list_clear(&option_not, 0);
 	string_list_clear(&option_config, 0);
 	string_list_clear(&server_options, 0);
+	strvec_clear(&alternates);
 
 	free(remote_name);
 	strbuf_release(&reflog_msg);
-- 
2.55.0.822.g20453c30eb.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 4 of 85 in “odb: write alternates at creation time”
  1. 0/8 odb: write alternates at creation timePatrick Steinhardt, Aug 25, 2026
  2. 1/8 builtin/clone: defer setup of the object databasePatrick Steinhardt, Aug 25, 2026
  3. 2/8 builtin/clone: move around `setup_reference()`Patrick Steinhardt, Aug 25, 2026
  4. 3/8 builtin/clone: refactor handling of "--reference{,-if-able}"Patrick Steinhardt, Aug 25, 2026
  5. 4/8 builtin/clone: move setup of alternates for shared local clonesPatrick Steinhardt, Aug 25, 2026
  6. 5/8 builtin/clone: move setup of alternates for non-shared local clonesPatrick Steinhardt, Aug 25, 2026
  7. 6/8 odb/source: support writing alternates when creating the databasePatrick Steinhardt, Aug 25, 2026
  8. 7/8 builtin/clone: write alternates via `odb_create_on_disk()`Patrick Steinhardt, Aug 25, 2026
  9. 8/8 odb/source: remove the ability to write alternatesPatrick Steinhardt, Aug 25, 2026
  10. Toon ClaesAug 28, 2026
  11. Toon ClaesAug 28, 2026
  12. Toon ClaesAug 28, 2026
  13. Toon ClaesAug 28, 2026
  14. Toon ClaesAug 28, 2026
  15. Junio C HamanoAug 28, 2026
  16. Patrick SteinhardtAug 31, 2026
  17. Patrick SteinhardtAug 31, 2026
  18. Patrick SteinhardtAug 31, 2026
  19. Patrick SteinhardtAug 31, 2026
  20. 0/8 odb: write alternates at creation timePatrick Steinhardt, Aug 31, 2026
  21. 1/8 builtin/clone: defer setup of the object databasePatrick Steinhardt, Aug 31, 2026
  22. 2/8 builtin/clone: move around `setup_reference()`Patrick Steinhardt, Aug 31, 2026
  23. 3/8 builtin/clone: refactor handling of "--reference{,-if-able}"Patrick Steinhardt, Aug 31, 2026
  24. 4/8 builtin/clone: move setup of alternates for shared local clonesPatrick Steinhardt, Aug 31, 2026
  25. 5/8 builtin/clone: move setup of alternates for non-shared local clonesPatrick Steinhardt, Aug 31, 2026
  26. 6/8 odb/source: support writing alternates when creating the databasePatrick Steinhardt, Aug 31, 2026
  27. 7/8 builtin/clone: write alternates via `odb_create_on_disk()`Patrick Steinhardt, Aug 31, 2026
  28. 8/8 odb/source: remove the ability to write alternatesPatrick Steinhardt, Aug 31, 2026
  29. Toon ClaesSep 1, 2026
  30. Justin ToblerSep 6, 2026
  31. Justin ToblerSep 6, 2026
  32. Justin ToblerSep 6, 2026
  33. Patrick SteinhardtSep 7, 2026
  34. Patrick SteinhardtSep 7, 2026
  35. 0/9 odb: write alternates at creation timePatrick Steinhardt, Sep 7, 2026
  36. 1/9 setup: split up concerns of `init_db()`Patrick Steinhardt, Sep 7, 2026
  37. 2/9 builtin/clone: defer setup of the object databasePatrick Steinhardt, Sep 7, 2026
  38. 3/9 builtin/clone: move around `setup_reference()`Patrick Steinhardt, Sep 7, 2026
  39. 4/9 builtin/clone: refactor handling of "--reference{,-if-able}"Patrick Steinhardt, Sep 7, 2026
  40. 5/9 builtin/clone: move setup of alternates for shared local clonesPatrick Steinhardt, Sep 7, 2026
  41. 6/9 builtin/clone: move setup of alternates for non-shared local clonesPatrick Steinhardt, Sep 7, 2026
  42. 7/9 odb/source: support writing alternates when creating the databasePatrick Steinhardt, Sep 7, 2026
  43. 8/9 builtin/clone: write alternates via `odb_create_on_disk()`Patrick Steinhardt, Sep 7, 2026
  44. 9/9 odb/source: remove the ability to write alternatesPatrick Steinhardt, Sep 7, 2026
  45. Justin ToblerSep 8, 2026
  46. Justin ToblerSep 8, 2026
  47. Justin ToblerSep 8, 2026
  48. Justin ToblerSep 8, 2026
  49. Patrick SteinhardtSep 9, 2026
  50. Patrick SteinhardtSep 9, 2026
  51. 0/9 odb: write alternates at creation timePatrick Steinhardt, Sep 9, 2026
  52. 1/9 setup: split up concerns of `init_db()`Patrick Steinhardt, Sep 9, 2026
  53. 2/9 builtin/clone: defer setup of the object databasePatrick Steinhardt, Sep 9, 2026
  54. 3/9 builtin/clone: move around `setup_reference()`Patrick Steinhardt, Sep 9, 2026
  55. 4/9 builtin/clone: refactor handling of "--reference{,-if-able}"Patrick Steinhardt, Sep 9, 2026
  56. 5/9 builtin/clone: move setup of alternates for shared local clonesPatrick Steinhardt, Sep 9, 2026
  57. 6/9 builtin/clone: move setup of alternates for non-shared local clonesPatrick Steinhardt, Sep 9, 2026
  58. 7/9 odb/source: support writing alternates when creating the databasePatrick Steinhardt, Sep 9, 2026
  59. 8/9 builtin/clone: write alternates via `odb_create_on_disk()`Patrick Steinhardt, Sep 9, 2026
  60. 9/9 odb/source: remove the ability to write alternatesPatrick Steinhardt, Sep 9, 2026
  61. Justin ToblerSep 9, 2026
  62. Karthik NayakSep 10, 2026
  63. Karthik NayakSep 10, 2026
  64. Karthik NayakSep 10, 2026
  65. Karthik NayakSep 10, 2026
  66. Karthik NayakSep 10, 2026
  67. Karthik NayakSep 10, 2026
  68. Karthik NayakSep 10, 2026
  69. Karthik NayakSep 10, 2026
  70. Patrick SteinhardtSep 10, 2026
  71. Patrick SteinhardtSep 10, 2026
  72. 0/9 odb: write alternates at creation timePatrick Steinhardt, Sep 10, 2026
  73. 1/9 setup: split up concerns of `init_db()`Patrick Steinhardt, Sep 10, 2026
  74. 2/9 builtin/clone: defer setup of the object databasePatrick Steinhardt, Sep 10, 2026
  75. 3/9 builtin/clone: move around `setup_reference()`Patrick Steinhardt, Sep 10, 2026
  76. 4/9 builtin/clone: refactor handling of "--reference{,-if-able}"Patrick Steinhardt, Sep 10, 2026
  77. 5/9 builtin/clone: move setup of alternates for shared local clonesPatrick Steinhardt, Sep 10, 2026
  78. 6/9 builtin/clone: move setup of alternates for non-shared local clonesPatrick Steinhardt, Sep 10, 2026
  79. 7/9 odb/source: support writing alternates when creating the databasePatrick Steinhardt, Sep 10, 2026
  80. 8/9 builtin/clone: write alternates via `odb_create_on_disk()`Patrick Steinhardt, Sep 10, 2026
  81. 9/9 odb/source: remove the ability to write alternatesPatrick Steinhardt, Sep 10, 2026
  82. Karthik NayakSep 10, 2026
  83. Patrick SteinhardtSep 11, 2026
  84. Patrick SteinhardtSep 11, 2026
  85. Karthik NayakSep 14, 2026

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.