git-annex/Upgrade.hs

85 lines
2 KiB
Haskell
Raw Normal View History

2010-11-14 18:44:24 +00:00
{- git-annex upgrade support
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Upgrade where
2010-12-13 15:35:00 +00:00
import System.IO.Error (try)
2010-11-14 18:44:24 +00:00
import System.Directory
import Control.Monad.State (liftIO)
2011-02-01 03:27:53 +00:00
import Control.Monad (filterM, forM_)
2010-11-14 18:44:24 +00:00
import System.Posix.Files
2010-12-13 15:35:00 +00:00
import System.FilePath
2010-11-14 18:44:24 +00:00
import Content
2010-11-14 18:44:24 +00:00
import Types
import Locations
import qualified GitRepo as Git
import qualified Annex
import qualified Backend
import Messages
import Version
import Utility
2010-11-14 18:44:24 +00:00
{- Uses the annex.version git config setting to automate upgrades. -}
upgrade :: Annex Bool
upgrade = do
version <- getVersion
case version of
Just "0" -> upgradeFrom0
Nothing -> return True -- repo not initted yet, no version
Just v | v == currentVersion -> return True
Just _ -> error "this version of git-annex is too old for this git repository!"
upgradeFrom0 :: Annex Bool
upgradeFrom0 = do
showSideAction "Upgrading object directory layout..."
g <- Annex.gitRepo
-- do the reorganisation of the files
let olddir = gitAnnexDir g
2010-12-13 15:35:00 +00:00
keys <- getKeysPresent0' olddir
2011-02-01 03:27:53 +00:00
forM_ keys $ \k -> moveAnnex k $ olddir </> keyFile k
2010-11-14 18:44:24 +00:00
-- update the symlinks to the files
files <- liftIO $ Git.inRepo g [Git.workTree g]
2010-11-14 18:44:24 +00:00
fixlinks files
Annex.queueRun
setVersion
return True
where
fixlinks [] = return ()
fixlinks (f:fs) = do
r <- Backend.lookupFile f
case r of
Nothing -> return ()
Just (k, _) -> do
link <- calcGitLink f k
liftIO $ removeFile f
liftIO $ createSymbolicLink link f
Annex.queue "add" [Param "--"] f
2010-11-14 18:44:24 +00:00
fixlinks fs
2010-12-13 15:35:00 +00:00
getKeysPresent0' :: FilePath -> Annex [Key]
getKeysPresent0' dir = do
exists <- liftIO $ doesDirectoryExist dir
if (not exists)
then return []
else do
contents <- liftIO $ getDirectoryContents dir
files <- liftIO $ filterM present contents
return $ map fileKey files
2010-12-13 15:35:00 +00:00
where
present d = do
result <- try $
getFileStatus $ dir ++ "/" ++ takeFileName d
case result of
Right s -> return $ isRegularFile s
Left _ -> return False