From b12cb2581c7965ab5c06a26bc7893e1338828014 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Tue, 17 Nov 2026 11:52:21 -0800 Subject: [PATCH] git-clone: Import repositories with uninitialized submodules `git-clone` previously rejected a source tree containing a submodule, so even layers that only touched sibling files could not use it. The projection now records each submodule at the commit pinned by the source and leaves its working directory empty. Layers can patch sibling files while the submodule repository is offline. Layerctl does not fetch or check out submodule contents. Editing a submodule or updating its pin through layerctl is unsupported. The import copies `.gitmodules` verbatim to the clone destination. To initialize a submodule later with Git, nested imports may need a path entry in the projection's root `.gitmodules`, and relative URLs may need adjustment for the projection's remote. --- CHANGELOG.md | 5 ++ README.md | 8 ++- internal/operations/git_clone.go | 22 +++++-- internal/operations/git_clone_test.go | 83 +++++++++++++++++++++++---- skills/layerctl/SKILL.md | 6 +- 5 files changed, 105 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a57efe..954ef5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 53dc19f..853e84a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/internal/operations/git_clone.go b/internal/operations/git_clone.go index bca321f..3ec1793 100644 --- a/internal/operations/git_clone.go +++ b/internal/operations/git_clone.go @@ -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)) } @@ -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") } @@ -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) @@ -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) } @@ -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]}) diff --git a/internal/operations/git_clone_test.go b/internal/operations/git_clone_test.go index f7710fd..d19da2d 100644 --- a/internal/operations/git_clone_test.go +++ b/internal/operations/git_clone_test.go @@ -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" ) @@ -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) } } diff --git a/skills/layerctl/SKILL.md b/skills/layerctl/SKILL.md index 7ead2cf..eba6799 100644 --- a/skills/layerctl/SKILL.md +++ b/skills/layerctl/SKILL.md @@ -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.