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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<base>.insteadOf`, so the caller's own rewrites of a trusted prefix also receive credentials.

Expand Down
20 changes: 19 additions & 1 deletion ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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])
}
}
Expand All @@ -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 {
Expand Down
Loading
Loading