From ca6320d95de044da5affd3a330681c642415eb40 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 16 May 2019 13:38:21 +0200
Subject: [PATCH] fix(gitFs): catch missing baseBranch

---
 lib/platform/git/storage.ts | 30 +++++++++++++++++++++++-------
 lib/types.d.ts              |  5 +++++
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/lib/platform/git/storage.ts b/lib/platform/git/storage.ts
index 82c039dd99..2f8bfe4133 100644
--- a/lib/platform/git/storage.ts
+++ b/lib/platform/git/storage.ts
@@ -184,14 +184,30 @@ class Storage {
     if (branchName) {
       logger.debug(`Setting baseBranch to ${branchName}`);
       this._config.baseBranch = branchName;
-      if (branchName !== 'master') {
-        this._config.baseBranchSha = (await this._git!.raw([
-          'rev-parse',
-          'origin/' + branchName,
-        ])).trim();
+      try {
+        if (branchName !== 'master') {
+          this._config.baseBranchSha = (await this._git!.raw([
+            'rev-parse',
+            'origin/' + branchName,
+          ])).trim();
+        }
+        await this._git!.checkout([branchName, '-f']);
+        await this._git!.reset('hard');
+      } catch (err) /* istanbul ignore next */ {
+        if (
+          err.message.includes(
+            'unknown revision or path not in the working tree'
+          )
+        ) {
+          const error = new Error('config-validation');
+          error.validationError = 'baseBranch not found';
+          error.validationMessage =
+            'The following configured baseBranch could not be found: ' +
+            branchName;
+          throw error;
+        }
+        throw err;
       }
-      await this._git!.checkout([branchName, '-f']);
-      await this._git!.reset('hard');
     }
   }
 
diff --git a/lib/types.d.ts b/lib/types.d.ts
index 331a2b54c8..4df000bb6b 100644
--- a/lib/types.d.ts
+++ b/lib/types.d.ts
@@ -15,6 +15,11 @@ declare namespace Renovate {
 
 declare var logger: Renovate.ILogger;
 
+declare interface Error {
+  validationError?: string;
+  validationMessage?: string;
+}
+
 declare namespace NodeJS {
   interface Global {
     gitAuthor?: { name: string; email: string };
-- 
GitLab