Skip to content
bracesdevPublic

About

Compose Git repositories and changes as ordered layers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

layerctl

Layerctl helps you maintain declaratively composed Git repositories. It’s designed to keep a patched fork current as its upstream project evolves.

Pin an upstream revision and keep your changes in named layers. To try a new upstream release, update the pin and replay the stack to see which layers still apply. Each enabled layer produces its own commit, so you can review changes individually, submit them upstream, and drop the layers upstream has incorporated.

Layers can import Git sources, apply patches, add files, and run scripts. Layerctl combines them into a Git workspace called a projection, retaining the imported Git history. You can inspect a projection with ordinary Git commands and capture committed edits back into your layer definitions.

Layer definitions

A project contains one layer.toml per numbered directory, such as layers/0001-source/layer.toml. Layerctl runs layers in directory order and operations in the order listed in each manifest. Each enabled layer creates one Git commit.

  • Enablement — Layers are enabled by default. Set enabled = false in [layer] to skip a layer.
  • Dependencies — By default a layer depends on the previous enabled layer. Set depends-on = ["0001-source"] in [layer] to replace that default with earlier enabled layers, or use depends-on = [] to declare no dependencies. Dependencies leave the numbered execution order in place.
  • Commit messages — Set commit-msg in [vcs] to nonempty UTF-8 without NUL. Layerctl keeps the exact bytes, including trailing newlines. Add co-authors = ["Example Contributor <contributor@example.com>"] to append standard Co-authored-by trailers.
Operation Fields What it does
git-clone url, optional dir, ref, commit, depth Import a Git source into the workspace, keeping its ancestry.
patch file Apply a patch to the workspace.
overlay directory Add files from a directory. Existing paths cause a conflict.
script file, optional args Execute a program with the workspace as its working directory.

Declare each operation with a [[operation]] entry and its corresponding table, such as [operation.patch]. Patch files, overlay directories, and script paths are relative to the layer directory. See the authoring walkthrough for how to build and edit a stack.

This layer.toml imports two sources, applies a patch to the combined workspace, and runs a configuration script. Replace the example URLs and provide the patch and script at the paths shown.

[layer]
enabled = true
# depends-on = ["0001-base"] # optional; each dependency must be earlier and enabled

[vcs]
commit-msg = """
Compose tools
"""
co-authors = ["Example Contributor <contributor@example.com>"] # optional

[[operation]]
[operation.git-clone]
url = "https://example.com/project.git"
ref = "refs/heads/main" # optional branch, tag, or other ref
# commit = "0123456789abcdef0123456789abcdef01234567" # optional exact commit
# depth = 20 # optional positive history depth

[[operation]]
[operation.git-clone]
url = "https://example.com/other.git"
dir = "vendor/other"
ref = "refs/tags/v1.0.0"

[[operation]]
[operation.patch]
file = "changes.patch"

[[operation]]
[operation.script]
file = "scripts/configure.sh"
args = ["example"]

The compression manifest combines two pinned sources, a patch, an overlay, and a script in one layer. Its inputs are included in the compression example.

Git sources

  • Destination — Omit dir or set it to "" to import at the workspace root. Use a workspace-relative path such as dir = "vendor/other" for another source. Layerctl rejects paths through symlinks or Git metadata and refuses conflicting files or index entries.
  • Revision — Use ref for a branch, tag, or other Git ref. Pin an exact commit for repeatable builds. When both are set, Git must be able to prove the commit is reachable from the ref.
  • History — Set a positive depth for a shallow import. This can prevent reachability checks. Shallow boundaries affect the entire workspace object database, so Layerctl rejects overlapping source histories if either import is shallow.
  • Ancestry — The previous workspace commit is the first parent when present. Imported source commits become additional parents. Layerctl imports the source tree and keeps its Git metadata out of the workspace files.
  • Submodules — Layerctl preserves submodule pins, leaves their directories empty, and copies .gitmodules verbatim. Nested imports and relative URLs may require manual Git configuration to initialize submodules. Manage the submodules themselves with Git.

Scripts and pre-checkout hooks

Both script types run from the composed workspace root. Their executable paths resolve from the layer directory. They can access resources outside the workspace, and check and refresh verification replay them. Run definitions whose scripts you trust.

  • operation.script — Runs the executable directly with its configured arguments. Success stages tracked changes and non-ignored new files. A nonzero exit stops the projection.
  • pre-checkout-patch on git-clone — Applies a layer-relative patch to the source in an isolated index after fetch and before checkout. Write patch paths relative to the source root, even for a nested import. This is useful for normalizing paths that collide on a case-insensitive filesystem.
  • pre-checkout-script on git-clone — Runs after the optional patch, with Git pointed at an isolated bare source repository and a private index. Print one full commit or tree SHA on stdout and send diagnostics to stderr.
    • A commit supplies both the materialized tree and the imported parent. Return deterministic commit IDs when refreshing so the imported parents match on replay.
    • A tree supplies the materialized tree and keeps the fetched commit as the imported parent.
Scope Environment variables
Both script types LAYERCTL_WORKTREE_DIR, LAYERCTL_DEFINITION_DIR, LAYERCTL_LAYER_DIR, LAYERCTL_LAYER_ID, LAYERCTL_OPERATION_INDEX (one-based)
Pre-checkout bare repository GIT_DIR, LAYERCTL_GIT_DIR, LAYERCTL_GITCLONE_BARE_DIR
Pre-checkout private index GIT_INDEX_FILE, LAYERCTL_GITCLONE_INDEX_FILE
Pre-checkout input LAYERCTL_SOURCE_COMMIT, LAYERCTL_SOURCE_TREE

For example, a pre-checkout script can add a file to the prepared index and return the resulting tree.

#!/bin/sh
set -eu
blob=$(printf 'enabled\n' | git hash-object -w --stdin)
git update-index --add --cacheinfo "100644,$blob,prepared.txt"
git write-tree

Try it

Install layerctl and make sure Git has a name and email configured. From this repository’s root, build the basic example. It imports a tagged Layerctl release and adds a file in a second layer.

layers=examples/basic/layers
workspace=$(layerctl --layers-dir "$layers" projection create demo)
git -C "$workspace" log --oneline -2 --first-parent
cat "$workspace/DEMO-NOTE.md"
layerctl --layers-dir "$layers" graph --format mermaid

The log shows the two layer commits. The source layer’s parent is the original release commit. The Mermaid graph shows the note layer’s dependency.

  • Run layerctl --layers-dir "$layers" projection delete demo when you’re done with the workspace.
  • Browse the example stacks for the layer definitions, patches, overlays, and scripts.
  • The authoring walkthrough covers capturing and refreshing layers from a projection.

Run or install

Git is required at runtime. Check your installation with layerctl --version.

Prebuilt binaries

Download the archive for your platform and its checksums from GitHub Releases.

Go

Use Go 1.26 or later. Add $GOBIN to your PATH, or use $(go env GOPATH)/bin if GOBIN is unset.

go install github.com/bracesdev/layerctl/cmd/layerctl@v0.3.0
go run github.com/bracesdev/layerctl/cmd/layerctl@v0.3.0 --help

Replace @v0.3.0 with @main to use the default branch.

Nix flake

nix run github:bracesdev/layerctl/v0.3.0 -- --version
nix run github:bracesdev/layerctl/v0.3.0 -- graph --format mermaid
  • Omit /v0.3.0 to use the default branch. Run the graph command from a project containing layers.
  • From a checkout, use nix run . -- --version or nix build ..
  • The flake uses the systems exposed by nixpkgs-unstable, including Apple Silicon and x86-64 Linux. It exports a named packages.<system>.layerctl package and a default package.
  • Reuse the Nix package recipe with pkgs.callPackage. It uses buildGoModule and accepts a source override.

Nix without flakes

With nixpkgs in NIX_PATH, run nix-build from the checkout. The resulting binary is ./result/bin/layerctl.

Work with projections

Command Use it to
layerctl projection create NAME --worktree PATH Compose a workspace at a new path. Add --through LAYER to stop at a particular layer. Omit --worktree to use the project state directory.
layerctl projection list / layerctl projection path NAME List projections or locate one workspace.
layerctl check Replay layers in a temporary projection. A failed check can leave that projection available for inspection.
layerctl graph / layerctl graph --format mermaid Print Graphviz DOT or Mermaid. The graph includes disabled layers and shows effective dependencies.
layerctl layer add ID --from NAME Capture committed edits in a new layer. Start the projection through the preceding enabled layer.
layerctl layer refresh ID --from NAME Update an existing layer from committed edits. Start the projection through the layer you’re updating.
layerctl projection delete NAME Remove the owned workspace, including any uncaptured changes.
  • Refresh — The projection must be clean. Layerctl preserves the existing operations, appends a patch, and verifies the result by replaying the layer. Keep manifests intended for refresh comment-free.
  • Conflicts — Resolve the affected files in the reported workspace and run layerctl projection continue NAME to finish composing. Update the layer definition separately to keep the resolution for future builds.
  • Interrupted operations — If a process stops mid-operation, inspect the workspace, save any needed edits, then delete and recreate the projection.
  • Stale locks — Verify that no layerctl process is using the projection before running layerctl projection unlock NAME.

Project discovery and state

Layerctl looks upward from your working directory for the nearest directory containing layers/. Discovery crosses Git boundaries and also works outside Git. Use the global --layers-dir flag before the command to select a specific stack.

layerctl --layers-dir vendor/example-tool/layers check
  • Relative paths resolve from the current directory. Capture and refresh write back to the selected layers directory. Scripts receive its parent as LAYERCTL_DEFINITION_DIR.
  • Git projects — Layerctl stores state in the Git common directory. The root layers/ uses layerctl/compositions. Other layer directories get separate namespaces under layerctl/definitions.
  • Standalone projects — Layerctl uses an absolute $XDG_STATE_HOME/layerctl when set. The fallback is ~/.local/state/layerctl on Linux and the platform user configuration directory plus layerctl on macOS and Windows. The canonical layers path identifies the project.
  • Each stack has its own projection names, locks, and state. Graph inspection leaves state untouched.

Older definitions

  • Replace upstream.json with git-clone operations and convert each legacy layer to a layer.toml manifest. The current CLI expects manifest definitions and has no upstream or layer migrate commands.
  • An incomplete projection from an older version may need to be recreated if its recovery fingerprint changed. Old projection refs and worktrees remain until you remove them.

Development

Go 1.26 and Git are required. Run these commands from the repository root.

go test ./...
go install ./cmd/layerctl
  • cmd/layerctl/ contains the CLI entry point. internal/ holds definitions, operations, projections, layer capture, and graph rendering.
  • Tests live alongside their packages. Run nix flake check to build and test the Nix package.
  • To build release archives locally, run goreleaser release --snapshot --clean.

Releases

  • Add a dated version section to CHANGELOG.md, then push a semantic version tag such as v1.2.3. The GitHub Actions workflow runs tests and uses that section for the release notes.
  • GoReleaser publishes Linux and macOS archives for amd64 and arm64, with SHA-256 checksums.
  • layerctl --version reports the version and source commit. Local Go builds report 0.0.0-dev with the revision when available and -dirty for uncommitted changes. Versioned go install builds report the module version and may show the commit as unknown. Linker flags can override both values.

History

Layerctl was extracted from abg-OAI/codex at a780a43. The standalone project adds manifest-defined operations and multi-repository composition. The original source is copyright OpenAI and distributed under the Apache License 2.0.

About

Compose Git repositories and changes as ordered layers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages