GitDB2

4.0.12 · active · verified Sun Apr 12

gitdb2 is a 'mirror package' that provides the `gitdb` library, which implements a pure-Python object database for Git. It handles the storage and retrieval of Git objects (blobs, trees, commits, tags) and is a core dependency for libraries like `GitPython`. The current version is 4.0.12, with releases driven primarily by `GitPython`'s needs and Python version compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `GitDB` instance, store a 'blob' object, and then retrieve it using its SHA1 hash. It utilizes `tempfile` for creating a temporary database location.

import tempfile
import os
import hashlib
from gitdb import GitDB, IStream

# Create a temporary directory for the GitDB
with tempfile.TemporaryDirectory() as temp_dir:
    # GitDB expects an 'objects' directory within its base path
    db_path = os.path.join(temp_dir, 'objects')
    os.makedirs(db_path) # Ensure the 'objects' directory exists

    # Initialize GitDB
    db = GitDB(db_path)
    print(f"GitDB initialized at: {db_path}")

    # Create some dummy content for a Git 'blob' object
    content = b"Hello, GitDB! This is a test blob.\n"
    size = len(content)
    object_type = b"blob"

    # Git objects are stored as: type + space + size + null byte + content
    header = object_type + b" " + str(size).encode('ascii') + b"\0"
    stored_data = header + content
    sha1 = hashlib.sha1(stored_data).hexdigest()

    # Create an IStream object for input
    # IStream takes type (bytes), size (int), and data (bytes)
    istream = IStream(object_type, size, content)

    # Store the object in the GitDB
    # The put method returns an object with the hexsha attribute
    stored_sha = db.put(istream).hexsha
    print(f"Stored object with SHA1: {stored_sha}")

    # Verify the SHA1 matches our expectation
    print(f"Expected SHA1: {sha1}")
    assert stored_sha == sha1

    # Retrieve the object using its SHA1 hash
    retrieved_istream = db.get(stored_sha)
    retrieved_content = retrieved_istream.read()

    print(f"Retrieved content type: {retrieved_istream.type.decode()}")
    print(f"Retrieved content size: {retrieved_istream.size}")
    print(f"Retrieved content: {retrieved_content.decode().strip()}")

    # Assert retrieved data matches original
    assert retrieved_istream.type == object_type
    assert retrieved_istream.size == size
    assert retrieved_content == content

    print("Object stored and retrieved successfully.")

view raw JSON →