2016-05-05 16:29:19 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
2018-04-01 07:04:33 +09:00
|
|
|
- Written in 2016, 2018 by fr33domlover <fr33domlover@riseup.net>.
|
2016-05-05 16:29:19 +09:00
|
|
|
-
|
|
|
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
|
|
|
-
|
|
|
|
- The author(s) have dedicated all copyright and related and neighboring
|
|
|
|
- rights to this software to the public domain worldwide. This software is
|
|
|
|
- distributed without any warranty.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
- with this software. If not, see
|
|
|
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Vervis.Darcs
|
|
|
|
( readSourceView
|
2016-06-04 15:57:54 +09:00
|
|
|
, readWikiView
|
2016-05-08 23:28:03 +09:00
|
|
|
, readChangesView
|
2018-04-10 07:00:01 +09:00
|
|
|
, lastChange
|
2016-05-05 16:29:19 +09:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2016-06-04 15:57:54 +09:00
|
|
|
import Prelude hiding (lookup)
|
2016-05-05 16:29:19 +09:00
|
|
|
|
2016-06-04 15:57:54 +09:00
|
|
|
import Control.Applicative ((<|>))
|
2016-05-13 17:49:19 +09:00
|
|
|
import Control.Monad.Trans.Class (lift)
|
|
|
|
import Control.Monad.Trans.Except (ExceptT (ExceptT), runExceptT)
|
2016-07-02 17:51:29 +09:00
|
|
|
import Darcs.Util.Path
|
|
|
|
import Darcs.Util.Tree
|
|
|
|
import Darcs.Util.Tree.Hashed
|
2016-06-04 15:57:54 +09:00
|
|
|
import Data.Bool (bool)
|
|
|
|
import Data.Maybe (listToMaybe, mapMaybe)
|
2016-05-05 16:29:19 +09:00
|
|
|
import Data.Text (Text)
|
|
|
|
import Data.Text.Encoding (encodeUtf8, decodeUtf8With)
|
|
|
|
import Data.Text.Encoding.Error (strictDecode)
|
2018-04-10 07:00:01 +09:00
|
|
|
import Data.Time.Clock (UTCTime, getCurrentTime, diffUTCTime)
|
2016-05-05 16:29:19 +09:00
|
|
|
import Data.Traversable (for)
|
2018-03-21 08:45:09 +09:00
|
|
|
import Development.Darcs.Internal.Hash.Codec
|
|
|
|
import Development.Darcs.Internal.Inventory.Parser
|
|
|
|
import Development.Darcs.Internal.Inventory.Read
|
|
|
|
import Development.Darcs.Internal.Inventory.Types
|
|
|
|
import Development.Darcs.Internal.Patch.Types
|
2016-05-05 16:29:19 +09:00
|
|
|
import System.FilePath ((</>))
|
|
|
|
|
|
|
|
import qualified Data.ByteString.Lazy as BL (ByteString)
|
2016-05-08 23:28:03 +09:00
|
|
|
import qualified Data.ByteString.Base16 as B16 (encode)
|
2016-05-05 16:29:19 +09:00
|
|
|
import qualified Data.Foldable as F (find)
|
2016-05-08 23:28:03 +09:00
|
|
|
import qualified Data.Text as T (takeWhile, stripEnd)
|
2016-05-05 16:29:19 +09:00
|
|
|
|
2016-05-08 23:28:03 +09:00
|
|
|
import Darcs.Local.Repository
|
2016-05-13 17:49:19 +09:00
|
|
|
import Data.Either.Local (maybeRight)
|
2016-05-08 23:28:03 +09:00
|
|
|
import Data.EventTime.Local
|
|
|
|
import Data.Text.UTF8.Local (decodeStrict)
|
|
|
|
import Data.Time.Clock.Local ()
|
|
|
|
import Vervis.Changes
|
2016-05-05 16:29:19 +09:00
|
|
|
import Vervis.Foundation (Widget)
|
|
|
|
import Vervis.Readme
|
|
|
|
import Vervis.SourceTree
|
2016-06-04 15:57:54 +09:00
|
|
|
import Vervis.Wiki (WikiView (..))
|
2016-05-05 16:29:19 +09:00
|
|
|
|
|
|
|
dirToAnchoredPath :: [EntryName] -> AnchoredPath
|
|
|
|
dirToAnchoredPath = AnchoredPath . map (Name . encodeUtf8)
|
|
|
|
|
|
|
|
matchType :: ItemType -> EntryType
|
|
|
|
matchType TreeType = TypeTree
|
|
|
|
matchType BlobType = TypeBlob
|
|
|
|
|
|
|
|
nameToText :: Name -> Text
|
|
|
|
nameToText (Name b) = decodeUtf8With strictDecode b
|
|
|
|
|
|
|
|
itemToEntry :: Name -> TreeItem IO -> DirEntry
|
|
|
|
itemToEntry name item = DirEntry (matchType $ itemType item) (nameToText name)
|
|
|
|
|
|
|
|
findReadme :: [(Name, TreeItem IO)] -> IO (Maybe (Text, BL.ByteString))
|
|
|
|
findReadme pairs =
|
|
|
|
case F.find (isReadme . nameToText . fst) pairs of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just (name, item) ->
|
|
|
|
case item of
|
|
|
|
File (Blob load _hash) -> do
|
|
|
|
content <- load
|
|
|
|
return $ Just (nameToText name, content)
|
|
|
|
_ -> return Nothing
|
|
|
|
|
|
|
|
itemToSourceView :: EntryName -> TreeItem IO -> IO (SourceView BL.ByteString)
|
|
|
|
itemToSourceView name (File (Blob load _hash)) = do
|
|
|
|
content <- load
|
|
|
|
return $ SourceFile $ FileView name content
|
|
|
|
itemToSourceView name (SubTree tree) = do
|
|
|
|
let items = listImmediate tree
|
|
|
|
mreadme <- findReadme items
|
|
|
|
return $ SourceDir DirectoryView
|
|
|
|
{ dvName = Just name
|
|
|
|
, dvEntries = map (uncurry itemToEntry) items
|
|
|
|
, dvReadme = mreadme
|
|
|
|
}
|
|
|
|
itemToSourceView _name (Stub _load _hash) = error "supposed to be expanded"
|
|
|
|
|
2016-06-04 15:57:54 +09:00
|
|
|
readStubbedTree :: FilePath -> IO (Tree IO)
|
|
|
|
readStubbedTree path = do
|
|
|
|
let darcsDir = path </> "_darcs"
|
|
|
|
(msize, hash) <- readPristineRoot darcsDir
|
|
|
|
let pristineDir = darcsDir </> "pristine.hashed"
|
|
|
|
readDarcsHashed pristineDir (msize, hash)
|
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
readSourceView
|
|
|
|
:: FilePath
|
|
|
|
-- ^ Repository path
|
|
|
|
-> [EntryName]
|
|
|
|
-- ^ Path in the source tree pointing to a file or directory
|
|
|
|
-> IO (Maybe (SourceView Widget))
|
|
|
|
readSourceView path dir = do
|
2016-06-04 15:57:54 +09:00
|
|
|
stubbedTree <- readStubbedTree path
|
2016-05-05 16:29:19 +09:00
|
|
|
msv <- if null dir
|
|
|
|
then do
|
|
|
|
let items = listImmediate stubbedTree
|
|
|
|
mreadme <- findReadme items
|
|
|
|
return $ Just $ SourceDir DirectoryView
|
|
|
|
{ dvName = Nothing
|
|
|
|
, dvEntries = map (uncurry itemToEntry) items
|
|
|
|
, dvReadme = mreadme
|
|
|
|
}
|
|
|
|
else do
|
|
|
|
let anch = dirToAnchoredPath dir
|
|
|
|
expandedTree <- expandPath stubbedTree anch
|
|
|
|
let mitem = find expandedTree anch
|
|
|
|
for mitem $ itemToSourceView (last dir)
|
|
|
|
return $ renderSources dir <$> msv
|
2016-05-08 23:28:03 +09:00
|
|
|
|
2016-06-04 15:57:54 +09:00
|
|
|
readWikiView
|
|
|
|
:: (EntryName -> EntryName -> Maybe Text)
|
|
|
|
-- ^ Page name predicate. Returns 'Nothing' for a file which isn't a page.
|
|
|
|
-- For a page file, returns 'Just' the page name, which is the filename
|
|
|
|
-- with some parts possibly removed or added. For example, you may wish to
|
|
|
|
-- remove any extensions, replace underscores with spaces and so on.
|
|
|
|
-> (EntryName -> Bool)
|
|
|
|
-- ^ Main page predicate. This is used to pick a top-level page to display
|
|
|
|
-- as the wiki root page.
|
|
|
|
-> FilePath
|
|
|
|
-- ^ Repository path.
|
|
|
|
-> [EntryName]
|
|
|
|
-- ^ Path in the source tree pointing to a file. The last component doesn't
|
|
|
|
-- have to be the full name of the file though, but it much match the page
|
|
|
|
-- predicate for the actual file to be found.
|
|
|
|
-> IO (Maybe WikiView)
|
|
|
|
readWikiView isPage isMain path dir = do
|
|
|
|
stubbedTree <- readStubbedTree path
|
|
|
|
let (parent, ispage, mfile) =
|
|
|
|
if null dir
|
|
|
|
then
|
|
|
|
( []
|
|
|
|
, bool Nothing (Just Nothing) . isMain
|
|
|
|
, Nothing
|
|
|
|
)
|
|
|
|
else
|
|
|
|
( init dir
|
|
|
|
, maybe Nothing (Just . Just) . isPage lst
|
|
|
|
, Just $ Name $ encodeUtf8 lst
|
|
|
|
)
|
|
|
|
where
|
|
|
|
lst = last dir
|
|
|
|
anch = dirToAnchoredPath parent
|
|
|
|
matchBlob f (n, (File (Blob load _))) = f (nameToText n) load
|
|
|
|
matchBlob _ _ = Nothing
|
|
|
|
matchBlob' f (File (Blob load _)) = Just $ f load
|
|
|
|
matchBlob' _ _ = Nothing
|
|
|
|
page name load = (,) load . Just <$> ispage name
|
|
|
|
matchP = listToMaybe . mapMaybe (matchBlob page) . listImmediate
|
|
|
|
matchF t = mfile >>= lookup t >>= matchBlob' (flip (,) Nothing)
|
|
|
|
expandedTree <- expandPath stubbedTree anch
|
|
|
|
let mpage = case find expandedTree anch of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just (File _) -> Nothing
|
|
|
|
Just (Stub _ _) -> error "supposed to be expanded"
|
|
|
|
Just (SubTree tree) -> matchP tree <|> matchF tree
|
|
|
|
mkview Nothing b = WikiViewRaw b
|
|
|
|
mkview (Just mt) b = WikiViewPage mt b
|
|
|
|
for mpage $ \ (load, mmtitle) -> mkview mmtitle <$> load
|
|
|
|
|
2016-05-08 23:28:03 +09:00
|
|
|
readChangesView
|
|
|
|
:: FilePath
|
|
|
|
-- ^ Repository path
|
2016-05-13 17:49:19 +09:00
|
|
|
-> Int
|
|
|
|
-- ^ Offset, i.e. latest patches to skip
|
|
|
|
-> Int
|
|
|
|
-- ^ Limit, i.e. how many latest patches to take after the offset
|
|
|
|
-> IO (Maybe (Int, [LogEntry]))
|
|
|
|
-- ^ Total number of changes, and view of the chosen subset
|
|
|
|
readChangesView path off lim = fmap maybeRight $ runExceptT $ do
|
2016-05-18 05:34:22 +09:00
|
|
|
total <- ExceptT $ readLatestInventory path latestInventorySizeP
|
2016-05-13 17:49:19 +09:00
|
|
|
let off' = total - off - lim
|
2016-05-18 05:34:22 +09:00
|
|
|
ps <- ExceptT $ readLatestInventory path $ latestInventoryPageP off' lim
|
2016-05-13 17:49:19 +09:00
|
|
|
now <- lift getCurrentTime
|
|
|
|
let toLE pi h = LogEntry
|
|
|
|
{ leAuthor =
|
|
|
|
T.stripEnd $ T.takeWhile (/= '<') $ piAuthor pi
|
2016-05-18 05:34:22 +09:00
|
|
|
, leHash = decodeStrict $ encodePatchHash h
|
2016-05-13 17:49:19 +09:00
|
|
|
, leMessage = piTitle pi
|
|
|
|
, leTime =
|
2018-04-01 07:04:33 +09:00
|
|
|
( piTime pi
|
|
|
|
, intervalToEventTime $
|
|
|
|
FriendlyConvert $
|
|
|
|
now `diffUTCTime` piTime pi
|
|
|
|
)
|
2016-05-13 17:49:19 +09:00
|
|
|
}
|
2016-05-18 05:34:22 +09:00
|
|
|
return (total, map (uncurry toLE) $ reverse $ snd ps)
|
2018-04-10 07:00:01 +09:00
|
|
|
|
|
|
|
lastChange :: FilePath -> UTCTime -> IO (Maybe EventTime)
|
|
|
|
lastChange path now = fmap maybeRight $ runExceptT $ do
|
|
|
|
total <- ExceptT $ readLatestInventory path latestInventorySizeP
|
|
|
|
let lim = 1
|
|
|
|
off = total - lim
|
|
|
|
(_, l) <- ExceptT $ readLatestInventory path $ latestInventoryPageP off lim
|
|
|
|
return $ case reverse l of
|
|
|
|
[] -> Never
|
|
|
|
(pi, _ph) : _ ->
|
|
|
|
intervalToEventTime $
|
|
|
|
FriendlyConvert $
|
|
|
|
now `diffUTCTime` piTime pi
|