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

Re: [PATCH v3] submodule: prevent overwriting .gitmodules entry on path reuse

From
JAYATHEERTH K <jayatheerthkulkarni2005@gmail.com>
Date
May 16, 2025, 08:51 UTC
Message-ID
<CA+rGoLfYLcx+tM6nif7E5vZDce7PeGyGJkTRraT7kF3ACAzBfA@mail.gmail.com>
In-Reply-To
<xmqqplga6c80.fsf@gitster.g>
On Thu, May 15, 2025 at 4:18 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 15 quoted lines
>
> K Jayatheerth <jayatheerthkulkarni2005@gmail.com> writes:
>
> > Add helper `submodule_active_matches_path()` so we can
> > re-implement the old “is this path already covered by
> > submodule.active?” logic without re-reading the config twice.
>
> Having duplicated code to implement what is supposed to be the same
> thing is a bug waiting to happen by them diverging from each other.
>
> Isn't the fact that our configuration reading code reads things just
> once and the caches the result good enough for the purpose of this
> code path?  Do we have a measurement that tells us that the extra
> complexity is worth the maintenance headache?
>

Well when I first sent this patch I didn't quite understand why test 4137 was failing then I read the tests and I didn't have a lot of idea of the code. But I think it' better to remove helper as you said I will send a new patch with a different approach as I've spent some time understanding this now.

Show 34 quoted lines
> > @@ -3443,7 +3452,11 @@ 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 strbuf buf = STRBUF_INIT;
> > +     int i;
> > +     int allocated_sm_name = 0;
>
> A separate flag is not wrong per-se, but the idiom used in this
> project more often is to have an extra pointer variable that points
> at an allocated piece of memory (or NULL), and free the piece of
> memory unconditionally.
>
> "git grep -e to_free" to see the idiom in action.  Even better yet,
> this codepath already uses the idiom.
>
> By doing so
>
> > +     if (allocated_sm_name)
> > +             free((char *)add_data.sm_name);
>
> becomes
>
>         free(sm_name_to_free);
>
> and we can keep the "add_data.sm_name is pointing at a borrowed
> piece of memory, and we will _never_ free anything through that
> pointer" memory ownership rule.  We were borrowing from a separate
> variable sm_name_to_free, and we may free it when add_data is
> getting destroyed, or we may be borrowing from the .sm_path string,
> which we would free it when add_data is getting destroyed.
>
Interesting, this is good, I'm going to copy this : )
Thank you,
-Jayatheerth
Previous: Junio C HamanoNext: K Jayatheerth
Message 9 of 24 in “submodule: prevent overwriting .gitmodules entry on path reuse”
  1. submodule: prevent overwriting .gitmodules entry on path reuseK Jayatheerth, May 10, 2025
  2. JAYATHEERTH KMay 10, 2025
  3. Junio C HamanoMay 12, 2025
  4. JAYATHEERTH KMay 12, 2025
  5. submodule: prevent overwriting .gitmodules entry on path reuseK Jayatheerth, May 13, 2025
  6. Junio C HamanoMay 13, 2025
  7. submodule: prevent overwriting .gitmodules entry on path reuseK Jayatheerth, May 14, 2025
  8. Junio C HamanoMay 14, 2025
  9. JAYATHEERTH KMay 16, 2025
  10. submodule: prevent overwriting .gitmodules entry on path reuseK Jayatheerth, May 16, 2025
  11. JAYATHEERTH KMay 16, 2025
  12. submodule: prevent overwriting .gitmodules entry on path reuseK Jayatheerth, May 18, 2025
  13. JAYATHEERTH KMay 18, 2025
  14. Junio C HamanoMay 19, 2025
  15. JAYATHEERTH KMay 20, 2025
  16. Junio C HamanoMay 20, 2025
  17. 0/2 Avoid submodule overwritten and skip redundant active entriesK Jayatheerth, May 24, 2025
  18. 1/2 submodule: prevent overwriting .gitmodules entry on path reuseK Jayatheerth, May 24, 2025
  19. 2/2 submodule: skip redundant active entries when pattern covers pathK Jayatheerth, May 24, 2025
  20. JAYATHEERTH KMay 24, 2025
  21. 0/2 Avoid submodule overwritten and skip redundant active entriesK Jayatheerth, May 24, 2025
  22. 1/2 The seventeenth batchK Jayatheerth, May 24, 2025
  23. JAYATHEERTH KMay 24, 2025
  24. 2/2 submodule: prevent overwriting .gitmodules entry on path reuseK Jayatheerth, May 24, 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.