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¶
- class libvcs.cmd.git.GitSubmoduleManager(*, path, cmd=None)[source]¶
Bases:
objectTraverse and manage git submodules with ORM-like filtering via QueryList.
Wrap some of git-submodule(1), manager.
- Parameters:
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') ''
- __init__(*, path, cmd=None)[source]¶
Wrap some of git-submodule(1), manager.
- Parameters:
- Return type:
None
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') ''
- path:
Path¶ Directory to check out
- run(command=None, local_flags=None, *, quiet=False, log_in_real_time=False, check_returncode=None, **kwargs)[source]¶
Run a command against a git repository’s submodules.
Wraps git submodule.
- Parameters:
command (
Optional[Literal['add','status','init','deinit','update','set-branch','set-url','summary','foreach','sync','absorbgitdirs']]) – Submodule command to run.local_flags (
list[str] |None) – Additional flags to pass.quiet (
bool) – Suppress output.log_in_real_time (bool)
check_returncode (bool | None)
kwargs (Any)
- Return type:
Examples
>>> GitSubmoduleManager(path=example_git_repo.path).run('status') ''
- add(repository, path=None, *, branch=None, force=False, name=None, depth=None, log_in_real_time=False, check_returncode=None)[source]¶
Add a new submodule.
- Parameters:
- Return type:
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
- init(*, path=None, log_in_real_time=False, check_returncode=None)[source]¶
Initialize submodules.
- Parameters:
- Return type:
Examples
>>> GitSubmoduleManager(path=example_git_repo.path).init() ''
- update(*, path=None, init=False, force=False, checkout=False, rebase=False, merge=False, recursive=False, remote=False, log_in_real_time=False, check_returncode=None)[source]¶
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)
check_returncode (bool | None)
- Return type:
Examples
>>> GitSubmoduleManager(path=example_git_repo.path).update() ''
>>> GitSubmoduleManager(path=example_git_repo.path).update(init=True) ''
- foreach(command, *, recursive=False, log_in_real_time=False, check_returncode=None)[source]¶
Run command in each submodule.
- Parameters:
- Return type:
Examples
>>> result = GitSubmoduleManager(path=example_git_repo.path).foreach( ... 'git status' ... ) >>> isinstance(result, str) True
- sync(*, path=None, recursive=False, log_in_real_time=False, check_returncode=None)[source]¶
Sync submodule URLs.
- Parameters:
- Return type:
Examples
>>> GitSubmoduleManager(path=example_git_repo.path).sync() ''
- summary(*, cached=False, files=False, summary_limit=None, commit=None, path=None, log_in_real_time=False, check_returncode=None)[source]¶
Show commit summary for submodules.
- Parameters:
- Return type:
Examples
>>> result = GitSubmoduleManager(path=example_git_repo.path).summary() >>> isinstance(result, str) True
- absorbgitdirs(*, path=None, log_in_real_time=False, check_returncode=None)[source]¶
Absorb git directories into superproject.
- Parameters:
- Return type:
Examples
>>> result = GitSubmoduleManager(path=example_git_repo.path).absorbgitdirs() >>> isinstance(result, str) True
- _ls(*, recursive=False, cached=False, log_in_real_time=False, check_returncode=None)[source]¶
Parse submodule status output into structured data.
- Parameters:
- Returns:
List of parsed submodule data.
- Return type:
Examples
>>> submodules = GitSubmoduleManager(path=example_git_repo.path)._ls() >>> isinstance(submodules, list) True
- ls(*, recursive=False, cached=False)[source]¶
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(path=None, name=None, **kwargs)[source]¶
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')
- class libvcs.cmd.git.GitSubmodule(name, path, url=None, sha=None, branch=None, status_prefix='', _cmd=None)[source]¶
Bases:
objectRepresent a git submodule.
- Parameters:
- status_prefix:
str= ''¶ ‘-’ not initialized, ‘+’ differs, ‘U’ conflicts.
- Type:
Status prefix
- _cmd:
GitSubmoduleEntryCmd|None= None¶ GitSubmoduleEntryCmd for operations on this submodule
- Type:
Internal
- property cmd: GitSubmoduleEntryCmd¶
Return command object for this submodule.
- class libvcs.cmd.git.GitSubmoduleEntryCmd(*, path, submodule_path, cmd=None)[source]¶
Bases:
objectRun git commands targeting a specific submodule.
Wrap some of git-submodule(1) for specific submodule operations.
- Parameters:
Examples
>>> GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ) <GitSubmoduleEntryCmd vendor/lib>
- __init__(*, path, submodule_path, cmd=None)[source]¶
Wrap some of git-submodule(1) for specific submodule operations.
- Parameters:
- Return type:
None
Examples
>>> GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ) <GitSubmoduleEntryCmd vendor/lib>
- path:
Path¶ Directory to check out
- run(command=None, local_flags=None, *, log_in_real_time=False, check_returncode=None, **kwargs)[source]¶
Run a command against a specific submodule.
Wraps git submodule.
- Parameters:
command (
Optional[Literal['add','status','init','deinit','update','set-branch','set-url','summary','foreach','sync','absorbgitdirs']]) – Submodule command to run.local_flags (
list[str] |None) – Additional flags to pass.log_in_real_time (bool)
check_returncode (bool | None)
kwargs (Any)
- Return type:
Examples
>>> GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).run('status') ''
- init(*, log_in_real_time=False, check_returncode=None)[source]¶
Initialize this submodule.
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).init() >>> 'error' in result.lower() or result == '' True
- update(*, init=False, force=False, checkout=False, rebase=False, merge=False, recursive=False, remote=False, log_in_real_time=False, check_returncode=None)[source]¶
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)
check_returncode (bool | None)
- Return type:
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).update() >>> 'error' in result.lower() or result == '' True
- deinit(*, force=False, log_in_real_time=False, check_returncode=None)[source]¶
Unregister this submodule.
- Parameters:
- Return type:
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).deinit() >>> 'error' in result.lower() or result == '' True
- set_branch(branch=None, *, default=False, log_in_real_time=False, check_returncode=None)[source]¶
Set branch for this submodule.
- Parameters:
- Return type:
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(url, *, log_in_real_time=False, check_returncode=None)[source]¶
Set URL for this submodule.
- Parameters:
- Return type:
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
- status(*, recursive=False, log_in_real_time=False, check_returncode=None)[source]¶
Get status of this submodule.
- Parameters:
- Return type:
Examples
>>> result = GitSubmoduleEntryCmd( ... path=example_git_repo.path, ... submodule_path='vendor/lib', ... ).status() >>> isinstance(result, str) True
- class libvcs.cmd.git.GitSubmoduleCmd(*, path, cmd=None)[source]¶
Bases:
objectRun git submodule commands (low-level, use GitSubmoduleManager for traversal).
Lite, typed, pythonic wrapper for git-submodule(1).
- Parameters:
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) ''
- __init__(*, path, cmd=None)[source]¶
Lite, typed, pythonic wrapper for git-submodule(1).
- Parameters:
- Return type:
None
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) ''
- path:
Path¶ Directory to check out
- run(command=None, local_flags=None, *, quiet=None, cached=None, log_in_real_time=False, check_returncode=None, **kwargs)[source]¶
Run a command against a git submodule.
Wraps git submodule.
- Return type:
- Parameters:
Examples
>>> GitSubmoduleCmd(path=example_git_repo.path).run() ''
- init(*, path=None, log_in_real_time=False, check_returncode=None)[source]¶
Git submodule init.
- Return type:
- Parameters:
Examples
>>> GitSubmoduleCmd(path=example_git_repo.path).init() ''
- update(*, path=None, init=None, force=None, checkout=None, rebase=None, merge=None, recursive=None, log_in_real_time=False, check_returncode=None, **kwargs)[source]¶
Git submodule update.
- Return type:
- Parameters:
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) ''