From b398f05ecce64234c24abd9a6b64024d19d03392 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Mon, 21 May 2018 20:54:05 +0000 Subject: [PATCH] Ignore line numbers when computing diff --- src/Vervis/Git.hs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Vervis/Git.hs b/src/Vervis/Git.hs index 93521e9..e9f4dc5 100644 --- a/src/Vervis/Git.hs +++ b/src/Vervis/Git.hs @@ -217,28 +217,39 @@ ep2fp = T.unpack . decodeUtf8 . B.intercalate "/" . map toBytes unModePerm :: ModePerm -> Word32 unModePerm (ModePerm w) = w +data Line = Line + { lineNumber :: Int + , lineText :: Text + } + +instance Eq Line where + Line _ t == Line _ s = t == s + +instance Ord Line where + Line _ t `compare` Line _ s = t `compare` s + mkdiff :: [Text] -> [Text] -> [(Bool, Int, Hunk)] mkdiff old new = let eitherOldNew (Old a) = Just $ Left a eitherOldNew (New a) = Just $ Right a eitherOldNew (Both _ _) = Nothing - stripLineNumber = fmap snd + stripLineNumber = fmap lineText mkhunk' (adds, pairs, rems) = Hunk { hunkAddFirst = stripLineNumber adds , hunkRemoveAdd = map (stripLineNumber *** stripLineNumber) pairs , hunkRemoveLast = stripLineNumber rems } - line ((n, _):_, _ , _) = (True, n) - line ([] , ((n, _) :| _, _):_, _) = (False, n) - line ([] , [] , (n, _):_) = (False, n) - line ([] , [] , []) = error "empty hunk" + line ((Line n _):_, _ , _) = (True, n) + line ([] , ((Line n _) :| _, _):_, _) = (False, n) + line ([] , [] , (Line n _):_) = (False, n) + line ([] , [] , []) = error "empty hunk" mkhunk h = let (n, l) = line h in (n, l, mkhunk' h) in map (mkhunk . groupEithers . N.toList) $ groupJusts $ map eitherOldNew $ - diff (zip [1..] old) (zip [1..] new) + diff (zipWith Line [1..] old) (zipWith Line [1..] new) accumEdits :: BlobStateDiff -> [Edit] -> [Edit] accumEdits (OnlyOld bs) es =