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

[PATCH v3 1/8] odb: refactor parsing of alternates to be self-contained

From
Patrick Steinhardt <ps@pks.im>
Date
Dec 11, 2025, 09:30 UTC
Message-ID
<20251211-b4-pks-odb-alternates-via-source-v3-1-00e3f54d07ba@pks.im>
In-Reply-To
<20251211-b4-pks-odb-alternates-via-source-v3-0-00e3f54d07ba@pks.im>

Parsing of the alternates file and environment variable is currently split up across multiple different functions and is entangled with `link_alt_odb_entries()`, which is responsible for linking the parsed object database sources. This results in two downsides:

  - We have mutual recursion between parsing alternates and linking them
    into the object database. This is because we also parse alternates
    that the newly added sources may have.
  - We mix up the actual logic to parse the data and to link them into
    place.

Refactor the logic so that parsing of the alternates file is entirely self-contained. Note that this doesn't yet fix the above two issues, but it is a necessary step to get there.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 odb.c | 70 ++++++++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 40 insertions(+), 30 deletions(-)
diff --git a/odb.c b/odb.c
index dc8f292f3d..9785f62cb6 100644
--- a/odb.c
+++ b/odb.c
@@ -216,39 +216,50 @@ static struct odb_source *link_alt_odb_entry(struct object_database *odb,
 	return alternate;
 }
 
-static const char *parse_alt_odb_entry(const char *string,
-				       int sep,
-				       struct strbuf *out)
+static void parse_alternates(const char *string,
+			     int sep,
+			     struct strvec *out)
 {
-	const char *end;
+	struct strbuf buf = STRBUF_INIT;
 
-	strbuf_reset(out);
+	while (*string) {
+		const char *end;
+
+		strbuf_reset(&buf);
+
+		if (*string == '#') {
+			/* comment; consume up to next separator */
+			end = strchrnul(string, sep);
+		} else if (*string == '"' && !unquote_c_style(&buf, string, &end)) {
+			/*
+			 * quoted path; unquote_c_style has copied the
+			 * data for us and set "end". Broken quoting (e.g.,
+			 * an entry that doesn't end with a quote) falls
+			 * back to the unquoted case below.
+			 */
+		} else {
+			/* normal, unquoted path */
+			end = strchrnul(string, sep);
+			strbuf_add(&buf, string, end - string);
+		}
 
-	if (*string == '#') {
-		/* comment; consume up to next separator */
-		end = strchrnul(string, sep);
-	} else if (*string == '"' && !unquote_c_style(out, string, &end)) {
-		/*
-		 * quoted path; unquote_c_style has copied the
-		 * data for us and set "end". Broken quoting (e.g.,
-		 * an entry that doesn't end with a quote) falls
-		 * back to the unquoted case below.
-		 */
-	} else {
-		/* normal, unquoted path */
-		end = strchrnul(string, sep);
-		strbuf_add(out, string, end - string);
+		if (*end)
+			end++;
+		string = end;
+
+		if (!buf.len)
+			continue;
+
+		strvec_push(out, buf.buf);
 	}
 
-	if (*end)
-		end++;
-	return end;
+	strbuf_release(&buf);
 }
 
 static void link_alt_odb_entries(struct object_database *odb, const char *alt,
 				 int sep, const char *relative_base, int depth)
 {
-	struct strbuf dir = STRBUF_INIT;
+	struct strvec alternates = STRVEC_INIT;
 
 	if (!alt || !*alt)
 		return;
@@ -259,13 +270,12 @@ static void link_alt_odb_entries(struct object_database *odb, const char *alt,
 		return;
 	}
 
-	while (*alt) {
-		alt = parse_alt_odb_entry(alt, sep, &dir);
-		if (!dir.len)
-			continue;
-		link_alt_odb_entry(odb, dir.buf, relative_base, depth);
-	}
-	strbuf_release(&dir);
+	parse_alternates(alt, sep, &alternates);
+
+	for (size_t i = 0; i < alternates.nr; i++)
+		link_alt_odb_entry(odb, alternates.v[i], relative_base, depth);
+
+	strvec_clear(&alternates);
 }
 
 static void read_info_alternates(struct object_database *odb,
-- 
2.52.0.270.g3f4935d65f.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 34 of 41 in “Refactor handling of alternates to work via sources”
  1. 0/8 Refactor handling of alternates to work via sourcesPatrick Steinhardt, Dec 8, 2025
  2. 1/8 odb: refactor parsing of alternates to be self-containedPatrick Steinhardt, Dec 8, 2025
  3. Justin ToblerDec 8, 2025
  4. 2/8 odb: resolve relative alternative paths when parsingPatrick Steinhardt, Dec 8, 2025
  5. Justin ToblerDec 9, 2025
  6. Patrick SteinhardtDec 9, 2025
  7. Justin ToblerDec 9, 2025
  8. Patrick SteinhardtDec 10, 2025
  9. 3/8 odb: move computation of normalized objdir into `alt_odb_usable()`Patrick Steinhardt, Dec 8, 2025
  10. Justin ToblerDec 9, 2025
  11. Patrick SteinhardtDec 9, 2025
  12. 4/8 odb: adapt `odb_add_to_alternates_file()` to call `odb_add_source()`Patrick Steinhardt, Dec 8, 2025
  13. 5/8 odb: remove mutual recursion when parsing alternatesPatrick Steinhardt, Dec 8, 2025
  14. Justin ToblerDec 9, 2025
  15. 6/8 odb: drop forward declaration of `read_info_alternates()`Patrick Steinhardt, Dec 8, 2025
  16. 7/8 odb: read alternates via sourcesPatrick Steinhardt, Dec 8, 2025
  17. Justin ToblerDec 9, 2025
  18. Patrick SteinhardtDec 10, 2025
  19. 8/8 odb: write alternates via sourcesPatrick Steinhardt, Dec 8, 2025
  20. 0/8 Refactor handling of alternates to work via sourcesPatrick Steinhardt, Dec 10, 2025
  21. 1/8 odb: refactor parsing of alternates to be self-containedPatrick Steinhardt, Dec 10, 2025
  22. 2/8 odb: resolve relative alternative paths when parsingPatrick Steinhardt, Dec 10, 2025
  23. 3/8 odb: move computation of normalized objdir into `alt_odb_usable()`Patrick Steinhardt, Dec 10, 2025
  24. 4/8 odb: adapt `odb_add_to_alternates_file()` to call `odb_add_source()`Patrick Steinhardt, Dec 10, 2025
  25. SZEDER GáborDec 11, 2025
  26. Patrick SteinhardtDec 11, 2025
  27. 5/8 odb: remove mutual recursion when parsing alternatesPatrick Steinhardt, Dec 10, 2025
  28. 6/8 odb: drop forward declaration of `read_info_alternates()`Patrick Steinhardt, Dec 10, 2025
  29. 7/8 odb: read alternates via sourcesPatrick Steinhardt, Dec 10, 2025
  30. 8/8 odb: write alternates via sourcesPatrick Steinhardt, Dec 10, 2025
  31. Justin ToblerDec 10, 2025
  32. Patrick SteinhardtDec 11, 2025
  33. 0/8 Refactor handling of alternates to work via sourcesPatrick Steinhardt, Dec 11, 2025
  34. 1/8 odb: refactor parsing of alternates to be self-containedPatrick Steinhardt, Dec 11, 2025
  35. 2/8 odb: resolve relative alternative paths when parsingPatrick Steinhardt, Dec 11, 2025
  36. 3/8 odb: move computation of normalized objdir into `alt_odb_usable()`Patrick Steinhardt, Dec 11, 2025
  37. 4/8 odb: stop splitting alternate in `odb_add_to_alternates_file()`Patrick Steinhardt, Dec 11, 2025
  38. 5/8 odb: remove mutual recursion when parsing alternatesPatrick Steinhardt, Dec 11, 2025
  39. 6/8 odb: drop forward declaration of `read_info_alternates()`Patrick Steinhardt, Dec 11, 2025
  40. 7/8 odb: read alternates via sourcesPatrick Steinhardt, Dec 11, 2025
  41. 8/8 odb: write alternates via sourcesPatrick Steinhardt, Dec 11, 2025

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.