From 7de273aa6b41d4db6e467bb38f3bf6fc6242051a Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 12 Oct 2020 10:36:49 +0200
Subject: [PATCH] fix(git): gracefully handle not a git repository failure

---
 lib/workers/repository/error.spec.ts | 7 +++++++
 lib/workers/repository/error.ts      | 4 ++++
 2 files changed, 11 insertions(+)

diff --git a/lib/workers/repository/error.spec.ts b/lib/workers/repository/error.spec.ts
index 29008a48b7..c086208d05 100644
--- a/lib/workers/repository/error.spec.ts
+++ b/lib/workers/repository/error.spec.ts
@@ -93,6 +93,13 @@ describe('workers/repository/error', () => {
       const res = await handleError(config, gitError);
       expect(res).toEqual(EXTERNAL_HOST_ERROR);
     });
+    it('rewrites git fatal error', async () => {
+      const gitError = new Error(
+        'fatal: not a git repository (or any parent up to mount point /mnt)\nStopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\n'
+      );
+      const res = await handleError(config, gitError);
+      expect(res).toEqual(REPOSITORY_TEMPORARY_ERROR);
+    });
     it('handles unknown error', async () => {
       const res = await handleError(config, new Error('abcdefg'));
       expect(res).toEqual(UNKNOWN_ERROR);
diff --git a/lib/workers/repository/error.ts b/lib/workers/repository/error.ts
index f0a39befbd..b8f739c0ed 100644
--- a/lib/workers/repository/error.ts
+++ b/lib/workers/repository/error.ts
@@ -183,6 +183,10 @@ export default async function handleError(
     // rewrite this error
     return EXTERNAL_HOST_ERROR;
   }
+  if (err.message.includes('fatal: not a git repository')) {
+    delete config.branchList; // eslint-disable-line no-param-reassign
+    return REPOSITORY_TEMPORARY_ERROR;
+  }
   // Swallow this error so that other repositories can be processed
   logger.error({ err }, `Repository has unknown error`);
   // delete branchList to avoid cleaning up branches
-- 
GitLab