{-|
License : GPL-2

A set of functions to identify and find Darcs repositories
from a given @URL@ or a given filesystem path.
-}

module Darcs.Repository.Identify
    ( maybeIdentifyRepository
    , identifyRepository
    , identifyRepositoryFor
    , IdentifyRepo(..)
    , ReadingOrWriting(..)
    , findRepository
    , amInRepository
    , amNotInRepository
    , amInHashedRepository
    , seekRepo
    , findAllReposInDir
    ) where

import Darcs.Prelude

import Control.Monad ( forM )
import Darcs.Repository.Format ( tryIdentifyRepoFormat
                               , readProblem
                               , transferProblem
                               )
import System.Directory ( doesDirectoryExist
                        , setCurrentDirectory
                        , createDirectoryIfMissing
                        , doesFileExist
                        , listDirectory
                        )
import System.FilePath.Posix ( (</>) )
import System.IO ( hPutStrLn, stderr )
import System.IO.Error ( catchIOError )
import Data.Maybe ( fromMaybe )

import Darcs.Repository.Old ( oldRepoFailMsg )
import Darcs.Repository.Flags ( UseCache(..), WorkRepo (..) )
import Darcs.Util.Path
    ( toFilePath
    , ioAbsoluteOrRemote
    , toPath
    )
import Darcs.Util.Exception ( catchall )
import Darcs.Util.URL ( isValidLocalPath )
import Darcs.Util.Workaround
    ( getCurrentDirectory
    )
import Darcs.Repository.Paths
    ( hashedInventoryPath
    , oldCurrentDirPath
    , oldPristineDirPath
    )
import Darcs.Repository.Prefs ( getCaches )
import Darcs.Repository.InternalTypes( Repository
                                     , PristineType(..)
                                     , mkRepo
                                     , repoFormat
                                     , repoPristineType
                                     )
import Darcs.Util.Global ( darcsdir )

import System.Mem( performGC )

-- | The status of a given directory: is it a darcs repository?
data IdentifyRepo rt p wR wU wT
    = BadRepository String -- ^ looks like a repository with some error
    | NonRepository String -- ^ safest guess
    | GoodRepository (Repository rt p wR wU wT)

-- | Tries to identify the repository in a given directory
maybeIdentifyRepository :: UseCache -> String -> IO (IdentifyRepo rt p wR wU wT)
maybeIdentifyRepository :: UseCache -> String -> IO (IdentifyRepo rt p wR wU wT)
maybeIdentifyRepository UseCache
useCache String
"." =
    do Bool
darcs <- String -> IO Bool
doesDirectoryExist String
darcsdir
       if Bool -> Bool
not Bool
darcs
        then IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> IdentifyRepo rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
String -> IdentifyRepo rt p wR wU wT
NonRepository (String -> IdentifyRepo rt p wR wU wT)
-> String -> IdentifyRepo rt p wR wU wT
forall a b. (a -> b) -> a -> b
$ String
"Missing " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
darcsdir String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" directory")
        else do
        Either String RepoFormat
repoFormatOrError <- String -> IO (Either String RepoFormat)
tryIdentifyRepoFormat String
"."
        String
here <- AbsoluteOrRemotePath -> String
forall a. FilePathOrURL a => a -> String
toPath (AbsoluteOrRemotePath -> String)
-> IO AbsoluteOrRemotePath -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> IO AbsoluteOrRemotePath
ioAbsoluteOrRemote String
"."
        case Either String RepoFormat
repoFormatOrError of
          Left String
err -> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall (m :: * -> *) a. Monad m => a -> m a
return (IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT))
-> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall a b. (a -> b) -> a -> b
$ String -> IdentifyRepo rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
String -> IdentifyRepo rt p wR wU wT
NonRepository String
err
          Right RepoFormat
rf ->
              case RepoFormat -> Maybe String
readProblem RepoFormat
rf of
              Just String
err -> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall (m :: * -> *) a. Monad m => a -> m a
return (IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT))
-> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall a b. (a -> b) -> a -> b
$ String -> IdentifyRepo rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
String -> IdentifyRepo rt p wR wU wT
BadRepository String
err
              Maybe String
Nothing -> do PristineType
pris <- IO PristineType
identifyPristine
                            Cache
cs <- UseCache -> String -> IO Cache
getCaches UseCache
useCache String
here
                            IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall (m :: * -> *) a. Monad m => a -> m a
return (IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT))
-> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall a b. (a -> b) -> a -> b
$ Repository rt p wR wU wT -> IdentifyRepo rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> IdentifyRepo rt p wR wU wT
GoodRepository (Repository rt p wR wU wT -> IdentifyRepo rt p wR wU wT)
-> Repository rt p wR wU wT -> IdentifyRepo rt p wR wU wT
forall a b. (a -> b) -> a -> b
$ String
-> RepoFormat -> PristineType -> Cache -> Repository rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
String
-> RepoFormat -> PristineType -> Cache -> Repository rt p wR wU wT
mkRepo String
here RepoFormat
rf PristineType
pris Cache
cs
maybeIdentifyRepository UseCache
useCache String
url' =
 do String
url <- AbsoluteOrRemotePath -> String
forall a. FilePathOrURL a => a -> String
toPath (AbsoluteOrRemotePath -> String)
-> IO AbsoluteOrRemotePath -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> IO AbsoluteOrRemotePath
ioAbsoluteOrRemote String
url'
    Either String RepoFormat
repoFormatOrError <- String -> IO (Either String RepoFormat)
tryIdentifyRepoFormat String
url
    case Either String RepoFormat
repoFormatOrError of
      Left String
e -> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall (m :: * -> *) a. Monad m => a -> m a
return (IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT))
-> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall a b. (a -> b) -> a -> b
$ String -> IdentifyRepo rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
String -> IdentifyRepo rt p wR wU wT
NonRepository String
e
      Right RepoFormat
rf -> case RepoFormat -> Maybe String
readProblem RepoFormat
rf of
                  Just String
err -> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall (m :: * -> *) a. Monad m => a -> m a
return (IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT))
-> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall a b. (a -> b) -> a -> b
$ String -> IdentifyRepo rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
String -> IdentifyRepo rt p wR wU wT
BadRepository String
err
                  Maybe String
Nothing ->  do Cache
cs <- UseCache -> String -> IO Cache
getCaches UseCache
useCache String
url
                                 IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall (m :: * -> *) a. Monad m => a -> m a
return (IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT))
-> IdentifyRepo rt p wR wU wT -> IO (IdentifyRepo rt p wR wU wT)
forall a b. (a -> b) -> a -> b
$ Repository rt p wR wU wT -> IdentifyRepo rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> IdentifyRepo rt p wR wU wT
GoodRepository (Repository rt p wR wU wT -> IdentifyRepo rt p wR wU wT)
-> Repository rt p wR wU wT -> IdentifyRepo rt p wR wU wT
forall a b. (a -> b) -> a -> b
$ String
-> RepoFormat -> PristineType -> Cache -> Repository rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
String
-> RepoFormat -> PristineType -> Cache -> Repository rt p wR wU wT
mkRepo String
url RepoFormat
rf PristineType
NoPristine Cache
cs

identifyPristine :: IO PristineType
identifyPristine :: IO PristineType
identifyPristine =
    do Bool
pristine <- String -> IO Bool
doesDirectoryExist String
oldPristineDirPath
       Bool
current  <- String -> IO Bool
doesDirectoryExist String
oldCurrentDirPath
       Bool
hashinv  <- String -> IO Bool
doesFileExist      String
hashedInventoryPath
       case (Bool
pristine Bool -> Bool -> Bool
|| Bool
current, Bool
hashinv) of
           (Bool
False, Bool
False) -> PristineType -> IO PristineType
forall (m :: * -> *) a. Monad m => a -> m a
return PristineType
NoPristine
           (Bool
True,  Bool
False) -> PristineType -> IO PristineType
forall (m :: * -> *) a. Monad m => a -> m a
return PristineType
PlainPristine
           (Bool
False, Bool
True ) -> PristineType -> IO PristineType
forall (m :: * -> *) a. Monad m => a -> m a
return PristineType
HashedPristine
           (Bool, Bool)
_ -> String -> IO PristineType
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Multiple pristine trees."

-- | identifyRepository identifies the repo at 'url'. Warning:
-- you have to know what kind of patches are found in that repo.
identifyRepository :: UseCache -> String -> IO (Repository rt p wR wU wT)
identifyRepository :: UseCache -> String -> IO (Repository rt p wR wU wT)
identifyRepository UseCache
useCache String
url =
    do IdentifyRepo rt p wR wU wT
er <- UseCache -> String -> IO (IdentifyRepo rt p wR wU wT)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
UseCache -> String -> IO (IdentifyRepo rt p wR wU wT)
maybeIdentifyRepository UseCache
useCache String
url
       case IdentifyRepo rt p wR wU wT
er of
         BadRepository String
s -> String -> IO (Repository rt p wR wU wT)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
s
         NonRepository String
s -> String -> IO (Repository rt p wR wU wT)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
s
         GoodRepository Repository rt p wR wU wT
r -> Repository rt p wR wU wT -> IO (Repository rt p wR wU wT)
forall (m :: * -> *) a. Monad m => a -> m a
return Repository rt p wR wU wT
r

data ReadingOrWriting = Reading | Writing

-- | @identifyRepositoryFor repo url@ identifies (and returns) the repo at 'url',
-- but fails if it is not compatible for reading from and writing to.
identifyRepositoryFor :: ReadingOrWriting
                      -> Repository rt p wR wU wT
                      -> UseCache
                      -> String
                      -> IO (Repository rt p vR vU vT)
identifyRepositoryFor :: ReadingOrWriting
-> Repository rt p wR wU wT
-> UseCache
-> String
-> IO (Repository rt p vR vU vT)
identifyRepositoryFor ReadingOrWriting
what Repository rt p wR wU wT
us UseCache
useCache String
them_loc = do
  Repository rt p vR vU vT
them <- UseCache -> String -> IO (Repository rt p vR vU vT)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
UseCache -> String -> IO (Repository rt p wR wU wT)
identifyRepository UseCache
useCache String
them_loc
  case
    case ReadingOrWriting
what of
      ReadingOrWriting
Reading -> RepoFormat -> RepoFormat -> Maybe String
transferProblem (Repository rt p vR vU vT -> RepoFormat
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> RepoFormat
repoFormat Repository rt p vR vU vT
them) (Repository rt p wR wU wT -> RepoFormat
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> RepoFormat
repoFormat Repository rt p wR wU wT
us)
      ReadingOrWriting
Writing -> RepoFormat -> RepoFormat -> Maybe String
transferProblem (Repository rt p wR wU wT -> RepoFormat
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> RepoFormat
repoFormat Repository rt p wR wU wT
us) (Repository rt p vR vU vT -> RepoFormat
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> RepoFormat
repoFormat Repository rt p vR vU vT
them) 
    of
      Just String
e -> String -> IO (Repository rt p vR vU vT)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (Repository rt p vR vU vT))
-> String -> IO (Repository rt p vR vU vT)
forall a b. (a -> b) -> a -> b
$ String
"Incompatibility with repository " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
them_loc String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e
      Maybe String
Nothing -> Repository rt p vR vU vT -> IO (Repository rt p vR vU vT)
forall (m :: * -> *) a. Monad m => a -> m a
return Repository rt p vR vU vT
them

amInRepository :: WorkRepo -> IO (Either String ())
amInRepository :: WorkRepo -> IO (Either String ())
amInRepository (WorkRepoDir String
d) =
  do
    String -> IO ()
setCurrentDirectory String
d
    IdentifyRepo Any Any Any Any Any
status <- UseCache -> String -> IO (IdentifyRepo Any Any Any Any Any)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
UseCache -> String -> IO (IdentifyRepo rt p wR wU wT)
maybeIdentifyRepository UseCache
YesUseCache String
"."
    case IdentifyRepo Any Any Any Any Any
status of
      GoodRepository Repository Any Any Any Any Any
_ -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> Either String ()
forall a b. b -> Either a b
Right ())
      BadRepository  String
e -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$ String
"While " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
d String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" looks like a repository directory, we have a problem with it:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e)
      NonRepository  String
_ -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Either String ()
forall a b. a -> Either a b
Left String
"You need to be in a repository directory to run this command.")
  IO (Either String ())
-> (IOError -> IO (Either String ())) -> IO (Either String ())
forall a. IO a -> (IOError -> IO a) -> IO a
`catchIOError`
    \IOError
e -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Either String ()
forall a b. a -> Either a b
Left (IOError -> String
forall a. Show a => a -> String
show IOError
e))
  
amInRepository WorkRepo
_ =
  Either String () -> Maybe (Either String ()) -> Either String ()
forall a. a -> Maybe a -> a
fromMaybe (String -> Either String ()
forall a b. a -> Either a b
Left String
"You need to be in a repository directory to run this command.") (Maybe (Either String ()) -> Either String ())
-> IO (Maybe (Either String ())) -> IO (Either String ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Maybe (Either String ()))
seekRepo

amInHashedRepository :: WorkRepo -> IO (Either String ())
amInHashedRepository :: WorkRepo -> IO (Either String ())
amInHashedRepository WorkRepo
wd
 = do Either String ()
inrepo <- WorkRepo -> IO (Either String ())
amInRepository WorkRepo
wd
      case Either String ()
inrepo of
       Right ()
_ -> do PristineType
pristine <- IO PristineType
identifyPristine
                     case PristineType
pristine of
                       PristineType
HashedPristine -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> Either String ()
forall a b. b -> Either a b
Right ())
                       PristineType
_ -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Either String ()
forall a b. a -> Either a b
Left String
oldRepoFailMsg)
       Either String ()
left    -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return Either String ()
left

-- | hunt upwards for the darcs repository
-- This keeps changing up one parent directory, testing at each
-- step if the current directory is a repository or not.
-- The result is:
--   Nothing, if no repository found
--   Just (Left errorMessage), if bad repository found
--   Just (Right ()), if good repository found.
-- WARNING this changes the current directory for good if matchFn succeeds
seekRepo :: IO (Maybe (Either String ()))
seekRepo :: IO (Maybe (Either String ()))
seekRepo = IO String
getCurrentDirectory IO String
-> (String -> IO (Maybe (Either String ())))
-> IO (Maybe (Either String ()))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> IO (Maybe (Either String ()))
helper where
  helper :: String -> IO (Maybe (Either String ()))
helper String
startpwd = do
    IdentifyRepo Any Any Any Any Any
status <- UseCache -> String -> IO (IdentifyRepo Any Any Any Any Any)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
UseCache -> String -> IO (IdentifyRepo rt p wR wU wT)
maybeIdentifyRepository UseCache
YesUseCache String
"."
    case IdentifyRepo Any Any Any Any Any
status of
      GoodRepository Repository Any Any Any Any Any
_ -> Maybe (Either String ()) -> IO (Maybe (Either String ()))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Either String ()) -> IO (Maybe (Either String ())))
-> (Either String () -> Maybe (Either String ()))
-> Either String ()
-> IO (Maybe (Either String ()))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either String () -> Maybe (Either String ())
forall a. a -> Maybe a
Just (Either String () -> IO (Maybe (Either String ())))
-> Either String () -> IO (Maybe (Either String ()))
forall a b. (a -> b) -> a -> b
$ () -> Either String ()
forall a b. b -> Either a b
Right ()
      BadRepository String
e -> Maybe (Either String ()) -> IO (Maybe (Either String ()))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Either String ()) -> IO (Maybe (Either String ())))
-> (Either String () -> Maybe (Either String ()))
-> Either String ()
-> IO (Maybe (Either String ()))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either String () -> Maybe (Either String ())
forall a. a -> Maybe a
Just (Either String () -> IO (Maybe (Either String ())))
-> Either String () -> IO (Maybe (Either String ()))
forall a b. (a -> b) -> a -> b
$ String -> Either String ()
forall a b. a -> Either a b
Left String
e
      NonRepository String
_ ->
        IO (Maybe (Either String ()))
-> (IOError -> IO (Maybe (Either String ())))
-> IO (Maybe (Either String ()))
forall a. IO a -> (IOError -> IO a) -> IO a
catchIOError
          (do String
cd <- String -> String
forall a. FilePathLike a => a -> String
toFilePath (String -> String) -> IO String -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` IO String
getCurrentDirectory
              String -> IO ()
setCurrentDirectory String
".."
              String
cd' <- String -> String
forall a. FilePathLike a => a -> String
toFilePath (String -> String) -> IO String -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` IO String
getCurrentDirectory
              if String
cd' String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= String
cd
                then String -> IO (Maybe (Either String ()))
helper String
startpwd
                else do
                  String -> IO ()
setCurrentDirectory String
startpwd
                  Maybe (Either String ()) -> IO (Maybe (Either String ()))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Either String ())
forall a. Maybe a
Nothing)
          (\IOError
e -> do
             Handle -> String -> IO ()
hPutStrLn Handle
stderr (String
"Warning: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ IOError -> String
forall a. Show a => a -> String
show IOError
e)
             Maybe (Either String ()) -> IO (Maybe (Either String ()))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Either String ())
forall a. Maybe a
Nothing)

-- The performGC in this function is a workaround for a library/GHC bug,
-- http://hackage.haskell.org/trac/ghc/ticket/2924 -- (doesn't seem to be a
-- problem on fast machines, but virtual ones trip this from time to time)
amNotInRepository :: WorkRepo -> IO (Either String ())
amNotInRepository :: WorkRepo -> IO (Either String ())
amNotInRepository (WorkRepoDir String
d) = do
    Bool -> String -> IO ()
createDirectoryIfMissing Bool
False String
d
       IO () -> IO () -> IO ()
forall a. IO a -> IO a -> IO a
`catchall` (IO ()
performGC IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> String -> IO ()
createDirectoryIfMissing Bool
False String
d)
    -- note that the above could always fail
    String -> IO ()
setCurrentDirectory String
d
    WorkRepo -> IO (Either String ())
amNotInRepository WorkRepo
WorkRepoCurrentDir
amNotInRepository WorkRepo
_ = do
       IdentifyRepo Any Any Any Any Any
status <- UseCache -> String -> IO (IdentifyRepo Any Any Any Any Any)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
UseCache -> String -> IO (IdentifyRepo rt p wR wU wT)
maybeIdentifyRepository UseCache
YesUseCache String
"."
       case IdentifyRepo Any Any Any Any Any
status of
         GoodRepository Repository Any Any Any Any Any
_ -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Either String ()
forall a b. a -> Either a b
Left String
"You may not run this command in a repository.")
         BadRepository String
e  -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$ String
"You may not run this command in a repository.\nBy the way, we have a problem with it:\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e)
         NonRepository String
_  -> Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> Either String ()
forall a b. b -> Either a b
Right ())

findRepository :: WorkRepo -> IO (Either String ())
findRepository :: WorkRepo -> IO (Either String ())
findRepository WorkRepo
workrepo =
  case WorkRepo
workrepo of
    WorkRepoPossibleURL String
d | String -> Bool
isValidLocalPath String
d -> do
      String -> IO ()
setCurrentDirectory String
d
      WorkRepo -> IO (Either String ())
findRepository WorkRepo
WorkRepoCurrentDir
    WorkRepoDir String
d -> do
      String -> IO ()
setCurrentDirectory String
d
      WorkRepo -> IO (Either String ())
findRepository WorkRepo
WorkRepoCurrentDir
    WorkRepo
_ -> Either String () -> Maybe (Either String ()) -> Either String ()
forall a. a -> Maybe a -> a
fromMaybe (() -> Either String ()
forall a b. b -> Either a b
Right ()) (Maybe (Either String ()) -> Either String ())
-> IO (Maybe (Either String ())) -> IO (Either String ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Maybe (Either String ()))
seekRepo
  IO (Either String ())
-> (IOError -> IO (Either String ())) -> IO (Either String ())
forall a. IO a -> (IOError -> IO a) -> IO a
`catchIOError` \IOError
e ->
    Either String () -> IO (Either String ())
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Either String ()
forall a b. a -> Either a b
Left (IOError -> String
forall a. Show a => a -> String
show IOError
e))

-- | @findAllReposInDir topDir@ returns all paths to repositories under @topDir@.
findAllReposInDir :: FilePath -> IO [FilePath]
findAllReposInDir :: String -> IO [String]
findAllReposInDir String
topDir = do
  Bool
isDir <- String -> IO Bool
doesDirectoryExist String
topDir
  if Bool
isDir
    then do
      IdentifyRepo Any Any Any Any Any
status <- UseCache -> String -> IO (IdentifyRepo Any Any Any Any Any)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
UseCache -> String -> IO (IdentifyRepo rt p wR wU wT)
maybeIdentifyRepository UseCache
NoUseCache String
topDir
      case IdentifyRepo Any Any Any Any Any
status of
        GoodRepository Repository Any Any Any Any Any
repo
          | PristineType
HashedPristine <- Repository Any Any Any Any Any -> PristineType
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> PristineType
repoPristineType Repository Any Any Any Any Any
repo -> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return [String
topDir]
          | Bool
otherwise -> [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return [] -- old fashioned or broken repo
        IdentifyRepo Any Any Any Any Any
_             -> String -> IO [String]
getRecursiveDarcsRepos' String
topDir
    else [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  where
    getRecursiveDarcsRepos' :: String -> IO [String]
getRecursiveDarcsRepos' String
d = do
      [String]
names <- String -> IO [String]
listDirectory String
d
      [[String]]
paths <- [String] -> (String -> IO [String]) -> IO [[String]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [String]
names ((String -> IO [String]) -> IO [[String]])
-> (String -> IO [String]) -> IO [[String]]
forall a b. (a -> b) -> a -> b
$ \String
name -> do
        let path :: String
path = String
d String -> String -> String
</> String
name
        String -> IO [String]
findAllReposInDir String
path
      [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[String]]
paths)