diff --git a/README.md b/README.md
index 0b1ef26..f60b169 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,7 @@ fmt.Println(clone.Head(ctx, dst))
Pass `true` as the final argument for a full clone, including when an existing shallow checkout needs to be unshallowed. Clone and fetch errors are returned as `*clone.UnreachableError`, except when the context was canceled or reached its deadline. `errors.As` retrieves the URL and underlying Git error. `ValidateURL` accepts `https://` URLs, while `ValidateRef` rejects leading hyphens, `..`, and characters outside letters, digits, `.`, `_`, `/`, and `-`.
-Use `EnsureWithOptions` to include submodules. Submodules are initialized and updated recursively with depth 1 after both a new clone and an existing checkout update. This step is best-effort because submodules may be large or refer to unavailable URLs; failures leave the parent checkout usable. Context cancellation is still returned. Recursion is bounded: submodules are fetched one nesting level at a time, at most 8 levels deep and 256 submodules in total. When fetching a repository's submodules would exceed either limit, the submodule step stops before fetching them; submodules already fetched are kept and `Submodules` reports the rest as unavailable. Submodule URLs come from the cloned repository's `.gitmodules`, so submodules are fetched with the ambient credential helper disabled. Other ambient Git credentials are not disabled: an unscoped `http.extraHeader`, `GIT_ASKPASS` or `core.askPass` can still authenticate a repository-chosen submodule URL. Callers that fetch untrusted repositories with `RecurseSubmodules` should scope those credentials to trusted URLs or remove them.
+Use `EnsureWithOptions` to include submodules. Submodules are initialized and updated recursively with depth 1 after both a new clone and an existing checkout update. This step is best-effort because submodules may be large or refer to unavailable URLs; failures leave the parent checkout usable. Context cancellation is still returned. Recursion is bounded: submodules are fetched one nesting level at a time, at most 8 levels deep and 256 submodules in total. When fetching a repository's submodules would exceed either limit, the submodule step stops before fetching them; submodules already fetched are kept and `Submodules` reports the rest as unavailable. With `RecurseSubmodules`, updating an existing checkout first removes everything in it except `.git`, then resets and checks submodules out again from their repositories under `.git/modules`, fetching only commits those lack. No submodule keeps files from an earlier commit, including after a failed or canceled update, and any other files added to the working tree are discarded. A submodule that could not be fetched is left empty, as an uninitialized submodule appears. Submodule URLs come from the cloned repository's `.gitmodules`, so submodules are fetched with the ambient credential helper disabled. Other ambient Git credentials are not disabled: an unscoped `http.extraHeader`, `GIT_ASKPASS` or `core.askPass` can still authenticate a repository-chosen submodule URL. Callers that fetch untrusted repositories with `RecurseSubmodules` should scope those credentials to trusted URLs or remove them.
To fetch private submodules, list trusted URL prefixes in `SubmoduleCredentialURLs`. A submodule whose resolved URL is under one of them is fetched with the ambient credential helper; prefixes match on path segment boundaries, so `https://github.com/my-org` covers `https://github.com/my-org/lib.git` but not `https://github.com/my-org-two/lib.git`. Any repository can name a trusted URL in its `.gitmodules`, so every repository the helper can read under those prefixes may end up in a checkout of an untrusted repository. Prefixes must be `https://` with no userinfo, query, fragment or dot segments; `ValidateSubmoduleCredentialURLs` checks a list up front. The prefix is matched against the URL in `.gitmodules`, before Git applies `url..insteadOf`, so the caller's own rewrites of a trusted prefix also receive credentials.
diff --git a/ensure.go b/ensure.go
index c7d7ddb..e46379e 100644
--- a/ensure.go
+++ b/ensure.go
@@ -80,6 +80,11 @@ func ensure(ctx context.Context, retry Retry, url, dst, ref string, options Ensu
onIssue: options.OnSubmoduleIssue,
}
if _, err := os.Stat(filepath.Join(dst, ".git")); err == nil {
+ if options.RecurseSubmodules {
+ if err := clearWorktree(dst); err != nil {
+ return err
+ }
+ }
if err := fetchRef(ctx, retry, url, dst, ref, options.Full); err != nil {
return err
}
@@ -141,9 +146,16 @@ func updateSubmodules(ctx context.Context, retry Retry, dst string, enabled bool
// submodules per Git call. Each batch is counted before it is fetched, and the
// walk stops before exceeding the budget, keeping what was already fetched.
// Like git's own --recursive, it descends only into submodules checked out at
-// their gitlink. Only context errors are returned.
+// their gitlink. A submodule left away from its gitlink is emptied, since a
+// failed update can leave a partial checkout. Only context and removal errors
+// are returned.
func updateSubmodulesWithin(ctx context.Context, retry Retry, dst string, update submoduleUpdate) error {
policy := retry.Resolved()
+ root, err := os.OpenRoot(dst)
+ if err != nil {
+ return err
+ }
+ defer func() { _ = root.Close() }()
queue := []pendingSubmodules{{dir: dst, depth: 1}}
fetched := 0
for len(queue) > 0 {
@@ -184,6 +196,9 @@ func updateSubmodulesWithin(ctx context.Context, retry Retry, dst string, update
})
continue
}
+ if err := emptySubmodule(root, joinSlash(current.prefix, link.path)); err != nil {
+ return err
+ }
update.reportLinks(ctx, policy, current, []gitlink{link}, SubmoduleIssueFetchFailed, outputs[link.path])
}
}
@@ -210,6 +225,9 @@ func updateSubmoduleLevel(ctx context.Context, policy Retry, dir string, links [
return nil, ctxErr
}
}
+ if err := syncModuleRemotes(ctx, policy, dir, links); err != nil {
+ return nil, err
+ }
anonymous, authenticated := paths, []string(nil)
if len(trusted) > 0 {
diff --git a/ensure_test.go b/ensure_test.go
index fb97a40..6cb0723 100644
--- a/ensure_test.go
+++ b/ensure_test.go
@@ -3,11 +3,13 @@ package clone
import (
"context"
"errors"
+ "fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
+ "strconv"
"strings"
"sync/atomic"
"testing"
@@ -751,7 +753,7 @@ func assertCheckedOut(t *testing.T, dst string, want map[string]bool) {
t.Errorf("%s missing: %v", name, err)
}
if !present && !os.IsNotExist(err) {
- t.Errorf("%s exists beyond the submodule budget (err %v)", name, err)
+ t.Errorf("%s exists, want absent (err %v)", name, err)
}
}
}
@@ -928,3 +930,424 @@ func TestUpdateSubmodulesWithinSkipsCheckoutNotAtGitlink(t *testing.T) {
t.Error("stale checkout HEAD was never compared with its gitlink")
}
}
+
+// assertEmptyDir checks that dir exists and holds nothing, as Git leaves an
+// uninitialized submodule.
+func assertEmptyDir(t *testing.T, dir string) {
+ t.Helper()
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ t.Fatalf("read %s: %v", dir, err)
+ }
+ if len(entries) != 0 {
+ t.Errorf("%s holds %d entries, want none", dir, len(entries))
+ }
+}
+
+func TestCachePrepareClearsSubmoduleThatFailsToUpdate(t *testing.T) {
+ requireGit(t)
+
+ top := newTestRepository(t, "top.txt")
+ library := newTestRepository(t, "library.txt")
+ const (
+ topURL = "https://clone.test/top.git"
+ libraryURL = "https://clone.test/library.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, libraryURL: library.dir})
+ addTestSubmodule(t, top.dir, libraryURL, "lib")
+
+ var issues []string
+ cache := Cache{Root: t.TempDir(), RecurseSubmodules: true, OnSubmoduleIssue: collectIssues(&issues)}
+ dst := filepath.Join(t.TempDir(), "workspace", "src")
+ ctx := context.Background()
+ if _, err := cache.Prepare(ctx, topURL, "", dst); err != nil {
+ t.Fatalf("first Prepare: %v", err)
+ }
+ assertCheckedOut(t, dst, map[string]bool{"lib/library.txt": true})
+
+ missing := "0123456789abcdef0123456789abcdef01234567"
+ runGitTest(t, top.dir, "update-index", "--cacheinfo", "160000,"+missing+",lib")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "point lib at a missing commit")
+
+ if _, err := cache.Prepare(ctx, topURL, "", dst); err != nil {
+ t.Fatalf("second Prepare: %v", err)
+ }
+ assertEmptyDir(t, filepath.Join(dst, "lib"))
+ if want := []string{"lib fetch_failed " + libraryURL}; !slices.Equal(issues, want) {
+ t.Errorf("issues = %q, want %q", issues, want)
+ }
+}
+
+func TestEnsureWithOptionsRemovesDeletedSubmoduleCheckouts(t *testing.T) {
+ requireGit(t)
+
+ // top declares gone and sub; sub declares leaf.
+ top := newTestRepository(t, "top.txt")
+ gone := newTestRepository(t, "gone.txt")
+ sub := newTestRepository(t, "sub.txt")
+ leaf := newTestRepository(t, "leaf.txt")
+ const (
+ topURL = "https://clone.test/top.git"
+ goneURL = "https://clone.test/gone.git"
+ subURL = "https://clone.test/sub.git"
+ leafURL = "https://clone.test/leaf.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, goneURL: gone.dir, subURL: sub.dir, leafURL: leaf.dir})
+ addTestSubmodule(t, sub.dir, leafURL, "leaf")
+ addTestSubmodule(t, top.dir, goneURL, "gone")
+ addTestSubmodule(t, top.dir, subURL, "sub")
+
+ dst := filepath.Join(t.TempDir(), "checkout")
+ ctx := context.Background()
+ options := EnsureOptions{RecurseSubmodules: true}
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("first EnsureWithOptions: %v", err)
+ }
+ assertCheckedOut(t, dst, map[string]bool{"gone/gone.txt": true, "sub/sub.txt": true, "sub/leaf/leaf.txt": true})
+
+ runGitTest(t, sub.dir, "rm", "--quiet", "leaf")
+ runGitTest(t, sub.dir, "commit", "--quiet", "-m", "remove leaf")
+ runGitTest(t, top.dir, "rm", "--quiet", "gone")
+ runGitTest(t, top.dir, "update-index", "--cacheinfo", "160000,"+runGitTest(t, sub.dir, "rev-parse", "HEAD")+",sub")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "remove gone, update sub")
+
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("second EnsureWithOptions: %v", err)
+ }
+ assertCheckedOut(t, dst, map[string]bool{"top.txt": true, "gone": false, "sub/sub.txt": true, "sub/leaf": false})
+}
+
+// fetchWithoutSubmodules moves dst to its origin's HEAD the way an update
+// interrupted before the submodule step leaves it.
+func fetchWithoutSubmodules(t *testing.T, dst string) {
+ t.Helper()
+ runGitTest(t, dst, "fetch", "--quiet", "origin", "HEAD")
+ runGitTest(t, dst, "reset", "--quiet", "--hard", "--no-recurse-submodules", "FETCH_HEAD")
+}
+
+func TestEnsureWithOptionsRecoversFromInterruptedUpdate(t *testing.T) {
+ requireGit(t)
+
+ top := newTestRepository(t, "top.txt")
+ gone := newTestRepository(t, "gone.txt")
+ const (
+ topURL = "https://clone.test/top.git"
+ goneURL = "https://clone.test/gone.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, goneURL: gone.dir})
+ addTestSubmodule(t, top.dir, goneURL, "gone")
+
+ dst := filepath.Join(t.TempDir(), "checkout")
+ ctx := context.Background()
+ options := EnsureOptions{RecurseSubmodules: true}
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("first EnsureWithOptions: %v", err)
+ }
+ runGitTest(t, top.dir, "rm", "--quiet", "gone")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "remove gone")
+ fetchWithoutSubmodules(t, dst)
+
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("EnsureWithOptions: %v", err)
+ }
+ assertCheckedOut(t, dst, map[string]bool{"gone": false})
+}
+
+// The working tree of a recursive checkout is rebuilt on each update, so
+// files added to it do not survive; a non-recursive update keeps them.
+func TestEnsureWithOptionsRebuildsWorktreeOnlyWhenRecursive(t *testing.T) {
+ topURL, dst := newLibraryCheckout(t)
+ ctx := context.Background()
+ for _, recurse := range []bool{false, true} {
+ if err := os.WriteFile(filepath.Join(dst, "local.txt"), []byte("local\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", EnsureOptions{RecurseSubmodules: recurse}); err != nil {
+ t.Fatalf("recurse %v: EnsureWithOptions: %v", recurse, err)
+ }
+ assertCheckedOut(t, dst, map[string]bool{"local.txt": !recurse, "top.txt": true, "lib/library.txt": true})
+ }
+}
+
+func TestEnsureCleansSubmoduleReplacedByTrackedDirectory(t *testing.T) {
+ requireGit(t)
+
+ top := newTestRepository(t, "top.txt")
+ library := newTestRepository(t, "old.c")
+ const (
+ topURL = "https://clone.test/top.git"
+ libraryURL = "https://clone.test/library.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, libraryURL: library.dir})
+ addTestSubmodule(t, top.dir, libraryURL, "lib")
+
+ dst := filepath.Join(t.TempDir(), "checkout")
+ ctx := context.Background()
+ options := EnsureOptions{RecurseSubmodules: true}
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("first EnsureWithOptions: %v", err)
+ }
+
+ runGitTest(t, top.dir, "rm", "--quiet", "lib")
+ if err := os.MkdirAll(filepath.Join(top.dir, "lib"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(top.dir, "lib", "new.c"), []byte("new\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ runGitTest(t, top.dir, "add", "lib/new.c")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "vendor lib as files")
+
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("second EnsureWithOptions: %v", err)
+ }
+ entries, err := os.ReadDir(filepath.Join(dst, "lib"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var names []string
+ for _, entry := range entries {
+ names = append(names, entry.Name())
+ }
+ if !slices.Equal(names, []string{"new.c"}) {
+ t.Errorf("lib holds %q, want only new.c", names)
+ }
+}
+
+// A path that was a gitlink can sit under a symlink in a later commit. The
+// cleanup must not follow it, here into .git.
+func TestEnsureDoesNotRemoveThroughSymlinkedParent(t *testing.T) {
+ requireGit(t)
+
+ for _, recurse := range []bool{false, true} {
+ top := newTestRepository(t, "top.txt")
+ sub := newTestRepository(t, "sub.txt")
+ const (
+ topURL = "https://clone.test/top.git"
+ subURL = "https://clone.test/sub.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, subURL: sub.dir})
+ runGitTest(t, top.dir, "config", "-f", ".gitmodules", "submodule.objects.path", "alias/objects")
+ runGitTest(t, top.dir, "config", "-f", ".gitmodules", "submodule.objects.url", subURL)
+ runGitTest(t, top.dir, "add", ".gitmodules")
+ runGitTest(t, top.dir, "update-index", "--add", "--cacheinfo", "160000,"+sub.commit+",alias/objects")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "gitlink under alias")
+
+ dst := filepath.Join(t.TempDir(), "checkout")
+ ctx := context.Background()
+ options := EnsureOptions{RecurseSubmodules: recurse}
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("recurse %v: first EnsureWithOptions: %v", recurse, err)
+ }
+ runGitTest(t, top.dir, "rm", "--quiet", "--cached", "alias/objects")
+ if err := os.Symlink(".git", filepath.Join(top.dir, "alias")); err != nil {
+ t.Skipf("symlinks unavailable: %v", err)
+ }
+ runGitTest(t, top.dir, "add", "alias")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "alias is a symlink")
+
+ _ = EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options)
+ if _, err := os.Stat(filepath.Join(dst, ".git", "objects")); err != nil {
+ t.Errorf("recurse %v: .git/objects after update: %v", recurse, err)
+ }
+ }
+}
+
+// An update canceled after a submodule moved but before its own submodules
+// did leaves a stale nested checkout under one at its gitlink.
+func TestEnsureWithOptionsRecoversInterruptedNestedUpdate(t *testing.T) {
+ requireGit(t)
+
+ top := newTestRepository(t, "top.txt")
+ sub := newTestRepository(t, "sub.txt")
+ leaf := newTestRepository(t, "leaf.txt")
+ const (
+ topURL = "https://clone.test/top.git"
+ subURL = "https://clone.test/sub.git"
+ leafURL = "https://clone.test/leaf.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, subURL: sub.dir, leafURL: leaf.dir})
+ addTestSubmodule(t, sub.dir, leafURL, "leaf")
+ addTestSubmodule(t, top.dir, subURL, "sub")
+
+ dst := filepath.Join(t.TempDir(), "checkout")
+ ctx := context.Background()
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", EnsureOptions{RecurseSubmodules: true}); err != nil {
+ t.Fatalf("EnsureWithOptions: %v", err)
+ }
+
+ if err := os.WriteFile(filepath.Join(leaf.dir, "leaf.txt"), []byte("leaf2\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ runGitTest(t, leaf.dir, "commit", "--quiet", "-am", "leaf2")
+ runGitTest(t, sub.dir, "update-index", "--cacheinfo", "160000,"+runGitTest(t, leaf.dir, "rev-parse", "HEAD")+",leaf")
+ runGitTest(t, sub.dir, "commit", "--quiet", "-m", "update leaf")
+ runGitTest(t, top.dir, "update-index", "--cacheinfo", "160000,"+runGitTest(t, sub.dir, "rev-parse", "HEAD")+",sub")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "update sub")
+ fetchWithoutSubmodules(t, dst)
+ fetchWithoutSubmodules(t, filepath.Join(dst, "sub"))
+
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", EnsureOptions{RecurseSubmodules: true}); err != nil {
+ t.Fatalf("second EnsureWithOptions: %v", err)
+ }
+ content, err := os.ReadFile(filepath.Join(dst, "sub", "leaf", "leaf.txt"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(content) != "leaf2\n" {
+ t.Errorf("nested submodule content = %q, want leaf2", content)
+ }
+}
+
+func TestEnsureClearsMovedSubmodulePath(t *testing.T) {
+ requireGit(t)
+
+ top := newTestRepository(t, "top.txt")
+ library := newTestRepository(t, "library.txt")
+ const (
+ topURL = "https://clone.test/top.git"
+ libraryURL = "https://clone.test/library.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, libraryURL: library.dir})
+ addTestSubmodule(t, top.dir, libraryURL, "old")
+
+ dst := filepath.Join(t.TempDir(), "checkout")
+ ctx := context.Background()
+ options := EnsureOptions{RecurseSubmodules: true}
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("first EnsureWithOptions: %v", err)
+ }
+ runGitTest(t, top.dir, "mv", "old", "new")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "move library")
+
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("second EnsureWithOptions: %v", err)
+ }
+ assertCheckedOut(t, dst, map[string]bool{"old": false, "new/library.txt": true})
+}
+
+// newLibraryCheckout returns a checkout of a repository with a submodule at
+// lib, fetched recursively.
+func newLibraryCheckout(t *testing.T) (string, string) {
+ t.Helper()
+ requireGit(t)
+ top := newTestRepository(t, "top.txt")
+ library := newTestRepository(t, "library.txt")
+ const (
+ topURL = "https://clone.test/top.git"
+ libraryURL = "https://clone.test/library.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, libraryURL: library.dir})
+ addTestSubmodule(t, top.dir, libraryURL, "lib")
+ dst := filepath.Join(t.TempDir(), "checkout")
+ if err := EnsureWithOptions(context.Background(), Retry{}, topURL, dst, "", EnsureOptions{RecurseSubmodules: true}); err != nil {
+ t.Fatalf("EnsureWithOptions: %v", err)
+ }
+ return topURL, dst
+}
+
+// A submodule URL can name an empty repository, which leaves a checkout with
+// an unborn HEAD. That is a missing submodule, not a reason to fail.
+func TestEnsureTreatsEmptySubmoduleRepositoryAsMissing(t *testing.T) {
+ requireGit(t)
+
+ top := newTestRepository(t, "top.txt")
+ empty := t.TempDir()
+ runGitTest(t, empty, "init", "--quiet", "-b", "main")
+ const (
+ topURL = "https://clone.test/top.git"
+ emptyURL = "https://clone.test/empty.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, emptyURL: empty})
+ runGitTest(t, top.dir, "config", "-f", ".gitmodules", "submodule.lib.path", "lib")
+ runGitTest(t, top.dir, "config", "-f", ".gitmodules", "submodule.lib.url", emptyURL)
+ runGitTest(t, top.dir, "add", ".gitmodules")
+ runGitTest(t, top.dir, "update-index", "--add", "--cacheinfo", "160000,"+top.commit+",lib")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "submodule from an empty repository")
+
+ var issues []string
+ options := EnsureOptions{RecurseSubmodules: true, OnSubmoduleIssue: collectIssues(&issues)}
+ dst := filepath.Join(t.TempDir(), "checkout")
+ for range 2 {
+ issues = nil
+ if err := EnsureWithOptions(context.Background(), Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("EnsureWithOptions: %v", err)
+ }
+ assertEmptyDir(t, filepath.Join(dst, "lib"))
+ if want := []string{"lib fetch_failed " + emptyURL}; !slices.Equal(issues, want) {
+ t.Errorf("issues = %q, want %q", issues, want)
+ }
+ }
+}
+
+func TestEnsureWithOptionsRecoversFromBrokenSubmoduleGitfile(t *testing.T) {
+ for _, target := range []string{"missing", "no-head"} {
+ topURL, dst := newLibraryCheckout(t)
+ lib := filepath.Join(dst, "lib")
+ gitDir := filepath.Join(dst, ".git", "broken-"+target)
+ if target == "no-head" {
+ if err := os.MkdirAll(gitDir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := os.WriteFile(filepath.Join(lib, ".git"), []byte("gitdir: "+gitDir+"\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := EnsureWithOptions(context.Background(), Retry{}, topURL, dst, "", EnsureOptions{RecurseSubmodules: true}); err != nil {
+ t.Fatalf("%s: EnsureWithOptions: %v", target, err)
+ }
+ assertCheckedOut(t, dst, map[string]bool{"lib/library.txt": true})
+ }
+}
+
+// addTestGitConfig appends key=value to the GIT_CONFIG_* entries a test set.
+func addTestGitConfig(t *testing.T, key, value string) {
+ t.Helper()
+ count, err := strconv.Atoi(os.Getenv("GIT_CONFIG_COUNT"))
+ if err != nil {
+ count = 0
+ }
+ t.Setenv(fmt.Sprintf("GIT_CONFIG_KEY_%d", count), key)
+ t.Setenv(fmt.Sprintf("GIT_CONFIG_VALUE_%d", count), value)
+ t.Setenv("GIT_CONFIG_COUNT", fmt.Sprint(count+1))
+}
+
+// With extensions.submodulePathConfig, a submodule named with a slash keeps
+// its repository at an encoded location. The reused repository must still be
+// pointed at the new URL, or update fetches from the old one.
+func TestEnsureWithOptionsSyncsURLWithSubmodulePathConfig(t *testing.T) {
+ requireGit(t)
+
+ first := newTestRepository(t, "first.txt")
+ second := newTestRepository(t, "second.txt")
+ top := newTestRepository(t, "top.txt")
+ const (
+ topURL = "https://clone.test/top.git"
+ firstURL = "https://clone.test/first.git"
+ secondURL = "https://clone.test/second.git"
+ )
+ configureTestURLs(t, map[string]string{topURL: top.dir, firstURL: first.dir, secondURL: second.dir})
+ addTestGitConfig(t, "init.defaultSubmodulePathConfig", "true")
+ runGitTest(t, top.dir, "submodule", "add", "--quiet", "--name", "vendor/dependency", firstURL, "deps/library")
+ runGitTest(t, top.dir, "commit", "--quiet", "-am", "add submodule")
+
+ dst := filepath.Join(t.TempDir(), "checkout")
+ ctx := context.Background()
+ options := EnsureOptions{RecurseSubmodules: true}
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("first EnsureWithOptions: %v", err)
+ }
+ if got := runGitTest(t, dst, "config", "--get", "extensions.submodulePathConfig"); got != "true" {
+ t.Skipf("Git does not support extensions.submodulePathConfig (got %q)", got)
+ }
+
+ runGitTest(t, top.dir, "config", "--file", ".gitmodules", "submodule.vendor/dependency.url", secondURL)
+ runGitTest(t, top.dir, "add", ".gitmodules")
+ runGitTest(t, top.dir, "update-index", "--cacheinfo", "160000,"+second.commit+",deps/library")
+ runGitTest(t, top.dir, "commit", "--quiet", "-m", "retarget submodule")
+
+ if err := EnsureWithOptions(ctx, Retry{}, topURL, dst, "", options); err != nil {
+ t.Fatalf("second EnsureWithOptions: %v", err)
+ }
+ assertCheckedOut(t, dst, map[string]bool{"deps/library/second.txt": true})
+}
diff --git a/submodule_clear.go b/submodule_clear.go
new file mode 100644
index 0000000..9fe8afd
--- /dev/null
+++ b/submodule_clear.go
@@ -0,0 +1,151 @@
+package clone
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "io"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+// clearWorktree removes everything in dst except .git, so the reset that
+// follows rebuilds the working tree and no submodule keeps files from an
+// earlier checkout. Submodule repositories under .git/modules are kept, so
+// the update checks them out again without fetching what they already hold.
+func clearWorktree(dst string) error {
+ root, err := os.OpenRoot(dst)
+ if err != nil {
+ return err
+ }
+ defer func() { _ = root.Close() }()
+ entries, err := fs.ReadDir(root.FS(), ".")
+ if err != nil {
+ return fmt.Errorf("clear worktree: %w", err)
+ }
+ for _, entry := range entries {
+ if entry.Name() == ".git" {
+ continue
+ }
+ if err := root.RemoveAll(entry.Name()); err != nil {
+ return fmt.Errorf("clear worktree: %w", err)
+ }
+ }
+ return nil
+}
+
+// syncModuleRemotes points each existing submodule repository under dir's
+// .git/modules at the URL sync recorded for it. Sync updates a repository
+// only through its checkout, which clearWorktree removed, and update would
+// otherwise fetch from the URL an earlier commit declared.
+func syncModuleRemotes(ctx context.Context, policy Retry, dir string, links []gitlink) error {
+ for _, link := range links {
+ gitDir, err := moduleGitDir(ctx, policy, dir, link.name)
+ if err != nil {
+ return err
+ }
+ if gitDir == "" || !isGitDir(gitDir) {
+ continue
+ }
+ url := submoduleURLConfig(ctx, policy, dir, link.name)
+ if url == "" {
+ continue
+ }
+ if _, err := policy.Run(ctx, "", nil, "config", "--file", filepath.Join(gitDir, "config"), "remote.origin.url", url); err != nil {
+ if ctxErr := ctx.Err(); ctxErr != nil {
+ return ctxErr
+ }
+ }
+ }
+ return nil
+}
+
+// moduleGitDir returns where dir's repository keeps the submodule named name,
+// or "" when that cannot be resolved. With extensions.submodulePathConfig Git
+// records the location, relative to the checkout, in submodule..gitdir
+// rather than using modules/ under the Git directory.
+func moduleGitDir(ctx context.Context, policy Retry, dir, name string) (string, error) {
+ if out, err := policy.Run(ctx, dir, nil, "config", "-z", "--get", "submodule."+name+".gitdir"); err == nil {
+ location := strings.TrimSuffix(out, "\x00")
+ if !filepath.IsAbs(location) {
+ location = filepath.Join(dir, location)
+ }
+ return location, nil
+ } else if ctxErr := ctx.Err(); ctxErr != nil {
+ return "", ctxErr
+ }
+ out, err := policy.Run(ctx, dir, nil, "rev-parse", "--git-path", "modules/"+name)
+ if err != nil {
+ return "", ctx.Err()
+ }
+ path := strings.TrimRight(out, "\r\n")
+ if !filepath.IsAbs(path) {
+ path = filepath.Join(dir, path)
+ }
+ return path, nil
+}
+
+func isGitDir(path string) bool {
+ for _, name := range []string{"HEAD", "config"} {
+ info, err := os.Lstat(filepath.Join(path, name))
+ if err != nil || !info.Mode().IsRegular() {
+ return false
+ }
+ }
+ return true
+}
+
+// emptySubmodule replaces the directory at the slash path rel with an empty
+// one, as an uninitialized submodule appears. The submodule's repository
+// under .git/modules is kept for a later update.
+func emptySubmodule(root *os.Root, rel string) error {
+ info, err := lstatBelow(root, rel)
+ if err != nil || info == nil {
+ return err
+ }
+ name := filepath.FromSlash(rel)
+ if info.IsDir() {
+ dir, err := root.Open(name)
+ if err != nil {
+ return fmt.Errorf("remove stale submodule %s: %w", rel, err)
+ }
+ _, err = dir.ReadDir(1)
+ _ = dir.Close()
+ if errors.Is(err, io.EOF) {
+ return nil
+ }
+ }
+ if err := root.RemoveAll(name); err != nil {
+ return fmt.Errorf("remove stale submodule %s: %w", rel, err)
+ }
+ if err := root.Mkdir(name, dirPerm); err != nil {
+ return fmt.Errorf("remove stale submodule %s: %w", rel, err)
+ }
+ return nil
+}
+
+// lstatBelow returns the entry at the slash path rel, or nil when it is
+// absent or reached through anything but real directories. os.Root follows a
+// symlink that stays inside the root, which could otherwise point a removal
+// at .git.
+func lstatBelow(root *os.Root, rel string) (fs.FileInfo, error) {
+ parts := strings.Split(rel, "/")
+ for i := 1; i <= len(parts); i++ {
+ info, err := root.Lstat(filepath.FromSlash(strings.Join(parts[:i], "/")))
+ if errors.Is(err, fs.ErrNotExist) {
+ return nil, nil
+ }
+ if err != nil {
+ return nil, fmt.Errorf("inspect stale submodule %s: %w", rel, err)
+ }
+ if i == len(parts) {
+ return info, nil
+ }
+ if !info.IsDir() {
+ return nil, nil
+ }
+ }
+ return nil, nil
+}