Files
orca/src/main/git/source-control/git-read-cache-invalidation.ts
T

68 lines
2.8 KiB
TypeScript

import type { GitDiffResult } from '../../../shared/git/diff/git-diff-compare-types'
import type { GitStatusResult } from '../../../shared/git/status/git-status-types'
import { InFlightPromiseDedupe } from '../../../shared/in-flight-promise-dedupe'
import {
clearGitStatusLineStatsCache,
clearGitStatusLineStatsCacheKey
} from '../../../shared/git/status/git-status-line-stats-cache'
import { invalidateGitBranchLineTotalInFlight } from '../../../shared/git/status/git-branch-line-total'
import { GitStatusReadLeaseOwner } from '../../../shared/git/status/git-status-read-lease-owner'
import { invalidateGitUpstreamStatusReads } from './upstream'
import { clearSubmodulePathsCache } from './submodule-paths'
import { resolvedUpstreamNameCache } from './resolved-upstream-name-cache'
import { getStatusLineStatsCacheKey } from './status-branch-line-total-input'
import {
FILE_DIFF_CACHE_LIMITS,
NUMSTAT_CACHE_LIMITS,
SettledDiffCache
} from './settled-diff-cache'
export const gitDiffReadDedupe = new InFlightPromiseDedupe<GitDiffResult>()
/** Settled diff results, valid only while their stamped git state holds. */
export const settledDiffCache = new SettledDiffCache(FILE_DIFF_CACHE_LIMITS)
/** Settled status numstat passes, keyed per worktree and area. */
export const settledNumstatCache = new SettledDiffCache(NUMSTAT_CACHE_LIMITS)
export const statusReadLeaseOwner = new GitStatusReadLeaseOwner<GitStatusResult>()
// Clear every read cache so post-mutation calls cannot join stale pre-mutation reads.
export function invalidateGitReadCaches(): void {
gitDiffReadDedupe.clear()
settledDiffCache.clear()
settledNumstatCache.clear()
statusReadLeaseOwner.invalidate()
invalidateGitBranchLineTotalInFlight()
invalidateGitUpstreamStatusReads()
clearGitStatusLineStatsCache()
clearSubmodulePathsCache()
resolvedUpstreamNameCache.clear()
}
// A change made outside Orca: later status reads must neither join a read begun before it nor reuse its line counts.
// `null` means the changed checkouts are unknown.
export function invalidateGitStatusReads(worktreePaths: readonly string[] | null): void {
if (worktreePaths === null) {
statusReadLeaseOwner.invalidate()
clearGitStatusLineStatsCache()
return
}
for (const worktreePath of worktreePaths) {
// Status-read keys start with this one (getStatusReadKey).
const scope = getStatusLineStatsCacheKey(worktreePath)
statusReadLeaseOwner.invalidateWhere((key) => key.startsWith(`${scope}\0`))
clearGitStatusLineStatsCacheKey(scope)
}
}
export async function runWithGitReadCacheInvalidation<T>(run: () => Promise<T>): Promise<T> {
invalidateGitReadCaches()
try {
return await run()
} finally {
// Invalidate again: reads started during the mutation can also be stale.
invalidateGitReadCaches()
}
}