git-annex/Command/Log.hs

207 lines
5.9 KiB
Haskell
Raw Normal View History

{- git-annex command
-
- Copyright 2012 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU GPL version 3 or higher.
-}
{-# LANGUAGE CPP #-}
module Command.Log where
import qualified Data.Set as S
import qualified Data.Map as M
import qualified Data.ByteString.Lazy.Char8 as L
import Data.Char
import Data.Time.Clock.POSIX
import Data.Time
#if ! MIN_VERSION_time(1,5,0)
import System.Locale
#endif
import Command
import Logs
import qualified Logs.Presence
import Annex.CatFile
import qualified Annex.Branch
import qualified Git
import Git.Command
import qualified Remote
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
}
type Outputter = Bool -> POSIXTime -> [UUID] -> Annex ()
cmd :: Command
2015-07-13 14:44:51 +00:00
cmd = withGlobalOptions annexedMatchingOptions $
command "log" SectionQuery "shows location log"
2015-07-13 14:44:51 +00:00
paramPaths (seek <$$> optParser)
2012-01-06 21:24:03 +00:00
2015-07-13 14:44:51 +00:00
data LogOptions = LogOptions
{ logFiles :: CmdParams
, rawDateOption :: Bool
2015-07-13 14:44:51 +00:00
, gourceOption :: Bool
, passthruOptions :: [CommandParam]
}
2015-07-13 14:44:51 +00:00
optParser :: CmdParamsDesc -> Parser LogOptions
optParser desc = LogOptions
<$> cmdParams desc
<*> switch
( long "raw-date"
<> help "display seconds from unix epoch"
)
2015-07-13 14:44:51 +00:00
<*> switch
( long "gource"
<> help "format output for gource"
)
<*> (concat <$> many passthru)
2012-11-12 05:05:04 +00:00
where
2015-07-13 14:44:51 +00:00
passthru :: Parser [CommandParam]
passthru = datepassthru "since"
<|> datepassthru "after"
<|> datepassthru "until"
<|> datepassthru "before"
<|> (mkpassthru "max-count" <$> strOption
( long "max-count" <> metavar paramNumber
<> help "limit number of logs displayed"
))
datepassthru n = mkpassthru n <$> strOption
( long n <> metavar paramDate
<> help ("show log " ++ n ++ " date")
)
mkpassthru n v = [Param ("--" ++ n), Param v]
2015-07-13 14:44:51 +00:00
seek :: LogOptions -> CommandSeek
seek o = do
m <- Remote.uuidDescriptions
zone <- liftIO getCurrentTimeZone
2015-07-13 14:44:51 +00:00
withFilesInGit (whenAnnexed $ start m zone o) (logFiles o)
start
:: M.Map UUID String
-> TimeZone
2015-07-13 14:44:51 +00:00
-> LogOptions
-> FilePath
-> Key
-> CommandStart
2015-07-13 14:44:51 +00:00
start m zone o file key = do
(ls, cleanup) <- getLog key (passthruOptions o)
showLog output (readLog ls)
void $ liftIO cleanup
stop
2012-11-12 05:05:04 +00:00
where
output
| rawDateOption o = normalOutput lookupdescription file show
| gourceOption o = gourceOutput lookupdescription file
| otherwise = normalOutput lookupdescription file (showTimeStamp zone)
2012-11-12 05:05:04 +00:00
lookupdescription u = fromMaybe (fromUUID u) $ M.lookup u m
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)
sequence_ $ compareChanges outputter $ sets ++ [previous]
2012-11-12 05:05:04 +00:00
where
genesis = (0, S.empty)
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
normalOutput :: (UUID -> String) -> FilePath -> (POSIXTime -> String) -> Outputter
normalOutput lookupdescription file formattime present ts us =
liftIO $ mapM_ (putStrLn . format) us
2012-11-12 05:05:04 +00:00
where
time = formattime ts
2012-11-12 05:05:04 +00:00
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 =
liftIO $ mapM_ (putStrLn . intercalate "|" . format) us
2012-11-12 05:05:04 +00:00
where
time = takeWhile isDigit $ show ts
addel = if present then "A" else "M"
format u = [ time, lookupdescription u, addel, file ]
{- 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
- removed. -}
compareChanges :: Ord a => (Bool -> POSIXTime -> [a] -> b) -> [(POSIXTime, S.Set a)] -> [b]
compareChanges format changes = concatMap diff $ zip changes (drop 1 changes)
2012-11-12 05:05:04 +00:00
where
diff ((ts, new), (_, old)) =
[format True ts added, format False ts removed]
where
added = S.toList $ S.difference new old
removed = S.toList $ S.difference old new
{- 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. -}
getLog :: Key -> [CommandParam] -> Annex ([String], IO Bool)
2012-01-07 01:27:42 +00:00
getLog key os = do
Clean up handling of git directory and git worktree. Baked into the code was an assumption that a repository's git directory could be determined by adding ".git" to its work tree (or nothing for bare repos). That fails when core.worktree, or GIT_DIR and GIT_WORK_TREE are used to separate the two. This was attacked at the type level, by storing the gitdir and worktree separately, so Nothing for the worktree means a bare repo. A complication arose because we don't learn where a repository is bare until its configuration is read. So another Location type handles repositories that have not had their config read yet. I am not entirely happy with this being a Location type, rather than representing them entirely separate from the Git type. The new code is not worse than the old, but better types could enforce more safety. Added support for core.worktree. Overriding it with -c isn't supported because it's not really clear what to do if a git repo's config is read, is not bare, and is then overridden to bare. What is the right git directory in this case? I will worry about this if/when someone has a use case for overriding core.worktree with -c. (See Git.Config.updateLocation) Also removed and renamed some functions like gitDir and workTree that misused git's terminology. One minor regression is known: git annex add in a bare repository does not print a nice error message, but runs git ls-files in a way that fails earlier with a less nice error message. This is because before --work-tree was always passed to git commands, even in a bare repo, while now it's not.
2012-05-18 20:38:26 +00:00
top <- fromRepo Git.repoPath
p <- liftIO $ relPathCwdToFile top
config <- Annex.getGitConfig
let logfile = p </> locationLogFile config key
inRepo $ pipeNullSplit $
[ Param "log"
, Param "-z"
, Param "--pretty=format:%ct"
, Param "--raw"
, Param "--abbrev=40"
, Param "--remove-empty"
2012-01-07 01:27:42 +00:00
] ++ os ++
[ Param $ Git.fromRef 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)
2012-11-12 05:05:04 +00:00
where
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)
2012-10-18 04:45:22 +00:00
parseRaw l = go $ words l
2012-11-12 05:05:04 +00:00
where
go (_:_:oldsha:newsha:_) = (Git.Ref oldsha, Git.Ref newsha)
go _ = error $ "unable to parse git log output: " ++ l
parseTimeStamp :: String -> POSIXTime
parseTimeStamp = utcTimeToPOSIXSeconds . fromMaybe (error "bad timestamp") .
2015-05-10 19:54:58 +00:00
#if MIN_VERSION_time(1,5,0)
parseTimeM True defaultTimeLocale "%s"
#else
parseTime defaultTimeLocale "%s"
2015-05-10 19:54:58 +00:00
#endif
2012-01-07 03:43:18 +00:00
showTimeStamp :: TimeZone -> POSIXTime -> String
showTimeStamp zone = formatTime defaultTimeLocale rfc822DateFormat
. utcToZonedTime zone . posixSecondsToUTCTime