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 - stages, without catching errors. Useful if one command wants to run
- part of another command. -} - part of another command. -}
callCommandAction :: CommandStart -> CommandCleanup 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 where
start = stage $ maybe skip perform start = stage $ maybe skip perform
perform = stage $ maybe failure cleanup perform = stage $ maybe failure cleanup
cleanup = stage $ status cleanup = stage $ status
stage = (=<<) stage = (=<<)
skip = return True skip = return Nothing
failure = showEndFail >> return False failure = showEndFail >> return (Just False)
status r = showEndResult r >> return r status r = showEndResult r >> return (Just r)
{- Do concurrent output when that has been requested. -} {- Do concurrent output when that has been requested. -}
allowConcurrentOutput :: Annex a -> Annex a allowConcurrentOutput :: Annex a -> Annex a

View file

@ -54,3 +54,14 @@ batchInput parser a = do
batchInput parser a batchInput parser a
where where
parseerr s = error $ "Batch input parse failure: " ++ s 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 Annex.Version
import qualified Database.Keys import qualified Database.Keys
import Types.Key import Types.Key
import CmdLine.Batch
cmd :: Command cmd :: Command
cmd = notBareRepo $ withGlobalOptions (jobsOption : jsonOption : fileMatchingOptions) $ cmd = notBareRepo $ withGlobalOptions (jobsOption : jsonOption : fileMatchingOptions) $
@ -31,6 +32,7 @@ cmd = notBareRepo $ withGlobalOptions (jobsOption : jsonOption : fileMatchingOpt
data AddOptions = AddOptions data AddOptions = AddOptions
{ addThese :: CmdParams { addThese :: CmdParams
, includeDotFiles :: Bool , includeDotFiles :: Bool
, batchOption :: BatchMode
} }
optParser :: CmdParamsDesc -> Parser AddOptions optParser :: CmdParamsDesc -> Parser AddOptions
@ -40,6 +42,7 @@ optParser desc = AddOptions
( long "include-dotfiles" ( long "include-dotfiles"
<> help "don't skip dotfiles" <> help "don't skip dotfiles"
) )
<*> parseBatchOption
{- Add acts on both files not checked into git yet, and unlocked files. {- Add acts on both files not checked into git yet, and unlocked files.
- -
@ -47,15 +50,20 @@ optParser desc = AddOptions
seek :: AddOptions -> CommandSeek seek :: AddOptions -> CommandSeek
seek o = allowConcurrentOutput $ do seek o = allowConcurrentOutput $ do
matcher <- largeFilesMatcher 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 ( start file
, startSmall file , startSmall file
) )
go $ withFilesNotInGit (not $ includeDotFiles o) case batchOption o of
ifM (versionSupportsUnlockedPointers <||> isDirect) Batch -> batchInput Right $
( go withFilesMaybeModified batchCommandAction . gofile
, go withFilesOldUnlocked 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. -} {- Pass file off to git-add. -}
startSmall :: FilePath -> CommandStart 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, * whereis --json: Urls are now listed inside the remote that claims them,
rather than all together at the end. 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 * Force output to be line-buffered, even when it's not connected to the
terminal. This is particuarly important for commands with --batch terminal. This is particuarly important for commands with --batch
output, which was not always being flushed at an appropriate time. output, which was not always being flushed at an appropriate time.

View file

@ -8,8 +8,9 @@ git annex add `[path ...]`
# DESCRIPTION # DESCRIPTION
Adds files in the path to the annex. If no path is specified, adds Adds the specified files to the annex. If a directory is specified,
files from the current directory and below. 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 Files that are already checked into git and are unmodified, or that
git has been configured to ignore will be silently skipped. 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 Enable JSON output. This is intended to be parsed by programs that use
git-annex. Each line of output is a JSON object. 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 # SEE ALSO
[[git-annex]](1) [[git-annex]](1)

View file

@ -1,3 +1,7 @@
should be extremely helpful when adding many files one at a time ;) should be extremely helpful when adding many files one at a time ;)
[[!meta author=yoh]] [[!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]]