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

[PATCH 8/8] odb: write alternates via sources

From
Patrick Steinhardt <ps@pks.im>
Date
Dec 8, 2025, 08:04 UTC
Message-ID
<20251208-b4-pks-odb-alternates-via-source-v1-8-e7ebb8b18c03@pks.im>
In-Reply-To
<20251208-b4-pks-odb-alternates-via-source-v1-0-e7ebb8b18c03@pks.im>

Refactor writing of alternates so that the actual business logic is structured around the object database source we want to write the alternate to. Same as with the preceding commit, this will eventually allow us to have different logic for writing alternates depending on the backend used.

Note that after the refactoring we start to call `odb_add_source()` unconditionally. This is fine though as we know to skip adding sources that are tracked already.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 odb.c | 51 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 16 deletions(-)
diff --git a/odb.c b/odb.c
index bf364fe3dd..9e7d078a46 100644
--- a/odb.c
+++ b/odb.c
@@ -271,25 +271,28 @@ static struct odb_source *odb_add_source(struct object_database *odb,
 	return alternate;
 }
 
-void odb_add_to_alternates_file(struct object_database *odb,
-				const char *dir)
+static int odb_source_write_alternate(struct odb_source *source,
+				      const char *alternate)
 {
 	struct lock_file lock = LOCK_INIT;
-	char *alts = repo_git_path(odb->repo, "objects/info/alternates");
+	char *path = xstrfmt("%s/%s", source->path, "info/alternates");
 	FILE *in, *out;
 	int found = 0;
+	int ret;
 
-	hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
+	hold_lock_file_for_update(&lock, path, LOCK_DIE_ON_ERROR);
 	out = fdopen_lock_file(&lock, "w");
-	if (!out)
-		die_errno(_("unable to fdopen alternates lockfile"));
+	if (!out) {
+		ret = error_errno(_("unable to fdopen alternates lockfile"));
+		goto out;
+	}
 
-	in = fopen(alts, "r");
+	in = fopen(path, "r");
 	if (in) {
 		struct strbuf line = STRBUF_INIT;
 
 		while (strbuf_getline(&line, in) != EOF) {
-			if (!strcmp(dir, line.buf)) {
+			if (!strcmp(alternate, line.buf)) {
 				found = 1;
 				break;
 			}
@@ -298,20 +301,36 @@ void odb_add_to_alternates_file(struct object_database *odb,
 
 		strbuf_release(&line);
 		fclose(in);
+	} else if (errno != ENOENT) {
+		ret = error_errno(_("unable to read alternates file"));
+		goto out;
 	}
-	else if (errno != ENOENT)
-		die_errno(_("unable to read alternates file"));
 
 	if (found) {
 		rollback_lock_file(&lock);
 	} else {
-		fprintf_or_die(out, "%s\n", dir);
-		if (commit_lock_file(&lock))
-			die_errno(_("unable to move new alternates file into place"));
-		if (odb->loaded_alternates)
-			odb_add_source(odb, dir, 0);
+		fprintf_or_die(out, "%s\n", alternate);
+		if (commit_lock_file(&lock)) {
+			ret = error_errno(_("unable to move new alternates file into place"));
+			goto out;
+		}
 	}
-	free(alts);
+
+	ret = 0;
+
+out:
+	free(path);
+	return ret;
+}
+
+void odb_add_to_alternates_file(struct object_database *odb,
+				const char *dir)
+{
+	int ret = odb_source_write_alternate(odb->sources, dir);
+	if (ret < 0)
+		die(NULL);
+	if (odb->loaded_alternates)
+		odb_add_source(odb, dir, 0);
 }
 
 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
-- 
2.52.0.270.g3f4935d65f.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 19 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.