factor out stagedFiles

This commit is contained in:
Joey Hess 2010-10-29 17:26:26 -04:00
parent 7c0777c60d
commit c9347693d7
2 changed files with 16 additions and 15 deletions

View file

@ -172,16 +172,8 @@ findWanted FilesMissing params repo = do
findWanted Description params _ = do findWanted Description params _ = do
return $ [unwords params] return $ [unwords params]
findWanted FilesToBeCommitted params repo = do findWanted FilesToBeCommitted params repo = do
files <- mapM gitcached params files <- mapM (Git.stagedFiles repo) params
return $ foldl (++) [] files return $ foldl (++) [] files
where
gitcached p = do
-- ask git for files staged for commit that
-- are being added, moved, or changed (but not deleted)
fs0 <- Git.pipeRead repo ["diff", "--cached",
"--name-only", "--diff-filter=ACMRT",
"-z", "HEAD", p]
return $ filter (not . null) $ split "\0" fs0
findWanted _ params _ = return params findWanted _ params _ = return params
{- Parses command line and returns two lists of actions to be {- Parses command line and returns two lists of actions to be

View file

@ -32,7 +32,8 @@ module GitRepo (
remotesAdd, remotesAdd,
repoRemoteName, repoRemoteName,
inRepo, inRepo,
notInRepo notInRepo,
stagedFiles
) where ) where
import Monad (when, unless) import Monad (when, unless)
@ -46,7 +47,7 @@ import System.Cmd.Utils
import System.IO import System.IO
import IO (bracket_) import IO (bracket_)
import Data.String.Utils import Data.String.Utils
import Data.Map as Map hiding (map, split) import qualified Data.Map as Map hiding (map, split)
import Network.URI import Network.URI
import Maybe import Maybe
import Char import Char
@ -60,7 +61,7 @@ data RepoLocation = Dir FilePath | Url URI
data Repo = Repo { data Repo = Repo {
location :: RepoLocation, location :: RepoLocation,
config :: Map String String, config :: Map.Map String String,
remotes :: [Repo], remotes :: [Repo],
-- remoteName holds the name used for this repo in remotes -- remoteName holds the name used for this repo in remotes
remoteName :: Maybe String remoteName :: Maybe String
@ -211,6 +212,14 @@ notInRepo repo location = do
s <- pipeRead repo ["ls-files", "--others", "--exclude-standard", location] s <- pipeRead repo ["ls-files", "--others", "--exclude-standard", location]
return $ lines s return $ lines s
{- Passed a location, returns a list of the files that are staged for
- commit that are being added, moved, or changed (but not deleted). -}
stagedFiles :: Repo -> FilePath -> IO [FilePath]
stagedFiles repo location = do
fs0 <- pipeRead repo ["diff", "--cached", "--name-only",
"--diff-filter=ACMRT", "-z", "HEAD", location]
return $ filter (not . null) $ split "\0" fs0
{- Runs git config and populates a repo with its config. -} {- Runs git config and populates a repo with its config. -}
configRead :: Repo -> IO Repo configRead :: Repo -> IO Repo
configRead repo@(Repo { location = Dir d }) = do configRead repo@(Repo { location = Dir d }) = do
@ -239,8 +248,8 @@ configTrue s = map toLower s == "true"
configRemotes :: Repo -> [Repo] configRemotes :: Repo -> [Repo]
configRemotes repo = map construct remotes configRemotes repo = map construct remotes
where where
remotes = toList $ filter $ config repo remotes = Map.toList $ filter $ config repo
filter = filterWithKey (\k _ -> isremote k) filter = Map.filterWithKey (\k _ -> isremote k)
isremote k = (startswith "remote." k) && (endswith ".url" k) isremote k = (startswith "remote." k) && (endswith ".url" k)
remotename k = (split "." k) !! 1 remotename k = (split "." k) !! 1
construct (k,v) = (gen v) { remoteName = Just $ remotename k } construct (k,v) = (gen v) { remoteName = Just $ remotename k }
@ -263,7 +272,7 @@ configGet repo key defaultValue =
Map.findWithDefault defaultValue key (config repo) Map.findWithDefault defaultValue key (config repo)
{- Access to raw config Map -} {- Access to raw config Map -}
configMap :: Repo -> Map String String configMap :: Repo -> Map.Map String String
configMap repo = config repo configMap repo = config repo
{- Finds the current git repository, which may be in a parent directory. -} {- Finds the current git repository, which may be in a parent directory. -}