* assistant --autostop: Avoid crashing when ~/.config/git-annex/autostart
lists a directory that it cannot chdir to.
* Honor --force option when operating on a local git remote.
- * Added a --json-exceptions option, which makes some exceptions be output
- in json.
-- Joey Hess <id@joeyh.name> Sat, 08 Apr 2023 13:57:18 -0400
<> help "include error messages in JSON"
<> hidden
)
- , annexFlag (setAnnexState $ Annex.setOutput (JSONOutput jsonexceptionsoptions))
- ( long "json-exceptions"
- <> help "include exceptions in JSON"
- <> hidden
- )
]
where
stdjsonoptions = JSONOptions
{ jsonProgress = False
, jsonErrorMessages = False
- , jsonExceptions = False
}
jsonerrormessagesoptions = stdjsonoptions { jsonErrorMessages = True }
- jsonexceptionsoptions = stdjsonoptions { jsonExceptions = True }
jsonProgressOption :: [AnnexOption]
jsonProgressOption =
jsonoptions = JSONOptions
{ jsonProgress = True
, jsonErrorMessages = False
- , jsonExceptions = False
}
-- Note that a command that adds this option should wrap its seek
let p' = toRawFilePath p
relf <- liftIO $ relPathCwdToFile p'
ifM (not <$> (exists p' <||> hidden currbranch relf))
- ( prob FileNotFound (QuotedPath (toRawFilePath p) <> " not found")
+ ( prob (QuotedPath (toRawFilePath p) <> " not found")
, ifM (viasymlink stopattop (upFrom relf))
- ( prob FileBeyondSymbolicLink (QuotedPath (toRawFilePath p) <> " is beyond a symbolic link")
+ ( prob (QuotedPath (toRawFilePath p) <> " is beyond a symbolic link")
, return True
)
)
<$> catObjectMetaDataHidden f currbranch
| otherwise = return False
- prob eid msg = do
- showException False eid msg
+ prob msg = do
+ toplevelWarning False msg
Annex.incError
return False
showEndFail,
showEndResult,
endResult,
- ExceptionId(..),
- showException,
toplevelWarning,
warning,
earlyWarning,
endResult True = "ok"
endResult False = "failed"
-{- Unique ids for different exceptions. Do not change the constructors. -}
-data ExceptionId
- = FileNotFound
- | FileBeyondSymbolicLink
- deriving (Show)
-
-{- Displays an message that is not associated with any file being
- - processed. -}
-showException :: Bool -> ExceptionId -> StringContainingQuotedPath -> Annex ()
-showException makeway eid msg = do
- when makeway $
- outputMessage JSON.none id "\n"
- outputException (show eid) (mentionedfile msg)
- ("git-annex: " <> msg <> "\n")
- where
- mentionedfile (QuotedPath p) = Just p
- mentionedfile (a :+: b) = mentionedfile a <|> mentionedfile b
- mentionedfile (UnquotedString _) = Nothing
- mentionedfile (UnquotedByteString _) = Nothing
-
toplevelWarning :: Bool -> StringContainingQuotedPath -> Annex ()
toplevelWarning makeway s = warning' makeway id ("git-annex: " <> s)
qp <- coreQuotePath <$> Annex.getGitConfig
liftIO $ outputSerialized h $ OutputError $ decodeBS $
consolewhitespacef $ quote qp msg
- _ -> outputError' consolewhitespacef msg s
-
-outputError' :: (S.ByteString -> S.ByteString) -> StringContainingQuotedPath -> MessageState -> Annex ()
-outputError' consolewhitespacef msg s
- | concurrentOutputEnabled s = do
- qp <- coreQuotePath <$> Annex.getGitConfig
- concurrentMessage s True (decodeBS $ consolewhitespacef $ quote qp msg) go
- | otherwise = go
+ _
+ | concurrentOutputEnabled s -> do
+ qp <- coreQuotePath <$> Annex.getGitConfig
+ concurrentMessage s True (decodeBS $ consolewhitespacef $ quote qp msg) go
+ | otherwise -> go
where
go = do
qp <- coreQuotePath <$> Annex.getGitConfig
liftIO $ S.hPutStr stderr (consolewhitespacef $ quote qp msg)
liftIO $ hFlush stderr
-outputException :: String -> Maybe RawFilePath -> StringContainingQuotedPath -> Annex ()
-outputException eid mfile msg = withMessageState $ \s -> case outputType s of
- (JSONOutput jsonoptions) | jsonExceptions jsonoptions ->
- liftIO $ flushed $ JSON.emit $
- JSON.exceptionObject eid (decodeBS (noquote msg)) mfile
- (SerializedOutput h _) -> do
- qp <- coreQuotePath <$> Annex.getGitConfig
- liftIO $ outputSerialized h $ OutputException eid mfile $ decodeBS $
- quote qp msg
- _ -> outputError' id msg s
-
q :: Monad m => m ()
q = noop
{- git-annex command-line JSON output and input
-
- - Copyright 2011-2023 Joey Hess <id@joeyh.name>
+ - Copyright 2011-2021 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
ObjectMap(..),
JSONActionItem(..),
AddJSONActionItemFields(..),
- exceptionObject,
) where
import Control.Applicative
instance ToJSON' a => ToJSON' (AddJSONActionItemFields a) where
toJSON' (AddJSONActionItemFields a) = object [ ("fields", toJSON' a) ]
-
-exceptionObject :: String -> String -> Maybe RawFilePath -> Object
-exceptionObject eid msg mfile = case o of
- Object o' -> o'
- _ -> error "internal"
- where
- o = object
- [ "exception" .= toJSON' eid
- , "message" .= toJSON' msg
- , "file" .= toJSON' (maybe "" decodeBS mfile)
- ]
Left (OutputError msg) -> do
runannex $ outputError id $ UnquotedString msg
loop st
- Left (OutputException eid mfile msg) -> do
- runannex $ outputException eid mfile $ UnquotedString msg
- loop st
Left (JSONObject b) -> do
runannex $ withMessageState $ \s -> case outputType s of
JSONOutput _ -> liftIO $ flushed $ JSON.emit' b
import qualified Utility.Aeson as Aeson
import Utility.Metered
-import Utility.RawFilePath
import Control.Concurrent
import System.Console.Regions (ConsoleRegion)
data JSONOptions = JSONOptions
{ jsonProgress :: Bool
, jsonErrorMessages :: Bool
- , jsonExceptions :: Bool
}
deriving (Show)
adjustOutputType (JSONOutput old) (JSONOutput new) = JSONOutput $ JSONOptions
{ jsonProgress = jsonProgress old || jsonProgress new
, jsonErrorMessages = jsonErrorMessages old || jsonErrorMessages new
- , jsonExceptions = jsonExceptions old || jsonExceptions new
}
adjustOutputType _old new = new
data SerializedOutput
= OutputMessage S.ByteString
| OutputError String
- | OutputException String (Maybe RawFilePath) String
| BeginProgressMeter
| UpdateProgressMeterTotalSize TotalSize
| UpdateProgressMeter BytesProcessed
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-progress`
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* `--batch`
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to an url
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-progress`
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing an url.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular url.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* `--backend`
* `-z`
Makes batch input be delimited by nulls instead of the usual newlines.
+
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-progress`
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a key
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a key.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular key.
-
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a key
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a key.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular key.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* `--migrate-to-backend=backend`
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-progress`
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
- Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ Output the list of files in JSON format.
-* `--json-error-messages`
-
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
+ This is intended to be parsed by programs that use
+ git-annex. Each line of output is a JSON object.
-* `--json-exceptions`
+* `--json-error-messages`
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* `--batch`
* `--json`
- Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a key
- being processed.
+ Output the list of keys in JSON format.
-* `--json-error-messages`
-
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a key.
+ This is intended to be parsed by programs that use
+ git-annex. Each line of output is a JSON object.
-* `--json-exceptions`
+* `--json-error-messages`
- Output additional JSON objects for some exceptions that are not
- associated with a particular key.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* `--quiet`
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-progress`
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-progress`
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a item
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a item.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular item.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* `--bytes`
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
{"command":"metadata","file":"foo","key":"...","author":["bar"],...,"note":"...","success":true}
-* `--json-error-messages`
-
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
-
* `--json-error-messages`
Messages that would normally be output to standard error are included in
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-progress`
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-progress`
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a key
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a key.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular key.
-
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
-
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a key
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a key.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular key.
-
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* Also the [[git-annex-common-options]](1) can be used.
* `--json`
Enable JSON output. This is intended to be parsed by programs that use
- git-annex. Each line of output is a JSON object corresponding to a file
- being processed.
+ git-annex. Each line of output is a JSON object.
* `--json-error-messages`
- Adds an "error-messages" field to the JSON that contains messages that
- would normally be output to the standard error when processing a file.
-
-* `--json-exceptions`
-
- Output additional JSON objects for some exceptions that are not
- associated with a particular file.
+ Messages that would normally be output to standard error are included in
+ the JSON instead.
* `--format=value`
So eg:
- {"exception":"UNIQUEID", "file":"foo", "message":"foo not found"}
+ {"exception":"UNIQUEID", "file":"foo", "error-messages":["foo not found"]}
That seems about right to me, and it future proofs git-annex to be able to
report other exceptions in json output later on.
+++ /dev/null
-[[!comment format=mdwn
- username="joey"
- subject="""comment 5"""
- date="2023-04-25T18:43:20Z"
- content="""
-I've implemented --json-exceptions:
-
- joey@darkstar:~/tmp/xxx>git-annex add dne --json-exceptions
- {"exception":"FileNotFound","file":"dne","message":"git-annex: dne not found\n"}
- add: 1 failed
-
-Note that when a command like `git-annex get` is run on a file that exists,
-but is not checked into git, `git ls-files` still displays its own error message,
-so --json-exceptions doesn't help with this:
-
- joey@darkstar:~/tmp/xxx>touch foo
- joey@darkstar:~/tmp/xxx>git-annex get foo --json-exceptions
- error: pathspec 'foo' did not match any file(s) known to git
- Did you forget to 'git add'?
- get: 1 failed
-
-Datalad will still have to parse the git output if it wants to know what file
-it failed on in that case.
-"""]]
--- /dev/null
+[[!comment format=mdwn
+ username="joey"
+ subject="""Re: comment 6"""
+ date="2023-04-25T21:10:21Z"
+ content="""
+Oh well spotted yarikoptic! I wish I had noticed your comment 2 hours ago,
+but I was head down implementing --json-exceptions.
+
+Ok, so `addurl` does `giveup "bad url"` and that does indeed result in json
+output that lacks a `key` and has `"file":null` as well.
+
+I'm sure that somewhere in `git-annex add`, it's possible for it to
+`giveup` with an error too. Oh of course... a device file!
+
+ joey@darkstar:/home/joey/tmp/xxx>git-annex add --force-small null --json --json-error-messages
+ {"command":"add","error-messages":["git-annex: null is not a regular file"],"file":"null","input":["null"],"note":"adding content to git repository","success":false}
+ add: 1 failed
+
+Other perhaps more likely cases where add can `giveup` include when it's unable
+to remove all write perms due to an xattr, and probably some permissions
+problems.
+
+So, json consumers of add already have to deal with the `key` being missing.
+
+Now, it might be that some git-annex commands don't `giveup`, so this would be
+a new complication for consumers of their json. But so would adding a `giveup`
+for any reason to a command, and I don't worry about that.
+
+While I've implemented --json-exceptions, I don't like the complexity, so this new
+information inclines me to rip it back out and instead handle the case of the
+nonexistant file like:
+
+ {"command":"add","exception":"UNIQUEID","file":"foo","input":["foo"],"error-messages":["foo not found"],"success":false}
+"""]]