Skip to content

Commit

Permalink
add Git.Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kfish committed Feb 21, 2010
1 parent c3d946c commit c112c16
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Git/Commit.hs
@@ -0,0 +1,63 @@
module Git.Commit (
Commit(..),
commitParse,
commitPretty
) where

import qualified Data.ByteString.Lazy.Char8 as C
import Data.Digest.Pure.SHA (Digest, sha1, showDigest)

type Author = String

type Date = String

data Commit = Commit {
commitParent :: C.ByteString, --Digest,
commitAuthor :: C.ByteString, --Person,
commitAuthorDate :: C.ByteString, --Date,
commitCommitter :: C.ByteString, --Person,
commitCommitterDate :: C.ByteString, --Date,
commitMessage :: C.ByteString
}

------------------------------------------------------------
-- commitParse
--

e = C.empty

defCommit = Commit e e e e e e

commitParse bs = commitParseLines defCommit (C.lines bs)

commitParseLines c [] = c

commitParseLines c (l:ls)
| C.null l = c{commitMessage = C.unlines ls}
| otherwise = commitParseLines (commitModLine c l) ls

commitModLine c l = commitMod c (C.unpack hd) bdy
where (hd, bdy) = C.span (/= ' ') l

commitMod c hd bdy
| hd == "commit" = c
| hd == "parent" = c{commitParent = bdy}
| hd == "author" = c{commitAuthor = pName, commitAuthorDate = t}
| hd == "committer" = c{commitCommitter = pName, commitCommitterDate = t}
| otherwise = c
where
(pTZ:pTime:pAs) = reverse $ C.words bdy
pName = C.unwords $ reverse pAs
t = C.unwords [pTime, pTZ]

------------------------------------------------------------
-- commitPretty
--

commitPretty (Commit p a ad c cd m) =
C.unlines [
C.concat [(C.pack "Author: "), a],
C.concat [(C.pack "Date: "), ad],
C.empty,
m
]
5 changes: 5 additions & 0 deletions ght.cabal
Expand Up @@ -10,6 +10,11 @@ Stability: experimental
Build-Type: Simple
Cabal-Version: >= 1.2

------------------------------------------------------------
library
Build-Depends: base, data-default, bytestring, SHA
Exposed-Modules: Git.Commit

------------------------------------------------------------
-- ght tool
--
Expand Down

0 comments on commit c112c16

Please sign in to comment.