git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/2] submodule: prevent overwriting .gitmodules on path reuse
Date: Thu, 24 Jul 2025 13:53:51 -0700	[thread overview]
Message-ID: <xmqqjz3xz4ww.fsf@gitster.g> (raw)
In-Reply-To: <20250724152418.45226-2-jayatheerthkulkarni2005@gmail.com> (K. Jayatheerth's message of "Thu, 24 Jul 2025 20:54:17 +0530")

K Jayatheerth <jayatheerthkulkarni2005@gmail.com> writes:

> Adding a submodule at a path that previously hosted
> another submodule (e.g., 'child') reuses the submodule
> name derived from the path. If the original submodule
> was only moved (e.g., to 'child_old') and not renamed,
> this silently overwrites its configuration in .gitmodules.
>
> This behavior loses user configuration and causes
> confusion when the original submodule is expected
> to remain intact. It assumes that the path-derived
> name is always safe to reuse, even though the name
> might still be in use elsewhere in the repository.
>
> Teach module_add() to check if the computed submodule
> name already exists in the repository's submodule config,
> and if so, refuse the operation unless the user explicitly
> renames the submodule or uses the --force option,
> which will automatically generate a unique name by
> appending a number (e.g., child1).
>
> Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
> ---

Very well described.

> +	existing = submodule_from_name(the_repository,
> +					null_oid(the_hash_algo),
> +					add_data.sm_name);
> +	
> +	if (existing && strcmp(existing->path, add_data.sm_path)) {
> +		if (!force) {
> +			die(_("submodule name '%s' already used for path '%s'"),
> +			add_data.sm_name, existing->path);

I'll locally fix this funny indentation; not a reason to require an
update.

> +		}
> +		/* --force: build <name><n> until unique */
> +		for (i = 1; ; i++) {

I think you can narrow the scope of "i" to this loop alone.  I'll
locally do so (and if anything breaks, which I doubt); not a reason
to require an update.

> + ...
> +		# Now adding a *new* repo at the old name must fail
> +		git init ../child2-origin &&
> +		git -C ../child2-origin commit --allow-empty -m init &&
> +		test_must_fail git submodule add ../child2-origin child

Shouldn't we also check what this failed command tell the end-user?  E.g.

	test_must_fail git submodule add ../child2-origin child	2>err &&
	test_grep "alreayd used for" err


  reply	other threads:[~2025-07-24 20:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24 15:24 [PATCH 0/2] Avoid submodule overwritten and skip redundant active entries K Jayatheerth
2025-07-24 15:24 ` [PATCH 1/2] submodule: prevent overwriting .gitmodules on path reuse K Jayatheerth
2025-07-24 20:53   ` Junio C Hamano [this message]
2025-07-24 15:24 ` [PATCH 2/2] submodule: skip redundant active entries when pattern covers path K Jayatheerth
2025-07-24 21:21   ` Junio C Hamano
2025-07-25 16:24 ` [PATCH 0/2] Avoid submodule overwritten and skip redundant active entries K Jayatheerth
2025-07-25 16:24   ` [PATCH 1/2] submodule: prevent overwriting .gitmodules on path reuse K Jayatheerth
2025-07-25 16:24   ` [PATCH 2/2] submodule: skip redundant active entries when pattern covers path K Jayatheerth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqjz3xz4ww.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=jayatheerthkulkarni2005@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).