add --batch

This commit is contained in:
Joey Hess 2016-01-19 17:46:46 -04:00
parent 5a781f4e8f
commit 7d0ece86f6
Failed to extract signature
6 changed files with 51 additions and 13 deletions

View file

@ -134,15 +134,20 @@ includeCommandAction a = account =<< tryIO (callCommandAction a)
- stages, without catching errors. Useful if one command wants to run
- part of another command. -}
callCommandAction :: CommandStart -> CommandCleanup
callCommandAction = start
callCommandAction = fromMaybe True <$$> callCommandAction'
{- Like callCommandAction, but returns Nothing when the command did not
- perform any action. -}
callCommandAction' :: CommandStart -> Annex (Maybe Bool)
callCommandAction' = start
where
start = stage $ maybe skip perform
perform = stage $ maybe failure cleanup
cleanup = stage $ status
stage = (=<<)
skip = return True
failure = showEndFail >> return False
status r = showEndResult r >> return r
skip = return Nothing
failure = showEndFail >> return (Just False)
status r = showEndResult r >> return (Just r)
{- Do concurrent output when that has been requested. -}
allowConcurrentOutput :: Annex a -> Annex a

View file

@ -54,3 +54,14 @@ batchInput parser a = do
batchInput parser a
where
parseerr s = error $ "Batch input parse failure: " ++ s
-- Runs a CommandStart in batch mode.
--
-- The batch mode user expects to read a line of output, and it's up to the
-- CommandStart to generate that output as it succeeds or fails to do its
-- job. However, if it stops without doing anything, it won't generate
-- any output, so in that case, batchBadInput is used to provide the caller
-- with an empty line.
batchCommandAction :: CommandStart -> Annex ()
batchCommandAction a = maybe (batchBadInput Batch) (const noop)
=<< callCommandAction' a

View file

@ -22,6 +22,7 @@ import Annex.FileMatcher
import Annex.Version
import qualified Database.Keys
import Types.Key
import CmdLine.Batch
cmd :: Command
cmd = notBareRepo $ withGlobalOptions (jobsOption : jsonOption : fileMatchingOptions) $
@ -31,6 +32,7 @@ cmd = notBareRepo $ withGlobalOptions (jobsOption : jsonOption : fileMatchingOpt
data AddOptions = AddOptions
{ addThese :: CmdParams
, includeDotFiles :: Bool
, batchOption :: BatchMode
}
optParser :: CmdParamsDesc -> Parser AddOptions
@ -40,6 +42,7 @@ optParser desc = AddOptions
( long "include-dotfiles"
<> help "don't skip dotfiles"
)
<*> parseBatchOption
{- Add acts on both files not checked into git yet, and unlocked files.
-
@ -47,15 +50,20 @@ optParser desc = AddOptions
seek :: AddOptions -> CommandSeek
seek o = allowConcurrentOutput $ do
matcher <- largeFilesMatcher
let go a = flip a (addThese o) $ \file -> ifM (checkFileMatcher matcher file <||> Annex.getState Annex.force)
let gofile file = ifM (checkFileMatcher matcher file <||> Annex.getState Annex.force)
( start file
, startSmall file
)
go $ withFilesNotInGit (not $ includeDotFiles o)
ifM (versionSupportsUnlockedPointers <||> isDirect)
( go withFilesMaybeModified
, go withFilesOldUnlocked
)
case batchOption o of
Batch -> batchInput Right $
batchCommandAction . gofile
NoBatch -> do
let go a = a gofile (addThese o)
go (withFilesNotInGit (not $ includeDotFiles o))
ifM (versionSupportsUnlockedPointers <||> isDirect)
( go withFilesMaybeModified
, go withFilesOldUnlocked
)
{- Pass file off to git-add. -}
startSmall :: FilePath -> CommandStart

2
debian/changelog vendored
View file

@ -2,7 +2,7 @@ git-annex (6.20160115) UNRELEASED; urgency=medium
* whereis --json: Urls are now listed inside the remote that claims them,
rather than all together at the end.
* info: Support --batch mode.
* info, add: Support --batch mode.
* Force output to be line-buffered, even when it's not connected to the
terminal. This is particuarly important for commands with --batch
output, which was not always being flushed at an appropriate time.

View file

@ -8,8 +8,9 @@ git annex add `[path ...]`
# DESCRIPTION
Adds files in the path to the annex. If no path is specified, adds
files from the current directory and below.
Adds the specified files to the annex. If a directory is specified,
acts on all files inside the directory and its subdirectories.
If no path is specified, adds files from the current directory and below.
Files that are already checked into git and are unmodified, or that
git has been configured to ignore will be silently skipped.
@ -59,6 +60,15 @@ annexed content, and other symlinks.
Enable JSON output. This is intended to be parsed by programs that use
git-annex. Each line of output is a JSON object.
* `--batch`
Enables batch mode, in which a file to add is read in a line from stdin,
the file is added, and repeat.
Note that if a file is skipped (due to not existing, being gitignored,
already being in git etc), an empty line will be output instead of the
normal output produced when adding a file.
# SEE ALSO
[[git-annex]](1)

View file

@ -1,3 +1,7 @@
should be extremely helpful when adding many files one at a time ;)
[[!meta author=yoh]]
> Implemented; made it not recurse into directories and output a blank line
> if it doesn't add the file, so there's aways 1 line of output for each
> input. [[done]] --[[Joey]]