git-annex/Command/Log.hs

126 lines
3.4 KiB
Haskell
Raw Normal View History

{- git-annex command
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.Log where
import qualified Data.Set as S
import qualified Data.ByteString.Lazy.Char8 as L
import Data.Time.Clock.POSIX
import Data.Time
import System.Locale
import Data.Char
import Common.Annex
import Command
import qualified Logs.Location
import qualified Logs.Presence
import Annex.CatFile
import qualified Annex.Branch
import qualified Git
import Git.Command
import qualified Remote
2012-01-06 21:24:03 +00:00
import qualified Option
2012-01-07 01:27:42 +00:00
import qualified Annex
2012-01-06 22:54:48 +00:00
data RefChange = RefChange
{ changetime :: POSIXTime
, oldref :: Git.Ref
, newref :: Git.Ref
}
def :: [Command]
2012-01-07 01:27:42 +00:00
def = [withOptions options $
2012-01-06 21:24:03 +00:00
command "log" paramPaths seek "shows location log"]
2012-01-07 01:27:42 +00:00
options :: [Option]
options =
2012-01-07 01:48:30 +00:00
[ Option.field [] "since" paramDate "show log since date"
, Option.field [] "after" paramDate "show log after date"
, Option.field [] "until" paramDate "show log until date"
2012-01-07 01:27:42 +00:00
, Option.field [] "before" paramDate "show log before date"
, Option.field ['n'] "max-count" paramNumber "limit number of logs displayed"
]
2012-01-06 21:48:02 +00:00
seek :: [CommandSeek]
2012-01-07 01:27:42 +00:00
seek = [withValue (concat <$> mapM getoption options) $ \os ->
withFilesInGit $ whenAnnexed $ start os]
where
getoption o = maybe [] (use o) <$>
Annex.getField (Option.name o)
use o v = [Param ("--" ++ Option.name o), Param v]
2012-01-07 01:27:42 +00:00
start :: [CommandParam] -> FilePath -> (Key, Backend) -> CommandStart
start os file (key, _) = do
showLog file =<< readLog <$> getLog key os
stop
2012-01-06 22:54:48 +00:00
showLog :: FilePath -> [RefChange] -> Annex ()
showLog file ps = do
2012-01-06 20:24:40 +00:00
zone <- liftIO getCurrentTimeZone
2012-01-06 22:54:48 +00:00
sets <- mapM (getset newref) ps
previous <- maybe (return genesis) (getset oldref) (lastMaybe ps)
2012-01-06 21:24:03 +00:00
mapM_ (diff zone) $ zip sets (drop 1 sets ++ [previous])
where
2012-01-06 21:24:03 +00:00
genesis = (0, S.empty)
2012-01-06 22:54:48 +00:00
getset select change = do
s <- S.fromList <$> get (select change)
return (changetime change, s)
get ref = map toUUID . Logs.Presence.getLog . L.unpack <$>
catObject ref
2012-01-06 20:24:40 +00:00
diff zone ((ts, new), (_, old)) = do
let time = show $ utcToLocalTime zone $
posixSecondsToUTCTime ts
output time True added
output time False removed
where
2012-01-06 20:24:40 +00:00
added = S.difference new old
removed = S.difference old new
output time present s = do
rs <- map (dropWhile isSpace) . lines <$>
Remote.prettyPrintUUIDs "log" (S.toList s)
liftIO $ mapM_ (putStrLn . format) rs
where
2012-01-06 22:54:48 +00:00
addel = if present then "+" else "-"
format r = unwords
2012-01-06 22:54:48 +00:00
[ addel, time, file, "|", r ]
2012-01-06 21:24:03 +00:00
getLog :: Key -> [CommandParam] -> Annex [String]
2012-01-07 01:27:42 +00:00
getLog key os = do
top <- fromRepo Git.workTree
p <- liftIO $ relPathCwdToFile top
let logfile = p </> Logs.Location.logFile key
2012-01-06 21:24:03 +00:00
inRepo $ pipeNullSplit $
[ Params "log -z --pretty=format:%ct --raw --abbrev=40"
2012-01-07 01:27:42 +00:00
] ++ os ++
2012-01-06 21:24:03 +00:00
[ Param $ show Annex.Branch.fullname
, Param "--"
, Param logfile
]
2012-01-06 22:54:48 +00:00
readLog :: [String] -> [RefChange]
2012-01-06 21:24:03 +00:00
readLog = mapMaybe (parse . lines)
where
2012-01-06 22:54:48 +00:00
parse (ts:raw:[]) = let (old, new) = parseRaw raw in
Just RefChange
{ changetime = parseTimeStamp ts
, oldref = old
, newref = new
}
parse _ = Nothing
-- Parses something like ":100644 100644 oldsha newsha M"
2012-01-06 21:24:03 +00:00
parseRaw :: String -> (Git.Ref, Git.Ref)
parseRaw l = (Git.Ref oldsha, Git.Ref newsha)
where
ws = words l
oldsha = ws !! 2
newsha = ws !! 3
parseTimeStamp :: String -> POSIXTime
parseTimeStamp = utcTimeToPOSIXSeconds . fromMaybe (error "bad timestamp") .
parseTime defaultTimeLocale "%s"