Skip to content

Commit 5430d01

Browse files
authored
Merge pull request #224 from neilime/fix/git-suuport-grafted-pull-refs
fix(git): supports grafted pull refs
2 parents f8ca396 + e3bdf20 commit 5430d01

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

__tests__/git.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,30 @@ describe('ref', () => {
165165
expect(ref).toEqual('refs/tags/8.0.0');
166166
});
167167

168+
it('returns mocked detached pull request merge ref (shallow clone)', async () => {
169+
jest.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => {
170+
const fullCmd = `${cmd} ${args?.join(' ')}`;
171+
let result = '';
172+
switch (fullCmd) {
173+
case 'git branch --show-current':
174+
result = '';
175+
break;
176+
case 'git show -s --pretty=%D':
177+
result = 'grafted, HEAD, pull/221/merge';
178+
break;
179+
}
180+
return Promise.resolve({
181+
stdout: result,
182+
stderr: '',
183+
exitCode: 0
184+
});
185+
});
186+
187+
const ref = await Git.ref();
188+
189+
expect(ref).toEqual('refs/pull/221/merge');
190+
});
191+
168192
it('should throws an error when detached HEAD ref is not supported', async () => {
169193
jest.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => {
170194
const fullCmd = `${cmd} ${args?.join(' ')}`;

src/git.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,18 @@ export class Git {
137137
return `refs/tags/${ref.split(':')[1].trim()}`;
138138
}
139139

140-
// Otherwise, it's a branch "<origin>/<branch-name>, <branch-name>"
140+
// Branch refs are formatted as "<origin>/<branch-name>, <branch-name>"
141141
const branchMatch = ref.match(/^[^/]+\/[^/]+, (.+)$/);
142142
if (branchMatch) {
143143
return `refs/heads/${branchMatch[1].trim()}`;
144144
}
145145

146+
// Pull request merge refs are formatted as "pull/<number>/<state>"
147+
const prMatch = ref.match(/^pull\/\d+\/(head|merge)$/);
148+
if (prMatch) {
149+
return `refs/${ref}`;
150+
}
151+
146152
throw new Error(`Unsupported detached HEAD ref in "${res}"`);
147153
}
148154

0 commit comments

Comments
 (0)