From fdf02986cf2c7db74cc8ca8ebd2786922652ff9b Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 22 Dec 2011 22:03:18 -0400 Subject: find --json --- Command/Find.hs | 16 +++++++++------- Messages.hs | 10 +++++++++- Messages/JSON.hs | 9 ++++++++- debian/changelog | 3 ++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Command/Find.hs b/Command/Find.hs index 6050ff7bb..91386bbd0 100644 --- a/Command/Find.hs +++ b/Command/Find.hs @@ -28,15 +28,17 @@ start :: FilePath -> (Key, Backend Annex) -> CommandStart start file (key, _) = do -- only files inAnnex are shown, unless the user has requested -- others via a limit - whenM (liftM2 (||) (inAnnex key) limited) $ do - f <- Annex.getState Annex.format - case f of - Nothing -> liftIO $ putStrLn file - Just formatter -> liftIO $ putStr $ - Utility.Format.format formatter vars + whenM (liftM2 (||) (inAnnex key) limited) $ + unlessM (showFullJSON vars) $ do + f <- Annex.getState Annex.format + case f of + Nothing -> liftIO $ putStrLn file + Just formatter -> liftIO $ putStr $ + Utility.Format.format formatter $ + M.fromList vars stop where - vars = M.fromList + vars = [ ("file", file) , ("key", show key) , ("backend", keyBackendName key) diff --git a/Messages.hs b/Messages.hs index a7f14f485..1294e44f6 100644 --- a/Messages.hs +++ b/Messages.hs @@ -20,6 +20,7 @@ module Messages ( warning, indent, maybeShowJSON, + showFullJSON, showCustom, showHeader, showRaw, @@ -90,10 +91,17 @@ warning' w = do indent :: String -> String indent = join "\n" . map (\l -> " " ++ l) . lines -{- Shows a JSON value only when in json mode. -} +{- Shows a JSON fragment only when in json mode. -} maybeShowJSON :: JSON a => [(String, a)] -> Annex () maybeShowJSON v = handle (JSON.add v) q +{- Shows a complete JSON value, only when in json mode. -} +showFullJSON :: JSON a => [(String, a)] -> Annex Bool +showFullJSON v = Annex.getState Annex.output >>= liftIO . go + where + go Annex.JSONOutput = JSON.complete v >> return True + go _ = return False + {- Performs an action that outputs nonstandard/customized output, and - in JSON mode wraps its output in JSON.start and JSON.end, so it's - a complete JSON document. diff --git a/Messages/JSON.hs b/Messages/JSON.hs index a325ef130..f7a031e38 100644 --- a/Messages/JSON.hs +++ b/Messages/JSON.hs @@ -9,7 +9,8 @@ module Messages.JSON ( start, end, note, - add + add, + complete ) where import Text.JSON @@ -31,3 +32,9 @@ note s = add [("note", s)] add :: JSON a => [(String, a)] -> IO () add v = putStr $ Stream.add v + +complete :: JSON a => [(String, a)] -> IO () +complete v = putStr $ concat + [ Stream.start v + , Stream.end + ] diff --git a/debian/changelog b/debian/changelog index a27611f55..33d196fa3 100644 --- a/debian/changelog +++ b/debian/changelog @@ -10,8 +10,9 @@ git-annex (3.20111212) UNRELEASED; urgency=low * Add --include, which is the same as --not --exclude. * Can now be built with older git versions (before 1.7.7); the resulting binary should only be used with old git. - * Format strings can be specified using the new --find option, to control + * Format strings can be specified using the new --format option, to control what is output by git annex find. + * Support git annex find --json -- Joey Hess Mon, 12 Dec 2011 01:57:49 -0400 -- cgit v1.2.3