From: Junio C Hamano <gitster@pobox.com>
To: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2] submodule: prevent overwriting .gitmodules entry on path reuse
Date: Tue, 13 May 2025 14:44:33 -0700 [thread overview]
Message-ID: <xmqq7c2kgp8e.fsf@gitster.g> (raw)
In-Reply-To: <20250513033403.91365-1-jayatheerthkulkarni2005@gmail.com> (K. Jayatheerth's message of "Tue, 13 May 2025 09:04:03 +0530")
K Jayatheerth <jayatheerthkulkarni2005@gmail.com> writes:
> When a submodule is added at a path that previously hosted another submodule
> (e.g., 'child'), Git reuses the submodule name derived from the path and
> updates the corresponding entry in .gitmodules. This can silently overwrite
> existing configuration if the old submodule was only moved (e.g., to
> 'child_old') without renaming the submodule.
OK.
> This patch improves the `module_add()` logic by checking whether the
> submodule name already exists in the config but maps to a different path.
We frown upon a patch that says "This patch does X"; just give an
order to the codebase to "be like so". I.e. "Improve the module-add
by doing X..." is how we phrase a proposed change.
> In such a case, Git now errors out unless `--force` is specified, thus
> preventing accidental overwrites. To proceed safely, the user can provide
> a new name via `--name` or use `--force`.
The above explains what happens in module_add() quite well.
What is puzzling about this change is that the new helper function
and changes to configure_added_submodule() is not described at all
in the proposed log message. How are they relevant and why do we
need them?
> @@ -3443,6 +3452,7 @@ static int module_add(int argc, const char **argv, const char *prefix,
> int force = 0, quiet = 0, progress = 0, dissociate = 0;
> struct add_data add_data = ADD_DATA_INIT;
> const char *ref_storage_format = NULL;
> + const struct submodule *existing;
> char *to_free = NULL;
> struct option options[] = {
> OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
> @@ -3546,6 +3556,32 @@ static int module_add(int argc, const char **argv, const char *prefix,
> if(!add_data.sm_name)
> add_data.sm_name = add_data.sm_path;
>
> + 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 the name is in use, and the submodule with that name is at a
different path, then we are in trouble, OK.
> + if (!force)
> + die(_("submodule name '%s' already used for path '%s'"),
> + add_data.sm_name, existing->path);
> +
> + /* --force: build <name><n> until unique */
> + struct strbuf buf = STRBUF_INIT;
"-Wdeclaration-after-statement"
> + strbuf_addstr(&buf, add_data.sm_name);
> +
> + for (int i = 1; ; i++) {
> + strbuf_setlen(&buf, 0);
> + strbuf_addf(&buf, "%s%d", add_data.sm_name, i);
> +
> + if (!submodule_from_name(the_repository,
> + null_oid(the_hash_algo),
> + buf.buf))
> + break;
> + }
> +
> + add_data.sm_name = strbuf_detach(&buf, NULL);
> + }
> +
What is the memory ownership rule for add_data.sm_name? Earlier we
saw in a pre-context of a hunk that this was assigned from
add_data.sm_path, so in that codepath it is considered a borrowed
piece of memory, but here the member has to be the one that owns the
string detached from the strbuf, which eventually must be freed by
somebody, or it would be a memory leak.
next prev parent reply other threads:[~2025-05-13 21:44 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-10 5:45 [PATCH] submodule: prevent overwriting .gitmodules entry on path reuse K Jayatheerth
2025-05-10 5:57 ` JAYATHEERTH K
2025-05-12 12:32 ` Junio C Hamano
2025-05-12 13:26 ` JAYATHEERTH K
2025-05-13 3:34 ` [PATCH v2] " K Jayatheerth
2025-05-13 21:44 ` Junio C Hamano [this message]
2025-05-14 2:01 ` [PATCH v3] " K Jayatheerth
2025-05-14 22:47 ` Junio C Hamano
2025-05-16 8:51 ` JAYATHEERTH K
2025-05-16 17:49 ` [PATCH v4] " K Jayatheerth
2025-05-16 17:53 ` JAYATHEERTH K
2025-05-18 7:54 ` [PATCH v5] " K Jayatheerth
2025-05-18 7:58 ` JAYATHEERTH K
2025-05-19 15:41 ` Junio C Hamano
2025-05-20 1:31 ` JAYATHEERTH K
2025-05-20 15:28 ` Junio C Hamano
2025-05-24 6:48 ` [PATCH v6 0/2] Avoid submodule overwritten and skip redundant active entries K Jayatheerth
2025-05-24 6:48 ` [PATCH v6 1/2] submodule: prevent overwriting .gitmodules entry on path reuse K Jayatheerth
2025-05-24 6:48 ` [PATCH v6 2/2] submodule: skip redundant active entries when pattern covers path K Jayatheerth
2025-05-24 6:54 ` JAYATHEERTH K
2025-05-24 7:30 ` [PATCH v7 0/2] Avoid submodule overwritten and skip redundant active entries K Jayatheerth
2025-05-24 7:30 ` [PATCH v7 1/2] The seventeenth batch K Jayatheerth
2025-05-24 7:33 ` JAYATHEERTH K
2025-05-24 7:30 ` [PATCH v7 2/2] submodule: prevent overwriting .gitmodules entry on path reuse 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=xmqq7c2kgp8e.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).