From 1e2c30a96a7b708d701181bce8fbffc036a6556c Mon Sep 17 00:00:00 2001
From: Jamie Magee <JamieMagee@users.noreply.github.com>
Date: Fri, 17 Jan 2020 18:11:02 +0100
Subject: [PATCH] fix: warn if force push commit has no diff (#5099)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 lib/platform/git/storage.ts       | 14 ++++++++++++++
 test/platform/git/storage.spec.ts | 15 +++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/lib/platform/git/storage.ts b/lib/platform/git/storage.ts
index 0e0e40d576..1dcdcf5cde 100644
--- a/lib/platform/git/storage.ts
+++ b/lib/platform/git/storage.ts
@@ -475,6 +475,14 @@ export class Storage {
     }
   }
 
+  async hasDiff(branchName: string): Promise<boolean> {
+    try {
+      return (await this._git!.diff(['HEAD', branchName])) !== '';
+    } catch (err) {
+      return true;
+    }
+  }
+
   async commitFilesToBranch({
     branchName,
     files,
@@ -519,6 +527,12 @@ export class Storage {
         }
       }
       await this._git!.commit(message);
+      if (!(await this.hasDiff(`origin/${branchName}`))) {
+        logger.warn(
+          { branchName, fileNames },
+          'No file changes detected. Is this an empty force push?'
+        );
+      }
       await this._git!.push('origin', `${branchName}:${branchName}`, {
         '--force': true,
         '-u': true,
diff --git a/test/platform/git/storage.spec.ts b/test/platform/git/storage.spec.ts
index fb8ab75a0c..8156f9a7f4 100644
--- a/test/platform/git/storage.spec.ts
+++ b/test/platform/git/storage.spec.ts
@@ -263,6 +263,21 @@ describe('platform/git/storage', () => {
         message: 'Update something',
       });
     });
+    it('does not push when no diff', async () => {
+      const branchName = 'renovate/something';
+      const local = Git(tmpDir.path);
+      await local.push('origin', `master:${branchName}`);
+      await local.fetch([
+        'origin',
+        `refs/heads/${branchName}:refs/remotes/origin/${branchName}`,
+      ]);
+      const files = [];
+      await git.commitFilesToBranch({
+        branchName,
+        files,
+        message: 'Update something',
+      });
+    });
   });
 
   describe('getCommitMessages()', () => {
-- 
GitLab