From 66e1869c7267134cf9bb51f332e75f7e284dc417 Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.uk>
Date: Mon, 1 Jun 2020 19:54:57 +0100
Subject: [PATCH] git: Ensure that everything is clean

---
 bin/git-close-pull-request | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/bin/git-close-pull-request b/bin/git-close-pull-request
index 811a0450..ad06ed06 100755
--- a/bin/git-close-pull-request
+++ b/bin/git-close-pull-request
@@ -48,6 +48,7 @@ class ClosesPullRequests
     {
         $this->confirmCiStatusIsPassing();
         // TODO: Check that the current branch has a tracking branch.
+        $this->ensureWorkingDirectoryAndIndexAreClean();
         $this->fetchOrigin();
         $this->ensureFeatureBranchInSync();
         $this->ensureTargetBranchInSync();
@@ -58,6 +59,18 @@ class ClosesPullRequests
         $this->deleteLocalBranch();
     }
 
+    private function ensureWorkingDirectoryAndIndexAreClean(): void
+    {
+        echo 'Ensuring that index and working directory are clean...' . PHP_EOL;
+
+        $isIndexClean = $this->run('git diff --cached --exit-code', self::RUN_TYPE_COMMAND);
+        $isWorkingDirClean = $this->run('git diff --exit-code', self::RUN_TYPE_COMMAND);
+
+        if (!$isIndexClean || !$isWorkingDirClean) {
+            $this->dieWithMessage('Index or working dir not clean. Aborting.');
+        }
+    }
+
     private function getTargetBranchFromArgs(): string
     {
         if (!$targetBranchName = $this->getArg('t:', ['target:'])) {