2011-08-17 18:14:43 +00:00
|
|
|
{- git-annex repository initialization
|
|
|
|
-
|
2017-02-27 20:08:16 +00:00
|
|
|
- Copyright 2011-2017 Joey Hess <id@joeyh.name>
|
2011-08-17 18:14:43 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
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-02-15 22:16:50 +00:00
|
|
|
import qualified Git.LsFiles
|
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
|
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
|
2015-12-09 18:25:33 +00:00
|
|
|
import Annex.Link
|
2016-10-17 18:58:33 +00:00
|
|
|
import Annex.WorkTree
|
2013-02-14 18:10:36 +00:00
|
|
|
import Config
|
2013-02-15 22:16:50 +00:00
|
|
|
import Annex.Direct
|
2016-03-29 17:52:13 +00:00
|
|
|
import Annex.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
|
2016-02-16 20:15:32 +00:00
|
|
|
import Annex.Perms
|
2013-08-04 17:07:55 +00:00
|
|
|
import Utility.UserInfo
|
2016-06-13 18:54:34 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
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
|
2013-08-04 17:07:55 +00:00
|
|
|
#endif
|
2012-08-03 14:45:18 +00:00
|
|
|
|
|
|
|
genDescription :: Maybe String -> Annex String
|
|
|
|
genDescription (Just d) = return d
|
|
|
|
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
|
|
|
|
return $ concat $ case v of
|
|
|
|
Right username -> [username, at, hostname, ":", reldir]
|
|
|
|
Left _ -> [hostname, ":", reldir]
|
2012-08-03 14:45:18 +00:00
|
|
|
|
2015-12-15 21:17:13 +00:00
|
|
|
initialize :: Maybe String -> Maybe Version -> Annex ()
|
|
|
|
initialize mdescription mversion = 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
|
2015-12-15 21:17:13 +00:00
|
|
|
initialize' mversion
|
2015-09-09 17:56:37 +00:00
|
|
|
|
|
|
|
initSharedClone sharedclone
|
2014-04-16 00:13:35 +00:00
|
|
|
|
|
|
|
u <- getUUID
|
2015-04-20 18:01:41 +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.
|
2015-12-15 21:17:13 +00:00
|
|
|
initialize' :: Maybe Version -> Annex ()
|
|
|
|
initialize' mversion = do
|
2015-11-13 17:35:29 +00:00
|
|
|
checkLockSupport
|
2013-04-04 17:14:55 +00:00
|
|
|
checkFifoSupport
|
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
|
|
|
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) $
|
|
|
|
setVersion (fromMaybe defaultVersion mversion)
|
2016-01-01 19:09:42 +00:00
|
|
|
whenM versionSupportsUnlockedPointers $ do
|
2015-12-15 21:17:13 +00:00
|
|
|
configureSmudgeFilter
|
2016-10-17 18:58:33 +00:00
|
|
|
scanUnlockedFiles
|
2016-06-02 19:58:22 +00:00
|
|
|
v <- checkAdjustedClone
|
|
|
|
case v of
|
2016-10-05 20:23:09 +00:00
|
|
|
NeedUpgradeForAdjustedClone ->
|
|
|
|
void $ upgrade True versionForAdjustedClone
|
2016-06-02 19:58:22 +00:00
|
|
|
InAdjustedClone -> return ()
|
|
|
|
NotInAdjustedClone ->
|
2016-06-02 20:59:15 +00:00
|
|
|
ifM (crippledFileSystem <&&> (not <$> isBareRepo))
|
2016-06-02 19:58:22 +00:00
|
|
|
( ifM versionSupportsUnlockedPointers
|
|
|
|
( adjustToCrippledFileSystem
|
|
|
|
, do
|
|
|
|
enableDirectMode
|
|
|
|
setDirect True
|
|
|
|
)
|
|
|
|
-- Handle case where this repo was cloned from a
|
|
|
|
-- direct mode repo
|
2016-06-02 20:59:15 +00:00
|
|
|
, unlessM isBareRepo
|
2016-06-02 19:58:22 +00:00
|
|
|
switchHEADBack
|
|
|
|
)
|
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
|
2013-11-05 19:29:56 +00:00
|
|
|
hookUnWrite preCommitHook
|
2017-02-17 18:04:43 +00:00
|
|
|
hookUnWrite postReceiveHook
|
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 ()
|
2014-11-04 22:04:19 +00:00
|
|
|
ensureInitialized = getVersion >>= maybe needsinit checkUpgrade
|
2012-10-29 01:27:15 +00:00
|
|
|
where
|
|
|
|
needsinit = ifM Annex.Branch.hasSibling
|
2015-12-15 21:17:13 +00:00
|
|
|
( initialize Nothing Nothing
|
2016-11-16 01:29:54 +00:00
|
|
|
, giveup "First run: git-annex init"
|
2012-10-29 01:27:15 +00:00
|
|
|
)
|
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
|
2013-02-14 18:10:36 +00:00
|
|
|
probeCrippledFileSystem = do
|
2016-02-16 19:30:59 +00:00
|
|
|
tmp <- fromRepo gitAnnexTmpMiscDir
|
|
|
|
createAnnexDirectory tmp
|
2017-08-28 17:21:18 +00:00
|
|
|
probeCrippledFileSystem' tmp
|
2016-02-16 19:30:59 +00:00
|
|
|
|
2017-08-28 17:21:18 +00:00
|
|
|
probeCrippledFileSystem' :: FilePath -> Annex Bool
|
2013-08-04 17:07:55 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2016-05-05 19:48:58 +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-08-28 17:21:18 +00:00
|
|
|
liftIO $ writeFile f ""
|
2016-02-16 19:30:59 +00:00
|
|
|
uncrippled <- probe f
|
2017-08-28 17:21:18 +00:00
|
|
|
void $ liftIO $ tryIO $ allowWrite f
|
|
|
|
liftIO $ removeFile f
|
2013-02-15 18:17:31 +00:00
|
|
|
return $ not uncrippled
|
2013-02-14 18:10:36 +00:00
|
|
|
where
|
|
|
|
probe f = catchBoolIO $ do
|
|
|
|
let f2 = f ++ "2"
|
2017-08-28 17:21:18 +00:00
|
|
|
liftIO $ nukeFile f2
|
|
|
|
liftIO $ createSymbolicLink f f2
|
|
|
|
liftIO $ nukeFile f2
|
|
|
|
liftIO $ 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-08-28 17:21:18 +00:00
|
|
|
ifM ((== 0) <$> liftIO getRealUserID)
|
2015-08-19 16:36:17 +00:00
|
|
|
( return True
|
2017-08-28 17:21:18 +00:00
|
|
|
, do
|
|
|
|
r <- liftIO $ catchBoolIO $
|
|
|
|
writeFile f "2" >> return True
|
|
|
|
if r
|
|
|
|
then do
|
|
|
|
warning "Filesystem allows writing to files whose write bit is not set."
|
|
|
|
return False
|
|
|
|
else return True
|
|
|
|
|
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
|
|
|
|
|
|
|
checkCrippledFileSystem :: Annex ()
|
2013-04-03 07:52:41 +00:00
|
|
|
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
|
|
|
|
tmp <- fromRepo gitAnnexTmpMiscDir
|
|
|
|
let f = tmp </> "lockprobe"
|
|
|
|
createAnnexDirectory tmp
|
|
|
|
mode <- annexFileMode
|
|
|
|
liftIO $ do
|
|
|
|
nukeFile f
|
|
|
|
ok <- catchBoolIO $ do
|
|
|
|
Posix.dropLock =<< Posix.lockExclusive (Just mode) f
|
|
|
|
return True
|
|
|
|
nukeFile f
|
|
|
|
return ok
|
|
|
|
#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
|
2014-02-26 20:52:56 +00:00
|
|
|
tmp <- fromRepo gitAnnexTmpMiscDir
|
2013-04-04 17:14:55 +00:00
|
|
|
let f = tmp </> "gaprobe"
|
2015-04-03 18:57:06 +00:00
|
|
|
let f2 = tmp </> "gaprobe2"
|
2013-11-18 22:20:20 +00:00
|
|
|
createAnnexDirectory tmp
|
2013-04-04 17:14:55 +00:00
|
|
|
liftIO $ do
|
|
|
|
nukeFile f
|
2015-04-03 18:57:06 +00:00
|
|
|
nukeFile f2
|
2013-04-04 18:25:20 +00:00
|
|
|
ms <- tryIO $ do
|
|
|
|
createNamedPipe f ownerReadMode
|
2015-04-03 18:57:06 +00:00
|
|
|
createLink f f2
|
2013-04-04 18:25:20 +00:00
|
|
|
getFileStatus f
|
2013-04-04 17:14:55 +00:00
|
|
|
nukeFile f
|
2015-04-03 18:57:06 +00:00
|
|
|
nukeFile f2
|
2013-04-04 17:14:55 +00:00
|
|
|
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
|
|
|
|
|
|
|
enableDirectMode :: Annex ()
|
|
|
|
enableDirectMode = unlessM isDirect $ do
|
|
|
|
warning "Enabling direct mode."
|
|
|
|
top <- fromRepo Git.repoPath
|
|
|
|
(l, clean) <- inRepo $ Git.LsFiles.inRepo [top]
|
|
|
|
forM_ l $ \f ->
|
|
|
|
maybe noop (`toDirect` f) =<< isAnnexLink f
|
|
|
|
void $ liftIO clean
|
2013-12-02 16:34:16 +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"
|