Skip to content

indexserver: reuse shallow clones for opted-in repositories - #1162

Merged
keegancsmith merged 4 commits into
mainfrom
k/cache-git-repo
Sep 7, 2026
Merged

keegancsmith merged 4 commits into
mainfrom
k/cache-git-repo

Conversation

@keegancsmith

@keegancsmith keegancsmith commented Sep 5, 2026

Copy link
Copy Markdown
Member

Allow Sourcegraph to request CacheGitRepo so repeated monorepo indexing reuses existing Git objects. Clones expire after seven days or a restart; failed jobs, source/filter changes, opt-outs, and tenant deletion discard them. Git GC cannot bound retained promisor objects, so expiry deliberately starts fresh.

This PR's first commit refactors out the existing git repo management behaviour into a struct called gitRepoCache. Then the following commits add the behaviour around keeping the clone around.

Part of CU-3324

keegancsmith and others added 2 commits September 5, 2026 13:19
Keep Git acquisition and cleanup out of gitIndex so it only orchestrates
configuration and indexing. Group the existing Git operations under
gitRepoCache and lend the fetched directory through a callback.

This deliberately preserves temporary-clone behavior, including Git commands,
delta fallback, and unconditional best-effort deletion. Keeping this separate
from retention policy makes the upcoming monorepo cache change reviewable.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0717a-3acd-7026-977e-6ffe6d30e027
Co-authored-by: Amp <amp@ampcode.com>
…e reuse

Allow Sourcegraph to opt selected repositories into retaining shallow clones
between indexing jobs. Git GC retains unreachable promisor objects, so expire
clones after seven days rather than allowing frequently indexed monorepos to
grow indefinitely. The existing tmp-root cleanup still clears them on restart.

Keep reuse eligibility and completion metadata inside gitRepoCache. Failed or
interrupted jobs, source/filter changes, opt-outs, unassignment, and tenant
deletion must not leave stale clones reusable. This acquisition policy does
not affect search-index identity or the gitIndex callback.

Integration coverage verifies object reuse, updated searchable revisions,
filter expansion, delta fallback, and failure recovery. Lifecycle tests also
cover temporary clones, callback panics, and completion-marker failures.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0717a-3acd-7026-977e-6ffe6d30e027
Co-authored-by: Amp <amp@ampcode.com>
We can just always set atomic and no-auto-maintenance. Additionally we
can always do replaceall for the header config
@keegancsmith keegancsmith changed the title feat/indexserver: reuse shallow clones for opted-in repositories indexserver: reuse shallow clones for opted-in repositories Sep 5, 2026
@keegancsmith
keegancsmith marked this pull request as ready for review September 5, 2026 13:42
}

func (gitRepoCache) fetch(ctx context.Context, gitDir string, o *indexArgs, c gitIndexConfig, logger sglog.Logger) error {
if err := initGitRepo(ctx, gitDir, o, c); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this error if we call it twice?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never mind, it gets a fresh tmp dir


var gitCache gitRepoCache

const gitRepoCacheMaxAge = 7 * 24 * time.Hour

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth making this tunable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed will add envvar SRC_GIT_REPO_CACHE_MAX_AGE before merging

A fixed seven-day lifetime is a sensible default, but deployments may need a faster disk-pressure escape hatch or longer reuse window. Follow the existing indexserver duration environment pattern so operators can adjust the policy without expanding the per-repository protocol.

Amp-Thread-ID: https://ampcode.com/threads/T-01a07b93-fe3d-7234-b192-79dcabb36f90
Co-authored-by: Amp <amp@ampcode.com>
@keegancsmith
keegancsmith merged commit 596c362 into main Sep 7, 2026
6 of 7 checks passed
@keegancsmith
keegancsmith deleted the k/cache-git-repo branch September 7, 2026 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants