From ebf9087555367aeaa15e80f1de22d8ca48583dc3 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Wed, 23 Mar 2022 05:15:30 +0100
Subject: [PATCH] fix(git): git default branch fall back to remote show
 (#14766)

---
 lib/util/git/index.ts | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts
index 218a8366d0..58983ac67e 100644
--- a/lib/util/git/index.ts
+++ b/lib/util/git/index.ts
@@ -109,7 +109,17 @@ async function isDirectory(dir: string): Promise<boolean> {
 async function getDefaultBranch(git: SimpleGit): Promise<string> {
   // see https://stackoverflow.com/a/62352647/3005034
   try {
-    const res = await git.raw(['rev-parse', '--abbrev-ref', 'origin/HEAD']);
+    let res = await git.raw(['rev-parse', '--abbrev-ref', 'origin/HEAD']);
+    // istanbul ignore if
+    if (!res) {
+      logger.debug('Could not determine default branch using git rev-parse');
+      const headPrefix = 'HEAD branch: ';
+      res = (await git.raw(['remote', 'show', 'origin']))
+        .split('\n')
+        .map((line) => line.trim())
+        .find((line) => line.startsWith(headPrefix))
+        .replace(headPrefix, '');
+    }
     return res.replace('origin/', '').trim();
   } catch (err) /* istanbul ignore next */ {
     const errChecked = checkForPlatformFailure(err);
-- 
GitLab