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

Re: [PATCH 1/2] unpack-trees: lose USE_THE_REPOSITORY

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 9, 2026, 19:30 UTC
Message-ID
<xmqqecctzttt.fsf@gitster.g>
In-Reply-To
<avHNf-aaxdsyim7g@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 9 quoted lines
>> In any correctly written clients of this library file, the
>> repository instance that is pointed at by these two structs MUST be
>> the same one.  For now, make sure that is the case (and otherwise
>> BUG out) by introducing a unpack_repo() helper function that takes
>> these two structures (when available) and returns the repository.
>
> Fair. It's a bit curious of an interface to have this kind of ambiguity.
> But as an intermediate step I think the proposed solution is okayish,
> and I do like that we at least verify that it's the same repository.

Yeah, while I am reasonably sure that all three (when they exist) must always be pointing at the same repository, I may have missed a legitimate use case where that assumption does not hold. And that is why the initial implementation is checking them with paranoia to easily detect cases that I missed.

Show 32 quoted lines
>> +	if (istate) {
>> +		if (!istate->repo)
>> +			BUG("istate->repo == NULL");
>> +		repo = istate->repo;
>> +		camefrom = "istate->repo";
>> +	}
>> +	if (o) {
>> +		if (o->src_index) {
>> +			if (!o->src_index->repo)
>> +				BUG("o->src_index->repo == NULL");
>> +			if (repo && repo != o->src_index->repo)
>> +				BUG("%s != o->src_index->repo", camefrom);
>> +			repo = o->src_index->repo;
>> +			camefrom = "o->src_index->repo";
>> +		}
>> +
>> +		if (o->dst_index) {
>> +			if (!o->dst_index->repo)
>> +				BUG("o->dst_index->repo == NULL");
>> +			if (repo && repo != o->dst_index->repo)
>> +				BUG("%s != o->dst_index->repo", camefrom);
>> +			repo = o->dst_index->repo;
>> +		}
>> +	}
>> +	if (!repo)
>> +		BUG("no repository for unpack_trees()?");
>> +	return repo;
>> +
>> +}
>
> So this overall surprisingly complex, but I think it's sensible to be
> defensive.
Yes, we could do
	if (istate && istate->repo)
		return itate->repo;
	if (o && o->src_index && o->src_index->repo)
		return o->src_index->repo;
	if (o && o->dst_index && o->dst_index->repo)
		return o->dst_index->repo;
	BUG();

but then you'd need to start a debugger to tell which assumption was violated.

Show 11 quoted lines
>> @@ -345,15 +394,16 @@ static void load_gitmodules_file(struct index_state *index,
>>  				 struct checkout *state)
>>  {
>>  	int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE));
>> +	struct repository *repo = unpack_repo(index, NULL);
>
> This conversion here raises the question what we want to do in the case
> where the caller has both structures available, but we don't end up
> passing them to the pointer. This could in theory hide a bug where we
> have mismatching repositories when the entry points don't verify that
> property.

Puzzled. Here we do have an istate but have no unpack_trees_options available, so it is natural to pass NULL for the latter to tell the validator "we do not have unpack_trees_options". Are you worried about a case where a programmer who writes an unpack_repo() call passes NULL by mistake efven when istate or unpack_trees_options are available?

Thanks.
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 5 of 15 in “unpack-trees: lose USE_THE_REPOSITORY”
  1. 1/2 unpack-trees: lose USE_THE_REPOSITORYJunio C Hamano, Nov 7, 2026
  2. 2/2 unpack-trees: consistently use "struct index_state *istate"Junio C Hamano, Nov 7, 2026
  3. Derrick StoleeNov 9, 2026
  4. Patrick SteinhardtNov 9, 2026
  5. Junio C HamanoNov 9, 2026
  6. Patrick SteinhardtNov 10, 2026
  7. Junio C HamanoNov 10, 2026
  8. Patrick SteinhardtNov 10, 2026
  9. Derrick StoleeNov 9, 2026
  10. 0/2 unpack-trees.c w/o the_repositoryJunio C Hamano, Nov 9, 2026
  11. 1/2 unpack-trees: lose USE_THE_REPOSITORYJunio C Hamano, Nov 9, 2026
  12. 2/2 unpack-trees: consistently use "struct index_state *istate"Junio C Hamano, Nov 9, 2026
  13. Derrick StoleeNov 10, 2026
  14. Junio C HamanoNov 10, 2026
  15. Patrick SteinhardtNov 11, 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.