Metadata-Version: 2.4
Name: pl_vendor
Version: 0.6.4
Summary: Vendor Git repositories as ordinary files with a reproducible lockfile
Requires-Python: >=3.13
Description-Content-Type: text/markdown
Requires-Dist: cyclopts<6,>=5.0
Requires-Dist: jsonschema>=4.23
Requires-Dist: packaging>=24
Requires-Dist: rich>=14
Requires-Dist: tomlkit>=0.13

# pl_vendor

`pl_vendor` copies Git repositories or selected upstream files and directories into another repository as ordinary files and records their exact commits in a deterministic TOML lockfile.
It supports optional downstream patches and can verify that checked-in vendor trees still match their locked upstream commits.

## Requirements

- Python 3.13 or newer
- Git 2.30 or newer

Rich provides terminal progress and styling, while TOMLKit preserves unrelated content and comments when `pl-vendor` edits `pyproject.toml`.
Versioned Draft 2020-12 schemas for manifests and lockfiles ship in every distribution.

Every command checks the installed Git version before reading or changing vendored trees.

## Configuration

Initialize a repository before adding packages:

```sh
pl-vendor init
```

This idempotently adds `[tool.pl-vendor]` schema 3 to `pyproject.toml`.
It preserves unrelated content and comments, creates `pyproject.toml` when absent, and reports a no-op when the current schema is already present.
For a schema-1 or schema-2 manifest, it changes only the `schema-version` value to `3`.
For a 0.4 manifest, it atomically replaces `required-version` with `schema-version = 3` and removes the obsolete `run-uv-sync` setting.
This is safe to run immediately after `uv add --dev pl_vendor`, while `pyproject.toml` still has uncommitted package changes.

Check without changing files:

```sh
pl-vendor init --check
```

It exits 0 for schema 3, 1 for missing or migratable schema-1, schema-2, or 0.4 configuration, and 2 for malformed, pre-0.4, or future configuration.
Other commands do not migrate implicitly; they read schemas 1 and 2 unchanged and direct 0.4 users to `init`.

Add a package interactively:

```sh
pl-vendor add
```

The helper loads branch and tracked upstream-directory names from the Git repository;
press Tab at either prompt to complete them. It reports whether completion failed
because there is no internet connection or because the GitHub URL is inaccessible.
It then prints the shell-safe command assembled from your answers and asks for
confirmation before running it. Declining leaves the project unchanged.

Or provide its settings as arguments (the path defaults to `./.plvendor/NAME` and an omitted branch tracks the repository's default branch):

```sh
pl-vendor add example https://github.com/example/example.git
```

The default destination is relative to the project root that `uv` would discover from the current directory.
In a workspace, this places it under the workspace root.
This discovery reads local `pyproject.toml` files and does not require the `uv` executable.

Packages can instead be pinned to an exact commit.
Use `--subdirectory` to export only one directory from an upstream repository:

```sh
pl-vendor add prairielearn-schemas \
  https://github.com/PrairieLearn/PrairieLearn.git \
  .prairielearn/schemas \
  --commit 2c60f70a0ef817a2dc33fe15cb8e56de5035d6ed \
  --subdirectory apps/prairielearn/src/schemas/schemas
```

This creates or updates `pyproject.toml` at the root of the consuming Git repository, fetches the package, and records its revision.
The resulting manifest looks like:

```toml
[tool.pl-vendor]
schema-version = 3

[tool.pl-vendor.packages.example]
path = "./.plvendor/example"
url = "https://github.com/example/example.git"
```

PrairieLearn compatibility checks use the latest commit on the official
`PrairieLearn/PrairieLearn` `master` branch by default. A fork or deployment
branch can be configured independently of vendored packages:

```toml
[tool.pl-vendor.prairielearn]
url = "https://github.com/example/PrairieLearn.git"
branch = "production"
```

Both fields are optional and fall back independently to the official URL and
`master`. `pl-vendor init` leaves these defaults implicit.

The manifest schema and installed package version are independent.
pl-vendor 0.7 reads manifest schemas 1 through 3 and lockfile schemas 3 and 4, and writes lockfile schema 4; it recognizes an implicit 0.4 manifest only for `init` migration.
The `version` selector requires manifest schema 2, and the `patches-directory` and package `patch` settings require manifest schema 3.
It can read legacy schema-2 `plvendor-lock.yaml` files through the 0.7 release line.
See the [schema changelog](docs/schema-changelog.md) and [migration guide](docs/migrations.md), including exact standalone 0.3 instructions.

A commit-pinned subdirectory package looks like this:

```toml
[tool.pl-vendor.packages.prairielearn-schemas]
path = ".prairielearn/schemas"
url = "https://github.com/PrairieLearn/PrairieLearn.git"
commit = "2c60f70a0ef817a2dc33fe15cb8e56de5035d6ed"
subdirectory = "apps/prairielearn/src/schemas/schemas"
```

Use `sources` when one logical package consists of several files or directories from the same upstream revision.
Repeat `--source` on the command line:

```sh
pl-vendor add prairielearn-htmlmustache \
  https://github.com/PrairieLearn/PrairieLearn.git \
  .prairielearn/htmlmustache \
  --branch master \
  --source .htmlmustache.jsonc \
  --source apps/prairielearn/src/lib/element-schemas/htmlmustache-plugin.ts
```

The equivalent manifest is:

```toml
[tool.pl-vendor]
schema-version = 3

[tool.pl-vendor.packages.prairielearn-htmlmustache]
path = ".prairielearn/htmlmustache"
url = "https://github.com/PrairieLearn/PrairieLearn.git"
branch = "master"
sources = [
  ".htmlmustache.jsonc",
  "apps/prairielearn/src/lib/element-schemas/htmlmustache-plugin.ts",
]
```

Unlike `subdirectory`, which strips the selected directory itself, `sources` preserves every repository-relative path beneath the destination.
The example installs `.prairielearn/htmlmustache/.htmlmustache.jsonc` and `.prairielearn/htmlmustache/apps/prairielearn/src/lib/element-schemas/htmlmustache-plugin.ts`.
Each source may be a file, symbolic link, or directory.
Source paths must be nonempty, relative POSIX paths that do not contain `..`; duplicates and ancestor/descendant overlaps are rejected.
A source must exist as a file or directory at the resolved revision.
`sources` must be nonempty and cannot be combined with `subdirectory`.

Prefer one multi-source package over several entries for one logical upstream artifact: all selected paths then resolve, lock, update, patch, and verify at one atomic commit.

### Filtering selected files

Use package-level `include` and `exclude` lists to filter the assembled upstream tree.
For example, vendor the element schemas without their TypeScript tests:

```toml
[tool.pl-vendor]
schema-version = 3

[tool.pl-vendor.packages.htmlmustache]
url = "https://github.com/PrairieLearn/PrairieLearn.git"
path = ".htmlmustache"
subdirectory = "apps/prairielearn/src/lib/element-schemas"
exclude = ["**/*.test.ts"]
```

The equivalent command is:

```sh
pl-vendor add htmlmustache https://github.com/PrairieLearn/PrairieLearn.git \
  .htmlmustache --subdirectory apps/prairielearn/src/lib/element-schemas \
  --exclude '**/*.test.ts'
```

To install only `.htmlmustache/htmlmustache-plugin.ts`, set `include = ["htmlmustache-plugin.ts"]` in that package table, or use:

```sh
pl-vendor add htmlmustache https://github.com/PrairieLearn/PrairieLearn.git \
  .htmlmustache --subdirectory apps/prairielearn/src/lib/element-schemas \
  --include 'htmlmustache-plugin.ts'
```

This selects only that file; imported dependencies are not discovered automatically.
Repeat `--include` and `--exclude` for multiple patterns.
Quote shell globs so the shell does not expand them against local files.

Patterns match paths **relative to the assembled vendor tree**, before downstream patches.
`subdirectory` strips its upstream prefix, so the examples use `htmlmustache-plugin.ts`.
With `sources = ["apps/schemas"]`, paths retain their repository prefix, so an include would be `apps/schemas/plugin.ts`.
The destination prefix (such as `.htmlmustache/`) is never part of a pattern.
`sources` and `subdirectory` remain literal upstream paths, not globs.

Matching is case-sensitive and follows Python 3.13 `PurePosixPath.full_match`: `*`, `?`, and bracket classes match within path components; `**` spans zero or more directories.
In particular, `**/*.test.ts` matches both `root.test.ts` and `elements/nested.test.ts`.
Hidden entries participate in matching.

- Omitted `include` selects everything; otherwise its patterns are combined as a union.
  Exclusions always win, regardless of ordering.
- A matched directory includes or excludes its entire subtree.
  A trailing `/` restricts a pattern to directories: `tests/` excludes the root test directory, while `**/tests/` excludes test directories at any depth.
  Necessary parent directories remain; empty directories are pruned when filters are configured.
- Individual unmatched patterns are allowed.
  If filtering leaves no upstream files or symlinks, the operation fails before replacing that package, even if a patch could add files.
  `include = []` is invalid; `exclude = []` is allowed.
- Patterns must be nonempty relative POSIX strings.
  Absolute paths, backslashes, NULs, empty path components, and `.` or `..` components are rejected.
- Symlinks are matched as leaf entries, preserved verbatim, and never traversed.
  A directory-only pattern does not match a symlink pointing to a directory.

Filtering happens after source assembly and before patches.
Patches that modify excluded files fail; patches may explicitly add files whose paths match exclusions.
There is no second filtering pass after patches.
Add, sync, update, `check`, overwrite checks, and content-based upstream checks all use this same selected, patched tree.
Unexpected files in the installed tree still cause verification to fail.

Filtering does not change the upstream revision in the lockfile.

When changing an existing package's filters, commit the manifest change, then run `pl-vendor sync htmlmustache`.
If the installed tree differs from the new selection, the normal overwrite protection requires `pl-vendor sync htmlmustache --force`.
Review local edits before forcing replacement.
Sync retains the locked commit and leaves unrelated packages alone.

Packages track the repository's default branch when `branch`, `commit`, and `version` are all omitted.
Set `branch` to track a specific branch, `commit` to pin a complete lowercase 40-character Git SHA, or `version` to track upstream release tags; at most one of the three may be set.
`subdirectory` is relative to the upstream repository and defaults to its root.

Destination paths are canonicalized before comparison.
Exact, normalized, case-equivalent, and ancestor/descendant collisions are rejected, as are `.git`, `.vendor-patches`, `pyproject.toml`, `plvendor.lock`, `plvendor-lock.yaml`, their case variants, and paths beneath reserved directories.
Destinations also may not overlap the configured patches directory or any package's patch file.
Resolved paths must remain inside the repository; symlink escapes and non-directory destinations are rejected.

### Tracking release tags with version ranges

Set `version` to a [PEP 440 version specifier](https://packaging.python.org/en/latest/specifications/version-specifiers/), the same syntax uv uses for Python dependencies, to track the highest upstream tag in that range:

```toml
[tool.pl-vendor.packages.schemas]
path = ".schemas"
url = "https://github.com/example/schemas.git"
version = ">=1.4.0"
```

The equivalent command is `pl-vendor add schemas https://github.com/example/schemas.git .schemas --version '>=1.4.0'`; quote the specifier so the shell does not treat `>` as a redirect.
Any specifier works, including `>=1.4,<2`, `~=1.4`, `==1.4.*`, `==1.4.2`, and `!=1.5.0,>=1.4`.
A bare version such as `1.4.2` is rejected; write `==1.4.2`.

Tags name versions when they parse as PEP 440 versions, with an optional leading `v`: `1.4.2` and `v1.4.2` both count, while tags such as `release-1.4` or `schemas/v1.4` are ignored.
Annotated tags resolve to the commit they point to.
Pre-releases match only when the specifier itself names one, such as `>=2.0.0rc1`.
If two tags name the same version (for example `v1.4` and `1.4.0`) but point to different commits, resolution fails instead of guessing.

Version-range packages behave like branch packages: `lock` keeps the locked tag while it still satisfies the specifier, `lock --upgrade` and `update` advance to the highest matching tag, and `check upstream` and `check latest` compare against it.
When the specifier changes so that the locked version no longer matches, `lock --check` and `sync` fail without contacting the remote, and `lock` re-resolves the package.

The generated `plvendor.lock` records the resolved commit and, for version-range packages, the selected version and tag:

```toml
version = 4

[[package]]
name = "example"
revision = "0123456789abcdef0123456789abcdef01234567"

[[package]]
name = "schemas"
revision = "89abcdef0123456789abcdef0123456789abcdef"
version = "1.4.2"
tag = "v1.4.2"
```

Schema-3 `plvendor.lock` files remain readable. Read-only commands and `sync` leave them unchanged, while `lock`, `add`, `update`, and `remove` rewrite them as schema 4.

Schema-2 `plvendor-lock.yaml` remains readable in pl-vendor 0.7. Read-only
commands and `sync` warn but leave it unchanged. `lock`, `add`, `update`, and
`remove` atomically replace it with `plvendor.lock`; if both files exist,
pl-vendor refuses the ambiguous state. Run `pl-vendor lock` to migrate explicitly.
Legacy YAML support is removed in pl-vendor 0.8.

### Downstream patches

Each package may carry one optional downstream patch, applied only after the complete selected tree is assembled.
By default it lives at `.vendor-patches/<package-name>.patch`.
Manifest schema 3 can move the directory for every package, or point one package at an explicit file:

```toml
[tool.pl-vendor]
schema-version = 3
patches-directory = "patches"  # optional; defaults to ".vendor-patches"

[tool.pl-vendor.packages.example]
path = ".plvendor/example"
url = "https://github.com/example/example.git"
# Uses patches/example.patch when that file exists.

[tool.pl-vendor.packages.custom]
path = ".plvendor/custom"
url = "https://github.com/example/custom.git"
patch = "downstream/custom.patch"  # explicit override
```

An explicit package `patch` takes precedence over `<patches-directory>/<package-name>.patch`.
A missing implicit patch means the package is unpatched.
A missing explicit patch is an error: `add`, `sync`, `update`, and `remove` stop without changing anything, and `check installed` reports it.
`pl-vendor add --patch PATH` records an explicit patch, which must already exist.

Both settings are repository-relative POSIX paths and may not be absolute or contain `..`.
They may not overlap `.git`, `pyproject.toml`, the lockfiles, or any vendored package path, and no two packages may share a patch file, even one spelled with different case, or nest one package's patch path inside another's.
A patch file or directory that is a symlink must resolve inside the repository.
Schema-1 and schema-2 manifests keep using `.vendor-patches` unchanged; run `pl-vendor init` to migrate to schema 3 before setting either field.

Patch paths are relative to the installed vendored tree: stripped paths for a legacy `subdirectory` package and repository-relative paths for a `sources` package.

#### Capturing local edits

Edit the vendored tree directly, then save the result as the package's patch:

```sh
pl-vendor patch create example
pl-vendor check installed example
```

`patch create` diffs the unpatched upstream tree at the locked revision, after source selection and filtering, against the whole installed tree.
An existing patch and any later edits therefore become one consolidated patch; old hunks are never appended or stacked, and running the command again without new edits writes nothing.
Additions, deletions, binary files, the executable bit, and symlinks are captured; ignored, untracked files are left out, just as `check` ignores them.
The patch is written to the explicit `patch` path, or otherwise to `<patches-directory>/<package-name>.patch`, creating parent directories as needed.

Before replacing the patch file, the new patch is applied to a fresh copy of the upstream tree and must reproduce the installed tree exactly, so `check installed` passes afterwards.
The installed tree, `pyproject.toml`, and the lockfile are never modified, and any failure leaves the previous patch in place.
Re-running `patch create` while its own output is still uncommitted just updates that patch, but a patch file with uncommitted edits it did not write, such as hand edits, is not overwritten unless you pass `--force`.

When the installed tree has no local changes, an implicit patch is deleted and nothing is written in its place.
An explicit `patch` cannot be left empty, so the command stops and asks you to remove the `patch` setting and the file.
Trees containing nested `.git` metadata, named pipes, or other special files cannot be captured.

#### Updating patched packages

`pl-vendor update` first applies the saved patch to the new upstream tree as usual.
When the patch still applies, it is used unchanged and the patch file is not rewritten.

When it no longer applies, `update` three-way merges the old locked upstream tree, that tree with the patch applied, and the new upstream tree, all after source selection and filtering.
If upstream and downstream changed different lines, the merge succeeds: the patch is regenerated as one consolidated patch against the new revision, must replay onto it exactly, and is written together with the new vendored tree and lockfile in the same rollback-protected transaction.
The install summary marks the package `(patch rebased: PATH)`.
If upstream already contains every downstream change, an implicit patch is deleted; an explicit `patch` cannot be left empty, so `update` stops and asks you to remove the setting.

When both sides changed the same thing, `update` lists each conflicting path with its reason and the old and new revisions, and changes nothing.
A patch that pl-vendor wrote and you have not committed yet is rebased without `--force`. `--force` only permits overwriting local edits and uncommitted patch edits that pl-vendor did not write, such as hand edits; it never chooses a side of a conflict.
Either keep the package at its locked revision or resolve the conflict as described below.
These cases are reported as conflicts rather than merged:

- both sides edited the same or adjacent lines of a text file;
- one side deleted a file that the other changed, including a downstream rename of a file that upstream edited, since patches record renames as a deletion and an addition;
- both sides added the same path with different contents, or a file and a directory collide;
- both sides changed a symlink, changed a file into a symlink or back, or changed the same binary file.

A change to the executable bit on one side merges with content changes on the other.

#### Recovering from a patch conflict

To move a package with a conflicting patch to the new upstream revision, install that revision unpatched, reapply the old patch by hand, and capture the result.
For a package `example` vendored at `vendor/example` with an implicit patch:

```sh
cp .vendor-patches/example.patch example.patch.orig  # 1. keep the old patch
git rm .vendor-patches/example.patch                  # 2. leave the package unpatched
pl-vendor update example --force                      # 3. install the new revision
git apply --reject --directory=vendor/example example.patch.orig  # 4. reapply
# ...resolve each vendor/example/**/*.rej by hand, then delete it
pl-vendor patch create example                        # 5. capture the result
pl-vendor check installed example
rm example.patch.orig                                 # 6. clean up and commit
git add -A && git commit
```

For an explicit `patch`, copy that file in step 1, and in step 2 comment out the package's `patch = ...` line instead of removing the file.
Uncomment it again before step 5, which then rewrites the explicit file.
This works as long as no `<patches-directory>/example.patch` exists, because otherwise that file would apply once the setting is commented out.

Step 3 needs `--force` because, once the patch is gone, the installed tree no longer matches the unpatched locked revision.
That is safe here because step 1 saved the downstream changes.
In step 4, `git apply` applies every hunk it can and writes each one it cannot to a `.rej` file next to its target.
Step 5 consolidates everything into one patch against the new revision, and `check installed` confirms that it replays exactly.
The lockfile, vendored tree, and patch then change together in one commit.

#### Discarding local edits

To throw away edits to a vendored tree and keep the saved patch, run:

```sh
pl-vendor revert example
```

`revert` restores the locked upstream revision with the configured downstream patch applied, exactly as `pl-vendor sync example --force` would.
It rebuilds and verifies every selected package before changing anything, then prints the files it will revert, delete, or restore, including ignored, untracked files such as `__pycache__/` that are removed with the tree.
In a terminal it asks for confirmation; elsewhere, pass `--yes`, or the command stops without changing anything.
With no package names, every package is reverted, as with `sync`.
Several packages are reverted together as one rollback-protected transaction, and a tree that already matches is left alone without a prompt.
Patch files, `pyproject.toml`, and the lockfile are never modified.

## Commands

```sh
pl-vendor help
pl-vendor --version
pl-vendor list
pl-vendor list example first
pl-vendor init
pl-vendor init --check
pl-vendor update
pl-vendor update example second
pl-vendor update example second --force
pl-vendor add example https://github.com/example/example.git --branch release
pl-vendor remove example
pl-vendor remove first second
pl-vendor remove example --force
pl-vendor lock
pl-vendor lock --check
pl-vendor lock --upgrade
pl-vendor lock --upgrade-package example --upgrade-package second
pl-vendor sync
pl-vendor sync example second
pl-vendor sync example second --force
pl-vendor patch create example
pl-vendor patch create example --force
pl-vendor revert
pl-vendor revert example second --yes
pl-vendor check installed
pl-vendor check installed example second
pl-vendor check branches
pl-vendor check upstream content
pl-vendor check upstream revision example
pl-vendor check latest content
pl-vendor check latest revision
pl-vendor check prairielearn python
pl-vendor release build
pl-vendor release build --config releases/element.toml --push
pl-vendor release build --dry-run
```

`pl-vendor --version` prints the installed tool version.
`pl-vendor list [PACKAGE ...]` prints deterministic package names, full locked revisions, and paths in manifest or requested order, followed by the locked version in parentheses for version-range packages.

`add`, `remove`, `update`, and `sync` resolve, fetch, patch, and validate every selected package before changing the repository.
They then replace all trees and metadata as one rollback-protected transaction, including interruption and final-validation rollback.
Remove accepts one or more package names and deletes their complete managed trees only when they exactly match their locked, patched contents.
Pass `--force` to remove trees with local changes or missing content.
Removing the final package removes the `packages` table and lockfile while preserving the base `[tool.pl-vendor]` configuration and the rest of `pyproject.toml`; downstream patches are retained.

`pl-vendor lock` fills missing lock entries while preferring existing revisions.
Pass `--upgrade` or repeat `--upgrade-package NAME` to explicitly advance branch and version-range packages; commit-pinned packages never advance.
A commit pin is authoritative, so `lock` reconciles a stale lock entry to the manifest without contacting the remote.
Likewise, `lock` re-resolves a version-range package only when its locked version no longer satisfies the manifest specifier.
This does not modify vendored files.

`pl-vendor update` advances and syncs zero or more named packages in one step; no names means all.
`sync` accepts the same multi-package selection.
A package literally named `all` is selectable; the old `all` spelling means every package only when no such package exists.
For a pinned package, it installs the manifest commit and updates the lockfile to that exact value without rewriting the manifest.
If the manifest exists but the lockfile does not, `update` bootstraps a complete lockfile from the manifest instead of requiring a separate `lock` step.
Because the lockfile is generated state, `update` also replaces it when it has uncommitted changes; uncommitted `pyproject.toml` changes remain protected.

When a lockfile exists, `update` and `sync` compare each selected vendored tree with its locked, patched upstream contents and refuse to overwrite any differences.
This protects edits whether or not they are committed to the consuming repository.
Pass `--force` to replace those edits explicitly.
Destinations that already exactly match the requested upstream trees are left untouched; when neither the selected trees nor the lockfile changes, the command reports `No changes were made.`

`pl-vendor sync` does not update the lockfile: it replaces the selected vendored trees with their exact locked revisions and reapplies downstream patches.
Untracked extras are retained for comparison only when Git marks them ignored and they are absent upstream.
Tracked extras and upstream-required or tracked files are always compared, even when an ignore rule matches.
The same Git-aware comparison drives checks, overwrite protection, and no-op detection.
Replacing other local differences requires `--force`.

During `update`, `sync`, and `check`, entries from the same source share a temporary blobless Git object store.
Each revision is checked out once, using a sparse worktree containing only the configured upstream source paths unless an entry requests the repository root.
This avoids fetching unrelated file contents and reuses unchanged objects when entries select different commits from the same source.
Branch lookups are also batched per source URL.

Fetches from different sources run concurrently, with a progress bar for each source revision in an interactive terminal.
Revisions from one source run in sequence so they can safely share their Git object store.
The concurrency limit defaults to 50, matching uv's download limit.
Set `concurrent-downloads` under `[tool.pl-vendor]` in the consuming project's `pyproject.toml` to change it.

Like uv, pl-vendor writes human-readable operation status to standard error and keeps standard output empty.
In an interactive terminal, `add`, `update`, `sync`, and `check` show transient fetch and install progress.
Redirected output retains only deterministic phase summaries and package outcomes:

```text
Fetched 2 source revisions in 1.24s
Installed 1 package in 84ms
 + first abcdef12 -> vendor/first
 ~ second 12345678 -> vendor/second (patched)
 ~ third 87654321 -> vendor/third
```

Fetch counts represent unique source URL and revision pairs, while install counts represent destinations that were created or updated.
When a check needs both locked and branch-head contents, it includes them in one fetch phase and summary.
Every selected package gets an outcome record: `+` marks an update, `~` marks an unchanged destination, and `-` marks an error.
Revisions are shortened to eight characters for display; the lockfile remains authoritative for complete revisions.

The vendored-package `pl-vendor check` modes verify that each selected installed tree
exactly matches its locked, selected, and patched upstream contents. Package names
follow the complete check mode; with no package names, they check all packages.
`check installed` performs only this reproducibility check, while `check branches`
also rejects commit pins. Version-range packages count as tracking packages.

`check upstream content` additionally compares each branch package's locked contents
with its branch head, and each version-range package's locked contents with its
highest matching tag, while allowing pins. An upstream commit that changes only files
outside the configured selection does not fail this check. `check upstream revision`
instead requires the locked commit SHA to equal the branch head or matching tag. The corresponding
`check latest content` and `check latest revision` modes perform the same comparisons
but reject pinned packages. Checks never modify installed files or the lockfile.

`check prairielearn python` is a separate compatibility-only check. It resolves
the latest commit on the configured PrairieLearn branch, sparsely fetches its root
`pyproject.toml`, and checks the consuming project's root `[project].dependencies`
against PrairieLearn's exact direct dependency pins. A local requirement passes
when PrairieLearn declares the same normalized package name and its pinned version
satisfies the local version constraint. A `prairielearn` requirement, such as a
course library's dependency on PrairieLearn's Python package, is accepted as
PrairieLearn itself. Missing packages and incompatible versions are reported
together and produce exit status 1.

This check ignores local optional dependencies and dependency groups. Environment
markers, extras, and direct URL requirements are rejected because their runtime
support cannot be established from PrairieLearn's direct pins alone. The command
does not read `plvendor.lock` or legacy `plvendor-lock.yaml`, nor does it inspect
installed vendored trees; use
`check installed` for that purpose. It requires an initialized `[tool.pl-vendor]`
configuration, but it does not require any configured vendored packages.

Bare `check` is a usage error. Use `check installed`, `check upstream content`,
`check upstream revision`, `check latest content`, or `check latest revision` to
select the intended policy explicitly.

Repository commands discover the consuming repository from the current directory.
Common `--root`, `--no-progress`, and `--color` options are accepted before the command or after its complete leaf command, such as `--root PATH sync` or
`check latest content --root PATH`.
Pass `--no-progress` to suppress transient animation without hiding the durable summaries.
`--color auto|always|never` controls styling; `auto` is the default and also honors standard terminal color environment variables such as `NO_COLOR`.

The canonical add form is `add NAME URL [DESTINATION] --branch BRANCH`.
Run `add` without package arguments to enter the interactive workflow.

## Release branches

`pl-vendor release build` rebuilds a distributable branch, such as `release`, from
paths and vendored packages committed to a source branch, such as `main`. CI stays
owned by the consuming repository: a workflow checks out the repository and runs the
command. pl-vendor never generates workflows, syncs or fetches packages, or pushes
unless asked.

```sh
pl-vendor release build                                   # reads release.toml
pl-vendor release build --config releases/element.toml    # another output branch
pl-vendor release build --dry-run                         # validate and preview only
pl-vendor release build --push                            # also force-with-lease push
```

Every input comes from the committed source branch: the release config, the
`pyproject.toml` manifest and lockfile used by package includes, and every included
file. The release tree is assembled from Git objects in a temporary index, so working
tree content never reaches the release branch.

```toml
version = 2
source_branch = "main"      # default
target_branch = "release"   # default
history_source = "elements/pl-example"

[[include]]
source = "elements/pl-example"
destination = "."

[[include]]
source = ".agents"
destination = ".agents"

[[include]]
package = "shared-utils"
destination = "vendor/shared-utils"
```

- Each `[[include]]` sets `destination` and exactly one of `source` (a path in the
  source branch) or `package` (a name from `[tool.pl-vendor.packages]`). A directory
  maps its contents recursively, `destination = "."` places them at the release root,
  and a file destination includes the filename.
- A `package` include exports the package's committed installed tree, including
  applied downstream patches. The package must be declared in the committed manifest,
  have a matching lock record, and be installed at its path. The build does not
  verify installed contents against upstream; run `pl-vendor check installed` first,
  as CI does in the example below.
- `history_source` sets the release branch's ancestry independently of its contents:
  each release commit's parent is `git subtree split --prefix=<history_source>`.
- Version-1 configs, which have only `history_source` and `source` includes and build
  `main` into `release`, keep working unchanged. `source_branch`, `target_branch`, and
  `package` require `version = 2`.

Before changing any ref, the build rejects missing sources, invalid configuration,
unsafe or unnormalized paths, `.git` destinations, submodules, symlinks that point
outside the release tree, and destinations that duplicate, differ only by case from,
or nest inside another output file. An ordinary `source` include may contain a
vendored package directory; an explicit `package` include that overlaps another
include fails instead of overwriting it.

The build also refuses uncommitted or untracked changes to release inputs (the config,
`history_source`, included sources, package paths, and, for package includes,
`pyproject.toml` and the lockfile). Changes elsewhere are ignored. `--allow-dirty`
builds anyway, still from committed content only, and lists the changes it ignored.

If the existing target commit, local or `refs/remotes/<remote>/<target>`, already has
the generated tree, it is reused, so repeated builds are no-ops. Otherwise the
release commit takes its author, committer, and dates from the source commit, so the
same source always yields the same commit. The local branch is updated with a
compare-and-swap, and the build refuses to run while the target branch is checked out
in any worktree. `--dry-run` reports the outcome and tree without changing refs. It
may still write unreferenced Git objects.

`--push` pushes the exact release commit to `--remote` (default `origin`) with
`--force-with-lease`, expecting the remote branch to equal its remote-tracking ref, or
to be absent when no such ref exists. Fetch the target branch first so the lease is
current. A rejected push fails the command.

See the [migration guide](docs/migrations.md#replace-release-scripts-with-release-build)
to replace hand-written release scripts.

## Python API

The supported facade exports `VendorProject`, immutable `VendorPackage`,
`OperationResult`, `PackageResult`, `CheckResult`, `VendorError`, and `__version__`.
`VendorProject.revert(*names, confirm=None)` writes its preview to `output` and calls
`confirm()` before changing files; return `False` to cancel.
Pass a `VendorPackage` to `VendorProject.add()`; set `VendorPackage.version` to a
PEP 440 specifier for a version-range package. `PackageResult.version` reports the
locked version for such packages and is `None` otherwise.
Library operations return structured results and default to silent in-memory output;
the CLI owns terminal rendering and progress.
Failed checks return their primary diagnostics in `CheckResult.issues` in reporting
order; repair guidance and Git diff bodies remain presentation output rather than
structured issues.
Raw TOML/lock mutation, Git backends, reporting helpers, and PrairieLearn constants are internal implementation details.

## Development

This repository uses uv 0.9 or newer to manage its development environment:

```sh
uv sync --dev
make test
make format
make build
```

The test suite is kept in the top-level `tests` directory so it is separate from the installable `pl_vendor` package.
Tests that drive real Git use only local repositories and need no network access; `tests/test_patch_workflow.py` covers downstream patch management end to end.

To run a disposable walkthrough of the main workflow, including capturing, reverting, and rebasing a downstream patch, backed by local Git repositories:

```sh
./scripts/demo.sh
```

Pass `--keep` to retain the generated upstream and consumer repositories for inspection.

To publish a release, start from a clean working tree and run:

```sh
make publish-version VERSION=0.5.1
```

This runs the test suite and static checks, updates `pyproject.toml` and `uv.lock`, builds the distributions, creates a release commit and annotated `v0.5.1` tag, and atomically pushes both to `origin`.
The tag starts the PyPI publishing workflow on GitHub-hosted runners.
Use `REMOTE=name` to publish through a different Git remote.
The requested version must be semantically newer than the current package version and all local or remote release tags.

If `pyproject.toml` and `uv.lock` already contain the target version in an unpushed commit, publish that commit without creating a redundant release commit:

```sh
make publish-version VERSION=0.5.1 ALLOW_PREBUMPED_VERSION=1
```

This bypass requires the requested version to equal the current project version, the release tag to be absent locally and remotely, and the local branch to be ahead of its remote branch.

To publish an existing tag whose earlier workflow did not upload to PyPI, first make sure this workflow is present on `main`, then run:

```sh
gh workflow run publish.yml --ref main -f release_tag=v0.5.1
```

The manually supplied tag must match both the release-tag syntax and the version in that tag's `pyproject.toml`.
