Skip to content

submodule: lookup by path when name differs from path - #7486

Open
bokic wants to merge 1 commit into
libgit2:mainfrom
bokic:main
Open

bokic wants to merge 1 commit into
libgit2:mainfrom
bokic:main

Conversation

@bokic

@bokic bokic commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Summary

When a submodule's configured name in `.gitmodules` differs from its path (e.g. `[submodule "config"]` with `path = src/config`), `git_submodule__lookup_with_cache()` failed to match
the `.gitmodules` configuration when queried by path—such as during `git_status_list_new()` and diff generation. As a result, submodule config options like `ignore = dirty` were
ignored and `git_submodule_ignore()` fell back to `GIT_SUBMODULE_IGNORE_NONE`, reporting clean submodules as modified in the working tree.

Related / partially addresses: #5304 (when `ignore` is missed due to `name != path`), #7400 (submodule `name` vs `path` mismatch).

### Root Cause

In `git_submodule__lookup_with_cache()` (`src/libgit2/submodule.c`), the fallback that searches `.gitmodules` by `path` (`find_by_path`) only ran when `location == 0 || location ==
GIT_SUBMODULE_STATUS_IN_WD`.

When looking up a submodule by path where `name != path`, `git_submodule_reload()` still finds the gitlink in `HEAD` and the index, producing `IN_HEAD | IN_INDEX` (and `IN_WD`) without
`IN_CONFIG`. Because `location` was neither `0` nor `GIT_SUBMODULE_STATUS_IN_WD`, the `.gitmodules` path lookup was skipped.

### Changes

- **`src/libgit2/submodule.c`**:
  - Trigger the `.gitmodules` path lookup whenever `(location & GIT_SUBMODULE_STATUS_IN_CONFIG) == 0`.
  - Validate the resolved `data.name` with `git_submodule_name_is_valid()`: if invalid and the submodule is already present in `HEAD` or the index, ignore the invalid config entry
  (preserving existing `submodule::escape` behavior); otherwise reload so the expected error is returned.
- **`tests/libgit2/submodule/lookup.c`**:
  - Added `test_submodule_lookup__by_path_when_name_differs` covering lookup by path, `git_submodule_name()`, `git_submodule_ignore()`, `GIT_SUBMODULE_STATUS_IN_CONFIG`, and
  `git_status_file()` with `ignore = dirty`.

### Testing

- Ran `libgit2_tests -ssubmodule -sstatus` (all tests passed).

---
*Note: This fix was developed with AI assistance.*

When a submodule's configured name in .gitmodules differs from its path, looking it up by path (as done during status and diff) still finds the gitlink in HEAD and the index, setting IN_HEAD and IN_INDEX in location without IN_CONFIG.

Because git_submodule__lookup_with_cache() only fell back to searching .gitmodules by path when location was 0 or IN_WD, it skipped the path lookup and returned an unconfigured submodule (defaulting ignore to GIT_SUBMODULE_IGNORE_NONE).

Check !(location & GIT_SUBMODULE_STATUS_IN_CONFIG) instead so .gitmodules is searched by path whenever no config entry was matched, while still ignoring invalid configured names when the submodule is already present in HEAD or the index.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant