feat(submodule): add deinit method to Submodule (#2014)#2129
Draft
mvanhorn wants to merge 1 commit intogitpython-developers:mainfrom
Draft
feat(submodule): add deinit method to Submodule (#2014)#2129mvanhorn wants to merge 1 commit intogitpython-developers:mainfrom
mvanhorn wants to merge 1 commit intogitpython-developers:mainfrom
Conversation
…#2014) Mirrors the pattern of `Submodule.add`, `Submodule.update`, and `Submodule.remove` by exposing `git submodule deinit` as a first-class method, so callers no longer need the `repo.git.submodule('deinit', ...)` workaround noted in the issue. The method delegates to `git submodule deinit [--force] -- <path>` via `self.repo.git.submodule`, decorated with `@unbare_repo` to match the other mutating Submodule methods. It unregisters the submodule from `.git/config` and clears the working-tree directory while leaving `.gitmodules` and `.git/modules/<name>` intact, so a later `update()` can re-initialize. Closes gitpython-developers#2014.
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a first-class Submodule.deinit(force=False) API to GitPython’s submodule object model, so callers can deinitialize a submodule without manually invoking repo.git.submodule('deinit', ...).
Changes:
- Introduces
Submodule.deinit(force: bool = False)as a thin wrapper aroundgit submodule deinit [--force] -- <path>. - Marks
deinitas unsupported for bare repositories via@unbare_repo. - Updates typing imports to support the new method’s local argument list construction.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1271
to
+1299
| @unbare_repo | ||
| def deinit(self, force: bool = False) -> "Submodule": | ||
| """Run ``git submodule deinit`` on this submodule. | ||
|
|
||
| This is a thin wrapper around ``git submodule deinit <path>``, paralleling | ||
| :meth:`add`, :meth:`update`, and :meth:`remove`. It unregisters the | ||
| submodule (removes its entry from ``.git/config`` and empties the | ||
| working-tree directory) without deleting the submodule from | ||
| ``.gitmodules`` or its checked-out repository under ``.git/modules/``. | ||
| A subsequent :meth:`update` will re-initialize the submodule from the | ||
| retained contents. | ||
|
|
||
| :param force: | ||
| If ``True``, pass ``--force`` to ``git submodule deinit``. This | ||
| allows deinitialization even when the submodule's working tree has | ||
| local modifications that would otherwise block the command. | ||
|
|
||
| :return: | ||
| self | ||
|
|
||
| :note: | ||
| Doesn't work in bare repositories. | ||
| """ | ||
| args: List[str] = [] | ||
| if force: | ||
| args.append("--force") | ||
| args.extend(["--", self.path]) | ||
| self.repo.git.submodule("deinit", *args) | ||
| return self |
There was a problem hiding this comment.
The new public API Submodule.deinit() isn't covered by tests. Please add a targeted unit test that asserts the underlying git invocation (e.g., by mocking submodule.repo.git.submodule) for both force=False and force=True, including verifying the -- separator and the provided path argument.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2014.
Adds a `Submodule.deinit(force=False)` method that wraps `git submodule deinit [--force] -- `, paralleling the existing `add` / `update` / `remove` methods. Callers no longer need the `repo.git.submodule('deinit', ...)` workaround mentioned in the issue.
Behavior
Why a thin wrapper
The full semantics of `git submodule deinit` (recursive children, working-tree checks, .git/modules retention) are already handled correctly by the git porcelain. Re-implementing that logic in Python for parity with the existing `remove` method would duplicate significant behavior. A thin wrapper gives callers a typed entry point without duplicating git's state machine.
Verification
🤖 AI-assisted.