From 2446f446b781591a47caa35d2c42e14d07aac7d8 Mon Sep 17 00:00:00 2001
From: Gabriel-Ladzaretti
 <97394622+Gabriel-Ladzaretti@users.noreply.github.com>
Date: Tue, 26 Jul 2022 07:41:09 +0300
Subject: [PATCH] fix(git/cache): getCachedBehindBaseResult returns true when
 branch is not modified (#16752)

---
 lib/util/git/behind-base-branch-cache.spec.ts | 13 ++++++++++++-
 lib/util/git/behind-base-branch-cache.ts      |  2 +-
 lib/util/git/index.spec.ts                    | 15 +++++++--------
 3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/lib/util/git/behind-base-branch-cache.spec.ts b/lib/util/git/behind-base-branch-cache.spec.ts
index e711c11cc2..30d7fa9a6b 100644
--- a/lib/util/git/behind-base-branch-cache.spec.ts
+++ b/lib/util/git/behind-base-branch-cache.spec.ts
@@ -1,4 +1,4 @@
-import { mocked } from '../../../test/util';
+import { mocked, partial } from '../../../test/util';
 import * as _repositoryCache from '../cache/repository';
 import type { BranchCache, RepoCacheData } from '../cache/repository/types';
 import { getCachedBehindBaseResult } from './behind-base-branch-cache';
@@ -23,6 +23,17 @@ describe('util/git/behind-base-branch-cache', () => {
       expect(getCachedBehindBaseResult('foo', '111')).toBeNull();
     });
 
+    it('returns null if cache is partially defined', () => {
+      const branchName = 'branchName';
+      const branchCache = partial<BranchCache>({
+        branchName,
+        isModified: false,
+      });
+      const repoCache: RepoCacheData = { branches: [branchCache] };
+      repositoryCache.getCache.mockReturnValue(repoCache);
+      expect(getCachedBehindBaseResult(branchName, '111')).toBeNull();
+    });
+
     it('returns true if target SHA has changed', () => {
       repoCache.branches = [
         { branchName: 'foo', sha: 'aaa', parentSha: '222' } as BranchCache,
diff --git a/lib/util/git/behind-base-branch-cache.ts b/lib/util/git/behind-base-branch-cache.ts
index 5b9677dbe6..6283062df7 100644
--- a/lib/util/git/behind-base-branch-cache.ts
+++ b/lib/util/git/behind-base-branch-cache.ts
@@ -12,7 +12,7 @@ export function getCachedBehindBaseResult(
     (branch) => branch.branchName === branchName
   );
 
-  if (!cachedBranch) {
+  if (!cachedBranch?.parentSha) {
     return null;
   }
 
diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts
index ca69d170d0..52159d6073 100644
--- a/lib/util/git/index.spec.ts
+++ b/lib/util/git/index.spec.ts
@@ -1,7 +1,7 @@
 import fs from 'fs-extra';
 import Git from 'simple-git';
 import tmp from 'tmp-promise';
-import { mocked } from '../../../test/util';
+import { mocked, partial } from '../../../test/util';
 import { GlobalConfig } from '../../config/global';
 import {
   CONFIG_VALIDATION,
@@ -258,14 +258,13 @@ describe('util/git/index', () => {
     });
 
     it('should return result even if non-default and not under branchPrefix', async () => {
-      const parentSha = await git.getBranchParentSha('develop');
+      const parentSha = 'SHA';
+      const branchCache = partial<BranchCache>({
+        branchName: 'develop',
+        parentSha: parentSha,
+      });
       repoCache.getCache.mockReturnValueOnce({}).mockReturnValueOnce({
-        branches: [
-          {
-            branchName: 'develop',
-            parentSha: parentSha,
-          } as BranchCache,
-        ],
+        branches: [branchCache],
       });
       expect(await git.isBranchBehindBase('develop')).toBeTrue();
       expect(await git.isBranchBehindBase('develop')).toBeTrue(); // cache
-- 
GitLab