Implementation:Iterative Dvc Api Scm
| Knowledge Sources | |
|---|---|
| Domains | API, Version_Control |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Public API functions for listing Git branches, commits, and tags from DVC repositories, provided by the DVC library.
Description
The dvc/api/scm.py module (54 lines) exposes three public functions -- all_branches, all_commits, and all_tags -- that provide programmatic access to Git source control metadata through DVC's repository abstraction. These functions are part of DVC's public Python API.
All three functions follow an identical pattern: they accept an optional repo parameter (a local path or remote Git URL), open the repository using Repo.open, and delegate to the corresponding scm (source control manager) method. all_branches calls _repo.scm.list_branches() to return branch names, all_commits calls _repo.scm.list_all_commits() to return commit SHAs, and all_tags calls _repo.scm.list_tags() to return tag names. Each function returns a list[str].
The repo parameter supports the same location formats as other DVC API functions: local file system paths, HTTP URLs, and SSH URLs (e.g., [user@]server:project.git). When omitted, DVC resolves the repository by walking up from the current working directory.
Usage
Use these functions when you need to enumerate Git references in a DVC repository programmatically -- for example, to build branch selectors in a UI, to iterate over all tagged model versions for batch evaluation, or to list all commits for experiment comparison in CI/CD pipelines.
Code Reference
Source Location
- Repository: DVC
- File:
dvc/api/scm.py - Lines: L1-54
Signature
def all_branches(repo: Optional[str] = None) -> list[str]:
"""Get all Git branches in a DVC repository.
Args:
repo (str, optional): location of the DVC repository.
Returns:
List[str]: Names of the Git branches.
"""
...
def all_commits(repo: Optional[str] = None) -> list[str]:
"""Get all Git commits in a DVC repository.
Args:
repo (str, optional): location of the DVC repository.
Returns:
List[str]: SHAs of the Git commits.
"""
...
def all_tags(repo: Optional[str] = None) -> list[str]:
"""Get all Git tags in a DVC repository.
Args:
repo (str, optional): location of the DVC repository.
Returns:
List[str]: Names of the Git tags.
"""
...
Import
from dvc.api.scm import all_branches, all_commits, all_tags
I/O Contract
Inputs (Common to All Three Functions)
| Name | Type | Required | Description |
|---|---|---|---|
| repo | Optional[str] |
No | Location of the DVC repository. Can be a local file system path, an HTTP URL, or an SSH URL (e.g., [user@]server:project.git). Defaults to the current project directory (found by walking up from the current working directory).
|
Outputs
| Function | Return Type | Description |
|---|---|---|
| all_branches | list[str] |
Names of all Git branches in the repository. |
| all_commits | list[str] |
SHA hashes of all Git commits in the repository. |
| all_tags | list[str] |
Names of all Git tags in the repository. |
Usage Examples
Basic Usage
from dvc.api.scm import all_branches, all_commits, all_tags
# List all branches in the current repository
branches = all_branches()
print(f"Branches: {branches}")
# ['main', 'feature/new-model', 'experiment/lr-sweep']
# List all tags
tags = all_tags()
print(f"Tags: {tags}")
# ['v1.0.0', 'v1.1.0', 'v2.0.0']
# List all commits
commits = all_commits()
print(f"Total commits: {len(commits)}")
# Query a remote repository
remote_branches = all_branches(repo="https://github.com/myorg/myrepo")
remote_tags = all_tags(repo="https://github.com/myorg/myrepo")