1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
| {-# LANGUAGE GADTs, RankNTypes, FlexibleContexts, TypeFamilies, CPP #-}
module DarcsDen.Handler.Repository.Changes where
import Darcs.Patch.PatchInfoAnd (PatchInfoAnd, info)
import Darcs.Patch.Info (PatchInfo(..), piDate, makeFilename)
import Darcs.Patch.Patchy (Commute(..))
import Darcs.Patch.Permutations (commuteWhatWeCanFL)
import Darcs.Patch.Prim.V1.Core ( Prim(..), DirPatchType(..), FilePatchType(..))
-- import Darcs.Patch.Patchy ( Patchy )
-- import Darcs.Patch.Prim.Class ( FromPrim(..), PrimPatch)
import Darcs.Patch.Prim.Class (PrimPatchBase(..))
#ifdef DARCS28
import Darcs.Patch.FileName (fn2fp)
import Darcs.Witnesses.Ordered
import qualified Darcs.Witnesses.Ordered as WO
#else
import Darcs.Util.Path (fn2fp)
import Darcs.Patch.Rebase.NameHack ( NameHack(..) )
import Darcs.Patch.Witnesses.Ordered
import qualified Darcs.Patch.Witnesses.Ordered as WO
import Darcs.Repository.Flags
#endif
import Data.List (isPrefixOf, nub)
import Data.Time (UTCTime, readTime)
import System.Locale (defaultTimeLocale)
import System.Time (calendarTimeToString)
import qualified Darcs.Patch as P
import qualified Darcs.Repository as R
import qualified Data.ByteString as BS
import DarcsDen.Handler.Repository.Util
import DarcsDen.State.User
import DarcsDen.Util
--instance PrimPatch Prim
--instance Patchy Prim
--instance PrimPatchBase Prim where
-- type PrimOf Prim = Prim
--
--instance FromPrim Prim where
-- fromPrim = id
data Summary
= Removed FilePath
| Added FilePath
| Replaced FilePath String String
| Modified FilePath
| Preference String String String
deriving (Eq, Show)
data PatchLog =
PatchLog
{ pID :: String
, pDate :: UTCTime
, pName :: String
, pAuthor :: String
, pIsUser :: Bool
, pLog :: String
, pDepends :: [String]
}
deriving (Eq, Show)
data PatchChange
= Moved
{ cmFrom :: FilePath
, cmTo :: FilePath
}
| DirChange
{ cdName :: FilePath
, cdType :: DirChange
}
| FileChange
{ cfName :: FilePath
, cfType :: FileChange
}
| PrefChange
{ cpName :: String
, cpFrom :: String
, cpTo :: String
}
deriving (Eq, Show)
data DirChange
= DirRemoved
| DirAdded
deriving (Eq, Show)
data FileChange
= FileRemoved
| FileAdded
| FileHunk
{ fchLine :: Int
, fchRemove :: BS.ByteString
, fchAdd :: BS.ByteString
}
| FileBinary
| FileReplace
{ fchFind :: String
, fchReplace :: String
}
deriving (Eq, Show)
data PatchChanges =
PatchChanges
{ pPatch :: PatchLog
, pChanges :: [PatchChange]
}
deriving (Eq, Show)
toLog :: (PatchInfo, [PatchInfo]) -> PatchLog
toLog (i, ds) =
PatchLog
(take 20 $ makeFilename i)
(readTime defaultTimeLocale "%c" (calendarTimeToString (piDate i)))
(fromBS $ _piName i)
(fromBS $ _piAuthor i)
False
(doMarkdown . unlines . filter (not . ("Ignore-this" `isPrefixOf`)) . map fromBS $ _piLog i)
(map (take 20 . makeFilename) ds)
findUsers :: [PatchLog] -> IO [PatchLog]
findUsers = findUsers' []
where
findUsers' :: [(String, Maybe String)] -> [PatchLog] -> IO [PatchLog]
findUsers' _ [] = return []
findUsers' checked (p:ps) =
case lookup (pAuthor p) checked of
Just (Just n) -> do
rest <- findUsers' checked ps
return (p { pAuthor = n, pIsUser = True } : rest)
Just Nothing -> do
rest <- findUsers' checked ps
return (p { pAuthor = safeAuthorFrom (pAuthor p) } : rest)
Nothing -> do
mu <- getUserByEmail (emailFrom (pAuthor p))
case mu of
Just u -> do
rest <- findUsers' ((pAuthor p, Just (uName u)) : checked) ps
return (p { pAuthor = uName u, pIsUser = True } : rest)
Nothing -> do
rest <- findUsers' ((pAuthor p, Nothing) : checked) ps
return (p { pAuthor = safeAuthorFrom (pAuthor p) } : rest)
toChanges :: ((PatchInfo, [PatchInfo]), [PatchChange]) -> PatchChanges
toChanges (p, changes) =
PatchChanges (toLog p) (simplify [] changes)
where
simplify a [] = reverse a
simplify a (c@(FileChange n t):cs)
| t == FileBinary =
simplify (c:filter (notFile n) a) (filter (notFile n) cs)
simplify a (c@(FileChange _ (FileReplace _ _)):cs) =
simplify (c:a) cs
simplify a (FileChange n (FileHunk l f t):cs) =
simplify (FileChange n (FileHunk l (highlight False n f) (highlight False n t)):a) cs
simplify a (c@(FileChange _ _):cs) = simplify (c:a) cs
simplify a (c@(PrefChange _ _ _):cs) = simplify (c:a) cs
simplify a (_:cs) = simplify a cs
notFile n (FileChange { cfName = n' })
| n == n' = False
notFile _ _ = True
primToChange :: Prim x y -> PatchChange
primToChange (Move f t) = Moved (drop 2 $ fn2fp f) (drop 2 $ fn2fp t)
primToChange (DP f t) = DirChange (drop 2 $ fn2fp f) (fromDP t)
primToChange (FP f t) = FileChange (drop 2 $ fn2fp f) (fromFP t)
primToChange (ChangePref n f t) = PrefChange n f t
-- primToChange a = error ("primToChange not supported for " ++ show a)
fromDP :: DirPatchType x y -> DirChange
fromDP RmDir = DirRemoved
fromDP AddDir = DirAdded
fromFP :: FilePatchType x y -> FileChange
fromFP RmFile = FileRemoved
fromFP AddFile = FileAdded
fromFP (Hunk l rs as) = FileHunk l (unlinesBS rs) (unlinesBS as)
where
unlinesBS = BS.concat . map (`BS.append` toBS "\n")
fromFP (Binary _ _) = FileBinary
fromFP (TokReplace _ f r) = FileReplace f r
getChanges :: String -> Int -> IO ([PatchLog], Int)
#ifdef DARCS28
getChanges dir page = R.withRepositoryDirectory [] dir $ R.RepoJob $ \dr -> do
#else
getChanges dir page = R.withRepositoryDirectory YesUseCache dir $ R.RepoJob $ \dr -> do
#endif
pset <- R.readRepo dr
let ps = fromPS (\np -> (P.patch2patchinfo np, P.getdeps np)) pset
patches = map toLog (paginate 30 page ps)
prettyLog <- findUsers patches
return
( prettyLog
, ceiling ((fromIntegral (length ps) :: Double) / 30)
)
getPatch :: String -> String -> IO (Maybe PatchChanges)
#ifdef DARCS28
getPatch dir patch = R.withRepositoryDirectory [] dir $ R.PrimV1Job $ \dr -> do
#else
getPatch dir patch = R.withRepositoryDirectory YesUseCache dir $ R.PrimV1Job $ \dr -> do
#endif
pset <- R.readRepo dr
let ps = fromPS makeList pset
ps' = filter (\(n, _) -> take 20 n == patch) ps
case ps' of
[] -> return Nothing
((_, p):_) -> do
let cs = toChanges p
[l] <- findUsers [pPatch cs]
return $ Just cs{pPatch = l}
makeList :: (P.Effect p, PrimOf p ~ Prim)
=> P.Named p x y
-> (String, ((PatchInfo,[PatchInfo]), [PatchChange]))
makeList np =
(P.patchname np,
((P.patch2patchinfo np, P.getdeps np), domap np))
domap :: (P.Effect p, PrimOf p ~ Prim)
=> P.Named p x y
-> [PatchChange]
domap np = WO.mapFL primToChange $ (P.effect np)
--domap2 :: (P.Effect p)
-- => P.Named p x y
-- -> FL (forall a b . Prim a b) x y
--domap2 np = WO.mapFL_FL fromPrim (P.effect np)
fromPS :: P.RepoPatch p => (forall x0 y0 . P.Named p x0 y0 -> b) -> R.PatchSet p x1 y1 -> [b]
fromPS f = WO.mapRL f . WO.reverseFL . R.patchSetToPatches
summarize :: [PatchChange] -> [Summary]
summarize = nub . summarize'
where
summarize' [] = []
summarize' (FileChange n FileRemoved:cs) = (Removed n) : summarize cs
summarize' (FileChange n FileAdded:cs) = (Added n) : summarize cs
summarize' (FileChange n (FileReplace f t):cs) = (Replaced n f t) : summarize cs
summarize' (FileChange n _:cs) = (Modified n) : summarize cs
summarize' (PrefChange n f t:cs) = (Preference n f t) : summarize cs
summarize' (_:cs) = summarize cs
isModification :: PatchChange -> Bool
isModification (FileChange _ (FileHunk _ _ _)) = True
isModification _ = False
#ifdef DARCS28
findAllDeps :: Commute p
#else
findAllDeps :: (Commute p, NameHack p)
#endif
=> RL (PatchInfoAnd p) x y -> [(PatchInfo, [PatchInfo])]
findAllDeps NilRL = []
findAllDeps (p :<: ps) = (info p, findDeps ps NilFL p) : findAllDeps ps
#ifdef DARCS28
findDeps :: Commute p
#else
findDeps :: (Commute p, NameHack p)
#endif
=> RL (PatchInfoAnd p) x y
-> FL (PatchInfoAnd p) y z
-> PatchInfoAnd p z z1
-> [PatchInfo]
findDeps NilRL _ _ = []
findDeps (p :<: ps) deps me =
case commuteWhatWeCanFL (p :> deps) of
deps' :> p' :> NilFL ->
case commute (p' :> me) of
Just (me' :> _) ->
-- not a dependency
findDeps ps deps' me'
Nothing ->
-- a direct dependency
info p' : findDeps ps (p :>: deps) me
_ ->
-- an indirect dependency
findDeps ps (p :>: deps) me
|