2012-01-06 19:40:04 +00:00
|
|
|
{- 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
|
2012-01-07 22:13:12 +00:00
|
|
|
import qualified Data.Map as M
|
2012-01-06 19:40:04 +00:00
|
|
|
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 19:40:04 +00:00
|
|
|
|
2012-01-06 22:54:48 +00:00
|
|
|
data RefChange = RefChange
|
|
|
|
{ changetime :: POSIXTime
|
|
|
|
, oldref :: Git.Ref
|
|
|
|
, newref :: Git.Ref
|
|
|
|
}
|
|
|
|
|
2012-01-07 22:13:12 +00:00
|
|
|
type Outputter = Bool -> POSIXTime -> [UUID] -> Annex ()
|
|
|
|
|
2012-01-06 19:40:04 +00:00
|
|
|
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]
|
2012-01-07 22:13:12 +00:00
|
|
|
options = passthruOptions ++ [gourceOption]
|
|
|
|
|
|
|
|
passthruOptions :: [Option]
|
|
|
|
passthruOptions = map odate ["since", "after", "until", "before"] ++
|
2012-01-07 01:51:39 +00:00
|
|
|
[ Option.field ['n'] "max-count" paramNumber
|
|
|
|
"limit number of logs displayed"
|
2012-01-07 01:27:42 +00:00
|
|
|
]
|
2012-01-07 01:51:39 +00:00
|
|
|
where
|
|
|
|
odate n = Option.field [] n paramDate $
|
|
|
|
"show log " ++ n ++ " date"
|
2012-01-06 21:48:02 +00:00
|
|
|
|
2012-01-07 22:13:12 +00:00
|
|
|
gourceOption :: Option
|
|
|
|
gourceOption = Option.flag [] "gource" "format output for gource"
|
|
|
|
|
2012-01-06 19:40:04 +00:00
|
|
|
seek :: [CommandSeek]
|
2012-02-16 04:41:30 +00:00
|
|
|
seek = [withValue Remote.uuidDescriptions $ \m ->
|
2012-01-07 22:13:12 +00:00
|
|
|
withValue (liftIO getCurrentTimeZone) $ \zone ->
|
|
|
|
withValue (concat <$> mapM getoption passthruOptions) $ \os ->
|
|
|
|
withFlag gourceOption $ \gource ->
|
|
|
|
withFilesInGit $ whenAnnexed $ start m zone os gource]
|
2012-01-07 01:27:42 +00:00
|
|
|
where
|
|
|
|
getoption o = maybe [] (use o) <$>
|
|
|
|
Annex.getField (Option.name o)
|
|
|
|
use o v = [Param ("--" ++ Option.name o), Param v]
|
2012-01-06 19:40:04 +00:00
|
|
|
|
2012-02-16 04:41:30 +00:00
|
|
|
start :: M.Map UUID String -> TimeZone -> [CommandParam] -> Bool ->
|
2012-01-07 22:13:12 +00:00
|
|
|
FilePath -> (Key, Backend) -> CommandStart
|
|
|
|
start m zone os gource file (key, _) = do
|
|
|
|
showLog output =<< readLog <$> getLog key os
|
2012-01-07 04:22:16 +00:00
|
|
|
liftIO Git.Command.reap
|
2012-01-06 19:40:04 +00:00
|
|
|
stop
|
2012-01-07 22:13:12 +00:00
|
|
|
where
|
|
|
|
output
|
|
|
|
| gource = gourceOutput lookupdescription file
|
|
|
|
| otherwise = normalOutput lookupdescription file zone
|
|
|
|
lookupdescription u = fromMaybe (fromUUID u) $ M.lookup u m
|
2012-01-06 19:40:04 +00:00
|
|
|
|
2012-01-07 22:13:12 +00:00
|
|
|
showLog :: Outputter -> [RefChange] -> Annex ()
|
|
|
|
showLog outputter ps = do
|
2012-01-06 22:54:48 +00:00
|
|
|
sets <- mapM (getset newref) ps
|
|
|
|
previous <- maybe (return genesis) (getset oldref) (lastMaybe ps)
|
2012-01-07 22:13:12 +00:00
|
|
|
sequence_ $ compareChanges outputter $ sets ++ [previous]
|
2012-01-06 19:40:04 +00:00
|
|
|
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)
|
2012-01-06 19:40:04 +00:00
|
|
|
get ref = map toUUID . Logs.Presence.getLog . L.unpack <$>
|
|
|
|
catObject ref
|
2012-01-07 22:13:12 +00:00
|
|
|
|
|
|
|
normalOutput :: (UUID -> String) -> FilePath -> TimeZone -> Outputter
|
2012-02-16 04:41:30 +00:00
|
|
|
normalOutput lookupdescription file zone present ts us =
|
2012-01-07 22:13:12 +00:00
|
|
|
liftIO $ mapM_ (putStrLn . format) us
|
|
|
|
where
|
|
|
|
time = showTimeStamp zone ts
|
|
|
|
addel = if present then "+" else "-"
|
|
|
|
format u = unwords [ addel, time, file, "|",
|
|
|
|
fromUUID u ++ " -- " ++ lookupdescription u ]
|
|
|
|
|
|
|
|
gourceOutput :: (UUID -> String) -> FilePath -> Outputter
|
2012-02-16 04:41:30 +00:00
|
|
|
gourceOutput lookupdescription file present ts us =
|
2012-01-07 22:13:12 +00:00
|
|
|
liftIO $ mapM_ (putStrLn . intercalate "|" . format) us
|
|
|
|
where
|
|
|
|
time = takeWhile isDigit $ show ts
|
|
|
|
addel = if present then "A" else "M"
|
|
|
|
format u = [ time, lookupdescription u, addel, file ]
|
2012-01-06 19:40:04 +00:00
|
|
|
|
2012-01-07 04:11:15 +00:00
|
|
|
{- Generates a display of the changes (which are ordered with newest first),
|
|
|
|
- by comparing each change with the previous change.
|
2012-01-07 04:45:01 +00:00
|
|
|
- Uses a formatter to generate a display of items that are added and
|
2012-01-07 04:11:15 +00:00
|
|
|
- removed. -}
|
2012-01-07 22:13:12 +00:00
|
|
|
compareChanges :: Ord a => (Bool -> POSIXTime -> [a] -> b) -> [(POSIXTime, S.Set a)] -> [b]
|
2012-01-07 04:11:15 +00:00
|
|
|
compareChanges format changes = concatMap diff $ zip changes (drop 1 changes)
|
|
|
|
where
|
|
|
|
diff ((ts, new), (_, old)) =
|
|
|
|
[format True ts added, format False ts removed]
|
|
|
|
where
|
2012-01-07 22:13:12 +00:00
|
|
|
added = S.toList $ S.difference new old
|
|
|
|
removed = S.toList $ S.difference old new
|
2012-01-07 04:11:15 +00:00
|
|
|
|
2012-01-07 04:15:01 +00:00
|
|
|
{- Gets the git log for a given location log file.
|
|
|
|
-
|
|
|
|
- This is complicated by git log using paths relative to the current
|
|
|
|
- directory, even when looking at files in a different branch. A wacky
|
|
|
|
- relative path to the log file has to be used.
|
|
|
|
-
|
|
|
|
- The --remove-empty is a significant optimisation. It relies on location
|
|
|
|
- log files never being deleted in normal operation. Letting git stop
|
|
|
|
- once the location log file is gone avoids it checking all the way back
|
|
|
|
- to commit 0 to see if it used to exist, so generally speeds things up a
|
|
|
|
- *lot* for newish files. -}
|
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
|
2012-01-06 19:40:04 +00:00
|
|
|
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 $
|
2012-01-06 19:40:04 +00:00
|
|
|
[ Params "log -z --pretty=format:%ct --raw --abbrev=40"
|
2012-01-07 04:15:01 +00:00
|
|
|
, Param "--remove-empty"
|
2012-01-07 01:27:42 +00:00
|
|
|
] ++ os ++
|
2012-01-06 21:24:03 +00:00
|
|
|
[ Param $ show Annex.Branch.fullname
|
2012-01-06 19:40:04 +00:00
|
|
|
, 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)
|
2012-01-06 19:40:04 +00:00
|
|
|
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
|
|
|
|
}
|
2012-01-06 19:40:04 +00:00
|
|
|
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
|
2012-01-06 19:40:04 +00:00
|
|
|
|
|
|
|
parseTimeStamp :: String -> POSIXTime
|
|
|
|
parseTimeStamp = utcTimeToPOSIXSeconds . fromMaybe (error "bad timestamp") .
|
|
|
|
parseTime defaultTimeLocale "%s"
|
2012-01-07 03:43:18 +00:00
|
|
|
|
|
|
|
showTimeStamp :: TimeZone -> POSIXTime -> String
|
|
|
|
showTimeStamp zone = show . utcToLocalTime zone . posixSecondsToUTCTime
|