Maybe reduction pass 2

This commit is contained in:
Joey Hess 2011-05-15 12:25:58 -04:00
parent cad0e1c8b7
commit 3e15a8a791
4 changed files with 33 additions and 51 deletions

View file

@ -91,20 +91,12 @@ prepCommand Command { cmdseek = seek } params = do
{- Runs a command through the start, perform and cleanup stages -} {- Runs a command through the start, perform and cleanup stages -}
doCommand :: CommandStart -> CommandCleanup doCommand :: CommandStart -> CommandCleanup
doCommand start = do doCommand = start
s <- start where
case s of start = stage $ maybe (return True) perform
Nothing -> return True perform = stage $ maybe (showEndFail >> return False) cleanup
Just perform -> do cleanup = stage $ \r -> showEndResult r >> return r
p <- perform stage a b = b >>= a
case p of
Nothing -> do
showEndFail
return False
Just cleanup -> do
c <- cleanup
if c then showEndOk else showEndFail
return c
notAnnexed :: FilePath -> Annex (Maybe a) -> Annex (Maybe a) notAnnexed :: FilePath -> Annex (Maybe a) -> Annex (Maybe a)
notAnnexed file a = maybe a (const $ return Nothing) =<< Backend.lookupFile file notAnnexed file a = maybe a (const $ return Nothing) =<< Backend.lookupFile file

View file

@ -210,9 +210,9 @@ assertUrl repo action =
" not supported" " not supported"
configBare :: Repo -> Bool configBare :: Repo -> Bool
configBare repo = case Map.lookup "core.bare" $ config repo of configBare repo = maybe unknown configTrue $ Map.lookup "core.bare" $ config repo
Just v -> configTrue v where
Nothing -> error $ "it is not known if git repo " ++ unknown = error $ "it is not known if git repo " ++
repoDescribe repo ++ repoDescribe repo ++
" is a bare repository; config not read" " is a bare repository; config not read"
@ -260,11 +260,10 @@ workTreeFile repo@(Repo { location = Dir d }) file = do
where where
-- normalize both repo and file, so that repo -- normalize both repo and file, so that repo
-- will be substring of file -- will be substring of file
absrepo = case (absNormPath "/" d) of absrepo = maybe bad addTrailingPathSeparator $ absNormPath "/" d
Just f -> addTrailingPathSeparator f
Nothing -> error $ "bad repo" ++ repoDescribe repo
absfile c = maybe file id $ secureAbsNormPath c file absfile c = maybe file id $ secureAbsNormPath c file
inrepo f = absrepo `isPrefixOf` f inrepo f = absrepo `isPrefixOf` f
bad = error $ "bad repo" ++ repoDescribe repo
workTreeFile repo _ = assertLocal repo $ error "internal" workTreeFile repo _ = assertLocal repo $ error "internal"
{- Path of an URL repo. -} {- Path of an URL repo. -}
@ -627,23 +626,19 @@ expandTilde = expandt True
{- Finds the current git repository, which may be in a parent directory. -} {- Finds the current git repository, which may be in a parent directory. -}
repoFromCwd :: IO Repo repoFromCwd :: IO Repo
repoFromCwd = do repoFromCwd = getCurrentDirectory >>= seekUp isRepoTop >>= maybe norepo makerepo
cwd <- getCurrentDirectory where
top <- seekUp cwd isRepoTop makerepo = return . newFrom . Dir
case top of norepo = error "Not in a git repository."
-- repoFromAbsPath is not used to avoid looking for
-- "dir.git" directories.
(Just dir) -> return $ newFrom $ Dir dir
Nothing -> error "Not in a git repository."
seekUp :: FilePath -> (FilePath -> IO Bool) -> IO (Maybe FilePath) seekUp :: (FilePath -> IO Bool) -> FilePath -> IO (Maybe FilePath)
seekUp dir want = do seekUp want dir = do
ok <- want dir ok <- want dir
if ok if ok
then return (Just dir) then return (Just dir)
else case (parentDir dir) of else case (parentDir dir) of
"" -> return Nothing "" -> return Nothing
d -> seekUp d want d -> seekUp want d
isRepoTop :: FilePath -> IO Bool isRepoTop :: FilePath -> IO Bool
isRepoTop dir = do isRepoTop dir = do

View file

@ -45,6 +45,10 @@ showEndOk = verbose $ liftIO $ putStrLn "ok"
showEndFail :: Annex () showEndFail :: Annex ()
showEndFail = verbose $ liftIO $ putStrLn "\nfailed" showEndFail = verbose $ liftIO $ putStrLn "\nfailed"
showEndResult :: Bool -> Annex ()
showEndResult True = showEndOk
showEndResult False = showEndFail
showErr :: (Show a) => a -> Annex () showErr :: (Show a) => a -> Annex ()
showErr e = warning $ "git-annex: " ++ show e showErr e = warning $ "git-annex: " ++ show e

View file

@ -54,21 +54,14 @@ encryptableRemote c storeKeyEncrypted retrieveKeyFileEncrypted r =
cost = cost r + encryptedRemoteCostAdj cost = cost r + encryptedRemoteCostAdj
} }
where where
store k = do store k = cip k >>= maybe
v <- cipherKey c k (storeKey r k)
case v of (\x -> storeKeyEncrypted x k)
Nothing -> (storeKey r) k retrieve k f = cip k >>= maybe
Just x -> storeKeyEncrypted x k (retrieveKeyFile r k f)
retrieve k f = do (\x -> retrieveKeyFileEncrypted x f)
v <- cipherKey c k withkey a k = cip k >>= maybe (a k) (a . snd)
case v of cip = cipherKey c
Nothing -> (retrieveKeyFile r) k f
Just x -> retrieveKeyFileEncrypted x f
withkey a k = do
v <- cipherKey c k
case v of
Nothing -> a k
Just (_, k') -> a k'
{- Gets encryption Cipher. The decrypted Cipher is cached in the Annex {- Gets encryption Cipher. The decrypted Cipher is cached in the Annex
- state. -} - state. -}
@ -87,10 +80,8 @@ remoteCipher c = maybe expensive cached =<< Annex.getState Annex.cipher
{- Gets encryption Cipher, and encrypted version of Key. -} {- Gets encryption Cipher, and encrypted version of Key. -}
cipherKey :: Maybe RemoteConfig -> Key -> Annex (Maybe (Cipher, Key)) cipherKey :: Maybe RemoteConfig -> Key -> Annex (Maybe (Cipher, Key))
cipherKey Nothing _ = return Nothing cipherKey Nothing _ = return Nothing
cipherKey (Just c) k = do cipherKey (Just c) k = remoteCipher c >>= maybe (return Nothing) encrypt
cipher <- remoteCipher c where
case cipher of encrypt ciphertext = do
Just ciphertext -> do
k' <- liftIO $ encryptKey ciphertext k k' <- liftIO $ encryptKey ciphertext k
return $ Just (ciphertext, k') return $ Just (ciphertext, k')
Nothing -> return Nothing