Debian Codegraph @

Hledger/Cli/Commands/Diff.hs

raw
1 {-|
2
3 The @diff@ command compares two diff.
4
5 -}
6
7 {-# LANGUAGE OverloadedStrings #-}
8 {-# LANGUAGE TemplateHaskell #-}
9
10 module Hledger.Cli.Commands.Diff (
11 diffmode
12 ,diff
13 ) where
14
15 import Data.List.Extra ((\\), groupSortOn, nubBy, sortBy)
16 import Data.Function (on)
17 import Data.Ord (comparing)
18 import Data.Maybe (fromJust)
19 import Data.Time (diffDays)
20 import Data.Either (partitionEithers)
21 import Data.Text.IO qualified as T
22 import Lens.Micro (set)
23 import Safe (headDef)
24
25 import Hledger
26 import Hledger.Cli.CliOptions
27
28 -- | Command line options for this command.
29 diffmode = hledgerCommandMode
30 $(embedFileRelative "Hledger/Cli/Commands/Diff.txt")
31 []
32 [generalflagsgroup2]
33 confflags
34 ([], Just $ argsFlag "-f FILE1 -f FILE2 FULLACCOUNTTNAME")
35
36 data PostingWithPath = PostingWithPath {
37 ppposting :: Posting,
38 pptxnidx :: Int,
39 pppidx :: Int }
40 deriving (Show)
41
42 instance Eq PostingWithPath where
43 a == b = pptxnidx a == pptxnidx b
44 && pppidx a == pppidx b
45
46 pptxn :: PostingWithPath -> Transaction
47 pptxn = fromJust . ptransaction . ppposting
48
49 ppamountqty :: PostingWithPath -> Quantity
50 ppamountqty = aquantity . headDef nullamt . amounts . pamount . ppposting
51
52 allPostingsWithPath :: Journal -> [PostingWithPath]
53 allPostingsWithPath j = do
54 (txnidx, txn) <- zip [0..] $ jtxns j
55 (pidx, p) <- zip [0..] $ tpostings txn
56 return PostingWithPath { ppposting = p, pptxnidx = txnidx, pppidx = pidx }
57
58 combine :: ([a], [b]) -> [Either a b]
59 combine (ls, rs) = map Left ls ++ map Right rs
60
61 combinedBinBy :: Ord b => (a -> b) -> ([a], [a]) -> [([a], [a])]
62 combinedBinBy f = map partitionEithers . groupSortOn (either f f) . combine
63
64 greedyMaxMatching :: (Eq a, Eq b) => [(a,b)] -> [(a,b)]
65 greedyMaxMatching = greedyMaxMatching' []
66
67 greedyMaxMatching' :: (Eq a, Eq b) => [Either a b] -> [(a,b)] -> [(a,b)]
68 greedyMaxMatching' alreadyUsed ((l,r):rest)
69 | Left l `elem` alreadyUsed || Right r `elem` alreadyUsed
70 = greedyMaxMatching' alreadyUsed rest
71 | otherwise = (l,r) : greedyMaxMatching' (Left l : Right r : alreadyUsed) rest
72 greedyMaxMatching' _ [] = []
73
74 dateCloseness :: (PostingWithPath, PostingWithPath) -> Integer
75 dateCloseness = negate . uncurry (diffDays `on` tdate.pptxn)
76
77 type Matching = [(PostingWithPath, PostingWithPath)]
78
79 matching :: [PostingWithPath] -> [PostingWithPath] -> Matching
80 matching ppl ppr = do
81 (left, right) <- combinedBinBy ppamountqty (ppl, ppr) -- TODO: probably not a correct choice of bins
82 greedyMaxMatching $ sortBy (comparing dateCloseness) [ (l,r) | l <- left, r <- right ]
83
84 matchingPostings :: AccountName -> Journal -> [PostingWithPath]
85 matchingPostings acct j = filter ((== acct) . paccount . ppposting) $ allPostingsWithPath j
86
87 pickSide :: Side -> (a,a) -> a
88 pickSide L (l,_) = l
89 pickSide R (_,r) = r
90
91 unmatchedtxns :: Side -> [PostingWithPath] -> Matching -> [Transaction]
92 unmatchedtxns s pp m =
93 map pptxn $ nubBy ((==) `on` pptxnidx) $ pp \\ map (pickSide s) m
94
95 -- | The diff command.
96 diff :: CliOpts -> Journal -> IO ()
97 diff CliOpts{file_=[f1, f2], reportspec_=ReportSpec{_rsQuery=Acct acctRe}} _ = do
98 j1 <- orDieTrying $ readJournalFile (set ignore_assertions True definputopts) f1
99 j2 <- orDieTrying $ readJournalFile (set ignore_assertions True definputopts) f2
100
101 let acct = reString acctRe
102 let pp1 = matchingPostings acct j1
103 let pp2 = matchingPostings acct j2
104
105 let m = matching pp1 pp2
106
107 let unmatchedtxn1 = unmatchedtxns L pp1 m
108 let unmatchedtxn2 = unmatchedtxns R pp2 m
109
110 putStrLn "These transactions are in the first file only:\n"
111 mapM_ (T.putStr . showTransaction) unmatchedtxn1
112
113 putStrLn "These transactions are in the second file only:\n"
114 mapM_ (T.putStr . showTransaction) unmatchedtxn2
115
116 diff _ _ = error' "Please specify two input files. Usage: hledger diff -f FILE1 -f FILE2 FULLACCOUNTNAME"