2011-08-17 18:14:43 +00:00
|
|
|
{- git-annex repository initialization
|
|
|
|
-
|
2019-05-23 16:51:01 +00:00
|
|
|
- Copyright 2011-2019 Joey Hess <id@joeyh.name>
|
2011-08-17 18:14:43 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-08-17 18:14:43 +00:00
|
|
|
-}
|
|
|
|
|
2013-05-11 20:03:00 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2014-01-26 20:36:31 +00:00
|
|
|
module Annex.Init (
|
2011-08-17 22:38:26 +00:00
|
|
|
ensureInitialized,
|
2012-07-31 20:19:24 +00:00
|
|
|
isInitialized,
|
2011-08-17 18:36:20 +00:00
|
|
|
initialize,
|
2014-04-16 00:13:35 +00:00
|
|
|
initialize',
|
2013-02-15 18:17:31 +00:00
|
|
|
uninitialize,
|
2013-11-05 19:29:56 +00:00
|
|
|
probeCrippledFileSystem,
|
2016-02-16 19:30:59 +00:00
|
|
|
probeCrippledFileSystem',
|
2011-08-17 18:36:20 +00:00
|
|
|
) where
|
2011-08-17 18:14:43 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2013-05-14 00:48:44 +00:00
|
|
|
import qualified Annex
|
2011-08-17 18:14:43 +00:00
|
|
|
import qualified Git
|
2013-04-04 17:14:55 +00:00
|
|
|
import qualified Git.Config
|
2014-09-05 17:44:09 +00:00
|
|
|
import qualified Git.Objects
|
2011-10-04 04:40:47 +00:00
|
|
|
import qualified Annex.Branch
|
2011-11-02 18:18:21 +00:00
|
|
|
import Logs.UUID
|
2014-09-05 17:44:09 +00:00
|
|
|
import Logs.Trust.Basic
|
2017-02-27 20:08:16 +00:00
|
|
|
import Logs.Config
|
2014-09-05 17:44:09 +00:00
|
|
|
import Types.TrustLevel
|
2018-10-25 21:23:53 +00:00
|
|
|
import Types.RepoVersion
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Version
|
2015-01-27 21:38:06 +00:00
|
|
|
import Annex.Difference
|
2011-10-15 21:47:03 +00:00
|
|
|
import Annex.UUID
|
2016-10-17 18:58:33 +00:00
|
|
|
import Annex.WorkTree
|
2013-02-14 18:10:36 +00:00
|
|
|
import Config
|
2019-02-05 18:43:23 +00:00
|
|
|
import Config.Files
|
2018-08-28 14:26:51 +00:00
|
|
|
import Config.Smudge
|
2019-08-27 18:01:28 +00:00
|
|
|
import qualified Upgrade.V5.Direct as Direct
|
2018-12-03 16:57:23 +00:00
|
|
|
import qualified Annex.AdjustedBranch as AdjustedBranch
|
2013-07-05 16:24:28 +00:00
|
|
|
import Annex.Environment
|
2014-12-29 21:25:59 +00:00
|
|
|
import Annex.Hook
|
2015-12-09 19:42:16 +00:00
|
|
|
import Annex.InodeSentinal
|
2014-12-29 21:25:59 +00:00
|
|
|
import Upgrade
|
2019-01-17 19:40:44 +00:00
|
|
|
import Annex.Tmp
|
2013-08-04 17:07:55 +00:00
|
|
|
import Utility.UserInfo
|
2016-06-13 18:54:34 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2019-05-23 16:13:56 +00:00
|
|
|
import Annex.Perms
|
2013-08-04 17:07:55 +00:00
|
|
|
import Utility.FileMode
|
2015-08-19 16:36:17 +00:00
|
|
|
import System.Posix.User
|
2015-11-13 17:35:29 +00:00
|
|
|
import qualified Utility.LockFile.Posix as Posix
|
2019-09-12 18:11:19 +00:00
|
|
|
import Data.Either
|
2013-08-04 17:07:55 +00:00
|
|
|
#endif
|
2012-08-03 14:45:18 +00:00
|
|
|
|
2019-05-23 16:51:01 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2019-02-05 18:43:23 +00:00
|
|
|
checkCanInitialize :: Annex a -> Annex a
|
2019-02-08 17:20:24 +00:00
|
|
|
checkCanInitialize a = inRepo (noAnnexFileContent . Git.repoWorkTree) >>= \case
|
2017-12-13 18:34:32 +00:00
|
|
|
Nothing -> a
|
2019-02-05 18:43:23 +00:00
|
|
|
Just noannexmsg -> do
|
|
|
|
warning "Initialization prevented by .noannex file (remove the file to override)"
|
|
|
|
unless (null noannexmsg) $
|
|
|
|
warning noannexmsg
|
|
|
|
giveup "Not initialized."
|
2017-12-13 18:34:32 +00:00
|
|
|
|
2019-01-01 19:39:45 +00:00
|
|
|
genDescription :: Maybe String -> Annex UUIDDesc
|
|
|
|
genDescription (Just d) = return $ UUIDDesc $ encodeBS d
|
2012-08-03 14:45:18 +00:00
|
|
|
genDescription Nothing = do
|
2015-01-22 18:59:57 +00:00
|
|
|
reldir <- liftIO . relHome =<< liftIO . absPath =<< fromRepo Git.repoPath
|
2013-04-03 07:52:41 +00:00
|
|
|
hostname <- fromMaybe "" <$> liftIO getHostname
|
2012-08-03 14:45:18 +00:00
|
|
|
let at = if null hostname then "" else "@"
|
2016-06-08 19:04:15 +00:00
|
|
|
v <- liftIO myUserName
|
2019-01-01 19:39:45 +00:00
|
|
|
return $ UUIDDesc $ encodeBS $ concat $ case v of
|
2016-06-08 19:04:15 +00:00
|
|
|
Right username -> [username, at, hostname, ":", reldir]
|
|
|
|
Left _ -> [hostname, ":", reldir]
|
2012-08-03 14:45:18 +00:00
|
|
|
|
2019-02-05 18:43:23 +00:00
|
|
|
initialize :: Maybe String -> Maybe RepoVersion -> Annex ()
|
|
|
|
initialize mdescription mversion = checkCanInitialize $ do
|
2015-09-09 17:56:37 +00:00
|
|
|
{- Has to come before any commits are made as the shared
|
|
|
|
- clone heuristic expects no local objects. -}
|
|
|
|
sharedclone <- checkSharedClone
|
|
|
|
|
2015-04-20 18:01:41 +00:00
|
|
|
{- This will make the first commit to git, so ensure git is set up
|
|
|
|
- properly to allow commits when running it. -}
|
|
|
|
ensureCommit $ Annex.Branch.create
|
|
|
|
|
2011-08-17 18:14:43 +00:00
|
|
|
prepUUID
|
2019-02-05 18:43:23 +00:00
|
|
|
initialize' mversion
|
2015-09-09 17:56:37 +00:00
|
|
|
|
|
|
|
initSharedClone sharedclone
|
2014-04-16 00:13:35 +00:00
|
|
|
|
|
|
|
u <- getUUID
|
2019-05-23 16:51:01 +00:00
|
|
|
{- Avoid overwriting existing description with a default
|
|
|
|
- description. -}
|
2019-06-21 00:30:24 +00:00
|
|
|
whenM (pure (isJust mdescription) <||> not . M.member u <$> uuidDescMapRaw) $
|
2019-05-23 16:51:01 +00:00
|
|
|
describeUUID u =<< genDescription mdescription
|
2014-04-16 00:13:35 +00:00
|
|
|
|
2015-11-13 17:35:29 +00:00
|
|
|
-- Everything except for uuid setup, shared clone setup, and initial
|
|
|
|
-- description.
|
2019-02-05 18:43:23 +00:00
|
|
|
initialize' :: Maybe RepoVersion -> Annex ()
|
|
|
|
initialize' mversion = checkCanInitialize $ do
|
2015-11-13 17:35:29 +00:00
|
|
|
checkLockSupport
|
2013-04-04 17:14:55 +00:00
|
|
|
checkFifoSupport
|
2019-08-30 17:54:57 +00:00
|
|
|
checkCrippledFileSystem
|
2017-02-17 18:04:43 +00:00
|
|
|
unlessM isBareRepo $ do
|
2013-11-05 19:29:56 +00:00
|
|
|
hookWrite preCommitHook
|
2017-02-17 18:04:43 +00:00
|
|
|
hookWrite postReceiveHook
|
2015-01-27 21:38:06 +00:00
|
|
|
setDifferences
|
2015-12-15 21:17:13 +00:00
|
|
|
unlessM (isJust <$> getVersion) $
|
2019-08-30 17:54:57 +00:00
|
|
|
setVersion (fromMaybe defaultVersion mversion)
|
|
|
|
configureSmudgeFilter
|
|
|
|
showSideAction "scanning for unlocked files"
|
|
|
|
scanUnlockedFiles True
|
|
|
|
unlessM isBareRepo $ do
|
|
|
|
hookWrite postCheckoutHook
|
|
|
|
hookWrite postMergeHook
|
2018-12-03 16:57:23 +00:00
|
|
|
AdjustedBranch.checkAdjustedClone >>= \case
|
|
|
|
AdjustedBranch.InAdjustedClone -> return ()
|
|
|
|
AdjustedBranch.NotInAdjustedClone ->
|
2016-06-02 20:59:15 +00:00
|
|
|
ifM (crippledFileSystem <&&> (not <$> isBareRepo))
|
2019-08-26 18:52:55 +00:00
|
|
|
( AdjustedBranch.adjustToCrippledFileSystem
|
2016-06-02 19:58:22 +00:00
|
|
|
-- Handle case where this repo was cloned from a
|
|
|
|
-- direct mode repo
|
2016-06-02 20:59:15 +00:00
|
|
|
, unlessM isBareRepo
|
2019-08-26 19:52:19 +00:00
|
|
|
Direct.switchHEADBack
|
2016-06-02 19:58:22 +00:00
|
|
|
)
|
2017-02-27 20:08:16 +00:00
|
|
|
propigateSecureHashesOnly
|
2015-12-09 19:42:16 +00:00
|
|
|
createInodeSentinalFile False
|
2011-08-17 18:14:43 +00:00
|
|
|
|
|
|
|
uninitialize :: Annex ()
|
2012-04-27 16:21:38 +00:00
|
|
|
uninitialize = do
|
2019-10-08 18:34:00 +00:00
|
|
|
unHook
|
|
|
|
deconfigureSmudgeFilter
|
2012-04-27 16:21:38 +00:00
|
|
|
removeRepoUUID
|
2012-10-07 20:04:03 +00:00
|
|
|
removeVersion
|
2011-08-17 18:14:43 +00:00
|
|
|
|
2011-08-17 22:38:26 +00:00
|
|
|
{- Will automatically initialize if there is already a git-annex
|
2012-12-13 04:45:27 +00:00
|
|
|
- branch from somewhere. Otherwise, require a manual init
|
2017-02-11 09:38:49 +00:00
|
|
|
- to avoid git-annex accidentally being run in git
|
2013-11-05 20:42:59 +00:00
|
|
|
- repos that did not intend to use it.
|
|
|
|
-
|
|
|
|
- Checks repository version and handles upgrades too.
|
|
|
|
-}
|
2011-08-17 22:38:26 +00:00
|
|
|
ensureInitialized :: Annex ()
|
2019-09-01 17:29:55 +00:00
|
|
|
ensureInitialized = getVersion >>= maybe needsinit checkUpgrade
|
2012-10-29 01:27:15 +00:00
|
|
|
where
|
|
|
|
needsinit = ifM Annex.Branch.hasSibling
|
2019-08-27 16:23:34 +00:00
|
|
|
( initialize Nothing Nothing
|
|
|
|
, giveup $ "First run: git-annex init"
|
|
|
|
)
|
2011-08-17 18:36:20 +00:00
|
|
|
|
2012-07-31 20:19:24 +00:00
|
|
|
{- Checks if a repository is initialized. Does not check version for ugrade. -}
|
|
|
|
isInitialized :: Annex Bool
|
|
|
|
isInitialized = maybe Annex.Branch.hasSibling (const $ return True) =<< getVersion
|
|
|
|
|
2013-05-14 00:48:44 +00:00
|
|
|
{- A crippled filesystem is one that does not allow making symlinks,
|
|
|
|
- or removing write access from files. -}
|
2013-02-15 18:17:31 +00:00
|
|
|
probeCrippledFileSystem :: Annex Bool
|
2019-09-10 17:37:07 +00:00
|
|
|
probeCrippledFileSystem = withEventuallyCleanedOtherTmp $ \tmp -> do
|
2017-09-29 18:58:23 +00:00
|
|
|
(r, warnings) <- liftIO $ probeCrippledFileSystem' tmp
|
|
|
|
mapM_ warning warnings
|
|
|
|
return r
|
2016-02-16 19:30:59 +00:00
|
|
|
|
2017-09-29 18:58:23 +00:00
|
|
|
probeCrippledFileSystem' :: FilePath -> IO (Bool, [String])
|
2013-08-04 17:07:55 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2017-10-24 16:17:38 +00:00
|
|
|
probeCrippledFileSystem' _ = return (True, [])
|
2013-05-11 20:03:00 +00:00
|
|
|
#else
|
2016-05-05 19:48:58 +00:00
|
|
|
probeCrippledFileSystem' tmp = do
|
2013-02-15 18:17:31 +00:00
|
|
|
let f = tmp </> "gaprobe"
|
2017-09-29 18:58:23 +00:00
|
|
|
writeFile f ""
|
|
|
|
r <- probe f
|
|
|
|
void $ tryIO $ allowWrite f
|
|
|
|
removeFile f
|
|
|
|
return r
|
2013-02-14 18:10:36 +00:00
|
|
|
where
|
2017-09-29 18:58:23 +00:00
|
|
|
probe f = catchDefaultIO (True, []) $ do
|
2013-02-14 18:10:36 +00:00
|
|
|
let f2 = f ++ "2"
|
2017-09-29 18:58:23 +00:00
|
|
|
nukeFile f2
|
|
|
|
createSymbolicLink f f2
|
|
|
|
nukeFile f2
|
|
|
|
preventWrite f
|
2015-08-19 16:24:55 +00:00
|
|
|
-- Should be unable to write to the file, unless
|
|
|
|
-- running as root, but some crippled
|
2015-07-30 18:06:17 +00:00
|
|
|
-- filesystems ignore write bit removals.
|
2017-09-29 18:58:23 +00:00
|
|
|
ifM ((== 0) <$> getRealUserID)
|
|
|
|
( return (False, [])
|
2017-08-28 17:21:18 +00:00
|
|
|
, do
|
2017-09-29 18:58:23 +00:00
|
|
|
r <- catchBoolIO $ do
|
|
|
|
writeFile f "2"
|
|
|
|
return True
|
2017-08-28 17:21:18 +00:00
|
|
|
if r
|
2017-09-29 18:58:23 +00:00
|
|
|
then return (True, ["Filesystem allows writing to files whose write bit is not set."])
|
|
|
|
else return (False, [])
|
2015-08-19 16:36:17 +00:00
|
|
|
)
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2013-02-15 18:17:31 +00:00
|
|
|
|
2019-08-30 17:54:57 +00:00
|
|
|
checkCrippledFileSystem :: Annex ()
|
|
|
|
checkCrippledFileSystem = whenM probeCrippledFileSystem $ do
|
2013-02-15 22:16:50 +00:00
|
|
|
warning "Detected a crippled filesystem."
|
2013-02-15 18:17:31 +00:00
|
|
|
setCrippledFileSystem True
|
2013-05-14 00:48:44 +00:00
|
|
|
|
work around lack of receive.denyCurrentBranch in direct mode
Now that direct mode sets core.bare=true, git's normal prohibition about
pushing into the currently checked out branch doesn't work.
A simple fix for this would be an update hook which blocks the pushes..
but git hooks must be executable, and git-annex needs to be usable on eg,
FAT, which lacks x bits.
Instead, enabling direct mode switches the branch (eg master) to a special
purpose branch (eg annex/direct/master). This branch is not pushed when
syncing; instead any changes that git annex sync commits get written to
master, and it's pushed (along with synced/master) to the remote.
Note that initialization has been changed to always call setDirect,
even if it's just setDirect False for indirect mode. This is needed because
if the user has just cloned a direct mode repo, that nothing has synced
with before, it may have no master branch, and only a annex/direct/master.
Resulting in that branch being checked out locally too. Calling setDirect False
for indirect mode moves back out of this branch, to a new master branch,
and ensures that a manual "git push" doesn't push changes directly to
the annex/direct/master of the remote. (It's possible that the user
makes a commit w/o using git-annex and pushes it, but nothing I can do
about that really.)
This commit was sponsored by Jonathan Harrington.
2013-11-06 01:08:31 +00:00
|
|
|
{- Normally git disables core.symlinks itself when the
|
2016-05-10 18:42:57 +00:00
|
|
|
- filesystem does not support them. But, even if symlinks are
|
|
|
|
- supported, we don't use them by default in a crippled
|
|
|
|
- filesystem. -}
|
2013-05-14 18:18:34 +00:00
|
|
|
whenM (coreSymlinks <$> Annex.getGitConfig) $ do
|
|
|
|
warning "Disabling core.symlinks."
|
|
|
|
setConfig (ConfigKey "core.symlinks")
|
|
|
|
(Git.Config.boolConfig False)
|
|
|
|
|
2015-11-13 17:35:29 +00:00
|
|
|
probeLockSupport :: Annex Bool
|
|
|
|
probeLockSupport = do
|
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
return True
|
|
|
|
#else
|
2019-09-10 17:37:07 +00:00
|
|
|
withEventuallyCleanedOtherTmp $ \tmp -> do
|
2019-01-17 19:40:44 +00:00
|
|
|
let f = tmp </> "lockprobe"
|
|
|
|
mode <- annexFileMode
|
|
|
|
liftIO $ do
|
|
|
|
nukeFile f
|
2019-08-29 16:19:07 +00:00
|
|
|
let locktest =
|
|
|
|
Posix.lockExclusive (Just mode) f
|
|
|
|
>>= Posix.dropLock
|
|
|
|
ok <- isRight <$> tryNonAsync locktest
|
2019-01-17 19:40:44 +00:00
|
|
|
nukeFile f
|
|
|
|
return ok
|
2015-11-13 17:35:29 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-04 17:14:55 +00:00
|
|
|
probeFifoSupport :: Annex Bool
|
|
|
|
probeFifoSupport = do
|
2013-08-04 17:07:55 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2013-05-11 20:03:00 +00:00
|
|
|
return False
|
|
|
|
#else
|
2019-09-10 17:37:07 +00:00
|
|
|
withEventuallyCleanedOtherTmp $ \tmp -> do
|
2019-01-17 19:40:44 +00:00
|
|
|
let f = tmp </> "gaprobe"
|
|
|
|
let f2 = tmp </> "gaprobe2"
|
|
|
|
liftIO $ do
|
|
|
|
nukeFile f
|
|
|
|
nukeFile f2
|
|
|
|
ms <- tryIO $ do
|
|
|
|
createNamedPipe f ownerReadMode
|
|
|
|
createLink f f2
|
|
|
|
getFileStatus f
|
|
|
|
nukeFile f
|
|
|
|
nukeFile f2
|
|
|
|
return $ either (const False) isNamedPipe ms
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2013-04-04 17:14:55 +00:00
|
|
|
|
2015-11-13 17:35:29 +00:00
|
|
|
checkLockSupport :: Annex ()
|
|
|
|
checkLockSupport = unlessM probeLockSupport $ do
|
|
|
|
warning "Detected a filesystem without POSIX fcntl lock support."
|
|
|
|
warning "Enabling annex.pidlock."
|
|
|
|
setConfig (annexConfig "pidlock") (Git.Config.boolConfig True)
|
|
|
|
|
2013-04-04 17:14:55 +00:00
|
|
|
checkFifoSupport :: Annex ()
|
|
|
|
checkFifoSupport = unlessM probeFifoSupport $ do
|
|
|
|
warning "Detected a filesystem without fifo support."
|
|
|
|
warning "Disabling ssh connection caching."
|
|
|
|
setConfig (annexConfig "sshcaching") (Git.Config.boolConfig False)
|
work around lack of receive.denyCurrentBranch in direct mode
Now that direct mode sets core.bare=true, git's normal prohibition about
pushing into the currently checked out branch doesn't work.
A simple fix for this would be an update hook which blocks the pushes..
but git hooks must be executable, and git-annex needs to be usable on eg,
FAT, which lacks x bits.
Instead, enabling direct mode switches the branch (eg master) to a special
purpose branch (eg annex/direct/master). This branch is not pushed when
syncing; instead any changes that git annex sync commits get written to
master, and it's pushed (along with synced/master) to the remote.
Note that initialization has been changed to always call setDirect,
even if it's just setDirect False for indirect mode. This is needed because
if the user has just cloned a direct mode repo, that nothing has synced
with before, it may have no master branch, and only a annex/direct/master.
Resulting in that branch being checked out locally too. Calling setDirect False
for indirect mode moves back out of this branch, to a new master branch,
and ensures that a manual "git push" doesn't push changes directly to
the annex/direct/master of the remote. (It's possible that the user
makes a commit w/o using git-annex and pushes it, but nothing I can do
about that really.)
This commit was sponsored by Jonathan Harrington.
2013-11-06 01:08:31 +00:00
|
|
|
|
2015-09-09 17:56:37 +00:00
|
|
|
checkSharedClone :: Annex Bool
|
|
|
|
checkSharedClone = inRepo Git.Objects.isSharedClone
|
|
|
|
|
|
|
|
initSharedClone :: Bool -> Annex ()
|
|
|
|
initSharedClone False = return ()
|
|
|
|
initSharedClone True = do
|
2015-10-11 17:29:44 +00:00
|
|
|
showLongNote "Repository was cloned with --shared; setting annex.hardlink=true and making repository untrusted."
|
2014-09-05 17:44:09 +00:00
|
|
|
u <- getUUID
|
|
|
|
trustSet u UnTrusted
|
|
|
|
setConfig (annexConfig "hardlink") (Git.Config.boolConfig True)
|
2017-02-27 20:08:16 +00:00
|
|
|
|
|
|
|
{- Propigate annex.securehashesonly from then global config to local
|
|
|
|
- config. This makes a clone inherit a parent's setting, but once
|
|
|
|
- a repository has a local setting, changes to the global config won't
|
|
|
|
- affect it. -}
|
|
|
|
propigateSecureHashesOnly :: Annex ()
|
|
|
|
propigateSecureHashesOnly =
|
|
|
|
maybe noop (setConfig (ConfigKey "annex.securehashesonly"))
|
|
|
|
=<< getGlobalConfig "annex.securehashesonly"
|