convert GitRepo to struct with constructor

This commit is contained in:
Joey Hess 2010-10-10 15:54:02 -04:00
parent 7880dc16fe
commit dce9c2e080
7 changed files with 71 additions and 50 deletions

View file

@ -3,16 +3,12 @@
module Annex where module Annex where
import Backend
import System.Posix.Files import System.Posix.Files
import System.Directory import System.Directory
import GitRepo import GitRepo
import Utility import Utility
import Locations
{- An annexed file's content is stored somewhere under .git/annex/ -} import Backend
annexLoc repo key = do
dir <- gitDir repo
return $ dir ++ "/annex/" ++ key
{- Annexes a file, storing it in a backend, and then moving it into {- Annexes a file, storing it in a backend, and then moving it into
- the annex directory and setting up the symlink pointing to its content. -} - the annex directory and setting up the symlink pointing to its content. -}
@ -28,8 +24,28 @@ annexFile backends repo file = do
Just key -> symlink key Just key -> symlink key
where where
symlink key = do symlink key = do
dest <- annexLoc repo key dest <- annexDir repo key
createDirectoryIfMissing True (parentDir dest) createDirectoryIfMissing True (parentDir dest)
renameFile file dest renameFile file dest
createSymbolicLink dest file createSymbolicLink dest file
gitAdd repo file gitAdd repo file
{- Sets up a git repo for git-annex. May be called repeatedly. -}
gitPrep :: GitRepo -> IO ()
gitPrep repo = do
-- configure git to use union merge driver on state files
let attrLine = stateLoc ++ "/* merge=union"
attributes <- gitAttributes repo
exists <- doesFileExist attributes
if (not exists)
then do
writeFile attributes $ attrLine ++ "\n"
gitAdd repo attributes
else do
content <- readFile attributes
if (all (/= attrLine) (lines content))
then do
appendFile attributes $ attrLine ++ "\n"
gitAdd repo attributes
else return ()

View file

@ -19,6 +19,7 @@
module Backend where module Backend where
import System.Directory import System.Directory
import Locations
import GitRepo import GitRepo
import Utility import Utility

View file

@ -17,11 +17,13 @@ backend = Backend {
keyValue :: GitRepo -> FilePath -> IO (Maybe Key) keyValue :: GitRepo -> FilePath -> IO (Maybe Key)
keyValue repo file = return $ Just file keyValue repo file = return $ Just file
-- This backend does not really do any independant data storage, {- This backend does not really do any independant data storage,
-- it relies on the file contents in .git/annex/ in this repo, - it relies on the file contents in .git/annex/ in this repo,
-- and other accessible repos. So storing a file is a no-op. - and other accessible repos. So storing a file is a no-op. -}
dummyStore :: GitRepo -> FilePath -> Key -> IO (Bool) dummyStore :: GitRepo -> FilePath -> Key -> IO (Bool)
dummyStore repo file key = return True dummyStore repo file key = return True
{- Try to find a copy of the file in one of the other repos,
- and copy it over to this one. -}
copyFromOtherRepo :: IO Key -> FilePath -> IO (Bool) copyFromOtherRepo :: IO Key -> FilePath -> IO (Bool)
copyFromOtherRepo key file = error "copyFromOtherRepo unimplemented" -- TODO copyFromOtherRepo key file = error "copyFromOtherRepo unimplemented" -- TODO

View file

@ -8,79 +8,62 @@ import System.Path
import Data.String.Utils import Data.String.Utils
import Utility import Utility
type GitRepo = FilePath data GitRepo = GitRepo {
top :: FilePath,
remotes :: [GitRepo]
} deriving (Eq, Show, Read)
{- Long-term state is stored in files inside the .git-annex directory {- GitRepo constructor -}
- in the git repository. -} gitRepo :: FilePath -> IO GitRepo
stateLoc = ".git-annex" gitRepo dir = do
gitStateDir :: GitRepo -> FilePath -- TOOD query repo for configuration settings; other repositories; etc
gitStateDir repo = repo ++ "/" ++ stateLoc ++ "/" return GitRepo { top = dir, remotes = [] }
{- Path to a repository's gitattributes file. -} {- Path to a repository's gitattributes file. -}
gitAttributes :: GitRepo -> IO String gitAttributes :: GitRepo -> IO String
gitAttributes repo = do gitAttributes repo = do
bare <- isBareRepo repo bare <- isBareRepo (top repo)
if (bare) if (bare)
then return $ repo ++ "/info/.gitattributes" then return $ (top repo) ++ "/info/.gitattributes"
else return $ repo ++ "/.gitattributes" else return $ (top repo) ++ "/.gitattributes"
{- Path to a repository's .git directory. {- Path to a repository's .git directory.
- (For a bare repository, that is the root of the repository.) - (For a bare repository, that is the root of the repository.)
- TODO: support GIT_DIR -} - TODO: support GIT_DIR -}
gitDir :: GitRepo -> IO String gitDir :: GitRepo -> IO String
gitDir repo = do gitDir repo = do
bare <- isBareRepo repo bare <- isBareRepo (top repo)
if (bare) if (bare)
then return $ repo then return $ (top repo)
else return $ repo ++ "/.git" else return $ (top repo) ++ "/.git"
{- Given a relative or absolute filename, calculates the name to use {- Given a relative or absolute filename, calculates the name to use
- relative to a git repository directory (which must be absolute). - to refer to the file relative to a git repository directory.
- This is the same form displayed and used by git. -} - This is the same form displayed and used by git. -}
gitRelative :: GitRepo -> String -> String gitRelative :: GitRepo -> String -> String
gitRelative repo file = drop (length absrepo) absfile gitRelative repo file = drop (length absrepo) absfile
where where
-- normalize both repo and file, so that repo -- normalize both repo and file, so that repo
-- will be substring of file -- will be substring of file
absrepo = case (absNormPath "/" repo) of absrepo = case (absNormPath "/" (top repo)) of
Just f -> f ++ "/" Just f -> f ++ "/"
Nothing -> error $ "bad repo" ++ repo Nothing -> error $ "bad repo" ++ (top repo)
absfile = case (secureAbsNormPath absrepo file) of absfile = case (secureAbsNormPath absrepo file) of
Just f -> f Just f -> f
Nothing -> error $ file ++ " is not located inside git repository " ++ absrepo Nothing -> error $ file ++ " is not located inside git repository " ++ absrepo
{- Sets up a git repo for git-annex. May be called repeatedly. -}
gitPrep :: GitRepo -> IO ()
gitPrep repo = do
-- configure git to use union merge driver on state files
let attrLine = stateLoc ++ "/* merge=union"
attributes <- gitAttributes repo
exists <- doesFileExist attributes
if (not exists)
then do
writeFile attributes $ attrLine ++ "\n"
gitAdd repo attributes
else do
content <- readFile attributes
if (all (/= attrLine) (lines content))
then do
appendFile attributes $ attrLine ++ "\n"
gitAdd repo attributes
else return ()
{- Stages a changed file in git's index. -} {- Stages a changed file in git's index. -}
gitAdd repo file = do gitAdd repo file = do
-- TODO -- TODO
return () return ()
{- Finds the top of the current git repository, which may be in a parent {- Finds the current git repository, which may be in a parent directory. -}
- directory. -} currentRepo :: IO GitRepo
repoTop :: IO GitRepo currentRepo = do
repoTop = do
cwd <- getCurrentDirectory cwd <- getCurrentDirectory
top <- seekUp cwd isRepoTop top <- seekUp cwd isRepoTop
case top of case top of
(Just dir) -> return dir (Just dir) -> gitRepo dir
Nothing -> error "Not in a git repository." Nothing -> error "Not in a git repository."
seekUp :: String -> (String -> IO Bool) -> IO (Maybe String) seekUp :: String -> (String -> IO Bool) -> IO (Maybe String)

View file

@ -25,6 +25,7 @@ import System.Directory
import Data.Char import Data.Char
import GitRepo import GitRepo
import Utility import Utility
import Locations
data LogStatus = FilePresent | FileMissing | Undefined data LogStatus = FilePresent | FileMissing | Undefined
deriving (Eq) deriving (Eq)

18
Locations.hs Normal file
View file

@ -0,0 +1,18 @@
{- git-annex file locations
-}
module Locations where
import GitRepo
{- An annexed file's content is stored somewhere under .git/annex/ -}
annexDir :: GitRepo -> String -> IO FilePath
annexDir repo key = do
dir <- gitDir repo
return $ dir ++ "/annex/" ++ key
{- Long-term state is stored in files inside the .git-annex directory
- in the git repository. -}
stateLoc = ".git-annex"
gitStateDir :: GitRepo -> FilePath
gitStateDir repo = (top repo) ++ "/" ++ stateLoc ++ "/"

View file

@ -13,7 +13,7 @@ import qualified BackendUrl
backends = [BackendFile.backend, BackendChecksum.backend, BackendUrl.backend] backends = [BackendFile.backend, BackendChecksum.backend, BackendUrl.backend]
main = do main = do
repo <- repoTop repo <- currentRepo
gitPrep repo gitPrep repo
l <- readLog "demo.log" l <- readLog "demo.log"