2016-02-27 14:41:36 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
|
|
|
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
|
|
|
|
-
|
|
|
|
- ♡ 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.Handler.Repo
|
|
|
|
( getReposR
|
|
|
|
, postReposR
|
|
|
|
, getRepoNewR
|
|
|
|
, getRepoR
|
2016-04-12 09:19:04 +09:00
|
|
|
, getRepoSourceR
|
2016-04-12 06:35:26 +09:00
|
|
|
, getRepoCommitsR
|
2016-02-27 14:41:36 +09:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--TODO CONTINUE HERE
|
|
|
|
--
|
|
|
|
-- [/] maybe list project repos in personal overview too
|
|
|
|
-- [x] make repo list page
|
|
|
|
-- [x] add new repo creation link
|
|
|
|
-- [x] make new repo form
|
|
|
|
-- [x] write the git and mkdir parts that actually create the repo
|
2016-03-03 17:15:54 +09:00
|
|
|
-- [x] make repo view that shows a table of commits
|
2016-02-27 14:41:36 +09:00
|
|
|
|
2016-03-03 17:15:54 +09:00
|
|
|
import ClassyPrelude.Conduit hiding (unpack)
|
|
|
|
import Yesod hiding (Header, parseTime, (==.))
|
|
|
|
import Yesod.Auth
|
|
|
|
|
2016-04-12 07:13:32 +09:00
|
|
|
import Data.Byteable (toBytes)
|
2016-04-10 00:45:00 +09:00
|
|
|
import Data.Git.Graph
|
|
|
|
import Data.Git.Graph.Util
|
2016-04-12 09:19:04 +09:00
|
|
|
import Data.Git.Named (RefName (..))
|
2016-03-03 17:15:54 +09:00
|
|
|
import Data.Git.Ref (toHex)
|
2016-04-12 09:19:04 +09:00
|
|
|
import Data.Git.Repository
|
2016-04-12 19:06:21 +09:00
|
|
|
import Data.Git.Storage (withRepo, getObject_)
|
|
|
|
import Data.Git.Storage.Object (Object (..))
|
|
|
|
import Data.Git.Types (Blob (..), Commit (..), Person (..), entName)
|
2016-04-10 00:45:00 +09:00
|
|
|
import Data.Graph.Inductive.Graph (noNodes)
|
|
|
|
import Data.Graph.Inductive.Query.Topsort
|
2016-03-03 17:15:54 +09:00
|
|
|
import Data.Text (unpack)
|
|
|
|
import Data.Text.Encoding (decodeUtf8With)
|
|
|
|
import Data.Text.Encoding.Error (lenientDecode)
|
2016-02-27 14:41:36 +09:00
|
|
|
import Database.Esqueleto
|
2016-03-03 17:15:54 +09:00
|
|
|
import Data.Hourglass (timeConvert)
|
2016-02-27 14:41:36 +09:00
|
|
|
import System.Directory (createDirectoryIfMissing)
|
2016-03-03 17:15:54 +09:00
|
|
|
import System.Hourglass (dateCurrent)
|
|
|
|
|
2016-04-10 00:45:00 +09:00
|
|
|
import qualified Data.DList as D
|
2016-04-12 09:19:04 +09:00
|
|
|
import qualified Data.Set as S (member)
|
2016-04-12 19:06:21 +09:00
|
|
|
import qualified Data.Text.Lazy.Encoding as L (decodeUtf8With)
|
2016-04-10 00:45:00 +09:00
|
|
|
|
2016-03-03 17:15:54 +09:00
|
|
|
import Data.ByteString.Char8.Local (takeLine)
|
2016-02-27 14:41:36 +09:00
|
|
|
import Vervis.Form.Repo
|
2016-03-03 17:15:54 +09:00
|
|
|
import Vervis.Foundation
|
|
|
|
import Vervis.Git (timeAgo')
|
|
|
|
import Vervis.Path
|
|
|
|
import Vervis.Model
|
|
|
|
import Vervis.Settings
|
2016-02-27 14:41:36 +09:00
|
|
|
|
|
|
|
getReposR :: Text -> Text -> Handler Html
|
|
|
|
getReposR user proj = do
|
|
|
|
repos <- runDB $ select $ from $ \ (sharer, project, repo) -> do
|
|
|
|
where_ $
|
|
|
|
sharer ^. SharerIdent ==. val user &&.
|
|
|
|
sharer ^. SharerId ==. project ^. ProjectSharer &&.
|
|
|
|
repo ^. RepoProject ==. project ^. ProjectId
|
|
|
|
orderBy [asc $ repo ^. RepoIdent]
|
|
|
|
return $ repo ^. RepoIdent
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ mconcat
|
|
|
|
["Vervis > People > ", user, " > Projects > ", proj, " Repos"]
|
2016-04-12 06:24:10 +09:00
|
|
|
$(widgetFile "repo/repos")
|
2016-02-27 14:41:36 +09:00
|
|
|
|
|
|
|
postReposR :: Text -> Text -> Handler Html
|
|
|
|
postReposR user proj = do
|
|
|
|
Entity _pid person <- requireAuth
|
|
|
|
let sid = personIdent person
|
|
|
|
Entity pid _project <- runDB $ getBy404 $ UniqueProject proj sid
|
|
|
|
((result, widget), enctype) <- runFormPost $ newRepoForm sid pid
|
|
|
|
case result of
|
|
|
|
FormSuccess repo -> do
|
2016-03-03 17:15:54 +09:00
|
|
|
parent <- askProjectDir user proj
|
|
|
|
let path = parent </> unpack (repoIdent repo)
|
2016-02-27 14:41:36 +09:00
|
|
|
liftIO $ createDirectoryIfMissing True parent
|
|
|
|
liftIO $ initRepo $ fromString path
|
|
|
|
runDB $ insert_ repo
|
|
|
|
setMessage "Repo added."
|
|
|
|
redirectUltDest HomeR
|
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing"
|
2016-04-12 06:24:10 +09:00
|
|
|
defaultLayout $(widgetFile "repo/repo-new")
|
2016-02-27 14:41:36 +09:00
|
|
|
FormFailure l -> do
|
|
|
|
setMessage $ toHtml $ intercalate "; " l
|
2016-04-12 06:24:10 +09:00
|
|
|
defaultLayout $(widgetFile "repo/repo-new")
|
2016-02-27 14:41:36 +09:00
|
|
|
|
|
|
|
getRepoNewR :: Text -> Text -> Handler Html
|
|
|
|
getRepoNewR user proj = do
|
|
|
|
Entity _pid person <- requireAuth
|
|
|
|
let sid = personIdent person
|
|
|
|
Entity pid _project <- runDB $ getBy404 $ UniqueProject proj sid
|
|
|
|
((_result, widget), enctype) <- runFormPost $ newRepoForm sid pid
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ mconcat
|
|
|
|
["Vervis > People > ", user, " > Projects > ", proj, " > New Repo"]
|
2016-04-12 06:24:10 +09:00
|
|
|
$(widgetFile "repo/repo-new")
|
2016-02-27 14:41:36 +09:00
|
|
|
|
2016-04-10 00:45:00 +09:00
|
|
|
instance ResultList D.DList where
|
|
|
|
emptyList = D.empty
|
|
|
|
appendItem = flip D.snoc
|
|
|
|
|
2016-02-27 14:41:36 +09:00
|
|
|
getRepoR :: Text -> Text -> Text -> Handler Html
|
|
|
|
getRepoR user proj repo = do
|
|
|
|
repository <- runDB $ do
|
|
|
|
Entity sid _s <- getBy404 $ UniqueSharerIdent user
|
|
|
|
Entity pid _p <- getBy404 $ UniqueProject proj sid
|
|
|
|
Entity _rid r <- getBy404 $ UniqueRepo repo pid
|
|
|
|
return r
|
2016-03-03 17:15:54 +09:00
|
|
|
path <- askRepoDir user proj repo
|
2016-04-12 07:13:32 +09:00
|
|
|
view <- liftIO $ withRepo (fromString path) $ \ git -> do
|
2016-04-09 06:10:33 +09:00
|
|
|
oid <- resolveName git $ unpack $ repoMainBranch repository
|
2016-04-12 07:13:32 +09:00
|
|
|
commit <- getCommit git $ unObjId oid
|
|
|
|
tree <- getTree git $ commitTreeish commit
|
|
|
|
viewTree git tree
|
2016-03-03 17:15:54 +09:00
|
|
|
let toText = decodeUtf8With lenientDecode
|
2016-04-12 07:13:32 +09:00
|
|
|
mkrow (_perm, name, isTree) =
|
|
|
|
( if isTree then "[D]" else "[F]" :: Text
|
|
|
|
, toText $ toBytes name
|
2016-03-03 17:15:54 +09:00
|
|
|
)
|
2016-04-12 07:13:32 +09:00
|
|
|
rows = map mkrow view
|
2016-02-27 14:41:36 +09:00
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ intercalate " > " $
|
|
|
|
["Vervis", "People", user, "Projects", proj, "Repos", repo]
|
2016-04-12 06:24:10 +09:00
|
|
|
$(widgetFile "repo/repo")
|
2016-04-12 06:35:26 +09:00
|
|
|
|
2016-04-12 19:06:21 +09:00
|
|
|
getRepoSourceR :: Text -> Text -> Text -> Text -> [Text] -> Handler Html
|
|
|
|
getRepoSourceR user proj repo ref dir = do
|
2016-04-12 09:19:04 +09:00
|
|
|
path <- askRepoDir user proj repo
|
2016-04-12 19:06:21 +09:00
|
|
|
let toText = decodeUtf8With lenientDecode
|
|
|
|
toTextL = L.decodeUtf8With lenientDecode
|
2016-04-12 09:19:04 +09:00
|
|
|
minfo <- liftIO $ withRepo (fromString path) $ \ git -> do
|
|
|
|
branches <- branchList git
|
|
|
|
tags <- tagList git
|
|
|
|
let name = unpack ref
|
|
|
|
name' = RefName name
|
|
|
|
if name' `S.member` branches || name' `S.member` tags
|
|
|
|
then do
|
2016-04-12 19:06:21 +09:00
|
|
|
tipOid <- resolveName git name
|
|
|
|
mtree <- resolveTreeish git $ unObjId tipOid
|
2016-04-12 09:19:04 +09:00
|
|
|
case mtree of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just tree -> do
|
2016-04-12 19:06:21 +09:00
|
|
|
let dir' = map (entName . encodeUtf8) dir
|
|
|
|
mRootOid <- resolveTreePath git tree dir'
|
|
|
|
target <- case mRootOid of
|
|
|
|
Nothing -> return $ Right tree
|
|
|
|
Just oid -> do
|
|
|
|
obj <- getObject_ git (unObjId oid) True
|
|
|
|
case obj of
|
|
|
|
ObjTree t -> return $ Right t
|
|
|
|
ObjBlob b -> return $ Left b
|
|
|
|
_ -> error "expected tree or blob"
|
|
|
|
view <- case target of
|
|
|
|
Left b -> Left <$> return b
|
|
|
|
Right t -> Right <$> viewTree git t
|
2016-04-12 09:19:04 +09:00
|
|
|
return $ Just (branches, tags, view)
|
|
|
|
else return Nothing
|
|
|
|
case minfo of
|
|
|
|
Nothing -> notFound
|
|
|
|
Just (branches, tags, view) -> do
|
2016-04-12 19:06:21 +09:00
|
|
|
let mkrow (_perm, name, isTree) =
|
2016-04-12 09:19:04 +09:00
|
|
|
( if isTree then "[D]" else "[F]" :: Text
|
|
|
|
, toText $ toBytes name
|
|
|
|
)
|
2016-04-12 19:06:21 +09:00
|
|
|
display = case view of
|
|
|
|
Left b -> Left $ toTextL $ blobGetContent b
|
|
|
|
Right v -> Right $ map mkrow v
|
2016-04-12 09:19:04 +09:00
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ intercalate " > " $
|
|
|
|
["Vervis", "People", user, "Projects", proj, "Repos", repo]
|
|
|
|
$(widgetFile "repo/source")
|
|
|
|
|
2016-04-12 06:35:26 +09:00
|
|
|
getRepoCommitsR :: Text -> Text -> Text -> Handler Html
|
|
|
|
getRepoCommitsR user proj repo = do
|
|
|
|
repository <- runDB $ do
|
|
|
|
Entity sid _s <- getBy404 $ UniqueSharerIdent user
|
|
|
|
Entity pid _p <- getBy404 $ UniqueProject proj sid
|
|
|
|
Entity _rid r <- getBy404 $ UniqueRepo repo pid
|
|
|
|
return r
|
|
|
|
path <- askRepoDir user proj repo
|
|
|
|
pairs <- liftIO $ withRepo (fromString path) $ \ git -> do
|
|
|
|
oid <- resolveName git $ unpack $ repoMainBranch repository
|
|
|
|
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
|
|
|
|
return $ D.toList $ fmap (nodeLabel graph) nodes
|
|
|
|
now <- liftIO dateCurrent
|
|
|
|
let toText = decodeUtf8With lenientDecode
|
|
|
|
mkrow oid commit =
|
|
|
|
( toText $ personName $ commitAuthor commit
|
|
|
|
, toText $ toHex $ unObjId oid
|
|
|
|
, toText $ takeLine $ commitMessage commit
|
|
|
|
, timeAgo' now (timeConvert $ personTime $ commitAuthor commit)
|
|
|
|
)
|
|
|
|
rows = map (uncurry mkrow) pairs
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ intercalate " > " $
|
|
|
|
["Vervis", "People", user, "Projects", proj, "Repos", repo, "Commits"]
|
|
|
|
$(widgetFile "repo/commits")
|