mirror of
https://github.com/docker/actions-toolkit.git
synced 2024-11-23 03:16:09 +08:00
fix(git): supports grafted pull refs
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
This commit is contained in:
parent
f8ca396f63
commit
e3bdf20325
@ -165,6 +165,30 @@ describe('ref', () => {
|
|||||||
expect(ref).toEqual('refs/tags/8.0.0');
|
expect(ref).toEqual('refs/tags/8.0.0');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns mocked detached pull request merge 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, pull/221/merge';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return Promise.resolve({
|
||||||
|
stdout: result,
|
||||||
|
stderr: '',
|
||||||
|
exitCode: 0
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const ref = await Git.ref();
|
||||||
|
|
||||||
|
expect(ref).toEqual('refs/pull/221/merge');
|
||||||
|
});
|
||||||
|
|
||||||
it('should throws an error when detached HEAD ref is not supported', async () => {
|
it('should throws an error when detached HEAD ref is not supported', 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(' ')}`;
|
||||||
|
@ -137,12 +137,18 @@ export class Git {
|
|||||||
return `refs/tags/${ref.split(':')[1].trim()}`;
|
return `refs/tags/${ref.split(':')[1].trim()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, it's a branch "<origin>/<branch-name>, <branch-name>"
|
// Branch refs are formatted as "<origin>/<branch-name>, <branch-name>"
|
||||||
const branchMatch = ref.match(/^[^/]+\/[^/]+, (.+)$/);
|
const branchMatch = ref.match(/^[^/]+\/[^/]+, (.+)$/);
|
||||||
if (branchMatch) {
|
if (branchMatch) {
|
||||||
return `refs/heads/${branchMatch[1].trim()}`;
|
return `refs/heads/${branchMatch[1].trim()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pull request merge refs are formatted as "pull/<number>/<state>"
|
||||||
|
const prMatch = ref.match(/^pull\/\d+\/(head|merge)$/);
|
||||||
|
if (prMatch) {
|
||||||
|
return `refs/${ref}`;
|
||||||
|
}
|
||||||
|
|
||||||
throw new Error(`Unsupported detached HEAD ref in "${res}"`);
|
throw new Error(`Unsupported detached HEAD ref in "${res}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user