scmrepo

3.6.2 · active · verified Sat Apr 11

scmrepo is an SCM wrapper and fsspec filesystem for Git, commonly used within the DVC ecosystem. It provides a unified API for interacting with Git repositories using various backends like pygit2, dulwich, and gitpython, without necessarily requiring a full `git checkout`. The library is actively maintained, with frequent patch and minor releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Git repository using `scmrepo.git.Git`, commit a file, and then access that file system through `scmrepo.fs.GitFileSystem`.

import os
import shutil
import tempfile
from scmrepo.git import Git
from scmrepo.fs import GitFileSystem

# Create a temporary directory for the repository
repo_dir = tempfile.mkdtemp()
file_path = os.path.join(repo_dir, "test_file.txt")

try:
    # Initialize a Git repository
    git = Git(repo_dir)
    git.init()

    # Create and commit a file
    with open(file_path, "w") as f:
        f.write("Hello, scmrepo!\n")
    git.add(file_path)
    git.commit("Initial commit", allow_empty=True)

    # Use GitFileSystem to read the file from 'HEAD'
    fs = GitFileSystem(repo_dir, rev="HEAD")
    with fs.open("test_file.txt", "r") as f:
        content = f.read()
    print(f"Content of test_file.txt: {content.strip()}")

    # Demonstrate walking the file system
    print("\nFiles in repo (from GitFileSystem):")
    for root, dnames, fnames in fs.walk("/"):
        for dname in dnames:
            print(fs.path.join(root, dname))
        for fname in fnames:
            print(fs.path.join(root, fname))

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the temporary directory
    shutil.rmtree(repo_dir)
    print(f"Cleaned up temporary repository at {repo_dir}")

view raw JSON →