From b124b461c0ed0e0c72c8bafadbb445d27979bf38 Mon Sep 17 00:00:00 2001 From: joshvera Date: Tue, 1 Mar 2016 17:13:53 -0500 Subject: [PATCH] Empty diffs have empty hunks --- src/Renderer/Patch.hs | 6 ++++-- test/PatchOutputSpec.hs | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Renderer/Patch.hs b/src/Renderer/Patch.hs index 471d707e8..bbb1a92c9 100644 --- a/src/Renderer/Patch.hs +++ b/src/Renderer/Patch.hs @@ -1,6 +1,7 @@ module Renderer.Patch ( patch, - hunks + hunks, + Hunk(..) ) where import Alignment @@ -88,13 +89,14 @@ header blobs hunk = filepathHeader ++ blobOidHeader ++ maybeOffsetHeader -- | Render a diff as a series of hunks. hunks :: Renderer a [Hunk (SplitDiff a Info)] +hunks diff blobs | (Both (True, True) ==) $ Source.null . source <$> blobs = [Hunk { offset = Both (0, 0), changes = [], trailingContext = [] }] hunks diff blobs = hunksInRows (Both (1, 1)) . Prelude.fst $ splitDiffByLines diff (pure 0) (source <$> blobs) -- | Given beginning line numbers, turn rows in a split diff into hunks in a -- | patch. hunksInRows :: Both (Sum Int) -> [Row (SplitDiff a Info)] -> [Hunk (SplitDiff a Info)] hunksInRows start rows = case nextHunk start rows of - Nothing -> [Hunk { offset = Both (0, 0), changes = [], trailingContext = [] }] + Nothing -> [] Just (hunk, rest) -> hunk : hunksInRows (offset hunk <> hunkLength hunk) rest -- | Given beginning line numbers, return the next hunk and the remaining rows diff --git a/test/PatchOutputSpec.hs b/test/PatchOutputSpec.hs index c368b4431..04d2bf38e 100644 --- a/test/PatchOutputSpec.hs +++ b/test/PatchOutputSpec.hs @@ -12,5 +12,5 @@ import Test.Hspec spec :: Spec spec = parallel $ describe "hunks" $ - it "empty diffs have no hunks" $ - hunks (Free . Annotated (pure (Info (Range 0 0) mempty)) $ Leaf "") (Both (SourceBlob (fromList "") "abcde" "path2.txt", SourceBlob (fromList "") "xyz" "path2.txt")) `shouldBe` [] + it "empty diffs have empty hunks" $ + hunks (Free . Annotated (pure (Info (Range 0 0) mempty)) $ Leaf "") (Both (SourceBlob (fromList "") "abcde" "path2.txt", SourceBlob (fromList "") "xyz" "path2.txt")) `shouldBe` [Hunk {offset = Both (0, 0), changes = [], trailingContext = []}]