git-annex/Command/Init.hs

73 lines
1.7 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)
import Control.Monad (when, unless)
2010-11-14 18:44:24 +00:00
import System.Directory
import Command
import qualified Annex
import qualified Git
import qualified Branch
import UUID
2010-11-08 20:40:28 +00:00
import Version
2010-11-08 19:15:21 +00:00
import Messages
2010-12-03 04:33:41 +00:00
import Types
2011-01-28 16:35:51 +00:00
import Utility
command :: [Command]
command = [standaloneCommand "init" paramDesc seek
"initialize git-annex with repository description"]
seek :: [CommandSeek]
seek = [withWords start]
2010-11-11 22:54:52 +00:00
start :: CommandStartWords
start ws = do
2010-11-22 21:51:55 +00:00
when (null description) $
error "please specify a description of this repository\n"
showStart "init" description
2011-05-15 06:02:46 +00:00
next $ perform description
where
description = unwords ws
perform :: String -> CommandPerform
perform description = do
Branch.create
g <- Annex.gitRepo
u <- getUUID g
2010-11-08 20:40:28 +00:00
setVersion
describeUUID u description
unless (Git.repoIsLocalBare g) $
gitPreCommitHookWrite g
next $ return True
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-06-30 04:42:09 +00:00
viaTmp writeFile 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"