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
30 changes: 27 additions & 3 deletions src/utils/__tests__/git-review-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,13 +1123,13 @@ describe('git-review-cache', () => {
...harness.deps,
runGit: (command) => {
runGitCalls.push(command);
return command === 'symbolic-ref --short HEAD' ? 'feature/cache-a' : null;
return command === 'symbolic-ref HEAD' ? 'refs/heads/feature/cache-a' : null;
}
};

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

expect(runGitCalls).toEqual(['symbolic-ref --short HEAD']);
expect(runGitCalls).toEqual(['symbolic-ref 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.
Expand All @@ -1152,7 +1152,31 @@ describe('git-review-cache', () => {

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

expect(runGitCalls).toEqual(['symbolic-ref --short HEAD', 'rev-parse --short HEAD']);
expect(runGitCalls).toEqual(['symbolic-ref HEAD', 'rev-parse --short HEAD']);
expect(harness.cacheFiles.has(lockPathFromSpawn)).toBe(true);
});

// The refresh child writes the cache file of the branch's own name, so the
// render must read that one, not the file of "heads/<branch>"
it('keys the cache through runGit by the branch\'s own name when a tag has the same name', () => {
const harness = createHarness();
harness.addTag('feature/cache-a');
const lockPathFromSpawn = prepareCachePath(harness) + '.lock';

const deps: GitReviewCacheDeps = {
...harness.deps,
runGit: (command) => {
// As git prints it when a tag shadows the branch
if (command === 'symbolic-ref HEAD')
return 'refs/heads/feature/cache-a';
if (command === 'symbolic-ref --short HEAD')
return 'heads/feature/cache-a';
return null;
}
};

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

expect(harness.cacheFiles.has(lockPathFromSpawn)).toBe(true);
});
});
15 changes: 9 additions & 6 deletions src/utils/git-review-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,17 @@ function runGitForCache(args: string[], cwd: string, deps: GitReviewCacheDeps):

const BRANCH_REF_PREFIX = 'refs/heads/';

function getCurrentBranch(cwd: string, deps: GitReviewCacheDeps): string | null {
// The full ref, since --short turns it into "heads/<name>" when a tag has
// the same name, and no PR is on a branch by that name
const ref = runGitForCache(['symbolic-ref', 'HEAD'], cwd, deps);
// Read from the full ref, since --short turns it into "heads/<name>" when a
// tag has the same name, and no PR is on a branch by that name
function toBranchName(ref: string): string | null {
const branch = ref.startsWith(BRANCH_REF_PREFIX) ? ref.slice(BRANCH_REF_PREFIX.length) : ref;
return branch.length > 0 ? branch : null;
}

function getCurrentBranch(cwd: string, deps: GitReviewCacheDeps): string | null {
return toBranchName(runGitForCache(['symbolic-ref', 'HEAD'], cwd, deps));
}

function getCacheRef(cwd: string, deps: GitReviewCacheDeps): string {
const branch = getCurrentBranch(cwd, deps);
if (branch) {
Expand All @@ -227,8 +230,8 @@ function getCacheRefFromContext(context: RenderContext, cwd: string, deps: GitRe
return getCacheRef(cwd, deps);
}

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

Expand Down
Loading