module Darcs.Patch.Info
( PatchInfo(..)
, rawPatchInfo
, patchinfo
, invertName
, addJunk
, makePatchname
, readPatchInfo
, justName
, justAuthor
, justLog
, displayPatchInfo
, toXml
, toXmlShort
, piDate
, setPiDate
, piDateString
, piName
, piRename
, piAuthor
, piTag
, piLog
, showPatchInfo
, isTag
, escapeXML
, validDate
, validLog
, validAuthor
, validDatePS
, validLogPS
, validAuthorPS
) where
import Prelude ( (^) )
import Darcs.Prelude
import Data.Char ( isAscii )
import System.Random ( randomRIO )
import Numeric ( showHex )
import Control.Monad ( when, unless, void )
import Darcs.Util.ByteString
( decodeLocale
, packStringToUTF8
, unlinesPS
, unpackPSFromUTF8
)
import qualified Darcs.Patch.ReadMonads as RM ( take )
import Darcs.Patch.ReadMonads as RM ( skipSpace, char,
takeTill, anyChar, ParserM,
option,
takeTillChar,
linesStartingWithEndingWith)
import Darcs.Patch.Show ( ShowPatchFor(..) )
import qualified Data.ByteString as B (length, splitAt, null
,isPrefixOf, tail, concat
,empty, head, cons, append
,ByteString )
import qualified Data.ByteString.Char8 as BC
( index, head, notElem, all, unpack, pack )
import Data.List( isPrefixOf )
import Darcs.Util.Printer ( Doc, packedString,
empty, ($$), (<>), (<+>), vcat, text, cyanText, blueText, prefix )
import Darcs.Util.IsoDate ( readUTCDate )
import System.Time ( CalendarTime, calendarTimeToString, toClockTime,
toCalendarTime )
import System.IO.Unsafe ( unsafePerformIO )
import Darcs.Util.Hash ( sha1PS, SHA1 )
import Darcs.Util.Prompt ( promptYorn )
import Darcs.Util.Show ( appPrec )
data PatchInfo = PatchInfo { _piDate :: !B.ByteString
, _piName :: !B.ByteString
, _piAuthor :: !B.ByteString
, _piLog :: ![B.ByteString]
, isInverted :: !Bool
}
deriving (Eq,Ord)
instance Show PatchInfo where
showsPrec d (PatchInfo date name author log inverted) =
showParen (d > appPrec) $
showString "rawPatchInfo " . showsPrec (appPrec + 1) date .
showString " " . showsPrec (appPrec + 1) name .
showString " " . showsPrec (appPrec + 1) author .
showString " " . showsPrec (appPrec + 1) log .
showString " " . showsPrec (appPrec + 1) inverted
validDate :: String -> Bool
validDate = all validCharForDate
validDatePS :: B.ByteString -> Bool
validDatePS = BC.all validCharForDate
validCharForDate :: Char -> Bool
validCharForDate c = isAscii c && c /= '\n' && c /= ']'
validLog :: String -> Bool
validLog = notElem '\n'
validLogPS :: B.ByteString -> Bool
validLogPS = BC.notElem '\n'
validAuthor :: String -> Bool
validAuthor = notElem '*'
validAuthorPS :: B.ByteString -> Bool
validAuthorPS = BC.notElem '*'
rawPatchInfo :: String -> String -> String -> [String] -> Bool -> PatchInfo
rawPatchInfo date name author log inverted =
PatchInfo { _piDate = BC.pack $ validateDate date
, _piName = packStringToUTF8 $ validateName name
, _piAuthor = packStringToUTF8 $ validateAuthor author
, _piLog = map (packStringToUTF8 . validateLog) log
, isInverted = inverted }
where
validateAuthor = validate validAuthor "author"
validateName = validate validLog "patch name"
validateLog = validate validLog "log line"
validateDate = validate validDate "date"
validate test meta x =
if test x then x else error (unwords ["invalid",meta,show x])
patchinfo :: String -> String -> String -> [String] -> IO PatchInfo
patchinfo date name author log =
addJunk $ rawPatchInfo date name author log False
addJunk :: PatchInfo -> IO PatchInfo
addJunk pinf =
do x <- randomRIO (0,2^(128 ::Integer) :: Integer)
when (_piLog pinf /= ignoreJunk (_piLog pinf)) $
do putStrLn $ "Lines beginning with 'Ignore-this: ' " ++
"will not be shown when displaying a patch."
confirmed <- promptYorn "Proceed? "
unless confirmed $ fail "User cancelled because of Ignore-this."
return $ pinf { _piLog = BC.pack (head ignored++showHex x ""):
_piLog pinf }
ignored :: [String]
ignored = ["Ignore-this: "]
ignoreJunk :: [B.ByteString] -> [B.ByteString]
ignoreJunk = filter isnt_ignored
where isnt_ignored x = doesnt_start_with x (map BC.pack ignored)
doesnt_start_with x ys = not $ any (`B.isPrefixOf` x) ys
invertName :: PatchInfo -> PatchInfo
invertName pi = pi { isInverted = not (isInverted pi) }
justName :: PatchInfo -> String
justName pinf = if isInverted pinf then "UNDO: " ++ nameString
else nameString
where nameString = metadataToString (_piName pinf)
justAuthor :: PatchInfo -> String
justAuthor = metadataToString . _piAuthor
justLog :: PatchInfo -> String
justLog = unlines . map BC.unpack . _piLog
displayPatchInfo :: PatchInfo -> Doc
displayPatchInfo pi =
cyanText "patch " <> cyanText (show $ makePatchname pi)
$$ text "Author: " <> text (piAuthor pi)
$$ text "Date: " <> text (friendlyD $ _piDate pi)
$$ hfn (piName pi)
$$ vcat (map ((text " " <>) . text) (piLog pi))
where hfn x = case piTag pi of
Nothing -> inverted <+> text x
Just t -> text " tagged" <+> text t
inverted = if isInverted pi then text " UNDO:" else text " *"
piName :: PatchInfo -> String
piName = metadataToString . _piName
piRename :: PatchInfo -> String -> PatchInfo
piRename x n = x { _piName = packStringToUTF8 n }
piAuthor :: PatchInfo -> String
piAuthor = metadataToString . _piAuthor
isTag :: PatchInfo -> Bool
isTag pinfo = "TAG " `isPrefixOf` justName pinfo
readPatchDate :: B.ByteString -> CalendarTime
readPatchDate = readUTCDate . BC.unpack
piDate :: PatchInfo -> CalendarTime
piDate = readPatchDate . _piDate
piDateString :: PatchInfo -> String
piDateString = BC.unpack . _piDate
setPiDate :: String -> PatchInfo -> PatchInfo
setPiDate date pi = pi { _piDate = BC.pack date }
piLog :: PatchInfo -> [String]
piLog = map metadataToString . ignoreJunk . _piLog
piTag :: PatchInfo -> Maybe String
piTag pinf =
if l == t
then Just $ metadataToString r
else Nothing
where (l, r) = B.splitAt (B.length t) (_piName pinf)
t = BC.pack "TAG "
metadataToString :: B.ByteString -> String
metadataToString bs | '\xfffd' `notElem` bsUtf8 = bsUtf8
| otherwise = decodeLocale bs
where bsUtf8 = unpackPSFromUTF8 bs
friendlyD :: B.ByteString -> String
friendlyD d = unsafePerformIO $ do
ct <- toCalendarTime $ toClockTime $ readPatchDate d
return $ calendarTimeToString ct
toXml :: PatchInfo -> Doc
toXml = toXml' True
toXmlShort :: PatchInfo -> Doc
toXmlShort = toXml' False
toXml' :: Bool -> PatchInfo -> Doc
toXml' includeComments pi =
text "<patch"
<+> text "author='" <> escapeXMLByteString (_piAuthor pi) <> text "'"
<+> text "date='" <> escapeXMLByteString (_piDate pi) <> text "'"
<+> text "local_date='" <> escapeXML (friendlyD $ _piDate pi) <> text "'"
<+> text "inverted='" <> text (show $ isInverted pi) <> text "'"
<+> text "hash='" <> text (show $ makePatchname pi) <> text "'>"
$$ indent abstract
$$ text "</patch>"
where
indent = prefix " "
name = text "<name>" <> escapeXMLByteString (_piName pi) <> text "</name>"
abstract | includeComments = name $$ commentsAsXml (_piLog pi)
| otherwise = name
commentsAsXml :: [B.ByteString] -> Doc
commentsAsXml comments
| B.length comments' > 0 = text "<comment>"
<> escapeXMLByteString comments'
<> text "</comment>"
| otherwise = empty
where comments' = unlinesPS comments
escapeXML :: String -> Doc
escapeXML = text . strReplace '\'' "'" . strReplace '"' """ .
strReplace '>' ">" . strReplace '<' "<" . strReplace '&' "&"
escapeXMLByteString :: B.ByteString -> Doc
escapeXMLByteString = packedString . bstrReplace '\'' "'"
. bstrReplace '"' """
. bstrReplace '>' ">"
. bstrReplace '<' "<"
. bstrReplace '&' "&"
strReplace :: Char -> String -> String -> String
strReplace _ _ [] = []
strReplace x y (z:zs)
| x == z = y ++ strReplace x y zs
| otherwise = z : strReplace x y zs
bstrReplace :: Char -> String -> B.ByteString -> B.ByteString
bstrReplace c s bs | B.null bs = B.empty
| otherwise = if BC.head bs == c
then B.append (BC.pack s)
(bstrReplace c s (B.tail bs))
else B.cons (B.head bs)
(bstrReplace c s (B.tail bs))
makePatchname :: PatchInfo -> SHA1
makePatchname pi = sha1PS sha1_me
where b2ps True = BC.pack "t"
b2ps False = BC.pack "f"
sha1_me = B.concat [_piName pi,
_piAuthor pi,
_piDate pi,
B.concat $ _piLog pi,
b2ps $ isInverted pi]
showPatchInfo :: ShowPatchFor -> PatchInfo -> Doc
showPatchInfo ForDisplay = displayPatchInfo
showPatchInfo ForStorage = storePatchInfo
storePatchInfo :: PatchInfo -> Doc
storePatchInfo pi =
blueText "[" <> packedString (_piName pi)
$$ packedString (_piAuthor pi) <> text inverted <> packedString (_piDate pi)
<> myunlines (_piLog pi) <> blueText "] "
where inverted = if isInverted pi then "*-" else "**"
myunlines [] = empty
myunlines xs =
foldr (\s -> ((text "\n " <> packedString s) <>)) (text "\n") xs
readPatchInfo :: ParserM m => m PatchInfo
readPatchInfo = do
skipSpace
char '['
name <- takeTillChar '\n'
_ <- anyChar
author <- takeTillChar '*'
s2 <- RM.take 2
ct <- takeTill (\c->c==']'||c=='\n')
option () (void (char '\n'))
log <- linesStartingWithEndingWith ' ' ']'
return PatchInfo { _piDate = ct
, _piName = name
, _piAuthor = author
, _piLog = log
, isInverted = BC.index s2 1 /= '*'
}