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
18 changes: 15 additions & 3 deletions src/utils/__tests__/git-remote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe('git-remote utils', () => {

it('falls back to the tracking remote when upstream is not a remote name', () => {
mockExecFileSync.mockImplementationOnce(() => { throw new Error('No such remote'); });
mockExecFileSync.mockReturnValueOnce('hangie/feature/new-git-and-worktree-widgets\n');
mockExecFileSync.mockReturnValueOnce('refs/remotes/hangie/feature/new-git-and-worktree-widgets\n');
mockExecFileSync.mockReturnValueOnce('origin\nhangie\n');
mockExecFileSync.mockReturnValueOnce('https://github.com/hangie/ccstatusline.git\n');

Expand All @@ -301,7 +301,7 @@ describe('git-remote utils', () => {

it('matches the longest remote prefix when remote names contain slashes', () => {
mockExecFileSync.mockImplementationOnce(() => { throw new Error('No such remote'); });
mockExecFileSync.mockReturnValueOnce('team/upstream/feature/worktree\n');
mockExecFileSync.mockReturnValueOnce('refs/remotes/team/upstream/feature/worktree\n');
mockExecFileSync.mockReturnValueOnce('origin\nteam\nteam/upstream\n');
mockExecFileSync.mockReturnValueOnce('https://github.com/team/upstream-repo.git\n');

Expand All @@ -314,9 +314,21 @@ describe('git-remote utils', () => {
});
});

// The full ref: --abbrev-ref says `remotes/origin/feature` when a
// branch or tag is named `origin/feature`, and that matches no remote
it('reads the tracking branch as a full ref', () => {
mockExecFileSync.mockImplementationOnce(() => { throw new Error('No such remote'); });
mockExecFileSync.mockReturnValueOnce('refs/remotes/origin/feature\n');
mockExecFileSync.mockReturnValueOnce('origin\n');
mockExecFileSync.mockReturnValueOnce('https://github.com/me/proj.git\n');

expect(getUpstreamRemoteInfo({})?.name).toBe('origin');
expect(mockExecFileSync.mock.calls[1]?.[1]).toEqual([...GIT_HARDENING_ARGS, 'rev-parse', '--symbolic-full-name', '@{upstream}']);
});

it('returns null when the tracking remote cannot be resolved', () => {
mockExecFileSync.mockImplementationOnce(() => { throw new Error('No such remote'); });
mockExecFileSync.mockReturnValueOnce('hangie/feature/new-git-and-worktree-widgets\n');
mockExecFileSync.mockReturnValueOnce('refs/remotes/hangie/feature/new-git-and-worktree-widgets\n');
mockExecFileSync.mockReturnValueOnce('origin\n');

expect(getUpstreamRemoteInfo({})).toBeNull();
Expand Down
43 changes: 41 additions & 2 deletions src/utils/__tests__/git-review-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface PrCacheHarness {
spawnCalls: { args: string[]; command: string }[];
advanceNow: (milliseconds: number) => void;
setCurrentRef: (ref: string) => void;
addTag: (name: string) => void;
setOriginRemoteUrl: (url: string) => void;
setGlabAvailable: (available: boolean) => void;
setCliAuthedForHost: (cli: 'gh' | 'glab', host: string, authed: boolean) => void;
Expand All @@ -47,6 +48,7 @@ function createHarness(): PrCacheHarness {
const spawnCalls: { args: string[]; command: string }[] = [];
let now = 1_700_000_000_000;
let currentRef = 'feature/cache-a';
const tags = new Set<string>();
let originRemoteUrl: string | null = null;
let glabAvailable = false;
const authedHosts: Record<'gh' | 'glab', Set<string>> = {
Expand Down Expand Up @@ -79,8 +81,13 @@ function createHarness(): PrCacheHarness {
}
return `${originRemoteUrl}\n`;
}
if (cmd === 'git' && gitArgs[0] === 'symbolic-ref')
return `${currentRef}\n`;
if (cmd === 'git' && gitArgs[0] === 'symbolic-ref') {
// As git does: the full ref, or with --short the branch name,
// which becomes `heads/<name>` when a tag has the same name
if (!gitArgs.includes('--short'))
return `refs/heads/${currentRef}\n`;
return `${tags.has(currentRef) ? 'heads/' : ''}${currentRef}\n`;
}
if (cmd === 'git' && gitArgs[0] === 'rev-parse')
return 'abc123\n';
if (cmd === 'ssh' && commandArgs[0] === '-G') {
Expand Down Expand Up @@ -183,6 +190,9 @@ function createHarness(): PrCacheHarness {
setCurrentRef: (ref: string) => {
currentRef = ref;
},
addTag: (name: string) => {
tags.add(name);
},
setOriginRemoteUrl: (url: string) => {
originRemoteUrl = url;
},
Expand Down Expand Up @@ -735,6 +745,35 @@ describe('git-review-cache', () => {
}
});

// With a tag of the same name, `git symbolic-ref --short HEAD` says
// `heads/<branch>`, a branch no PR is on
it('pins gh to the branch\'s own name when a tag has the same name', () => {
const harness = createHarness();
harness.setOriginRemoteUrl('https://github.com/fork-owner/example-repo.git');
harness.addTag('feature/cache-a');
harness.ghResponses.push('');
harness.ghResponses.push('');

fetchGitReviewData('/tmp/repo', harness.deps);

const pinned = harness.execCalls.filter(call => call.cmd === 'gh' && call.args[0] === 'pr' && call.args.includes('--repo'));
expect(pinned.map(call => call.args.slice(-2))).toEqual([['--', 'feature/cache-a']]);
});

it('pins glab to the branch\'s own name when a tag has the same name', () => {
const harness = createHarness();
harness.setOriginRemoteUrl('git@gitlab.com:fork-owner/repo.git');
harness.setGlabAvailable(true);
harness.addTag('feature/cache-a');
harness.glabResponses.push('');
harness.glabResponses.push('');

fetchGitReviewData('/tmp/repo', harness.deps);

const pinned = harness.execCalls.filter(call => call.cmd === 'glab' && call.args[0] === 'mr' && call.args.includes('--repo'));
expect(pinned.map(call => call.args.slice(-2))).toEqual([['--', 'feature/cache-a']]);
});

it('passes the SSH host after --, so a host named like an option stays a host', () => {
const harness = createHarness();
harness.setOriginRemoteUrl('git@-oProxyCommand=x:owner/repo.git');
Expand Down
9 changes: 7 additions & 2 deletions src/utils/git-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,16 @@ export function getRemoteInfo(remoteName: string, context: RenderContext): Remot
};
}

const REMOTE_REF_PREFIX = 'refs/remotes/';

function getTrackingRemoteName(context: RenderContext): string | null {
const upstreamRef = runGit('rev-parse --abbrev-ref --symbolic-full-name @{upstream}', context);
if (!upstreamRef) {
// The full ref, since --abbrev-ref turns it into "remotes/<remote>/<name>"
// when a branch or tag is named "<remote>/<name>"
const fullRef = runGit('rev-parse --symbolic-full-name @{upstream}', context);
if (!fullRef?.startsWith(REMOTE_REF_PREFIX)) {
return null;
}
const upstreamRef = fullRef.slice(REMOTE_REF_PREFIX.length);

const remotes = listRemotes(context)
.slice()
Expand Down
7 changes: 6 additions & 1 deletion src/utils/git-review-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,13 @@ function runGitForCache(args: string[], cwd: string, deps: GitReviewCacheDeps):
}
}

const BRANCH_REF_PREFIX = 'refs/heads/';

function getCurrentBranch(cwd: string, deps: GitReviewCacheDeps): string | null {
const branch = runGitForCache(['symbolic-ref', '--short', 'HEAD'], cwd, deps);
// 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);
const branch = ref.startsWith(BRANCH_REF_PREFIX) ? ref.slice(BRANCH_REF_PREFIX.length) : ref;
return branch.length > 0 ? branch : null;
}

Expand Down
Loading