1
1
mirror of https://github.com/github/semantic.git synced 2024-12-26 00:12:29 +03:00

Merge pull request #518 from github/end-with-newline

Check if the patch ends with a newline
This commit is contained in:
Rob Rix 2016-03-16 19:01:20 -04:00
commit 1a55c8c750
2 changed files with 8 additions and 6 deletions

View File

@ -22,7 +22,10 @@ import Data.Monoid
-- | Render a diff in the traditional patch format. -- | Render a diff in the traditional patch format.
patch :: Renderer a String patch :: Renderer a String
patch diff sources = mconcat $ showHunk sources <$> hunks diff sources patch diff sources = case getLast $ foldMap (Last . Just) string of
Just c | c /= '\n' -> string ++ "\n\\ No newline at end of file\n"
_ -> string
where string = mconcat $ showHunk sources <$> hunks diff sources
-- | A hunk in a patch, including the offset, changes, and context. -- | A hunk in a patch, including the offset, changes, and context.
data Hunk a = Hunk { offset :: Both (Sum Int), changes :: [Change a], trailingContext :: [Row a] } data Hunk a = Hunk { offset :: Both (Sum Int), changes :: [Change a], trailingContext :: [Row a] }
@ -46,11 +49,10 @@ rowIncrement = fmap lineIncrement
-- | Given the before and after sources, render a hunk to a string. -- | Given the before and after sources, render a hunk to a string.
showHunk :: Both SourceBlob -> Hunk (SplitDiff a Info) -> String showHunk :: Both SourceBlob -> Hunk (SplitDiff a Info) -> String
showHunk blobs hunk = if last sourceHunk /= '\n' showHunk blobs hunk = header blobs hunk ++
then sourceHunk ++ "\n\\\\ No newline at end of file\n" concat (showChange sources <$> changes hunk) ++
else sourceHunk showLines (snd sources) ' ' (snd <$> trailingContext hunk)
where sources = source <$> blobs where sources = source <$> blobs
sourceHunk = header blobs hunk ++ concat (showChange sources <$> changes hunk) ++ showLines (snd sources) ' ' (snd <$> trailingContext hunk)
-- | Given the before and after sources, render a change to a string. -- | Given the before and after sources, render a change to a string.
showChange :: Both (Source Char) -> Change (SplitDiff a Info) -> String showChange :: Both (Source Char) -> Change (SplitDiff a Info) -> String

View File

@ -6,4 +6,4 @@ index .. 100644
console.log("hello, world"); console.log("hello, world");
+console.log("insertion"); +console.log("insertion");
\\ No newline at end of file \ No newline at end of file