From b2ee2496ee9b22df6d61b73e71ff09e17267e98c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 26 Oct 2022 13:58:20 -0400 Subject: [PATCH] remove whenAnnexed and ifAnnexed In preparation for adding a new variation on lookupKey. Sponsored-by: Max Thoursie on Patreon --- Annex/WorkTree.hs | 10 +--------- CmdLine/Batch.hs | 5 +++-- Command.hs | 1 - Command/Add.hs | 5 ++++- Command/AddUrl.hs | 15 ++++++++++----- Command/ImportFeed.hs | 5 ++++- Command/Info.hs | 7 ++++--- Command/ReKey.hs | 5 ++++- Command/Reinject.hs | 13 +++++++------ Command/RmUrl.hs | 11 +++++++---- Command/Sync.hs | 6 +++++- Command/Uninit.hs | 9 +++++++-- 12 files changed, 56 insertions(+), 36 deletions(-) diff --git a/Annex/WorkTree.hs b/Annex/WorkTree.hs index 95e0d18e2b..e065a2185c 100644 --- a/Annex/WorkTree.hs +++ b/Annex/WorkTree.hs @@ -1,6 +1,6 @@ {- git-annex worktree files - - - Copyright 2013-2021 Joey Hess + - Copyright 2013-2022 Joey Hess - - Licensed under the GNU AGPL version 3 or higher. -} @@ -45,14 +45,6 @@ lookupKey' catkeyfile file = isAnnexLink file >>= \case Just key -> return (Just key) Nothing -> catkeyfile file -{- Modifies an action to only act on files that are already annexed, - - and passes the key on to it. -} -whenAnnexed :: (RawFilePath -> Key -> Annex (Maybe a)) -> RawFilePath -> Annex (Maybe a) -whenAnnexed a file = ifAnnexed file (a file) (return Nothing) - -ifAnnexed :: RawFilePath -> (Key -> Annex a) -> Annex a -> Annex a -ifAnnexed file yes no = maybe no yes =<< lookupKey file - {- Find all annexed files and update the keys database for them. -} scanAnnexedFiles :: Annex () scanAnnexedFiles = Database.Keys.updateDatabase diff --git a/CmdLine/Batch.hs b/CmdLine/Batch.hs index 80c901ecca..0c2617230f 100644 --- a/CmdLine/Batch.hs +++ b/CmdLine/Batch.hs @@ -186,8 +186,9 @@ batchAnnexed fmt seeker keyaction = do matcher <- getMatcher batchFilesKeys fmt $ \(si, v) -> case v of - Right bf -> flip whenAnnexed bf $ \f k -> - checkpresent k $ + Right f -> lookupKey f >>= \case + Nothing -> return Nothing + Just k -> checkpresent k $ startAction seeker si f k Left k -> ifM (matcher (MatchingInfo (mkinfo k))) ( checkpresent k $ diff --git a/Command.hs b/Command.hs index 09e7cb55be..67d10dd125 100644 --- a/Command.hs +++ b/Command.hs @@ -11,7 +11,6 @@ module Command ( ) where import Annex.Common as ReExported -import Annex.WorkTree as ReExported (whenAnnexed, ifAnnexed) import Types.Command as ReExported import Types.DeferredParse as ReExported import CmdLine.Seek as ReExported diff --git a/Command/Add.hs b/Command/Add.hs index 5010fef0e7..e1742bed66 100644 --- a/Command/Add.hs +++ b/Command/Add.hs @@ -18,6 +18,7 @@ import Annex.FileMatcher import Annex.Link import Annex.Tmp import Annex.HashObject +import Annex.WorkTree import Messages.Progress import Git.FilePath import Git.Types @@ -202,7 +203,9 @@ start dr si file addunlockedmatcher = mk <- liftIO $ isPointerFile file maybe (go s) (fixuppointer s) mk where - go s = ifAnnexed file (addpresent s) (add s) + go s = lookupKey file >>= \case + Just k -> addpresent s k + Nothing -> add s add s = starting "add" (ActionItemTreeFile file) si $ skipWhenDryRun dr $ if isSymbolicLink s diff --git a/Command/AddUrl.hs b/Command/AddUrl.hs index 830cc09251..5961a18eb4 100644 --- a/Command/AddUrl.hs +++ b/Command/AddUrl.hs @@ -20,6 +20,7 @@ import Annex.Ingest import Annex.CheckIgnore import Annex.Perms import Annex.UUID +import Annex.WorkTree import Annex.YoutubeDl import Annex.UntrustedFilePath import Logs.Web @@ -183,7 +184,9 @@ startRemote addunlockedmatcher r o si file uri sz = do performRemote addunlockedmatcher r o uri (toRawFilePath file') sz performRemote :: AddUnlockedMatcher -> Remote -> AddUrlOptions -> URLString -> RawFilePath -> Maybe Integer -> CommandPerform -performRemote addunlockedmatcher r o uri file sz = ifAnnexed file adduri geturi +performRemote addunlockedmatcher r o uri file sz = lookupKey file >>= \case + Just k -> adduri k + Nothing -> geturi where loguri = setDownloader uri OtherDownloader adduri = addUrlChecked o loguri file (Remote.uuid r) checkexistssize @@ -270,7 +273,9 @@ checkPreserveFileNameSecurity f = do ] performWeb :: AddUnlockedMatcher -> AddUrlOptions -> URLString -> RawFilePath -> Url.UrlInfo -> CommandPerform -performWeb addunlockedmatcher o url file urlinfo = ifAnnexed file addurl geturl +performWeb addunlockedmatcher o url file urlinfo = lookupKey file >>= \case + Just k -> addurl k + Nothing -> geturl where geturl = next $ isJust <$> addUrlFile addunlockedmatcher (downloadOptions o) url urlinfo file addurl = addUrlChecked o url file webUUID $ \k -> @@ -335,9 +340,9 @@ downloadWeb addunlockedmatcher o url urlinfo file = tryyoutubedl tmp = youtubeDlFileNameHtmlOnly url >>= \case Right mediafile -> let f = youtubeDlDestFile o file (toRawFilePath mediafile) - in ifAnnexed f - (alreadyannexed (fromRawFilePath f)) - (dl f) + in lookupKey f >>= \case + Just k -> alreadyannexed (fromRawFilePath f) k + Nothing -> dl f Left err -> checkRaw (Just err) o Nothing (normalfinish tmp) where dl dest = withTmpWorkDir mediakey $ \workdir -> do diff --git a/Command/ImportFeed.hs b/Command/ImportFeed.hs index 816db01691..b6ee11be9e 100644 --- a/Command/ImportFeed.hs +++ b/Command/ImportFeed.hs @@ -40,6 +40,7 @@ import Command.AddUrl (addUrlFile, downloadRemoteFile, parseDownloadOptions, Dow import Annex.UUID import Backend.URL (fromUrl) import Annex.Content +import Annex.WorkTree import Annex.YoutubeDl import Types.MetaData import Logs.MetaData @@ -297,7 +298,9 @@ performDownload' started addunlockedmatcher opts cache todownload = case locatio - to be re-downloaded. -} makeunique url n file = ifM alreadyexists ( ifM forced - ( ifAnnexed (toRawFilePath f) checksameurl tryanother + ( lookupKey (toRawFilePath f) >>= \case + Just k -> checksameurl k + Nothing -> tryanother , tryanother ) , return $ Just f diff --git a/Command/Info.hs b/Command/Info.hs index 9e47512596..7eb7727f33 100644 --- a/Command/Info.hs +++ b/Command/Info.hs @@ -28,6 +28,7 @@ import Utility.DiskFree import Annex.Content import Annex.UUID import Annex.CatFile +import Annex.WorkTree import Logs.UUID import Logs.Trust import Logs.Location @@ -174,9 +175,9 @@ itemInfo o (si, p) = ifM (isdir p) Right u -> uuidInfo o u si Left _ -> do relp <- liftIO $ relPathCwdToFile (toRawFilePath p) - ifAnnexed relp - (fileInfo o (fromRawFilePath relp) si) - (treeishInfo o p si) + lookupKey relp >>= \case + Just k -> fileInfo o (fromRawFilePath relp) si k + Nothing -> treeishInfo o p si ) where isdir = liftIO . catchBoolIO . (isDirectory <$$> getFileStatus) diff --git a/Command/ReKey.hs b/Command/ReKey.hs index 165f48c078..91d0804222 100644 --- a/Command/ReKey.hs +++ b/Command/ReKey.hs @@ -16,6 +16,7 @@ import Annex.Perms import Annex.ReplaceFile import Logs.Location import Annex.InodeSentinal +import Annex.WorkTree import Utility.InodeCache import qualified Utility.RawFilePath as R @@ -61,7 +62,9 @@ seek o = case batchOption o of (toRawFilePath file, fromMaybe (giveup "bad key") (deserializeKey skey)) start :: SeekInput -> (RawFilePath, Key) -> CommandStart -start si (file, newkey) = ifAnnexed file go stop +start si (file, newkey) = lookupKey file >>= \case + Just k -> go k + Nothing -> stop where go oldkey | oldkey == newkey = stop diff --git a/Command/Reinject.hs b/Command/Reinject.hs index ad8e908a62..54492e235b 100644 --- a/Command/Reinject.hs +++ b/Command/Reinject.hs @@ -13,6 +13,7 @@ import Annex.Content import Backend import Types.KeySource import Utility.Metered +import Annex.WorkTree import qualified Git cmd :: Command @@ -45,9 +46,9 @@ startSrcDest :: [FilePath] -> CommandStart startSrcDest ps@(src:dest:[]) | src == dest = stop | otherwise = notAnnexed src' $ - ifAnnexed (toRawFilePath dest) - go - (giveup $ src ++ " is not an annexed file") + lookupKey (toRawFilePath dest) >>= \case + Just k -> go k + Nothing -> giveup $ src ++ " is not an annexed file" where src' = toRawFilePath src go key = starting "reinject" ai si $ @@ -79,9 +80,9 @@ notAnnexed :: RawFilePath -> CommandStart -> CommandStart notAnnexed src a = ifM (fromRepo Git.repoIsLocalBare) ( a - , ifAnnexed src - (giveup $ "cannot used annexed file as src: " ++ fromRawFilePath src) - a + , lookupKey src >>= \case + Just _ -> giveup $ "cannot used annexed file as src: " ++ fromRawFilePath src + Nothing -> a ) perform :: RawFilePath -> Key -> CommandPerform diff --git a/Command/RmUrl.hs b/Command/RmUrl.hs index 93443b227a..c5107bd0eb 100644 --- a/Command/RmUrl.hs +++ b/Command/RmUrl.hs @@ -9,6 +9,7 @@ module Command.RmUrl where import Command import Logs.Web +import Annex.WorkTree cmd :: Command cmd = notBareRepo $ @@ -46,10 +47,12 @@ batchParser s = case separate (== ' ') (reverse s) of return $ Right (f', reverse ru) start :: (SeekInput, (FilePath, URLString)) -> CommandStart -start (si, (file, url)) = flip whenAnnexed file' $ \_ key -> do - let ai = mkActionItem (key, AssociatedFile (Just file')) - starting "rmurl" ai si $ - next $ cleanup url key +start (si, (file, url)) = lookupKey file' >>= \case + Nothing -> stop + Just key -> do + let ai = mkActionItem (key, AssociatedFile (Just file')) + starting "rmurl" ai si $ + next $ cleanup url key where file' = toRawFilePath file diff --git a/Command/Sync.hs b/Command/Sync.hs index 15ccfd3763..781b4e6125 100644 --- a/Command/Sync.hs +++ b/Command/Sync.hs @@ -50,6 +50,7 @@ import Config.DynamicConfig import Annex.Path import Annex.Wanted import Annex.Content +import Annex.WorkTree import Command.Get (getKey') import qualified Command.Move import qualified Command.Export @@ -765,7 +766,10 @@ seekSyncContent o rs currbranch = do seekHelper fst3 ww LsFiles.inRepoDetails l seekincludinghidden origbranch mvar l bloomfeeder = - seekFiltered (const (pure True)) (\(si, f) -> ifAnnexed f (commandAction . gofile bloomfeeder mvar si f) noop) $ + let filterer = \(si, f) -> lookupKey f >>= \case + Just k -> (commandAction $ gofile bloomfeeder mvar si f k) + Nothing -> noop + in seekFiltered (const (pure True)) filterer $ seekHelper id ww (LsFiles.inRepoOrBranch origbranch) l ww = WarnUnmatchLsFiles diff --git a/Command/Uninit.hs b/Command/Uninit.hs index 38f50c8f6c..d8cba0c4df 100644 --- a/Command/Uninit.hs +++ b/Command/Uninit.hs @@ -18,6 +18,7 @@ import qualified Database.Keys import Annex.Content import Annex.Init import Annex.CheckIgnore +import Annex.WorkTree import Utility.FileMode import qualified Utility.RawFilePath as R @@ -50,13 +51,17 @@ seek ps = do l <- workTreeItems ww ps withFilesNotInGit (CheckGitIgnore False) - WarnUnmatchWorkTreeItems - (\(_, f) -> commandAction $ whenAnnexed (startCheckIncomplete . fromRawFilePath) f) + WarnUnmatchWorkTreeItems + checksymlinks l withFilesInGitAnnex ww (Command.Unannex.seeker True) l finish where ww = WarnUnmatchLsFiles + checksymlinks (_, f) = + commandAction $ lookupKey f >>= \case + Nothing -> stop + Just k -> startCheckIncomplete (fromRawFilePath f) k {- git annex symlinks that are not checked into git could be left by an - interrupted add. -}