Dulwich

1.1.0 · active · verified Sat Mar 28

Dulwich is a pure-Python implementation of Git, providing an interface to both local and remote Git repositories without relying on the `git` executable or native code like `pygit2`. It offers both lower-level "plumbing" and higher-level "porcelain" APIs for interacting with Git objects and operations. The current version is 1.1.0, and it generally releases new versions every few weeks to months.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a new Git repository, add a file, commit the changes using Dulwich's high-level 'porcelain' API, and then retrieve the latest commit message using its lower-level 'plumbing' API. It uses a temporary directory for a clean example.

import os
from dulwich.repo import Repo
from dulwich import porcelain
from tempfile import TemporaryDirectory

# Create a temporary directory for the repository
with TemporaryDirectory() as temp_dir:
    repo_path = os.path.join(temp_dir, 'my_repo')

    # Initialize a new repository
    repo = porcelain.init(repo_path)
    print(f"Initialized repository at: {repo_path}")

    # Create a file
    file_path = os.path.join(repo_path, 'README.md')
    with open(file_path, 'w') as f:
        f.write('# My Dulwich Repo\n')
    print(f"Created file: {file_path}")

    # Add the file to the index and commit
    porcelain.add(repo_path, ['README.md'])
    porcelain.commit(repo_path, message=b'Initial commit: Add README')
    print("Committed initial README.md")

    # Log the commit
    for entry in porcelain.log(repo_path, max_entries=1):
        print(f"Latest commit: {entry.commit.message.decode().strip()}")

    # Example of low-level (plumbing) API to get commit message
    low_level_repo = Repo(repo_path)
    head_id = low_level_repo.head()
    latest_commit = low_level_repo[head_id]
    print(f"Low-level API: Latest commit message: {latest_commit.message.decode().strip()}")

view raw JSON →