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

Re: [PATCH v4 7/9] odb/source: support writing alternates when creating the database

From
Karthik Nayak <karthik.188@gmail.com>
Date
Sep 10, 2026, 11:10 UTC
Message-ID
<CAOLa=ZQaPstiQmXm9=TyWPUxL6X2=Lcqeg6y2XeXzSJDpq-GBA@mail.gmail.com>
In-Reply-To
<20260909-pks-odb-write-alternates-at-creation-time-v4-7-d8a78ffc32e4@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 46 quoted lines
> Add the ability to write alternates when creating the object database.
> This change allows us to remove the `write_alternates()` callback in a
> subsequent patch.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  odb/source-files.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
>  odb/source.h       | 17 +++++++++---
>  setup.c            |  4 ++-
>  3 files changed, 91 insertions(+), 6 deletions(-)
>
> diff --git a/odb/source-files.c b/odb/source-files.c
> index b7b3a297bb..8fe65d91f8 100644
> --- a/odb/source-files.c
> +++ b/odb/source-files.c
> @@ -18,6 +18,7 @@
>  #include "run-command.h"
>  #include "strbuf.h"
>  #include "string-list.h"
> +#include "strmap.h"
>  #include "strvec.h"
>  #include "tree.h"
>  #include "write-or-die.h"
> @@ -51,9 +52,14 @@ static void odb_source_files_close(struct odb_source *source)
>  	odb_source_close(&files->packed->base);
>  }
>
> -static int odb_source_files_create_on_disk(struct odb_source *source)
> +static int odb_source_files_create_on_disk(struct odb_source *source,
> +					   const struct odb_create_on_disk_options *opts)
>  {
> +	struct lock_file alternates_lock = LOCK_INIT;
>  	struct strbuf path = STRBUF_INIT;
> +	struct strset seen = STRSET_INIT;
> +	struct strbuf line = STRBUF_INIT;
> +	int ret;
>
>  	safe_create_dir(source->odb->repo, source->path, 1);
>
> @@ -64,8 +70,74 @@ static int odb_source_files_create_on_disk(struct odb_source *source)
>  	strbuf_addf(&path, "%s/info", source->path);
>  	safe_create_dir(source->odb->repo, path.buf, 1);
>
> +	if (opts->alternates && opts->alternates->nr) {
> +		FILE *alternates, *orig;
> +

So this is similar to what we already do in `odb_source_files_write_alternate()`.

Show 29 quoted lines
> +		strbuf_reset(&path);
> +		strbuf_addf(&path, "%s/info/alternates", source->path);
> +
> +		repo_hold_lock_file_for_update(source->odb->repo, &alternates_lock,
> +					       path.buf, LOCK_DIE_ON_ERROR);
> +
> +		alternates = fdopen_lock_file(&alternates_lock, "w");
> +		if (!alternates) {
> +			ret = error_errno(_("unable to fdopen alternates lockfile"));
> +			goto out;
> +		}
> +
> +		/*
> +		 * The alternates file may already exist, e.g. when it has been
> +		 * seeded from a template directory. Read any preexisting
> +		 * entries so that we don't end up writing duplicates.
> +		 */
> +		orig = fopen(path.buf, "r");
> +		if (orig) {
> +			while (strbuf_getline(&line, orig) != EOF) {
> +				strset_add(&seen, line.buf);
> +				fprintf(alternates, "%s\n", line.buf);
> +			}
> +
> +			if (ferror(orig)) {
> +				ret = error_errno(_("unable to read alternates file"));
> +				fclose(orig);
> +				goto out;
> +			}
Shouldn't this be checked inside the for loop with every `fprintf` call?
Show 19 quoted lines
> +
> +			fclose(orig);
> +		} else if (errno != ENOENT) {
> +			ret = error_errno(_("unable to read alternates file"));
> +			goto out;
> +		}
> +
> +		for (size_t i = 0; i < opts->alternates->nr; i++) {
> +			const char *alternate = opts->alternates->v[i];
> +			if (!strset_add(&seen, alternate))
> +				continue;
> +			fprintf(alternates, "%s\n", alternate);
> +		}
> +
> +		if (ferror(alternates)) {
> +			ret = error_errno(_("unable to write alternates file"));
> +			goto out;
> +		}
> +
same here.
Show 35 quoted lines
> +		if (commit_lock_file(&alternates_lock)) {
> +			ret = error_errno(_("unable to commit alternates file"));
> +			goto out;
> +		}
> +	}
> +
> +	/* Reprepare the object database to activate alternates. */
> +	odb_reprepare(source->odb);
> +
> +	ret = 0;
> +
> +out:
> +	rollback_lock_file(&alternates_lock);
> +	strbuf_release(&line);
>  	strbuf_release(&path);
> -	return 0;
> +	strset_clear(&seen);
> +	return ret;
>  }
>
>  static void odb_source_files_prepare(struct odb_source *source,
> diff --git a/odb/source.h b/odb/source.h
> index ea8675247e..63f1c0c531 100644
> --- a/odb/source.h
> +++ b/odb/source.h
> @@ -36,6 +36,15 @@ struct object_id;
>  struct odb_stream;
>  struct strvec;
>
> +struct odb_create_on_disk_options {
> +	/*
> +	 * Alternates that shall be written into the newly created object
> +	 * database. Whether or not this option can be handled is specific to
> +	 * the backend.
> +	 */

Would it make sense to formalize errors thrown by backends, so we know when a backend specifically cannot handle alternates?

> +	const struct strvec *alternates;
> +};
> +
[snip]
Previous: Karthik NayakNext: Patrick Steinhardt
Message 69 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.