git-annex/Command/Init.hs

124 lines
3.1 KiB
Haskell
Raw Normal View History

{- git-annex command
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.Init where
import Control.Monad.State (liftIO)
2011-03-16 15:53:46 +00:00
import Control.Monad (when, unless)
2010-11-14 18:44:24 +00:00
import System.Directory
import System.FilePath
import Command
import qualified Annex
import qualified GitRepo as Git
import UUID
2010-11-08 20:40:28 +00:00
import Version
2010-11-08 19:15:21 +00:00
import Messages
2010-11-14 18:44:24 +00:00
import Locations
2010-12-03 04:33:41 +00:00
import Types
2011-01-28 16:35:51 +00:00
import Utility
command :: [Command]
command = [repoCommand "init" paramDesc seek
"initialize git-annex with repository description"]
seek :: [CommandSeek]
seek = [withString start]
2010-11-11 22:54:52 +00:00
{- Stores description for the repository etc. -}
start :: CommandStartString
start description = do
2010-11-22 21:51:55 +00:00
when (null description) $
error "please specify a description of this repository\n"
showStart "init" description
return $ Just $ perform description
perform :: String -> CommandPerform
perform description = do
g <- Annex.gitRepo
u <- getUUID g
2010-11-08 20:40:28 +00:00
setVersion
if Git.repoIsLocalBare g
then do
showLongNote $
"This is a bare repository, so its description cannot be committed.\n" ++
"To record the description, run this command in a clone of this repository:\n" ++
2011-03-03 21:22:17 +00:00
" git annex describe " ++ show u ++ " " ++ show description ++ "\n\n"
return $ Just $ return True
else do
describeUUID u description
liftIO $ gitAttributesWrite g
gitPreCommitHookWrite g
return $ Just cleanup
cleanup :: CommandCleanup
cleanup = do
g <- Annex.gitRepo
logfile <- uuidLog
liftIO $ Git.run g "add" [File logfile]
liftIO $ Git.run g "commit"
[ Params "-q -m"
2011-03-03 21:21:00 +00:00
, Param "git annex repository description"
, File logfile
]
return True
2010-11-14 18:44:24 +00:00
{- configure git to use union merge driver on state files, if it is not
- already -}
2010-12-03 04:33:41 +00:00
gitAttributesWrite :: Git.Repo -> IO ()
gitAttributesWrite repo = do
2010-11-14 18:44:24 +00:00
exists <- doesFileExist attributes
2010-11-22 21:51:55 +00:00
if not exists
2010-11-14 18:44:24 +00:00
then do
2011-03-16 15:53:46 +00:00
safeWriteFile attributes $ unlines attrLines
2010-11-14 18:44:24 +00:00
commit
else do
content <- readFile attributes
2011-03-16 15:53:46 +00:00
let present = lines content
let missing = filter (\l -> not $ l `elem` present) attrLines
unless (null missing) $ do
appendFile attributes $ unlines missing
2010-11-14 18:44:24 +00:00
commit
where
attributes = Git.attributes repo
commit = do
Git.run repo "add" [Param attributes]
Git.run repo "commit"
[ Params "-q -m"
, Param "git-annex setup"
, Param attributes
]
2010-11-14 18:44:24 +00:00
2011-03-16 15:53:46 +00:00
attrLines :: [String]
attrLines =
[ stateDir </> "*.log merge=union"
, stateDir </> "*/*/*.log merge=union"
]
2010-12-03 04:33:41 +00:00
2010-11-14 18:44:24 +00:00
{- set up a git pre-commit hook, if one is not already present -}
2010-12-03 04:33:41 +00:00
gitPreCommitHookWrite :: Git.Repo -> Annex ()
gitPreCommitHookWrite repo = do
exists <- liftIO $ doesFileExist hook
2010-11-22 21:51:55 +00:00
if exists
2010-12-03 04:33:41 +00:00
then warning $ "pre-commit hook (" ++ hook ++ ") already exists, not configuring"
else liftIO $ do
2011-01-28 16:35:51 +00:00
safeWriteFile hook preCommitScript
2010-11-14 18:44:24 +00:00
p <- getPermissions hook
setPermissions hook $ p {executable = True}
2010-12-03 04:33:41 +00:00
where
hook = preCommitHook repo
preCommitHook :: Git.Repo -> FilePath
preCommitHook repo =
Git.workTree repo ++ "/" ++ Git.gitDir repo ++ "/hooks/pre-commit"
preCommitScript :: String
preCommitScript =
"#!/bin/sh\n" ++
"# automatically configured by git-annex\n" ++
"git annex pre-commit .\n"