safe output to terminal for calckey inprogress and lookupkey
authorJoey Hess <joeyh@joeyh.name>
Wed, 12 Apr 2023 18:03:44 +0000 (14:03 -0400)
committerJoey Hess <joeyh@joeyh.name>
Wed, 12 Apr 2023 18:03:44 +0000 (14:03 -0400)
These are quite low-level, but still there is no point in displaying
escape sequences that have been embedded in a key to the terminal.

I think these are the only remaining commands that didn't use safe
output, except for cases where git-annex is speaking a protocol to
itself.

Sponsored-by: Kevin Mueller on Patreon
CHANGELOG
Command/CalcKey.hs
Command/Inprogress.hs
Command/LookupKey.hs

index 649f74f4d40c4d878d4f205b71470d1cddb94dbd..5a5581ce4dec217576caebe6a616ca59c2405308 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -6,7 +6,9 @@ git-annex (10.20230408) UNRELEASED; urgency=medium
     characters as-is in filenames.
   * Control characters in non-filename data coming from the repository or
     other possible untrusted sources are filtered out of the display of many
-    commands.
+    commands. When the command output is intended for use in scripting,
+    control characters are only filtered out when displaying to the
+    terminal.
   * find, findkeys, examinekey: When outputting to a terminal and --format
     is not used, quote unusual characters.
     (Similar to the behavior of GNU find.)
index 4003f8ce43ad6ef334b4f720d9114553ba0a3ccf..44aa69b59d98edc1d62afc34acda72c70a0a0a72 100644 (file)
@@ -11,6 +11,8 @@ import Command
 import Backend (genKey, defaultBackend)
 import Types.KeySource
 import Utility.Metered
+import Utility.Terminal
+import Utility.SafeOutput
 
 cmd :: Command
 cmd = noCommit $ noMessages $ dontCheck repoExists $
@@ -23,7 +25,9 @@ cmd = noCommit $ noMessages $ dontCheck repoExists $
 run :: () -> SeekInput -> String -> Annex Bool
 run _ _ file = tryNonAsync (genKey ks nullMeterUpdate =<< defaultBackend) >>= \case
        Right (k, _) -> do
-               liftIO $ putStrLn $ serializeKey k
+               IsTerminal isterminal <- liftIO $ checkIsTerminal stdout
+               let sk = serializeKey k
+               liftIO $ putStrLn $ if isterminal then safeOutput sk else sk
                return True
        Left _err -> return False
   where
index f5414e88154be51b4e2bf9761c9aa52ab1650cc6..6df55c6f035e41cc4332e9398e89ae4d6463e8c7 100644 (file)
@@ -9,6 +9,8 @@ module Command.Inprogress where
 
 import Command
 import Annex.Transfer
+import Utility.Terminal
+import Utility.SafeOutput
 
 import qualified Data.Set as S
 
@@ -32,14 +34,14 @@ seek o = do
        ts <- map (transferKey . fst) <$> getTransfers
        case keyOptions o of
                Just WantAllKeys ->
-                       forM_ ts $ commandAction . start'
+                       forM_ ts $ commandAction . (start' isterminal)
                Just (WantSpecificKey k)
-                       | k `elem` ts -> commandAction (start' k)
+                       | k `elem` ts -> commandAction (start' isterminal k)
                        | otherwise -> commandAction stop
                _ -> do
                        let s = S.fromList ts
                        let seeker = AnnexedFileSeeker
-                               { startAction = start s
+                               { startAction = start isterminal s
                                , checkContentPresent = Nothing
                                , usesLocationLog = False
                                }
@@ -48,14 +50,14 @@ seek o = do
   where
        ww = WarnUnmatchLsFiles
 
-start :: S.Set Key -> SeekInput -> RawFilePath -> Key -> CommandStart
-start s _si _file k
-       | S.member k s = start' k
+start :: IsTerminal -> S.Set Key -> SeekInput -> RawFilePath -> Key -> CommandStart
+start isterminal s _si _file k
+       | S.member k s = start' isterminal k
        | otherwise = stop
 
-start' :: Key -> CommandStart
-start' k = startingCustomOutput k $ do
+start' :: IsTerminal -> Key -> CommandStart
+start' (IsTerminal isterminal) k = startingCustomOutput k $ do
        tmpf <- fromRawFilePath <$> fromRepo (gitAnnexTmpObjectLocation k)
        whenM (liftIO $ doesFileExist tmpf) $
-               liftIO $ putStrLn tmpf
+               liftIO $ putStrLn (if isterminal then safeOutput tmpf else tmpf)
        next $ return True
index ce4d5a5500a4924f293cf5f0fc957dae339b3269..991bb5e4b945a0c23616607118dd2b73321c0a7b 100644 (file)
@@ -10,6 +10,8 @@ module Command.LookupKey where
 import Command
 import Annex.CatFile
 import qualified Git.LsFiles
+import Utility.Terminal
+import Utility.SafeOutput
 
 cmd :: Command
 cmd = notBareRepo $ noCommit $ noMessages $
@@ -23,7 +25,9 @@ run _ _ file = seekSingleGitFile file >>= \case
        Nothing -> return False
        Just file' -> catKeyFile file' >>= \case
                Just k  -> do
-                       liftIO $ putStrLn $ serializeKey k
+                       IsTerminal isterminal <- liftIO $ checkIsTerminal stdout
+                       let sk = serializeKey k
+                       liftIO $ putStrLn $ if isterminal then safeOutput sk else sk
                        return True
                Nothing -> return False