diff options
author | Joey Hess <joey@kitenet.net> | 2011-12-09 12:23:45 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2011-12-09 12:23:45 -0400 |
commit | 3f5f28b48754bc91620a6354ca70afe4c61c9894 (patch) | |
tree | 6c71b52187f442ce619ffac7c672fc7f0b8f84ce | |
parent | d64132a43ae176e8a1353d5463c5387a93da9ad7 (diff) |
factor out a stopUnless
code melt for lunch
-rw-r--r-- | Command.hs | 7 | ||||
-rw-r--r-- | Command/AddUrl.hs | 21 | ||||
-rw-r--r-- | Command/Drop.hs | 28 | ||||
-rw-r--r-- | Command/DropKey.hs | 17 | ||||
-rw-r--r-- | Command/Fix.hs | 9 | ||||
-rw-r--r-- | Command/Get.hs | 32 | ||||
-rw-r--r-- | Command/Migrate.hs | 24 | ||||
-rw-r--r-- | Command/Move.hs | 14 | ||||
-rw-r--r-- | Command/Unannex.hs | 24 |
9 files changed, 69 insertions, 107 deletions
diff --git a/Command.hs b/Command.hs index 86a83e30c..813a239cb 100644 --- a/Command.hs +++ b/Command.hs @@ -10,6 +10,7 @@ module Command ( noRepo, next, stop, + stopUnless, prepCommand, doCommand, whenAnnexed, @@ -49,6 +50,12 @@ next a = return $ Just a stop :: Annex (Maybe a) stop = return Nothing +{- Stops unless a condition is met. -} +stopUnless :: Annex Bool -> Annex (Maybe a) -> Annex (Maybe a) +stopUnless c a = do + ok <- c + if ok then a else stop + {- Prepares to run a command via the check and seek stages, returning a - list of actions to perform to run the command. -} prepCommand :: Command -> [String] -> Annex [CommandCleanup] diff --git a/Command/AddUrl.hs b/Command/AddUrl.hs index 945848e9f..75ca74031 100644 --- a/Command/AddUrl.hs +++ b/Command/AddUrl.hs @@ -45,18 +45,15 @@ download url file = do let dummykey = Backend.URL.fromUrl url tmp <- fromRepo $ gitAnnexTmpLocation dummykey liftIO $ createDirectoryIfMissing True (parentDir tmp) - ok <- liftIO $ Url.download url tmp - if ok - then do - [(backend, _)] <- Backend.chooseBackends [file] - k <- Backend.genKey tmp backend - case k of - Nothing -> stop - Just (key, _) -> do - moveAnnex key tmp - setUrlPresent key url - next $ Command.Add.cleanup file key True - else stop + stopUnless (liftIO $ Url.download url tmp) $ do + [(backend, _)] <- Backend.chooseBackends [file] + k <- Backend.genKey tmp backend + case k of + Nothing -> stop + Just (key, _) -> do + moveAnnex key tmp + setUrlPresent key url + next $ Command.Add.cleanup file key True nodownload :: String -> FilePath -> CommandPerform nodownload url file = do diff --git a/Command/Drop.hs b/Command/Drop.hs index ee3583869..0a4c9dfd6 100644 --- a/Command/Drop.hs +++ b/Command/Drop.hs @@ -37,13 +37,9 @@ start numcopies file (key, _) = autoCopies key (>) numcopies $ do else startRemote file numcopies key remote startLocal :: FilePath -> Maybe Int -> Key -> CommandStart -startLocal file numcopies key = do - present <- inAnnex key - if present - then do - showStart "drop" file - next $ performLocal key numcopies - else stop +startLocal file numcopies key = stopUnless (inAnnex key) $ do + showStart "drop" file + next $ performLocal key numcopies startRemote :: FilePath -> Maybe Int -> Key -> Remote.Remote Annex -> CommandStart startRemote file numcopies key remote = do @@ -55,12 +51,9 @@ performLocal key numcopies = lockContent key $ do (remotes, trusteduuids) <- Remote.keyPossibilitiesTrusted key untrusteduuids <- trustGet UnTrusted let tocheck = Remote.remotesWithoutUUID remotes (trusteduuids++untrusteduuids) - success <- canDropKey key numcopies trusteduuids tocheck [] - if success - then do - whenM (inAnnex key) $ removeAnnex key - next $ cleanupLocal key - else stop + stopUnless (canDropKey key numcopies trusteduuids tocheck []) $ do + whenM (inAnnex key) $ removeAnnex key + next $ cleanupLocal key performRemote :: Key -> Maybe Int -> Remote.Remote Annex -> CommandPerform performRemote key numcopies remote = lockContent key $ do @@ -75,12 +68,9 @@ performRemote key numcopies remote = lockContent key $ do untrusteduuids <- trustGet UnTrusted let tocheck = filter (/= remote) $ Remote.remotesWithoutUUID remotes (have++untrusteduuids) - success <- canDropKey key numcopies have tocheck [uuid] - if success - then do - ok <- Remote.removeKey remote key - next $ cleanupRemote key remote ok - else stop + stopUnless (canDropKey key numcopies have tocheck [uuid]) $ do + ok <- Remote.removeKey remote key + next $ cleanupRemote key remote ok where uuid = Remote.uuid remote diff --git a/Command/DropKey.hs b/Command/DropKey.hs index b63d481bf..ae2ad8b6a 100644 --- a/Command/DropKey.hs +++ b/Command/DropKey.hs @@ -21,18 +21,11 @@ seek :: [CommandSeek] seek = [withKeys start] start :: Key -> CommandStart -start key = do - present <- inAnnex key - if not present - then stop - else do - checkforced - showStart "dropkey" (show key) - next $ perform key - where - checkforced = - unlessM (Annex.getState Annex.force) $ - error "dropkey can cause data loss; use --force if you're sure you want to do this" +start key = stopUnless (not <$> inAnnex key) $ do + unlessM (Annex.getState Annex.force) $ + error "dropkey can cause data loss; use --force if you're sure you want to do this" + showStart "dropkey" (show key) + next $ perform key perform :: Key -> CommandPerform perform key = lockContent key $ do diff --git a/Command/Fix.hs b/Command/Fix.hs index 27c4b167e..f264106c3 100644 --- a/Command/Fix.hs +++ b/Command/Fix.hs @@ -23,12 +23,9 @@ seek = [withFilesInGit $ whenAnnexed start] start :: FilePath -> (Key, Backend Annex) -> CommandStart start file (key, _) = do link <- calcGitLink file key - l <- liftIO $ readSymbolicLink file - if link == l - then stop - else do - showStart "fix" file - next $ perform file link + stopUnless ((/=) link <$> liftIO (readSymbolicLink file)) $ do + showStart "fix" file + next $ perform file link perform :: FilePath -> FilePath -> CommandPerform perform file link = do diff --git a/Command/Get.hs b/Command/Get.hs index 093cd2cc5..b7023e2de 100644 --- a/Command/Get.hs +++ b/Command/Get.hs @@ -22,32 +22,24 @@ seek :: [CommandSeek] seek = [withNumCopies $ \n -> whenAnnexed $ start n] start :: Maybe Int -> FilePath -> (Key, Backend Annex) -> CommandStart -start numcopies file (key, _) = do - inannex <- inAnnex key - if inannex - then stop - else autoCopies key (<) numcopies $ do - from <- Annex.getState Annex.fromremote - case from of - Nothing -> go $ perform key - Just name -> do - -- get --from = copy --from - src <- Remote.byName name - ok <- Command.Move.fromOk src key - if ok - then go $ Command.Move.fromPerform src False key - else stop +start numcopies file (key, _) = stopUnless (not <$> inAnnex key) $ + autoCopies key (<) numcopies $ do + from <- Annex.getState Annex.fromremote + case from of + Nothing -> go $ perform key + Just name -> do + -- get --from = copy --from + src <- Remote.byName name + stopUnless (Command.Move.fromOk src key) $ + go $ Command.Move.fromPerform src False key where go a = do showStart "get" file next a perform :: Key -> CommandPerform -perform key = do - ok <- getViaTmp key (getKeyFile key) - if ok - then next $ return True -- no cleanup needed - else stop +perform key = stopUnless (getViaTmp key $ getKeyFile key) $ do + next $ return True -- no cleanup needed {- Try to find a copy of the file in one of the remotes, - and copy it to here. -} diff --git a/Command/Migrate.hs b/Command/Migrate.hs index c85d7c2ac..30288fc16 100644 --- a/Command/Migrate.hs +++ b/Command/Migrate.hs @@ -58,22 +58,18 @@ perform file oldkey newbackend = do cleantmp tmpfile case k of Nothing -> stop - Just (newkey, _) -> do - ok <- link src newkey - if ok - then do - -- Update symlink to use the new key. - liftIO $ removeFile file + Just (newkey, _) -> stopUnless (link src newkey) $ do + -- Update symlink to use the new key. + liftIO $ removeFile file - -- If the old key had some - -- associated urls, record them for - -- the new key as well. - urls <- getUrls oldkey - unless (null urls) $ - mapM_ (setUrlPresent newkey) urls + -- If the old key had some + -- associated urls, record them for + -- the new key as well. + urls <- getUrls oldkey + unless (null urls) $ + mapM_ (setUrlPresent newkey) urls - next $ Command.Add.cleanup file newkey True - else stop + next $ Command.Add.cleanup file newkey True where cleantmp t = liftIO $ whenM (doesFileExist t) $ removeFile t link src newkey = getViaTmpUnchecked newkey $ \t -> do diff --git a/Command/Move.hs b/Command/Move.hs index fd1ed9019..cc26eecda 100644 --- a/Command/Move.hs +++ b/Command/Move.hs @@ -108,17 +108,11 @@ toPerform dest move key = moveLock move key $ do fromStart :: Remote.Remote Annex -> Bool -> FilePath -> Key -> CommandStart fromStart src move file key | move = go - | otherwise = do - ishere <- inAnnex key - if ishere then stop else go + | otherwise = stopUnless (inAnnex key) go where - go = do - ok <- fromOk src key - if ok - then do - showMoveAction move file - next $ fromPerform src move key - else stop + go = stopUnless (fromOk src key) $ do + showMoveAction move file + next $ fromPerform src move key fromOk :: Remote.Remote Annex -> Key -> Annex Bool fromOk src key = do u <- getUUID diff --git a/Command/Unannex.hs b/Command/Unannex.hs index e97b6d05d..263ff88b4 100644 --- a/Command/Unannex.hs +++ b/Command/Unannex.hs @@ -25,21 +25,17 @@ seek = [withFilesInGit $ whenAnnexed start] {- The unannex subcommand undoes an add. -} start :: FilePath -> (Key, Backend Annex) -> CommandStart -start file (key, _) = do - ishere <- inAnnex key - if ishere - then do - force <- Annex.getState Annex.force - unless force $ do - top <- fromRepo Git.workTree - staged <- inRepo $ LsFiles.staged [top] - unless (null staged) $ - error "This command cannot be run when there are already files staged for commit." - Annex.changeState $ \s -> s { Annex.force = True } +start file (key, _) = stopUnless (inAnnex key) $ do + force <- Annex.getState Annex.force + unless force $ do + top <- fromRepo Git.workTree + staged <- inRepo $ LsFiles.staged [top] + unless (null staged) $ + error "This command cannot be run when there are already files staged for commit." + Annex.changeState $ \s -> s { Annex.force = True } - showStart "unannex" file - next $ perform file key - else stop + showStart "unannex" file + next $ perform file key perform :: FilePath -> Key -> CommandPerform perform file key = next $ cleanup file key |