Skip to content

Commit 61ca278

Browse files
committed
feat(submodule): add deinit method to Submodule (#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 #2014.
1 parent 75e6c6b commit 61ca278

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

git/objects/submodule/base.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
Callable,
5050
Dict,
5151
Iterator,
52+
List,
5253
Mapping,
5354
Sequence,
5455
TYPE_CHECKING,
@@ -1267,6 +1268,36 @@ def remove(
12671268

12681269
return self
12691270

1271+
@unbare_repo
1272+
def deinit(self, force: bool = False) -> "Submodule":
1273+
"""Run ``git submodule deinit`` on this submodule.
1274+
1275+
This is a thin wrapper around ``git submodule deinit <path>``, paralleling
1276+
:meth:`add`, :meth:`update`, and :meth:`remove`. It unregisters the
1277+
submodule (removes its entry from ``.git/config`` and empties the
1278+
working-tree directory) without deleting the submodule from
1279+
``.gitmodules`` or its checked-out repository under ``.git/modules/``.
1280+
A subsequent :meth:`update` will re-initialize the submodule from the
1281+
retained contents.
1282+
1283+
:param force:
1284+
If ``True``, pass ``--force`` to ``git submodule deinit``. This
1285+
allows deinitialization even when the submodule's working tree has
1286+
local modifications that would otherwise block the command.
1287+
1288+
:return:
1289+
self
1290+
1291+
:note:
1292+
Doesn't work in bare repositories.
1293+
"""
1294+
args: List[str] = []
1295+
if force:
1296+
args.append("--force")
1297+
args.extend(["--", self.path])
1298+
self.repo.git.submodule("deinit", *args)
1299+
return self
1300+
12701301
def set_parent_commit(self, commit: Union[Commit_ish, str, None], check: bool = True) -> "Submodule":
12711302
"""Set this instance to use the given commit whose tree is supposed to
12721303
contain the ``.gitmodules`` blob.

0 commit comments

Comments
 (0)