summaryrefslogtreecommitdiff
path: root/Version.hs
blob: 947af8cef5bb781bce4ab1785f26b4da7c57083c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{- git-annex repository versioning
 -
 - Copyright 2010 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Version where

import Control.Monad.State (liftIO)
import Control.Monad (unless)
import System.Directory

import Types
import qualified Annex
import qualified GitRepo as Git
import Locations
import Config

type Version = String

defaultVersion :: Version
defaultVersion = "2"

supportedVersions :: [Version]
supportedVersions = [defaultVersion]

upgradableVersions :: [Version]
upgradableVersions = ["0", "1"]

versionField :: String
versionField = "annex.version"

getVersion :: Annex Version
getVersion = do
	g <- Annex.gitRepo
	let v = Git.configGet g versionField ""
	if not $ null v
		then return v
		else do
			-- version 0 was not recorded in .git/config;
			-- such a repo should have an gitAnnexDir but no
			-- gitAnnexObjectDir.
			--
			-- version 1 may not be recorded if the user
			-- forgot to init. Such a repo should have a
			-- gitAnnexObjectDir already.
			d <- liftIO $ doesDirectoryExist $ gitAnnexDir g
			o <- liftIO $ doesDirectoryExist $ gitAnnexObjectDir g
			case (d, o) of
				(True, False) -> return "0"
				(True, True) -> return "1"
				_ -> do
					setVersion
					return defaultVersion

setVersion :: Annex ()
setVersion = setConfig versionField defaultVersion

checkVersion :: Annex ()
checkVersion = do
	v <- getVersion
	unless (v `elem` supportedVersions) $ do
		error $ "Repository version " ++ v ++ 
			" is not supported. " ++
			msg v
	where
		msg v
			| v `elem` upgradableVersions = "Upgrade this repository: git-annex upgrade"
			| otherwise = "Upgrade git-annex."