2011-06-23 06:30:20 +00:00
|
|
|
{- git-annex v2 -> v3 upgrade support
|
2011-06-22 20:02:43 +00:00
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-06-22 21:51:48 +00:00
|
|
|
module Upgrade.V2 where
|
2011-06-22 20:02:43 +00:00
|
|
|
|
|
|
|
import System.Directory
|
|
|
|
import System.FilePath
|
2011-06-24 16:09:04 +00:00
|
|
|
import Control.Monad.State (unless, when, liftIO)
|
2011-07-15 07:12:05 +00:00
|
|
|
import Data.List
|
2011-06-23 06:30:20 +00:00
|
|
|
import Data.Maybe
|
2011-06-22 20:02:43 +00:00
|
|
|
|
|
|
|
import Types.Key
|
|
|
|
import Types
|
2011-06-23 06:30:20 +00:00
|
|
|
import qualified Annex
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-06-23 06:30:20 +00:00
|
|
|
import qualified Branch
|
2011-06-22 20:02:43 +00:00
|
|
|
import Messages
|
|
|
|
import Utility
|
2011-08-22 20:14:12 +00:00
|
|
|
import Utility.Conditional
|
|
|
|
import Utility.SafeCommand
|
2011-07-01 21:23:01 +00:00
|
|
|
import LocationLog
|
2011-06-24 06:33:44 +00:00
|
|
|
import Content
|
2011-06-22 21:51:48 +00:00
|
|
|
|
2011-06-24 05:13:33 +00:00
|
|
|
olddir :: Git.Repo -> FilePath
|
|
|
|
olddir g
|
|
|
|
| Git.repoIsLocalBare g = ""
|
|
|
|
| otherwise = ".git-annex"
|
2011-06-23 06:30:20 +00:00
|
|
|
|
2011-06-23 03:37:46 +00:00
|
|
|
{- .git-annex/ moved to a git-annex branch.
|
|
|
|
-
|
|
|
|
- Strategy:
|
|
|
|
-
|
|
|
|
- * Create the git-annex branch.
|
|
|
|
- * Find each location log file in .git-annex/, and inject its content
|
|
|
|
- into the git-annex branch, unioning with any content already in
|
|
|
|
- there. (in passing, this deals with the semi transition that left
|
|
|
|
- some location logs hashed two different ways; both are found and
|
|
|
|
- merged).
|
|
|
|
- * Also inject remote.log, trust.log, and uuid.log.
|
|
|
|
- * git rm -rf .git-annex
|
|
|
|
- * Remove stuff that used to be needed in .gitattributes.
|
|
|
|
- * Commit changes.
|
|
|
|
-}
|
2011-06-22 21:51:48 +00:00
|
|
|
upgrade :: Annex Bool
|
|
|
|
upgrade = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "v2 to v3"
|
2011-06-23 06:30:20 +00:00
|
|
|
g <- Annex.gitRepo
|
2011-06-24 05:15:12 +00:00
|
|
|
let bare = Git.repoIsLocalBare g
|
|
|
|
|
2011-06-23 06:30:20 +00:00
|
|
|
Branch.create
|
2011-07-19 18:07:23 +00:00
|
|
|
showProgress
|
|
|
|
|
2011-06-24 16:09:04 +00:00
|
|
|
e <- liftIO $ doesDirectoryExist (olddir g)
|
|
|
|
when e $ do
|
|
|
|
mapM_ (\(k, f) -> inject f $ logFile k) =<< locationLogs g
|
|
|
|
mapM_ (\f -> inject f f) =<< logFiles (olddir g)
|
2011-06-23 18:49:21 +00:00
|
|
|
|
2011-06-24 06:33:44 +00:00
|
|
|
saveState
|
2011-07-19 18:07:23 +00:00
|
|
|
showProgress
|
2011-07-05 19:06:20 +00:00
|
|
|
|
|
|
|
when e $ liftIO $ do
|
|
|
|
Git.run g "rm" [Param "-r", Param "-f", Param "-q", File (olddir g)]
|
|
|
|
unless bare $ gitAttributesUnWrite g
|
2011-07-19 18:07:23 +00:00
|
|
|
showProgress
|
2011-07-05 19:06:20 +00:00
|
|
|
|
2011-07-15 07:12:05 +00:00
|
|
|
unless bare push
|
2011-06-23 18:49:21 +00:00
|
|
|
|
2011-06-23 06:30:20 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
locationLogs :: Git.Repo -> Annex [(Key, FilePath)]
|
|
|
|
locationLogs repo = liftIO $ do
|
|
|
|
levela <- dirContents dir
|
|
|
|
levelb <- mapM tryDirContents levela
|
|
|
|
files <- mapM tryDirContents (concat levelb)
|
2011-07-15 07:12:05 +00:00
|
|
|
return $ mapMaybe islogfile (concat files)
|
2011-06-23 06:30:20 +00:00
|
|
|
where
|
|
|
|
tryDirContents d = catch (dirContents d) (return . const [])
|
|
|
|
dir = gitStateDir repo
|
2011-07-15 07:12:05 +00:00
|
|
|
islogfile f = maybe Nothing (\k -> Just (k, f)) $
|
2011-06-23 06:30:20 +00:00
|
|
|
logFileKey $ takeFileName f
|
|
|
|
|
|
|
|
inject :: FilePath -> FilePath -> Annex ()
|
|
|
|
inject source dest = do
|
2011-06-24 05:13:33 +00:00
|
|
|
g <- Annex.gitRepo
|
|
|
|
new <- liftIO (readFile $ olddir g </> source)
|
2011-06-23 06:30:20 +00:00
|
|
|
prev <- Branch.get dest
|
|
|
|
Branch.change dest $ unlines $ nub $ lines prev ++ lines new
|
2011-07-19 18:07:23 +00:00
|
|
|
showProgress
|
2011-06-23 06:30:20 +00:00
|
|
|
|
|
|
|
logFiles :: FilePath -> Annex [FilePath]
|
|
|
|
logFiles dir = return . filter (".log" `isSuffixOf`)
|
|
|
|
=<< liftIO (getDirectoryContents dir)
|
2011-06-22 20:02:43 +00:00
|
|
|
|
2011-06-24 15:59:34 +00:00
|
|
|
push :: Annex ()
|
|
|
|
push = do
|
|
|
|
origin_master <- Branch.refExists "origin/master"
|
|
|
|
origin_gitannex <- Branch.hasOrigin
|
|
|
|
case (origin_master, origin_gitannex) of
|
|
|
|
(_, True) -> do
|
|
|
|
-- Merge in the origin's git-annex branch,
|
|
|
|
-- so that pushing the git-annex branch
|
|
|
|
-- will immediately work. Not pushed here,
|
|
|
|
-- because it's less obnoxious to let the user
|
|
|
|
-- push.
|
|
|
|
Branch.update
|
|
|
|
(True, False) -> do
|
|
|
|
-- push git-annex to origin, so that
|
|
|
|
-- "git push" will from then on
|
|
|
|
-- automatically push it
|
|
|
|
Branch.update -- just in case
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "pushing new git-annex branch to origin"
|
|
|
|
showOutput
|
2011-06-24 15:59:34 +00:00
|
|
|
g <- Annex.gitRepo
|
|
|
|
liftIO $ Git.run g "push" [Param "origin", Param Branch.name]
|
|
|
|
_ -> do
|
|
|
|
-- no origin exists, so just let the user
|
|
|
|
-- know about the new branch
|
|
|
|
Branch.update
|
|
|
|
showLongNote $
|
|
|
|
"git-annex branch created\n" ++
|
|
|
|
"Be sure to push this branch when pushing to remotes.\n"
|
2011-07-19 18:07:23 +00:00
|
|
|
showOutput
|
2011-06-24 15:59:34 +00:00
|
|
|
|
2011-06-22 20:02:43 +00:00
|
|
|
{- Old .gitattributes contents, not needed anymore. -}
|
|
|
|
attrLines :: [String]
|
|
|
|
attrLines =
|
|
|
|
[ stateDir </> "*.log merge=union"
|
|
|
|
, stateDir </> "*/*/*.log merge=union"
|
|
|
|
]
|
|
|
|
|
|
|
|
gitAttributesUnWrite :: Git.Repo -> IO ()
|
|
|
|
gitAttributesUnWrite repo = do
|
|
|
|
let attributes = Git.attributes repo
|
|
|
|
whenM (doesFileExist attributes) $ do
|
|
|
|
c <- readFileStrict attributes
|
2011-06-30 04:42:09 +00:00
|
|
|
liftIO $ viaTmp writeFile attributes $ unlines $
|
2011-07-15 07:12:05 +00:00
|
|
|
filter (`notElem` attrLines) $ lines c
|
2011-06-23 06:30:20 +00:00
|
|
|
Git.run repo "add" [File attributes]
|
2011-06-23 12:48:13 +00:00
|
|
|
|
|
|
|
stateDir :: FilePath
|
2011-07-15 07:12:05 +00:00
|
|
|
stateDir = addTrailingPathSeparator ".git-annex"
|
2011-06-23 12:48:13 +00:00
|
|
|
gitStateDir :: Git.Repo -> FilePath
|
|
|
|
gitStateDir repo = addTrailingPathSeparator $ Git.workTree repo </> stateDir
|