submodule¶
For git-submodule(1).
Overview¶
Manage git submodules using GitSubmoduleManager (collection-level)
and GitSubmoduleEntryCmd (per-submodule operations).
Note
GitSubmoduleCmd is the legacy interface. Use git.submodules
(GitSubmoduleManager) for the new Manager/Cmd pattern.
Example¶
from libvcs.cmd.git import Git
git = Git(path='/path/to/repo')
# Add a submodule
git.submodules.add(url='https://github.com/org/lib.git', path='vendor/lib')
# List all submodules
submodules = git.submodules.ls()
# Get a specific submodule and operate on it
submodule = git.submodules.get(path='vendor/lib')
submodule.init()
submodule.update()
submodule.deinit()
# Sync submodule URLs
git.submodules.sync()
API Reference¶
Bases:
objectTraverse and manage git submodules with ORM-like filtering via QueryList.
Wrap some of git-submodule(1), manager.
Examples
>>> GitSubmoduleManager(path=tmp_path) <GitSubmoduleManager path=...>
>>> GitSubmoduleManager(path=tmp_path).run('status') 'fatal: not a git repository (or any of the parent directories): .git'
>>> GitSubmoduleManager(path=example_git_repo.path).run('status') ''
Run a command against a git repository’s submodules.
Wraps git submodule.
Examples
>>> GitSubmoduleManager(path=example_git_repo.path).run('status') ''
Add a new submodule.
Examples
>>> git_remote_repo = create_git_remote_repo() >>> result = GitSubmoduleManager(path=example_git_repo.path).add( ... f'file://{git_remote_repo}', ... 'vendor/example' ... ) >>> 'error' in result.lower() or 'fatal' in result.lower() or result == '' True
Update submodules.
- Parameters:
path (list[str] | str | None) – Specific submodule path(s) to update.
init (bool) – Initialize if not already.
force (bool) – Discard local changes.
checkout (bool) – Check out superproject’s recorded commit.
rebase (bool) – Rebase current branch onto commit.
merge (bool) – Merge commit into current branch.
recursive (bool) – Recurse into nested submodules.
remote (bool) – Use remote tracking branch.
log_in_real_time (bool)
- Return type:
Examples
>>> GitSubmoduleManager(path=example_git_repo.path).update() ''
>>> GitSubmoduleManager(path=example_git_repo.path).update(init=True) ''
Run command in each submodule.
Examples
>>> result = GitSubmoduleManager(path=example_git_repo.path).foreach( ... 'git status' ... ) >>> isinstance(result, str) True
Show commit summary for submodules.
Examples
>>> result = GitSubmoduleManager(path=example_git_repo.path).summary() >>> isinstance(result, str) True
Absorb git directories into superproject.
Examples
>>> result = GitSubmoduleManager(path=example_git_repo.path).absorbgitdirs() >>> isinstance(result, str) True
Parse submodule status output into structured data.
Examples
>>> submodules = GitSubmoduleManager(path=example_git_repo.path)._ls() >>> isinstance(submodules, list) True
List submodules as GitSubmodule objects.
- Parameters:
- Returns:
List of submodules with ORM-like filtering.
- Return type:
Examples
>>> submodules = GitSubmoduleManager(path=example_git_repo.path).ls() >>> isinstance(submodules, QueryList) True
Get a specific submodule.
- Parameters:
- Returns:
The matching submodule.
- Return type:
- Raises:
ObjectDoesNotExist – If no matching submodule found.
MultipleObjectsReturned – If multiple submodules match.
Examples
>>> submodules = GitSubmoduleManager(path=example_git_repo.path) >>> # submodule = submodules.get(path='vendor/lib')
Filter submodules.
- Parameters:
- Returns:
Filtered list of submodules.
- Return type:
Examples
>>> submodules = GitSubmoduleManager(path=example_git_repo.path).filter() >>> isinstance(submodules, QueryList) True
Bases:
objectRepresent a git submodule.
Status prefix: ‘-’ not initialized, ‘+’ differs, ‘U’ conflicts.
-
attribute
Internal: GitSubmoduleEntryCmd for operations on this submodule
-
property
Return command object for this submodule.
Bases:
objectRun git commands targeting a specific submodule.
Wrap some of git-submodule(1) for specific submodule operations.
Examples
>>> GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ) <GitSubmoduleEntryCmd vendor/lib>
Run a command against a specific submodule.
Wraps git submodule.
Examples
>>> GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).run('status') ''
Initialize this submodule.
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).init() >>> 'error' in result.lower() or result == '' True
Update this submodule.
- Parameters:
init (bool) – Initialize if not already.
force (bool) – Discard local changes.
checkout (bool) – Check out superproject’s recorded commit.
rebase (bool) – Rebase current branch onto commit.
merge (bool) – Merge commit into current branch.
recursive (bool) – Recurse into nested submodules.
remote (bool) – Use remote tracking branch.
log_in_real_time (bool)
- Return type:
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).update() >>> 'error' in result.lower() or result == '' True
Unregister this submodule.
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).deinit() >>> 'error' in result.lower() or result == '' True
Set branch for this submodule.
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).set_branch('main') >>> 'fatal' in result.lower() or 'error' in result.lower() or result == '' True
Set URL for this submodule.
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).set_url('https://example.com/repo.git') >>> 'fatal' in result.lower() or 'error' in result.lower() or result == '' True
Get status of this submodule.
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).status() >>> isinstance(result, str) True
Absorb git directory for this submodule.
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).absorbgitdirs() >>> 'error' in result.lower() or result == '' True
Bases:
objectRun git submodule commands (low-level, use GitSubmoduleManager for traversal).
Lite, typed, pythonic wrapper for git-submodule(1).
Examples
>>> GitSubmoduleCmd(path=tmp_path) <GitSubmoduleCmd path=...>
>>> GitSubmoduleCmd(path=tmp_path).run(quiet=True) 'fatal: not a git repository (or any of the parent directories): .git'
>>> GitSubmoduleCmd(path=example_git_repo.path).run(quiet=True) ''
Run a command against a git submodule.
Wraps git submodule.
Examples
>>> GitSubmoduleCmd(path=example_git_repo.path).run() ''
Git submodule init.
Examples
>>> GitSubmoduleCmd(path=example_git_repo.path).init() ''
Git submodule update.
Examples
>>> GitSubmoduleCmd(path=example_git_repo.path).update() '' >>> GitSubmoduleCmd(path=example_git_repo.path).update(init=True) '' >>> GitSubmoduleCmd( ... path=example_git_repo.path ... ).update(init=True, recursive=True) '' >>> GitSubmoduleCmd(path=example_git_repo.path).update(force=True) '' >>> GitSubmoduleCmd(path=example_git_repo.path).update(checkout=True) '' >>> GitSubmoduleCmd(path=example_git_repo.path).update(rebase=True) '' >>> GitSubmoduleCmd(path=example_git_repo.path).update(merge=True) ''