use xargs

This commit is contained in:
Joey Hess 2010-10-27 13:12:02 -04:00
parent 24ee4439d4
commit 5b4fa4aeca
2 changed files with 12 additions and 18 deletions

View file

@ -36,7 +36,7 @@ shutdown = do
if (q == GitQueue.empty) if (q == GitQueue.empty)
then return () then return ()
else do else do
liftIO $ putStrLn "Recording state in git..." verbose $ liftIO $ putStrLn "Recording state in git..."
liftIO $ GitQueue.run g q liftIO $ GitQueue.run g q
-- clean up any files left in the temp directory, but leave -- clean up any files left in the temp directory, but leave

View file

@ -1,4 +1,4 @@
{- git repository command queues {- git repository command queue
-} -}
module GitQueue ( module GitQueue (
@ -9,6 +9,9 @@ module GitQueue (
) where ) where
import qualified Data.Map as M import qualified Data.Map as M
import System.IO
import System.Cmd.Utils
import Data.String.Utils
import qualified GitRepo as Git import qualified GitRepo as Git
@ -45,20 +48,11 @@ run repo queue = do
- Complicated by commandline length limits. -} - Complicated by commandline length limits. -}
runAction :: Git.Repo -> Action -> [FilePath] -> IO () runAction :: Git.Repo -> Action -> [FilePath] -> IO ()
runAction repo action files = do runAction repo action files = do
xargs [] 0 files if (null files)
then return ()
else runxargs
where where
arg_max = 2048 -- TODO get better ARG_MAX runxargs = pOpen WriteToPipe "xargs"
maxlen = arg_max - cmdlen (["-0", "git", subcommand action] ++ (params action))
c = (subcommand action):(params action) feedxargs
cmdlen = (length "git") + feedxargs h = hPutStr h $ join "\0" files
(foldl (\a b -> a + b + 1) 1 $ map length c)
xargs collect _ [] = exec collect
xargs collect len (f:fs) = do
let len' = len + 1 + length f
if (len' >= maxlen)
then do
exec collect
xargs [f] (length f) fs
else xargs (f:collect) len' fs
exec [] = return ()
exec fs = Git.run repo $ c ++ fs