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
43 changes: 43 additions & 0 deletions src/utils/__tests__/git-review-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,4 +1073,47 @@ describe('git-review-cache', () => {
);
expect(glabMrCalls).toHaveLength(1);
});

it('keys the cache through the render context runGit without spawning git for the ref', () => {
const harness = createHarness();
const lockPathFromSpawn = prepareCachePath(harness) + '.lock';
harness.execCalls.length = 0;

const runGitCalls: string[] = [];
const deps: GitReviewCacheDeps = {
...harness.deps,
runGit: (command) => {
runGitCalls.push(command);
return command === 'symbolic-ref --short HEAD' ? 'feature/cache-a' : null;
}
};

expect(getCachedGitReviewData('/tmp/repo', { context: { data: { cwd: '/tmp/repo' } } }, deps)).toBeNull();

expect(runGitCalls).toEqual(['symbolic-ref --short HEAD']);
expect(harness.execCalls.filter(call => call.cmd === 'git')).toEqual([]);
// Same cache file (and so the same lock the refresh child releases)
// as the uncached lookup.
expect(harness.cacheFiles.has(lockPathFromSpawn)).toBe(true);
});

it('falls back to rev-parse through runGit on a detached HEAD', () => {
const harness = createHarness();
harness.setCurrentRef('');
const lockPathFromSpawn = prepareCachePath(harness) + '.lock';

const runGitCalls: string[] = [];
const deps: GitReviewCacheDeps = {
...harness.deps,
runGit: (command) => {
runGitCalls.push(command);
return command === 'rev-parse --short HEAD' ? 'abc123' : null;
}
};

getCachedGitReviewData('/tmp/repo', { context: {} }, deps);

expect(runGitCalls).toEqual(['symbolic-ref --short HEAD', 'rev-parse --short HEAD']);
expect(harness.cacheFiles.has(lockPathFromSpawn)).toBe(true);
});
});
41 changes: 38 additions & 3 deletions src/utils/git-review-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import {
import os from 'node:os';
import path from 'node:path';

import type { RenderContext } from '../types/RenderContext';

import {
getVisibleWidth,
truncateStyledText
} from './ansi';
import { resolveExecutable } from './executable-path';
import { runGit } from './git';
import {
GIT_HARDENING_ARGS,
withGitHardeningEnv
Expand Down Expand Up @@ -50,6 +53,13 @@ export interface GitReviewData {

export interface GitReviewFetchOptions { includeChecks?: boolean }

export interface GitReviewCachedOptions extends GitReviewFetchOptions {
// The widget's render context. When given, the branch/HEAD lookup that
// keys the cache goes through git.ts's cached runGit (shared with the
// git-branch and git-sha widgets) instead of spawning git every render.
context?: RenderContext;
}

interface StoredGitReviewCache {
version: 1;
data: GitReviewData | null;
Expand Down Expand Up @@ -143,6 +153,7 @@ export interface GitReviewCacheDeps {
writeFileSync: typeof writeFileSync;
getHomedir: typeof os.homedir;
now: typeof Date.now;
runGit?: typeof runGit;
}

const DEFAULT_GIT_REVIEW_CACHE_DEPS: GitReviewCacheDeps = {
Expand All @@ -159,7 +170,8 @@ const DEFAULT_GIT_REVIEW_CACHE_DEPS: GitReviewCacheDeps = {
unlinkSync,
writeFileSync,
getHomedir: os.homedir,
now: Date.now
now: Date.now,
runGit
};

function getCacheDir(deps: GitReviewCacheDeps): string {
Expand Down Expand Up @@ -203,6 +215,26 @@ function getCacheRef(cwd: string, deps: GitReviewCacheDeps): string {
return 'unknown';
}

// Same ref as getCacheRef (which the refresh child still uses to derive the
// lock path it may release), read through the render's cached git commands.
function getCacheRefFromContext(context: RenderContext, cwd: string, deps: GitReviewCacheDeps): string {
if (!deps.runGit) {
return getCacheRef(cwd, deps);
}

const branch = deps.runGit('symbolic-ref --short HEAD', context)?.trim() ?? '';
if (branch.length > 0) {
return `branch:${branch}`;
}

const head = deps.runGit('rev-parse --short HEAD', context)?.trim() ?? '';
if (head.length > 0) {
return `head:${head}`;
}

return 'unknown';
}

function getCachePath(cwd: string, ref: string, deps: GitReviewCacheDeps): string {
const hash = createHash('sha256')
.update(cwd)
Expand Down Expand Up @@ -736,11 +768,14 @@ function scheduleRefresh(

export function getCachedGitReviewData(
cwd: string,
options: GitReviewFetchOptions = {},
options: GitReviewCachedOptions = {},
deps: GitReviewCacheDeps = DEFAULT_GIT_REVIEW_CACHE_DEPS
): GitReviewData | null {
const includeChecks = options.includeChecks ?? false;
const cachePath = getCachePath(cwd, getCacheRef(cwd, deps), deps);
const ref = options.context
? getCacheRefFromContext(options.context, cwd, deps)
: getCacheRef(cwd, deps);
const cachePath = getCachePath(cwd, ref, deps);
const cached = readCache(cachePath, deps);
const needsRefresh = cached === 'miss'
|| cached.stale
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/GitCiStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class GitCiStatusWidget implements Widget {
}

const cwd = this.deps.resolveGitCwd(context) ?? this.deps.getProcessCwd();
const checks = this.deps.getCachedGitReviewData(cwd, { includeChecks: true })?.checks;
const checks = this.deps.getCachedGitReviewData(cwd, { includeChecks: true, context })?.checks;
if (!checks) {
return isHidden(item, NO_DATA_HIDEABLE_STATE.key) ? null : NO_CHECKS;
}
Expand Down
5 changes: 4 additions & 1 deletion src/widgets/GitPr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export class GitPrWidget implements Widget {
}

const cwd = this.deps.resolveGitCwd(context) ?? this.deps.getProcessCwd();
const prData = this.deps.getCachedGitReviewData(cwd, { includeChecks: context.gitReviewNeedsChecks ?? false });
const prData = this.deps.getCachedGitReviewData(cwd, {
includeChecks: context.gitReviewNeedsChecks ?? false,
context
});
if (!prData) {
return isHidden(item, NO_DATA_HIDEABLE_STATE.key) ? null : `(no ${resolvePrNoun(null, context, this.deps)})`;
}
Expand Down
5 changes: 4 additions & 1 deletion src/widgets/__tests__/GitCiStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ describe('GitCiStatusWidget', () => {
it('uses process cwd when repo path is omitted', () => {
const getCachedGitReviewData = vi.fn(() => PASSING_PR);
render({}, { getCachedGitReviewData, resolveGitCwd: () => undefined });
expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/process-cwd', { includeChecks: true });
expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/process-cwd', {
includeChecks: true,
context: expect.objectContaining({}) as unknown
});
});
});
10 changes: 8 additions & 2 deletions src/widgets/__tests__/GitPr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,21 @@ describe('GitPrWidget', () => {
expect(result).toBe(
`${renderOsc8Link('https://github.com/owner/repo/pull/123', 'PR #123')} OPEN Fix authentication bug`
);
expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/process-cwd', { includeChecks: false });
expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/process-cwd', {
includeChecks: false,
context: expect.objectContaining({}) as unknown
});
});

it('should request checks when a CI widget shares the render context', () => {
const getCachedGitReviewData = vi.fn(() => SAMPLE_PR);

render({ cwd: '/tmp/repo', needsChecks: true }, { getCachedGitReviewData });

expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/repo', { includeChecks: true });
expect(getCachedGitReviewData).toHaveBeenCalledWith('/tmp/repo', {
includeChecks: true,
context: expect.objectContaining({}) as unknown
});
});

it('should truncate long titles', () => {
Expand Down
Loading