From f82d6cdd35b200017d4a3fedf20dbdbf87a4128d Mon Sep 17 00:00:00 2001
From: Eduardo Trujillo <ed@chromabits.com>
Date: Sun, 27 Dec 2020 15:17:13 -0800
Subject: [PATCH] feat(Git): Render empty changelogs

Adds support for empty diffs. Instead of throwing/erroring out, `shift` will
now render an empty changelog when the diff between two refs is empty.
---
 src/Shift/Git.hs       |  3 +--
 src/Shift/Rendering.hs | 37 +++++++++++++++++++++----------------
 src/Shift/Types.hs     |  2 +-
 3 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/src/Shift/Git.hs b/src/Shift/Git.hs
index c0c9afa..11b849a 100644
--- a/src/Shift/Git.hs
+++ b/src/Shift/Git.hs
@@ -7,7 +7,6 @@ module Shift.Git where
 
 import CMarkGFM (Node (Node), NodeType (DOCUMENT))
 import Control.Lens ((^.))
-import Control.Monad.Catch (throwM)
 import Control.Monad.Reader (runReaderT)
 import Control.Monad.State (runStateT)
 import Control.Monad.Trans (liftIO)
@@ -168,7 +167,7 @@ renderDiff repo (tx, ty) = do
   diff <- lookupCommitsDiff repo tx ty
 
   case diff of
-    [] -> throwM SEUnableToComputeDiff
+    [] -> printReport (tx, ty) def
     diff_ -> printReport (tx, ty) (generateReport . rights $ parseCommit <$> diff_)
 
 lookupCommitsDiff ::
diff --git a/src/Shift/Rendering.hs b/src/Shift/Rendering.hs
index c9e2f89..839fa9e 100644
--- a/src/Shift/Rendering.hs
+++ b/src/Shift/Rendering.hs
@@ -10,9 +10,10 @@ import Control.Monad.IO.Class (MonadIO)
 import Control.Monad.State (MonadState)
 import Control.Monad.Trans.Writer (execWriterT)
 import Control.Monad.Trans.Writer.Lazy (tell)
+import Data.Default (Default (def))
 import Data.Git (Commit, Ref, RefName (refNameRaw), commitAuthor)
 import Data.List (sortOn)
-import Data.Maybe (fromMaybe, fromMaybe, catMaybes)
+import Data.Maybe (catMaybes, fromMaybe)
 import Data.String.Conversions (cs)
 import Data.Text (Text)
 import qualified Data.Text as T (pack, take)
@@ -202,21 +203,25 @@ printReport ::
 printReport (rangeStart, rangeEnd) report = execWriterT $ do
   tell [headerOne $ renderRange rangeStart rangeEnd]
 
-  -- Print conventional commits
-  conventionalSection "New features:" crFeatures
-  conventionalSection "Fixes:" crFixes
-  conventionalSection "Updated documentation:" crDocs
-  conventionalSection "Refactors:" crRefactors
-  conventionalSection "Updated tests:" crTests
-  conventionalSection "Style fixes:" crStyles
-  conventionalSection "Chores:" crChores
-
-  -- Print miscs and merges
-  miscCommits <- mapM renderMiscCommit (report ^. crMisc)
-  mergeCommits <- mapM renderMergeCommit (report ^. crMerges)
-
-  textSection "Miscellaneous changes:" miscCommits
-  textSection "Merge commits:" mergeCommits
+  -- If the report is empty, don't render any changes.
+  if report == def
+    then tell [paragraphNode [textNode "No changes"]]
+    else do
+      -- Print conventional commits
+      conventionalSection "New features:" crFeatures
+      conventionalSection "Fixes:" crFixes
+      conventionalSection "Updated documentation:" crDocs
+      conventionalSection "Refactors:" crRefactors
+      conventionalSection "Updated tests:" crTests
+      conventionalSection "Style fixes:" crStyles
+      conventionalSection "Chores:" crChores
+
+      -- Print miscs and merges
+      miscCommits <- mapM renderMiscCommit (report ^. crMisc)
+      mergeCommits <- mapM renderMergeCommit (report ^. crMerges)
+
+      textSection "Miscellaneous changes:" miscCommits
+      textSection "Merge commits:" mergeCommits
   where
     conventionalSection label sectionLens = do
       let rawCommits = sortOn (\(_, _, pc) -> pc ^. ccScope) (report ^. sectionLens)
diff --git a/src/Shift/Types.hs b/src/Shift/Types.hs
index 69f745d..6a32e5b 100644
--- a/src/Shift/Types.hs
+++ b/src/Shift/Types.hs
@@ -149,7 +149,7 @@ data ChangeReport hash = ChangeReport
     _crBreakingChanges :: [BreakingChange],
     _crAffectedTickets :: HashSet TicketChange
   }
-  deriving (Show)
+  deriving (Show, Eq)
 
 instance Default (ChangeReport hash) where
   def =
-- 
GitLab