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

[PATCH v4 0/7] submodule: improve remote lookup logic

From
Jacob Keller <jacob.e.keller@intel.com>
Date
Jun 23, 2025, 23:11 UTC
Message-ID
<20250623-jk-submodule-helper-use-url-v4-0-133ef3d89569@gmail.com>

This series improves the git submodule remote lookup logic implemented in submodule--helper.

A few cleanups are done first:
* Remove the branch->merge_name array and replace it by directly using
  branch->merge[i]->src immediately. This is simpler and easier to reason
  about. While cleaning this up, also fix the issues with branch_release()
  not tearing down everything properly.
* remote_clear() failed to release the remote->push and remote->fetch
  refspec data. Fix this.
* The starts_with_dot(_dot)_slash helper functions are moved to dir.h for
  re-use, as these are used both within submodule--helper.c and
  submodule-config.c
* Several remote.c helper functions are refactored to take repository
  pointers, enabling use with a submodule repository pointer.

Next, the submodule--helper.c logic replaces the repo_get_default_remote() function with a repo_default_remote() function in remote.c, which is based on the more robust configuration reading logic. This helper uses similar logic but also allows returning the only valid remote in the case where a repository has exactly one remote. This way we do not fall back to "origin" if a user has renamed the remote without adding another.

This improved logic is a good first step, but won't handle cases where there are multiple remotes, with none of them being named "origin".

For the final improvement, notice that the parent project already stores the URL for the submodule in its .git/config or .gitmodules file. This URL is what we use to set the remote in the first place when cloning.

Add a repo_remote_from_url() helper which will iterate through the remotes and find the first remote with that URL. Use this in get_default_remote_submodule() to first try and find a remote by its URL. If unsuccessful, we still keep the original fallback logic, in the off chance that the user has changed the URL from within the submodule.

This method is more robust as it is less likely that the user has manually changed the submodule URL within the submodule but not also within the .git/config.

With this change, all commands which need the submodule remote will first look up by URL before trying to use the fallback logic, and should now be able to find a suitable remote regardless of now they are renamed.

Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
---
Changes in v4:
- Fix branch_has_merge_config to use branch->set_merge
- FREE_AND_NULL branch->merge in merge_clear()
- Link to v3: https://lore.kernel.org/r/20250618-jk-submodule-helper-use-url-v3-0-7c60f2679271@gmail.com
Changes in v3:
- Completely remove branch->merge_name, making the resulting logic much
  easier to understand.
- Link to v2: https://lore.kernel.org/r/20250617-jk-submodule-helper-use-url-v2-0-04cbb003177d@gmail.com
Changes in v2:
- Remove repo_get_default_remote() entirely. The extra checks it does are
  really only necessary if you're doing manual configuration lookup. This
  avoids the confusion of similarly named functions and is less code.
- Fix leaks in branch_release() and remote_clear().
- Add a forward declaration of struct repository.
- Verified tests pass with leak sanitizer now.
- Link to v1: https://lore.kernel.org/r/20250610-jk-submodule-helper-use-url-v1-0-6d14c1504e91@gmail.com
---
Jacob Keller (7):
      remote: remove branch->merge_name and fix branch_release()
      remote: fix tear down of struct remote
      dir: move starts_with_dot(_dot)_slash to dir.h
      remote: remove the_repository from some functions
      submodule--helper: improve logic for fallback remote name
      submodule: move get_default_remote_submodule()
      submodule: look up remotes by URL first
 dir.h                       |  23 +++++++
 remote.h                    |   8 ++-
 branch.c                    |   4 +-
 builtin/pull.c              |   2 +-
 builtin/submodule--helper.c | 106 ++++++++++++-------------------
 remote.c                    | 149 ++++++++++++++++++++++++++++----------------
 submodule-config.c          |  12 ----
 t/t7406-submodule-update.sh |  61 ++++++++++++++++++
 8 files changed, 230 insertions(+), 135 deletions(-)
---
base-commit: 16bd9f20a403117f2e0d9bcda6c6e621d3763e77
change-id: 20250610-jk-submodule-helper-use-url-e55d3c379faf
Best regards,
-- 
Jacob Keller <jacob.keller@gmail.com>
Next: Jacob Keller
Message 1 of 8 in “submodule: improve remote lookup logic”
  1. 0/7 submodule: improve remote lookup logicJacob Keller, Jun 23, 2025
  2. 1/7 remote: remove branch->merge_name and fix branch_release()Jacob Keller, Jun 23, 2025
  3. 3/7 dir: move starts_with_dot(_dot)_slash to dir.hJacob Keller, Jun 23, 2025
  4. 2/7 remote: fix tear down of struct remoteJacob Keller, Jun 23, 2025
  5. 4/7 remote: remove the_repository from some functionsJacob Keller, Jun 23, 2025
  6. 5/7 submodule--helper: improve logic for fallback remote nameJacob Keller, Jun 23, 2025
  7. 6/7 submodule: move get_default_remote_submodule()Jacob Keller, Jun 23, 2025
  8. 7/7 submodule: look up remotes by URL firstJacob Keller, Jun 23, 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.