Metadata-Version: 2.4
Name: vcti-git
Version: 2.0.0
Summary: Git repository operations for VCollab applications
Author: Visual Collaboration Technologies Inc.
License-Expression: LicenseRef-Proprietary
Project-URL: Repository, https://github.com/vcollab/vcti-python-git
Project-URL: Changelog, https://github.com/vcollab/vcti-python-git/blob/main/CHANGELOG.md
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: <3.15,>=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: gitpython; extra == "test"
Requires-Dist: pygit2; extra == "test"
Requires-Dist: dulwich; extra == "test"
Provides-Extra: lint
Requires-Dist: ruff; extra == "lint"
Provides-Extra: typecheck
Requires-Dist: mypy; extra == "typecheck"
Provides-Extra: gitpython
Requires-Dist: gitpython; extra == "gitpython"
Provides-Extra: pygit2
Requires-Dist: pygit2; extra == "pygit2"
Provides-Extra: dulwich
Requires-Dist: dulwich; extra == "dulwich"
Dynamic: license-file

# vcti-git

Git repository operations for VCollab applications

## Overview

`vcti-git` provides one class, `Repository`, with a small API for the Git
operations an application most often automates: clone, commit, manage
remotes, fetch, fast-forward pull and push. `Repository` is a thin façade
over one of three interchangeable backends — pygit2, GitPython or dulwich —
selected by argument, by environment variable or by auto-detection, so the
same code runs where a `git` binary is installed and where it is not.
Every failure is raised as `GitError` or one of its two subclasses,
whichever backend is active, so a caller never imports a backend's own
exception types. It is a focused wrapper rather than a full Git client:
branch management, history, merges and authentication are outside it.

## Installation

The backends are optional extras, and at least one is needed:

```bash
pip install "vcti-git[pygit2]"       # libgit2 wheel; no git binary needed
pip install "vcti-git[gitpython]"    # drives the system git binary
pip install "vcti-git[dulwich]"      # pure Python; no native dependencies
```

Installed without an extra, the package imports, but constructing a
`Repository` raises `GitError` with these instructions.

### In `requirements.txt`

```
vcti-git[pygit2]>=2.0.0
```

### In `pyproject.toml` dependencies

```toml
dependencies = [
    "vcti-git[pygit2]>=2.0.0",
]
```

---

## Quick Start

The snippets below assume three names of your own: `remote_url`, the URL
of a repository to clone; `clone_path`, where to clone it; and
`repo_path`, an existing clone whose `origin` you can push to.

### Clone a repository

```python
from vcti.git import Repository

repo = Repository(clone_path)
repo.clone(remote_url)
print(repo.is_valid())  # True
repo.close()
```

`clone(url, overwrite=True)` replaces an existing clone. It refuses a
directory that is neither empty nor a Git repository, and restores the
original if the new clone fails.

### Commit and push

```python
with Repository(repo_path) as repo:
    (repo.path / "notes.txt").write_text("hello\n")

    repo.commit("Add notes", add_all=True)  # stage everything, then commit
    repo.push()                             # the checked-out branch, to origin
```

Without `add_all=True`, `commit()` commits what is already staged. Used
as a context manager, `Repository` releases its handle on the underlying
library when the block ends; `repo.close()` does the same explicitly.

### Fetch and pull

```python
with Repository(repo_path) as repo:
    repo.fetch("origin", prune=True, tags=True)

    # Fetch, then fast-forward the checked-out branch. Never merges: if
    # the branch has diverged from the remote, GitError is raised.
    repo.pull()
```

### Manage remotes

```python
with Repository(repo_path) as repo:
    repo.add_remote("upstream", remote_url)  # registers it; does not fetch
    print(sorted(repo.list_remotes()))       # ['origin', 'upstream']
    repo.remove_remote("upstream")
```

### Select a backend

```python
repo = Repository(repo_path)                     # auto-detect
print(repo.backend_name)

repo = Repository(repo_path, backend="dulwich")  # explicit
```

Auto-detection prefers `pygit2`, then `gitpython`, then `dulwich`, among
those installed. The `VCTI_GIT_BACKEND` environment variable overrides
auto-detection without a code change, and the `backend=` argument
overrides both:

```bash
VCTI_GIT_BACKEND=dulwich python my-app.py
```

### Handle errors

Every exception `Repository` raises derives from `GitError`. Catch the
subclass you can act on, or the base class for everything:

```python
from vcti.git import GitError, RemoteError, RepositoryError

try:
    with Repository(repo_path) as repo:
        repo.fetch("no-such-remote")
except RemoteError as e:
    print(f"Remote operation failed: {e}")
except RepositoryError as e:
    print(f"Not a usable repository: {e}")
except GitError as e:
    print(f"Other git failure: {e}")
```

Messages start with the backend's tag — `[pygit2]`, `[gitpython]` or
`[dulwich]` — so a log line says which backend produced it.

---

## Public API

| Member | Purpose |
|--------|---------|
| `Repository(path, *, backend=None)` | A handle on the repository at `path`. `backend=None` consults `VCTI_GIT_BACKEND`, then auto-detects. |
| `.path` | The resolved, absolute local path |
| `.backend_name` | The backend in use |
| `.is_valid()` | Whether `path` is itself a Git repository |
| `.clone(url, *, overwrite=False)` | Clone `url` into `path` |
| `.commit(message, *, add_all=False)` | Commit what is staged; optionally stage everything first |
| `.list_remotes()` | The remotes, as `{name: url}` |
| `.add_remote(name, url)` | Register a remote, without fetching |
| `.remove_remote(name)` | Remove a remote |
| `.fetch(remote="origin", *, prune=False, tags=False)` | Fetch from a remote |
| `.pull(remote="origin", branch=None)` | Fetch and fast-forward the checked-out branch |
| `.push(remote="origin", branch=None, *, force=False)` | Push a branch; the checked-out one by default |
| `.close()` | Release the backend's handle; also done on leaving a `with` block |
| `GitError` | Base class of every error raised |
| `RepositoryError` | The path is not a usable repository, or not safe to overwrite |
| `RemoteError` | A remote operation failed: clone, fetch, pull, push, remote configuration |

## Backends

| Backend | Extra | Needs | Notes |
|---------|-------|-------|-------|
| `pygit2` | `vcti-git[pygit2]` | libgit2, shipped in the wheel | Preferred by auto-detection. Suits slim container images. `commit()` requires `user.name` and `user.email` in the git config. |
| `gitpython` | `vcti-git[gitpython]` | a `git` binary on `PATH` | Uses the system git's credential helpers. |
| `dulwich` | `vcti-git[dulwich]` | nothing native | Pure Python: slower, and the most portable. |

A `Repository` instance is not thread-safe; use one per thread.

---

## Dependencies

None required. Each backend is an optional extra and brings one library:
[pygit2](https://pypi.org/project/pygit2/),
[GitPython](https://pypi.org/project/GitPython/) or
[dulwich](https://pypi.org/project/dulwich/).

---

## Documentation

| If you want to… | Read |
|---|---|
| Get started using the package | Quick Start above |
| See practical usage and the pitfalls to avoid | [docs/patterns.md](docs/patterns.md) |
| Look up an error message and what to do about it | [docs/errors.md](docs/errors.md) |
| Understand the design decisions and what is out of scope | [docs/design.md](docs/design.md) |
| Navigate and understand the source | [docs/source-guide.md](docs/source-guide.md) |
