From 3cc94c1667072bbbedbb5acb9ba9c827680f5399 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 13 Dec 2017 14:34:32 -0400 Subject: [PATCH] .noannex file A top-level .noannex file will prevent git-annex init from being used in a repository. This is useful for repositories that have a policy reason not to use git-annex. The content of the file will be displayed to the user who tries to run git-annex init. This also affects git annex reinit and initialization via the webapp. It does not affect automatic inits, when there's a sibling git-annex branch already. This commit was supported by the NSF-funded DataLad project. --- Annex/Init.hs | 30 +++++++++++++++++++++++------ Annex/MakeRepo.hs | 2 +- CHANGELOG | 4 ++++ Command/ConfigList.hs | 2 +- Command/Init.hs | 2 +- Command/Reinit.hs | 2 +- Command/Upgrade.hs | 2 +- Test.hs | 2 +- doc/git-annex-init.mdwn | 5 +++++ doc/todo/prevent_unwanted_init.mdwn | 2 ++ 10 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Annex/Init.hs b/Annex/Init.hs index 0754d1f961..1d4093ff53 100644 --- a/Annex/Init.hs +++ b/Annex/Init.hs @@ -8,6 +8,7 @@ {-# LANGUAGE CPP #-} module Annex.Init ( + AutoInit(..), ensureInitialized, isInitialized, initialize, @@ -48,6 +49,23 @@ import System.Posix.User import qualified Utility.LockFile.Posix as Posix #endif +newtype AutoInit = AutoInit Bool + +checkCanInitialize :: AutoInit -> Annex a -> Annex a +checkCanInitialize (AutoInit True) a = a +checkCanInitialize (AutoInit False) a = fromRepo Git.repoWorkTree >>= \case + Nothing -> a + Just wt -> liftIO (catchMaybeIO (readFile (wt ".noannex"))) >>= \case + Nothing -> a + Just noannexmsg -> ifM (Annex.getState Annex.force) + ( a + , do + warning "Initialization prevented by .noannex file (use --force to override)" + unless (null noannexmsg) $ + warning noannexmsg + giveup "Not initialized." + ) + genDescription :: Maybe String -> Annex String genDescription (Just d) = return d genDescription Nothing = do @@ -59,8 +77,8 @@ genDescription Nothing = do Right username -> [username, at, hostname, ":", reldir] Left _ -> [hostname, ":", reldir] -initialize :: Maybe String -> Maybe Version -> Annex () -initialize mdescription mversion = do +initialize :: AutoInit -> Maybe String -> Maybe Version -> Annex () +initialize ai mdescription mversion = checkCanInitialize ai $ do {- Has to come before any commits are made as the shared - clone heuristic expects no local objects. -} sharedclone <- checkSharedClone @@ -70,7 +88,7 @@ initialize mdescription mversion = do ensureCommit $ Annex.Branch.create prepUUID - initialize' mversion + initialize' (AutoInit True) mversion initSharedClone sharedclone @@ -79,8 +97,8 @@ initialize mdescription mversion = do -- Everything except for uuid setup, shared clone setup, and initial -- description. -initialize' :: Maybe Version -> Annex () -initialize' mversion = do +initialize' :: AutoInit -> Maybe Version -> Annex () +initialize' ai mversion = checkCanInitialize ai $ do checkLockSupport checkFifoSupport checkCrippledFileSystem @@ -131,7 +149,7 @@ ensureInitialized :: Annex () ensureInitialized = getVersion >>= maybe needsinit checkUpgrade where needsinit = ifM Annex.Branch.hasSibling - ( initialize Nothing Nothing + ( initialize (AutoInit True) Nothing Nothing , giveup "First run: git-annex init" ) diff --git a/Annex/MakeRepo.hs b/Annex/MakeRepo.hs index e031966647..ac25c013d5 100644 --- a/Annex/MakeRepo.hs +++ b/Annex/MakeRepo.hs @@ -76,7 +76,7 @@ initRepo False _ dir desc mgroup = inDir dir $ do initRepo' :: Maybe String -> Maybe StandardGroup -> Annex () initRepo' desc mgroup = unlessM isInitialized $ do - initialize desc Nothing + initialize (AutoInit False) desc Nothing u <- getUUID maybe noop (defaultStandardGroup u) mgroup {- Ensure branch gets committed right away so it is diff --git a/CHANGELOG b/CHANGELOG index dc4cb773c1..ceedafb37c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -16,6 +16,10 @@ git-annex (6.20171125) UNRELEASED; urgency=medium (Previous try had a bug.) * lookupkey: Support being given an absolute filename to a file within the current git repository. + * A top-level .noannex file will prevent git-annex init from being used + in a repository. This is useful for repositories that have a policy + reason not to use git-annex. The content of the file will be displayed + to the user who tries to run git-annex init. -- Joey Hess Tue, 28 Nov 2017 13:48:44 -0400 diff --git a/Command/ConfigList.hs b/Command/ConfigList.hs index 383117e52c..9fa4e5a44f 100644 --- a/Command/ConfigList.hs +++ b/Command/ConfigList.hs @@ -45,7 +45,7 @@ findOrGenUUID = do else ifM (Annex.Branch.hasSibling <||> (isJust <$> Fields.getField Fields.autoInit)) ( do liftIO checkNotReadOnly - initialize Nothing Nothing + initialize (AutoInit True) Nothing Nothing getUUID , return NoUUID ) diff --git a/Command/Init.hs b/Command/Init.hs index e7f57c29c7..8ce82a75e8 100644 --- a/Command/Init.hs +++ b/Command/Init.hs @@ -45,7 +45,7 @@ start os = do perform :: InitOptions -> CommandPerform perform os = do - initialize + initialize (AutoInit False) (if null (initDesc os) then Nothing else Just (initDesc os)) (initVersion os) Annex.SpecialRemote.autoEnable diff --git a/Command/Reinit.hs b/Command/Reinit.hs index 6defa4e950..d47e83573c 100644 --- a/Command/Reinit.hs +++ b/Command/Reinit.hs @@ -36,6 +36,6 @@ perform s = do then return $ toUUID s else Remote.nameToUUID s storeUUID u - initialize' Nothing + initialize' (AutoInit False) Nothing Annex.SpecialRemote.autoEnable next $ return True diff --git a/Command/Upgrade.hs b/Command/Upgrade.hs index 1c3e62695b..ab245c29ee 100644 --- a/Command/Upgrade.hs +++ b/Command/Upgrade.hs @@ -25,6 +25,6 @@ start :: CommandStart start = do showStart' "upgrade" Nothing whenM (isNothing <$> getVersion) $ do - initialize Nothing Nothing + initialize (AutoInit False) Nothing Nothing r <- upgrade False latestVersion next $ next $ return r diff --git a/Test.hs b/Test.hs index 41f7dea766..766a6ca96f 100644 --- a/Test.hs +++ b/Test.hs @@ -1810,7 +1810,7 @@ intmpclonerepoInDirect a = intmpclonerepo $ ) where isdirect = annexeval $ do - Annex.Init.initialize Nothing Nothing + Annex.Init.initialize (Annex.Init.AutoInit False) Nothing Nothing Config.isDirect checkRepo :: Types.Annex a -> FilePath -> IO a diff --git a/doc/git-annex-init.mdwn b/doc/git-annex-init.mdwn index 29522181dc..4538f54e50 100644 --- a/doc/git-annex-init.mdwn +++ b/doc/git-annex-init.mdwn @@ -23,6 +23,11 @@ mark it as dead (see [[git-annex-dead]](1)). This command is entirely safe, although usually pointless, to run inside an already initialized git-annex repository. + +A top-level `.noannex` file will prevent git-annex init from being used +in a repository. This is useful for repositories that have a policy +reason not to use git-annex. The content of the file will be displayed +to the user who tries to run git-annex init. # OPTIONS diff --git a/doc/todo/prevent_unwanted_init.mdwn b/doc/todo/prevent_unwanted_init.mdwn index 6481ce6be6..b452a4735d 100644 --- a/doc/todo/prevent_unwanted_init.mdwn +++ b/doc/todo/prevent_unwanted_init.mdwn @@ -6,3 +6,5 @@ annoying. --[[Joey]] This could be a git configuration setting, or it could be something checked into the repo. Either might make sense depending on the scope in which one wants to prevent the accidental init. + +> [[done]] using .noannex file. --[[Joey]]