Merge pull request #217 from hoverkraft-tech/fix/handle-detached-ref-shallow-clone
fix(git): handle detached ref in shallow clone context
This commit is contained in:
@@ -141,6 +141,52 @@ describe('ref', () => {
|
|||||||
expect(ref).toEqual('refs/tags/8.0.0');
|
expect(ref).toEqual('refs/tags/8.0.0');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns mocked detached tag ref (shallow clone)', async () => {
|
||||||
|
jest.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => {
|
||||||
|
const fullCmd = `${cmd} ${args?.join(' ')}`;
|
||||||
|
let result = '';
|
||||||
|
switch (fullCmd) {
|
||||||
|
case 'git branch --show-current':
|
||||||
|
result = '';
|
||||||
|
break;
|
||||||
|
case 'git show -s --pretty=%D':
|
||||||
|
result = 'grafted, HEAD, tag: 8.0.0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return Promise.resolve({
|
||||||
|
stdout: result,
|
||||||
|
stderr: '',
|
||||||
|
exitCode: 0
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const ref = await Git.ref();
|
||||||
|
|
||||||
|
expect(ref).toEqual('refs/tags/8.0.0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throws an error when detached HEAD ref is not supported', async () => {
|
||||||
|
jest.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => {
|
||||||
|
const fullCmd = `${cmd} ${args?.join(' ')}`;
|
||||||
|
let result = '';
|
||||||
|
switch (fullCmd) {
|
||||||
|
case 'git branch --show-current':
|
||||||
|
result = '';
|
||||||
|
break;
|
||||||
|
case 'git show -s --pretty=%D':
|
||||||
|
result = 'wrong, HEAD, tag: 8.0.0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return Promise.resolve({
|
||||||
|
stdout: result,
|
||||||
|
stderr: '',
|
||||||
|
exitCode: 0
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(Git.ref()).rejects.toThrow('Cannot find detached HEAD ref in "wrong, HEAD, tag: 8.0.0"');
|
||||||
|
});
|
||||||
|
|
||||||
it('returns mocked detached branch ref', async () => {
|
it('returns mocked detached branch ref', async () => {
|
||||||
jest.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => {
|
jest.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => {
|
||||||
const fullCmd = `${cmd} ${args?.join(' ')}`;
|
const fullCmd = `${cmd} ${args?.join(' ')}`;
|
||||||
|
|||||||
@@ -123,13 +123,14 @@ export class Git {
|
|||||||
private static async getDetachedRef(): Promise<string> {
|
private static async getDetachedRef(): Promise<string> {
|
||||||
const res = await Git.exec(['show', '-s', '--pretty=%D']);
|
const res = await Git.exec(['show', '-s', '--pretty=%D']);
|
||||||
|
|
||||||
const refMatch = res.match(/^HEAD, (.*)$/);
|
// Can be "HEAD, <tagname>" or "grafted, HEAD, <tagname>"
|
||||||
|
const refMatch = res.match(/^(grafted, )?HEAD, (.*)$/);
|
||||||
|
|
||||||
if (!refMatch) {
|
if (!refMatch || !refMatch[2]) {
|
||||||
throw new Error(`Cannot find detached HEAD ref in "${res}"`);
|
throw new Error(`Cannot find detached HEAD ref in "${res}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ref = refMatch[1].trim();
|
const ref = refMatch[2].trim();
|
||||||
|
|
||||||
// Tag refs are formatted as "tag: <tagname>"
|
// Tag refs are formatted as "tag: <tagname>"
|
||||||
if (ref.startsWith('tag: ')) {
|
if (ref.startsWith('tag: ')) {
|
||||||
|
|||||||
Reference in New Issue
Block a user