git-python (pynickle)

1.1.2 · active · verified Mon Apr 13

git-python by pynickle is a lightweight Python wrapper that simplifies a small subset of Git commands for personal use. It functions primarily as a command-line tool, providing simplified commands like `init`, `add`, and `commit`. It is not the widely-used `GitPython` library (note the capitalization). The current version is 1.1.2, with an irregular release cadence focused on personal utility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to interact with `git-python` using its command-line interface via `subprocess`, which is its primary intended mode of operation. It initializes a new Git repository, creates a file, adds it, and commits it within a temporary directory.

import subprocess
import os
import tempfile
import shutil

# Create a temporary directory for the Git repo
temp_dir = tempfile.mkdtemp()
print(f"Created temporary directory: {temp_dir}")
original_cwd = os.getcwd()

try:
    os.chdir(temp_dir)

    # 1. Initialize a new gitpython repository
    print("Initializing gitpython repository...")
    result = subprocess.run(["gitpython", "gp", "init"], capture_output=True, text=True, check=True)
    print(f"init stdout:\n{result.stdout}")

    # 2. Create a file
    with open("example.txt", "w") as f:
        f.write("Hello, git-python!")
    print("Created example.txt")

    # 3. Add the file
    print("Adding example.txt...")
    result = subprocess.run(["gitpython", "gp", "add", "."], capture_output=True, text=True, check=True)
    print(f"add stdout:\n{result.stdout}")

    # 4. Commit the file
    print("Committing changes...")
    result = subprocess.run(["gitpython", "gp", "commit", "-m", "Initial commit via git-python CLI"], capture_output=True, text=True, check=True)
    print(f"commit stdout:\n{result.stdout}")

    # Verify using standard git CLI (optional, but good for demo)
    print("\nVerifying with standard 'git' command:")
    result = subprocess.run(["git", "log", "--oneline"], capture_output=True, text=True, check=True)
    print(f"git log stdout:\n{result.stdout}")

except subprocess.CalledProcessError as e:
    print(f"Error during subprocess call: {e}")
    print(f"Stdout: {e.stdout}")
    print(f"Stderr: {e.stderr}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    os.chdir(original_cwd)
    # Clean up the temporary directory
    print(f"Cleaning up temporary directory: {temp_dir}")
    shutil.rmtree(temp_dir)

view raw JSON →