Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [Unreleased]

### Added

- Import repositories containing Git submodules with their pins and original
`.gitmodules` file. Submodules remain uninitialized.

## [0.1.1] - 2026-11-17

### Fixed
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ git write-tree

Hooks are trusted programs and may change resources outside the repository. `check` and refresh verification replay them. When a hook returns a commit, make its creation deterministic: refresh checks that imported parent IDs match, so changing commit metadata between runs can make verification fail.

When both `ref` and `commit` are set, the commit must be reachable from the ref. A shallow depth can prevent this verification. Pin `commit` for reproducible results; otherwise the selected ref or default HEAD can move. Git shallow boundaries apply to the entire workspace object database, so overlapping source histories are rejected when either import is shallow. Clone imports refuse to overwrite existing files or index entries, and sources containing Git submodules are not supported. Paths must be workspace-relative and may not traverse symlinks or Git metadata.
When both `ref` and `commit` are set, the commit must be reachable from the ref. A shallow depth can prevent this verification. Pin `commit` for reproducible results; otherwise the selected ref or default HEAD can move. Git shallow boundaries apply to the entire workspace object database, so overlapping source histories are rejected when either import is shallow. Clone imports refuse conflicting files or index entries. Paths must be workspace-relative and may not traverse symlinks or Git metadata.

A clone preserves each submodule's pinned commit and leaves its directory empty.
Layerctl copies `.gitmodules` verbatim to the clone destination.
For nested imports or relative submodule URLs, initializing the submodules with
Git may require manual configuration in the projection.
Editing submodules through layerctl is unsupported.

```sh
layerctl graph # Graphviz DOT on stdout
Expand Down
22 changes: 16 additions & 6 deletions internal/operations/git_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func GitClone(ctx context.Context, workspace string, req GitCloneRequest) (resul
if want == "" {
want = "HEAD"
}
fetchArgs := []string{"-c", "protocol.file.allow=always", "fetch", "--no-tags"}
fetchArgs := []string{"-c", "protocol.file.allow=always", "fetch", "--no-recurse-submodules", "--no-tags"}
if req.Depth > 0 {
fetchArgs = append(fetchArgs, fmt.Sprintf("--depth=%d", req.Depth))
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func GitClone(ctx context.Context, workspace string, req GitCloneRequest) (resul
resultErr = errors.Join(resultErr, fmt.Errorf("restore workspace shallow state: %w", err))
}
}()
fetchObjects := []string{"-c", "protocol.file.allow=always", "fetch", "--update-shallow", "--no-tags", "--no-write-fetch-head", "--", source, commit}
fetchObjects := []string{"-c", "protocol.file.allow=always", "fetch", "--no-recurse-submodules", "--update-shallow", "--no-tags", "--no-write-fetch-head", "--", source, commit}
if req.PreCheckoutPatch != "" || req.PreCheckoutScript != "" {
fetchObjects = append(fetchObjects, "refs/layerctl/prepared")
}
Expand Down Expand Up @@ -307,6 +307,14 @@ func GitClone(ctx context.Context, workspace string, req GitCloneRequest) (resul
rollback()
return "", err
}
if e.mode == "160000" {
if err := root.Mkdir(path, 0755); err != nil {
rollback()
return "", fmt.Errorf("create submodule directory %q: %w", path, err)
}
createdDirs = append(createdDirs, path)
continue
}
sourcePath := filepath.Join(staged, filepath.FromSlash(e.path))
if e.mode == "120000" {
target, err := os.Readlink(sourcePath)
Expand Down Expand Up @@ -517,6 +525,9 @@ func stageBlobs(ctx context.Context, source, staged string, entries []entry) err
}()
reader := bufio.NewReader(stdout)
for _, e := range entries {
if e.mode == "160000" {
continue
}
if _, err := io.WriteString(stdin, e.object+"\n"); err != nil {
return fmt.Errorf("request source blob %q: %w", e.path, err)
}
Expand Down Expand Up @@ -654,10 +665,9 @@ func parseTree(tree []byte) ([]entry, error) {
return nil, fmt.Errorf("invalid source tree entry %q", path)
}
mode, kind := fields[0], fields[1]
if mode == "160000" || kind == "commit" {
return nil, fmt.Errorf("git-clone source contains submodule %q", path)
}
if kind != "blob" || (mode != "100644" && mode != "100755" && mode != "120000") {
gitlink := mode == "160000" && kind == "commit"
blob := kind == "blob" && (mode == "100644" || mode == "100755" || mode == "120000")
if !gitlink && !blob {
return nil, fmt.Errorf("unsupported source tree entry %q", path)
}
entries = append(entries, entry{path: path, mode: mode, object: fields[2]})
Expand Down
83 changes: 72 additions & 11 deletions internal/operations/git_clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package operations_test

import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/NonLogicalDev/util.layerctl/internal/cli"
"github.com/NonLogicalDev/util.layerctl/internal/operations"
)

Expand Down Expand Up @@ -193,18 +196,76 @@ func TestImportDoesNotReplaceDeletedButStagedFile(t *testing.T) {
}
}

func TestImportRejectsSubmoduleBeforeWriting(t *testing.T) {
src := repo(t)
write(t, src, "regular.txt", "regular", 0644)
head := commit(t, src, "base")
git(t, src, "update-index", "--add", "--cacheinfo", "160000,"+head+",external")
git(t, src, "commit", "-qm", "add gitlink")
work := repo(t)
if _, err := operations.GitClone(context.Background(), work, operations.GitCloneRequest{URL: src, Dir: "copy"}); err == nil || !strings.Contains(err.Error(), "submodule") {
t.Fatalf("submodule accepted: %v", err)
func TestProjectionPreservesUninitializedSubmodules(t *testing.T) {
child := repo(t)
write(t, child, "library", "child contents", 0644)
pin := commit(t, child, "library")
source := repo(t)
relativeURL, err := filepath.Rel(source, child)
if err != nil {
t.Fatal(err)
}
git(t, source, "-c", "protocol.file.allow=always", "submodule", "add", "--name", "library", filepath.ToSlash(relativeURL), "deps/lib")
write(t, source, "sibling", "before\n", 0644)
commit(t, source, "superproject")
// The child must only be needed when Git is asked to initialize it.
offline := filepath.Join(t.TempDir(), "offline")
if err := os.Rename(child, offline); err != nil {
t.Fatal(err)
}

defs := repo(t)
t.Chdir(defs)
t.Setenv("GIT_AUTHOR_NAME", "Fixture")
t.Setenv("GIT_AUTHOR_EMAIL", "fixture@example.invalid")
t.Setenv("GIT_COMMITTER_NAME", "Fixture")
t.Setenv("GIT_COMMITTER_EMAIL", "fixture@example.invalid")
write(t, defs, "layers/0001-import/layer.toml", fmt.Sprintf(`[vcs]
commit-msg = "Import sources"
[[operation]]
[operation.git-clone]
url = %q
[[operation]]
[operation.git-clone]
url = %q
dir = "vendor/source"
`, source, source), 0644)
write(t, defs, "layers/0002-edit/layer.toml", `[vcs]
commit-msg = "Edit a sibling"
[[operation]]
[operation.patch]
file = "sibling.patch"
`, 0644)
write(t, defs, "layers/0002-edit/sibling.patch", "diff --git a/sibling b/sibling\n--- a/sibling\n+++ b/sibling\n@@ -1 +1 @@\n-before\n+after\n", 0644)

workspace := filepath.Join(t.TempDir(), "projection")
if err := cli.Run(t.Context(), []string{"projection", "create", "demo", "--worktree", workspace}, io.Discard, io.Discard); err != nil {
t.Fatal(err)
}
if err := cli.Run(t.Context(), []string{"check"}, io.Discard, io.Discard); err != nil {
t.Fatal(err)
}
if got := git(t, workspace, "show", "HEAD:sibling"); got != "after" {
t.Fatalf("patched sibling = %q", got)
}
paths := []string{"deps/lib", "vendor/source/deps/lib"}
for _, path := range paths {
if got := git(t, workspace, "ls-tree", "HEAD", path); got != "160000 commit "+pin+"\t"+path {
t.Fatalf("unexpected gitlink: %s", got)
}
entries, err := os.ReadDir(filepath.Join(workspace, path))
if err != nil || len(entries) != 0 {
t.Fatalf("uninitialized %s: %v, %v", path, entries, err)
}
}
wantConfig := git(t, source, "rev-parse", "HEAD:.gitmodules")
for _, path := range []string{".gitmodules", "vendor/source/.gitmodules"} {
if got := git(t, workspace, "rev-parse", "HEAD:"+path); got != wantConfig {
t.Fatalf("%s blob = %s, want %s", path, got, wantConfig)
}
}
if _, err := os.Stat(filepath.Join(work, "copy")); !os.IsNotExist(err) {
t.Fatalf("failed clone created files: %v", err)
if got := git(t, workspace, "status", "--porcelain"); got != "" {
t.Fatalf("dirty projection: %s", got)
}
}

Expand Down
6 changes: 5 additions & 1 deletion skills/layerctl/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ file = "scripts/configure.sh"
args = ["example"]
```

Replace the example URL and commit with an accessible source and revision. `ref`, `commit`, `depth`, `depends-on`, and co-authors are optional. Paths for patches, overlays, and scripts are relative to the layer directory. Clone destinations are relative to the workspace; omitting `dir` or setting it to an empty string means its root. A clone does not overwrite existing files. Imported source commits become additional parents of the layer commit; the previous workspace commit is the first parent when present. `ref` and `commit` together require the commit to be reachable from the ref. Pin commits for reproducibility; a depth limit can prevent ancestry verification. Overlapping source histories are rejected when either is shallow, and submodules are not supported.
Replace the example URL and commit with an accessible source and revision. `ref`, `commit`, `depth`, `depends-on`, and co-authors are optional. Paths for patches, overlays, and scripts are relative to the layer directory. Clone destinations are relative to the workspace; omitting `dir` or setting it to an empty string means its root. A clone does not overwrite existing files. Imported source commits become additional parents of the layer commit; the previous workspace commit is the first parent when present. `ref` and `commit` together require the commit to be reachable from the ref. Pin commits for reproducibility; a depth limit can prevent ancestry verification. Overlapping source histories are rejected when either is shallow.

A clone preserves submodule pins and leaves the submodules uninitialized.
Layerctl copies `.gitmodules` verbatim to the clone destination.
Layers can modify sibling files. Editing submodules through layerctl is unsupported.

Layers are enabled by default. Disabled layers are skipped; an enabled layer's dependencies must exist earlier in the numbered order and be enabled. When `depends-on` is omitted, the previous enabled layer is the default dependency. An explicit list replaces that default; `depends-on = []` declares no dependencies. Layers still execute in numbered order. `vcs.co-authors` adds standard `Co-authored-by` trailers. Without co-authors, the exact commit message is retained.

Expand Down
Loading