2016-02-16 20:41:13 +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-02-16 20:41:13 +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/>.
|
|
|
|
-}
|
|
|
|
|
2016-02-23 17:45:03 +09:00
|
|
|
module Vervis.Git
|
2016-05-05 16:29:19 +09:00
|
|
|
( readSourceView
|
2016-05-06 19:29:02 +09:00
|
|
|
, readChangesView
|
2016-05-13 17:49:19 +09:00
|
|
|
, listRefs
|
2018-05-18 08:33:37 +09:00
|
|
|
, readPatch
|
2016-02-16 20:41:13 +09:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
import Data.Foldable (find)
|
2016-02-16 20:41:13 +09:00
|
|
|
import Data.Git
|
2016-05-06 19:29:02 +09:00
|
|
|
import Data.Git.Graph
|
2016-05-05 16:29:19 +09:00
|
|
|
import Data.Git.Harder
|
2018-05-18 08:33:37 +09:00
|
|
|
import Data.Git.Ref (fromHex, toHex)
|
|
|
|
import Data.Git.Repository (getCommit)
|
2016-05-05 16:29:19 +09:00
|
|
|
import Data.Git.Storage (getObject_)
|
|
|
|
import Data.Git.Storage.Object (Object (..))
|
2016-05-06 19:29:02 +09:00
|
|
|
import Data.Git.Types (GitTime (..))
|
|
|
|
import Data.Graph.Inductive.Graph (noNodes)
|
|
|
|
import Data.Graph.Inductive.Query.Topsort
|
2016-05-05 16:29:19 +09:00
|
|
|
import Data.Set (Set)
|
|
|
|
import Data.String (fromString)
|
2016-05-06 19:29:02 +09:00
|
|
|
import Data.Text (Text)
|
2018-05-18 08:33:37 +09:00
|
|
|
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
2018-04-01 07:04:33 +09:00
|
|
|
import Data.Time.Clock ()
|
|
|
|
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
|
2018-05-18 08:33:37 +09:00
|
|
|
import Data.Traversable (for)
|
2016-05-06 19:29:02 +09:00
|
|
|
import System.Hourglass (timeCurrent)
|
2018-05-18 08:33:37 +09:00
|
|
|
import Text.Email.Validate (emailAddress)
|
2018-04-01 07:04:33 +09:00
|
|
|
import Time.Types (Elapsed (..), Seconds (..))
|
2016-02-16 20:41:13 +09:00
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
import qualified Data.ByteString.Lazy as BL (ByteString)
|
2016-05-06 19:29:02 +09:00
|
|
|
import qualified Data.DList as D (DList, empty, snoc, toList)
|
2016-05-05 16:29:19 +09:00
|
|
|
import qualified Data.Set as S (member, mapMonotonic)
|
2018-05-18 08:33:37 +09:00
|
|
|
import qualified Data.Text as T (pack, unpack, break, strip)
|
2016-05-06 19:29:02 +09:00
|
|
|
import qualified Data.Text.Encoding as TE (decodeUtf8With)
|
|
|
|
import qualified Data.Text.Encoding.Error as TE (lenientDecode)
|
2016-02-16 20:41:13 +09:00
|
|
|
|
2016-05-06 19:29:02 +09:00
|
|
|
import Data.ByteString.Char8.Local (takeLine)
|
|
|
|
import Data.EventTime.Local
|
2016-05-05 16:29:19 +09:00
|
|
|
import Data.Git.Local
|
2016-05-06 19:29:02 +09:00
|
|
|
import Vervis.Changes
|
2016-05-05 16:29:19 +09:00
|
|
|
import Vervis.Foundation (Widget)
|
2018-05-18 08:33:37 +09:00
|
|
|
import Vervis.Patch
|
2016-05-05 16:29:19 +09:00
|
|
|
import Vervis.Readme
|
|
|
|
import Vervis.SourceTree
|
2016-02-16 20:41:13 +09:00
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
matchReadme :: (ModePerm, ObjId, Text, EntObjType) -> Bool
|
|
|
|
matchReadme (_, _, name, EntObjBlob) = isReadme name
|
|
|
|
matchReadme _ = False
|
2016-02-16 20:41:13 +09:00
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
-- | Find a README file in a directory. Return the filename and the file
|
|
|
|
-- content.
|
|
|
|
findReadme :: Git -> TreeRows -> IO (Maybe (Text, BL.ByteString))
|
|
|
|
findReadme git rows =
|
|
|
|
case find matchReadme rows of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just (_perm, oid, name, _etype) -> do
|
|
|
|
obj <- getObject_ git (unObjId oid) True
|
|
|
|
return $ case obj of
|
|
|
|
ObjBlob b -> Just (name, blobGetContent b)
|
|
|
|
_ -> Nothing
|
2016-02-16 20:41:13 +09:00
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
matchType :: EntObjType -> EntryType
|
|
|
|
matchType EntObjBlob = TypeBlob
|
|
|
|
matchType EntObjTree = TypeTree
|
2016-02-16 20:41:13 +09:00
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
rowToEntry :: (ModePerm, ObjId, Text, EntObjType) -> DirEntry
|
|
|
|
rowToEntry (_, _, name, etype) = DirEntry (matchType etype) name
|
2016-02-16 20:41:13 +09:00
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
loadSourceView
|
|
|
|
:: Git
|
|
|
|
-> Text
|
|
|
|
-> [Text]
|
|
|
|
-> IO (Set RefName, Set RefName, Maybe (SourceView BL.ByteString))
|
|
|
|
loadSourceView git refT dir = do
|
|
|
|
branches <- branchList git
|
|
|
|
tags <- tagList git
|
2016-05-06 19:29:02 +09:00
|
|
|
let refS = T.unpack refT
|
2016-05-05 16:29:19 +09:00
|
|
|
refN = RefName refS
|
|
|
|
msv <- if refN `S.member` branches || refN `S.member` tags
|
|
|
|
then do
|
|
|
|
tipOid <- resolveName git refS
|
|
|
|
mtree <- resolveTreeish git $ unObjId tipOid
|
|
|
|
case mtree of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just tree -> do
|
|
|
|
let dir' = map (entName . encodeUtf8) dir
|
|
|
|
view <- viewPath git tree dir'
|
|
|
|
Just <$> case view of
|
|
|
|
RootView rows -> do
|
|
|
|
mreadme <- findReadme git rows
|
|
|
|
let ents = map rowToEntry rows
|
|
|
|
return $ SourceDir $
|
|
|
|
DirectoryView Nothing ents mreadme
|
|
|
|
TreeView name _ rows -> do
|
|
|
|
mreadme <- findReadme git rows
|
|
|
|
let ents = map rowToEntry rows
|
|
|
|
return $ SourceDir $
|
|
|
|
DirectoryView (Just name) ents mreadme
|
|
|
|
BlobView name _ body ->
|
|
|
|
return $ SourceFile $ FileView name body
|
|
|
|
else return Nothing
|
|
|
|
return (branches, tags, msv)
|
2016-03-03 17:15:54 +09:00
|
|
|
|
2016-05-05 16:29:19 +09:00
|
|
|
readSourceView
|
|
|
|
:: FilePath
|
|
|
|
-- ^ Repository path
|
|
|
|
-> Text
|
|
|
|
-- ^ Name of branch or tag
|
|
|
|
-> [Text]
|
|
|
|
-- ^ Path in the source tree pointing to a file or directory
|
|
|
|
-> IO (Set Text, Set Text, Maybe (SourceView Widget))
|
|
|
|
-- ^ Branches, tags, view of the selected item
|
|
|
|
readSourceView path ref dir = do
|
|
|
|
(bs, ts, msv) <-
|
|
|
|
withRepo (fromString path) $ \ git -> loadSourceView git ref dir
|
2016-05-06 19:29:02 +09:00
|
|
|
let toTexts = S.mapMonotonic $ T.pack . refNameRaw
|
2016-05-05 16:29:19 +09:00
|
|
|
return (toTexts bs, toTexts ts, renderSources dir <$> msv)
|
2016-05-06 19:29:02 +09:00
|
|
|
|
|
|
|
instance ResultList D.DList where
|
|
|
|
emptyList = D.empty
|
|
|
|
appendItem = flip D.snoc
|
|
|
|
|
|
|
|
readChangesView
|
|
|
|
:: FilePath
|
|
|
|
-- ^ Repository path
|
|
|
|
-> Text
|
|
|
|
-- ^ Name of branch or tag
|
2016-05-13 17:49:19 +09:00
|
|
|
-> Int
|
|
|
|
-- ^ Offset, i.e. latest commits to skip
|
|
|
|
-> Int
|
|
|
|
-- ^ Limit, i.e. how many latest commits to take after the offset
|
|
|
|
-> IO (Int, [LogEntry])
|
|
|
|
-- ^ Total number of ref's changes, and view of selected ref's change log
|
|
|
|
readChangesView path ref off lim = withRepo (fromString path) $ \ git -> do
|
|
|
|
oid <- resolveName git $ T.unpack ref
|
|
|
|
graph <- loadCommitGraphPT git [oid]
|
|
|
|
let mnodes = topsortUnmixOrder graph (NodeStack [noNodes graph])
|
|
|
|
nodes = case mnodes of
|
|
|
|
Nothing -> error "commit graph contains a cycle"
|
|
|
|
Just ns -> ns
|
|
|
|
pairs = D.toList $ fmap (nodeLabel graph) nodes
|
|
|
|
pairs' = take lim $ drop off pairs
|
|
|
|
toText = TE.decodeUtf8With TE.lenientDecode
|
|
|
|
Elapsed now <- timeCurrent
|
|
|
|
let mkrow oid commit = LogEntry
|
|
|
|
{ leAuthor = toText $ personName $ commitAuthor commit
|
|
|
|
, leHash = toText $ toHex $ unObjId oid
|
|
|
|
, leMessage = toText $ takeLine $ commitMessage commit
|
|
|
|
, leTime =
|
2018-04-01 07:04:33 +09:00
|
|
|
( utc t
|
|
|
|
, intervalToEventTime $
|
|
|
|
FriendlyConvert $
|
|
|
|
now - t
|
|
|
|
)
|
2016-05-13 17:49:19 +09:00
|
|
|
}
|
|
|
|
where
|
|
|
|
Elapsed t = gitTimeUTC $ personTime $ commitAuthor commit
|
2018-04-01 07:04:33 +09:00
|
|
|
utc (Seconds i) = posixSecondsToUTCTime $ fromIntegral i
|
2016-05-13 17:49:19 +09:00
|
|
|
return (noNodes graph, map (uncurry mkrow) pairs')
|
|
|
|
|
|
|
|
listRefs :: FilePath -> IO (Set Text, Set Text)
|
|
|
|
listRefs path = withRepo (fromString path) $ \ git ->
|
|
|
|
(,) <$> listBranches git <*> listTags git
|
2018-05-18 08:33:37 +09:00
|
|
|
|
|
|
|
patch :: [Edit] -> Commit -> Patch
|
|
|
|
patch edits c = Patch
|
|
|
|
{ patchAuthorName = decodeUtf8 $ personName $ commitAuthor c
|
|
|
|
, patchAuthorEmail =
|
|
|
|
let b = personEmail $ commitAuthor c
|
|
|
|
in case emailAddress b of
|
|
|
|
Nothing -> error $ "Invalid email " ++ T.unpack (decodeUtf8 b)
|
|
|
|
Just e -> e
|
|
|
|
, patchTime =
|
|
|
|
let Elapsed (Seconds t) = gitTimeUTC $ personTime $ commitAuthor c
|
|
|
|
in posixSecondsToUTCTime $ fromIntegral t
|
|
|
|
, patchTitle = title
|
|
|
|
, patchDescription = desc
|
|
|
|
, patchDiff = edits
|
|
|
|
}
|
|
|
|
where
|
|
|
|
split t =
|
|
|
|
let (l, r) = T.break (\ c -> c == '\n' || c == '\r') t
|
|
|
|
in (T.strip l, T.strip r)
|
|
|
|
(title, desc) = split $ decodeUtf8 $ commitMessage c
|
|
|
|
|
|
|
|
readPatch :: FilePath -> Text -> IO (Patch, [Text])
|
|
|
|
readPatch path hash = withRepo (fromString path) $ \ git -> do
|
|
|
|
let ref = fromHex $ encodeUtf8 hash
|
|
|
|
c <- getCommit git ref
|
|
|
|
medits <- case commitParents c of
|
|
|
|
[] -> -- use the tree to generate list of AddFile diff parts
|
|
|
|
return $ Right []
|
|
|
|
[p] -> -- use getDiff to grab list of changes in the patch
|
|
|
|
return $ Right []
|
|
|
|
ps -> -- multiple parents! idk rn how to deal with this correctly
|
|
|
|
fmap Left $ for ps $ \ p ->
|
|
|
|
decodeUtf8 . takeLine . commitMessage <$> getCommit git p
|
|
|
|
return $ case medits of
|
|
|
|
Left parents -> (patch [] c, parents)
|
|
|
|
Right edits -> (patch edits c, [])
|