fix(git): handle detached ref in shallow clone context

Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
This commit is contained in:
Emilien Escalle
2023-12-03 17:13:47 +01:00
parent ddc04994d9
commit 7d829c430b
2 changed files with 50 additions and 3 deletions

View File

@@ -123,13 +123,14 @@ export class Git {
private static async getDetachedRef(): Promise<string> {
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}"`);
}
const ref = refMatch[1].trim();
const ref = refMatch[2].trim();
// Tag refs are formatted as "tag: <tagname>"
if (ref.startsWith('tag: ')) {