Skip to content

Commit 9779743

Browse files
committed
Correctly extract tag name when commit has multiple decorations
1 parent 9f1137c commit 9779743

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

__tests__/git.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,50 @@ describe('ref', () => {
408408
const ref = await Git.ref();
409409
expect(ref).toEqual('refs/remotes/unusual-format');
410410
});
411+
412+
it('returns mocked detached tag ref when commit also has branch decorations', async () => {
413+
vi.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => {
414+
const fullCmd = `${cmd} ${args?.join(' ')}`;
415+
let result = '';
416+
switch (fullCmd) {
417+
case 'git branch --show-current':
418+
result = '';
419+
break;
420+
case 'git show -s --pretty=%D':
421+
result = 'HEAD, tag: v8.0.0, origin/release-branch';
422+
break;
423+
}
424+
return Promise.resolve({
425+
stdout: result,
426+
stderr: '',
427+
exitCode: 0
428+
});
429+
});
430+
const ref = await Git.ref();
431+
expect(ref).toEqual('refs/tags/v8.0.0');
432+
});
433+
434+
it('returns mocked detached tag ref (shallow clone) when commit also has branch decorations', async () => {
435+
vi.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => {
436+
const fullCmd = `${cmd} ${args?.join(' ')}`;
437+
let result = '';
438+
switch (fullCmd) {
439+
case 'git branch --show-current':
440+
result = '';
441+
break;
442+
case 'git show -s --pretty=%D':
443+
result = 'grafted, HEAD, tag: v8.0.0, origin/release-branch';
444+
break;
445+
}
446+
return Promise.resolve({
447+
stdout: result,
448+
stderr: '',
449+
exitCode: 0
450+
});
451+
});
452+
const ref = await Git.ref();
453+
expect(ref).toEqual('refs/tags/v8.0.0');
454+
});
411455
});
412456

413457
describe('fullCommit', () => {

src/git.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class Git {
138138

139139
// Tag refs are formatted as "tag: <tagname>"
140140
if (ref.startsWith('tag: ')) {
141-
return `refs/tags/${ref.split(':')[1].trim()}`;
141+
return `refs/tags/${ref.slice('tag: '.length).split(',')[0].trim()}`;
142142
}
143143

144144
// Pull request merge refs are formatted as "pull/<number>/<state>"

0 commit comments

Comments
 (0)