2010-11-02 23:04:24 +00:00
|
|
|
{- 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
|
2011-01-27 21:00:32 +00:00
|
|
|
import System.FilePath
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
|
|
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
|
2010-12-30 19:06:26 +00:00
|
|
|
|
|
|
|
command :: [Command]
|
2011-03-19 22:58:49 +00:00
|
|
|
command = [repoCommand "init" paramDesc seek
|
2010-12-30 19:06:26 +00:00
|
|
|
"initialize git-annex with repository description"]
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2010-11-13 20:03:25 +00:00
|
|
|
seek = [withString start]
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
{- Stores description for the repository etc. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
start :: CommandStartString
|
2010-11-02 23:04:24 +00:00
|
|
|
start description = do
|
2010-11-22 21:51:55 +00:00
|
|
|
when (null description) $
|
|
|
|
error "please specify a description of this repository\n"
|
2010-11-02 23:04:24 +00:00
|
|
|
showStart "init" description
|
|
|
|
return $ Just $ perform description
|
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
perform :: String -> CommandPerform
|
2010-11-02 23:04:24 +00:00
|
|
|
perform description = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
u <- getUUID g
|
2010-11-08 20:40:28 +00:00
|
|
|
setVersion
|
2011-03-03 20:40:55 +00:00
|
|
|
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"
|
2011-03-03 20:40:55 +00:00
|
|
|
return $ Just $ return True
|
|
|
|
else do
|
|
|
|
describeUUID u description
|
|
|
|
liftIO $ gitAttributesWrite g
|
|
|
|
gitPreCommitHookWrite g
|
|
|
|
return $ Just cleanup
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
cleanup :: CommandCleanup
|
2010-11-02 23:04:24 +00:00
|
|
|
cleanup = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
logfile <- uuidLog
|
2011-02-28 20:10:16 +00:00
|
|
|
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"
|
2011-02-28 20:10:16 +00:00
|
|
|
, File logfile
|
|
|
|
]
|
2010-11-02 23:04:24 +00:00
|
|
|
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
|
2011-02-28 20:10:16 +00:00
|
|
|
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"
|