2016-04-06 19:33:29 +00:00
|
|
|
{- Temporarily changing the files git uses.
|
|
|
|
-
|
|
|
|
- Copyright 2014-2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2016-04-06 19:33:29 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.GitOverlay where
|
|
|
|
|
|
|
|
import qualified Control.Exception as E
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Git
|
|
|
|
import Git.Types
|
2016-05-17 17:30:59 +00:00
|
|
|
import Git.Index
|
2016-04-06 19:33:29 +00:00
|
|
|
import Git.Env
|
2018-10-13 05:36:06 +00:00
|
|
|
import Utility.Env
|
2016-04-06 19:33:29 +00:00
|
|
|
import qualified Annex
|
withAltRepo needs a separate queue of changes
The queue could potentially contain changes from before withAltRepo, and
get flushed inside the call, which would apply the changes to the modified
repo.
Or, changes could be queued in withAltRepo that were intended to affect
the modified repo, but don't get flushed until later.
I don't know of any cases where either happens, but better safe than sorry.
Note that this affect withIndexFile, which is used in git-annex branch
updates. So, it potentially makes things slower. Should not be by much;
the overhead consists only of querying the current queue a couple of times,
and potentially flushing changes queued within withAltRepo earlier, that
could have maybe been bundled with other later changes.
Notice in particular that the existing queue is not flushed when calling
withAltRepo. So eg when git annex add needs to stage files in the index,
it will still bundle them together efficiently.
2016-06-03 17:48:14 +00:00
|
|
|
import qualified Annex.Queue
|
2016-04-06 19:33:29 +00:00
|
|
|
|
|
|
|
{- Runs an action using a different git index file. -}
|
|
|
|
withIndexFile :: FilePath -> Annex a -> Annex a
|
2016-05-17 17:29:51 +00:00
|
|
|
withIndexFile f a = do
|
2016-05-22 18:58:04 +00:00
|
|
|
f' <- liftIO $ indexEnvVal f
|
2016-05-17 17:29:51 +00:00
|
|
|
withAltRepo
|
2016-09-29 17:36:48 +00:00
|
|
|
(usecachedgitenv $ \g -> liftIO $ addGitEnv g indexEnv f')
|
2016-05-17 17:29:51 +00:00
|
|
|
(\g g' -> g' { gitEnv = gitEnv g })
|
|
|
|
a
|
2016-09-29 17:36:48 +00:00
|
|
|
where
|
|
|
|
-- This is an optimisation. Since withIndexFile is run repeatedly,
|
2018-10-13 05:36:06 +00:00
|
|
|
-- and addGitEnv uses the slow getEnvironment when gitEnv is Nothing,
|
|
|
|
-- we cache the environment the first time, and reuse it in
|
2016-09-29 17:36:48 +00:00
|
|
|
-- subsequent calls.
|
|
|
|
--
|
|
|
|
-- (This could be done at another level; eg when creating the
|
|
|
|
-- Git object in the first place, but it's more efficient to let
|
|
|
|
-- the enviroment be inherited in all calls to git where it
|
|
|
|
-- does not need to be modified.)
|
|
|
|
usecachedgitenv m g = case gitEnv g of
|
|
|
|
Just _ -> m g
|
|
|
|
Nothing -> do
|
|
|
|
e <- Annex.withState $ \s -> case Annex.cachedgitenv s of
|
|
|
|
Nothing -> do
|
2018-10-13 05:36:06 +00:00
|
|
|
e <- getEnvironment
|
2016-09-29 17:36:48 +00:00
|
|
|
return (s { Annex.cachedgitenv = Just e }, e)
|
|
|
|
Just e -> return (s, e)
|
|
|
|
m (g { gitEnv = Just e })
|
2016-04-06 19:33:29 +00:00
|
|
|
|
2016-04-06 21:32:04 +00:00
|
|
|
{- Runs an action using a different git work tree.
|
|
|
|
-
|
|
|
|
- Smudge and clean filters are disabled in this work tree. -}
|
2016-04-06 19:33:29 +00:00
|
|
|
withWorkTree :: FilePath -> Annex a -> Annex a
|
|
|
|
withWorkTree d = withAltRepo
|
2016-04-06 21:32:04 +00:00
|
|
|
(\g -> return $ g { location = modlocation (location g), gitGlobalOpts = gitGlobalOpts g ++ disableSmudgeConfig })
|
|
|
|
(\g g' -> g' { location = location g, gitGlobalOpts = gitGlobalOpts g })
|
2016-04-06 19:33:29 +00:00
|
|
|
where
|
|
|
|
modlocation l@(Local {}) = l { worktree = Just d }
|
|
|
|
modlocation _ = error "withWorkTree of non-local git repo"
|
2016-04-06 21:32:04 +00:00
|
|
|
disableSmudgeConfig = map Param
|
|
|
|
[ "-c", "filter.annex.smudge="
|
|
|
|
, "-c", "filter.annex.clean="
|
|
|
|
]
|
2016-04-06 19:33:29 +00:00
|
|
|
|
|
|
|
{- Runs an action with the git index file and HEAD, and a few other
|
|
|
|
- files that are related to the work tree coming from an overlay
|
|
|
|
- directory other than the usual. This is done by pointing
|
|
|
|
- GIT_COMMON_DIR at the regular git directory, and GIT_DIR at the
|
2016-04-22 16:29:32 +00:00
|
|
|
- overlay directory.
|
|
|
|
-
|
|
|
|
- Needs git 2.2.0 or newer.
|
|
|
|
-}
|
2016-04-06 19:33:29 +00:00
|
|
|
withWorkTreeRelated :: FilePath -> Annex a -> Annex a
|
|
|
|
withWorkTreeRelated d = withAltRepo modrepo unmodrepo
|
|
|
|
where
|
2016-09-29 17:36:48 +00:00
|
|
|
modrepo g = liftIO $ do
|
2016-04-08 18:24:00 +00:00
|
|
|
g' <- addGitEnv g "GIT_COMMON_DIR" =<< absPath (localGitDir g)
|
|
|
|
g'' <- addGitEnv g' "GIT_DIR" d
|
|
|
|
return (g'' { gitEnvOverridesGitDir = True })
|
|
|
|
unmodrepo g g' = g'
|
|
|
|
{ gitEnv = gitEnv g
|
|
|
|
, gitEnvOverridesGitDir = gitEnvOverridesGitDir g
|
|
|
|
}
|
2016-04-06 19:33:29 +00:00
|
|
|
|
|
|
|
withAltRepo
|
2016-09-29 17:36:48 +00:00
|
|
|
:: (Repo -> Annex Repo)
|
2016-04-06 19:33:29 +00:00
|
|
|
-- ^ modify Repo
|
|
|
|
-> (Repo -> Repo -> Repo)
|
|
|
|
-- ^ undo modifications; first Repo is the original and second
|
|
|
|
-- is the one after running the action.
|
|
|
|
-> Annex a
|
|
|
|
-> Annex a
|
|
|
|
withAltRepo modrepo unmodrepo a = do
|
|
|
|
g <- gitRepo
|
2016-09-29 17:36:48 +00:00
|
|
|
g' <- modrepo g
|
withAltRepo needs a separate queue of changes
The queue could potentially contain changes from before withAltRepo, and
get flushed inside the call, which would apply the changes to the modified
repo.
Or, changes could be queued in withAltRepo that were intended to affect
the modified repo, but don't get flushed until later.
I don't know of any cases where either happens, but better safe than sorry.
Note that this affect withIndexFile, which is used in git-annex branch
updates. So, it potentially makes things slower. Should not be by much;
the overhead consists only of querying the current queue a couple of times,
and potentially flushing changes queued within withAltRepo earlier, that
could have maybe been bundled with other later changes.
Notice in particular that the existing queue is not flushed when calling
withAltRepo. So eg when git annex add needs to stage files in the index,
it will still bundle them together efficiently.
2016-06-03 17:48:14 +00:00
|
|
|
q <- Annex.Queue.get
|
|
|
|
v <- tryNonAsync $ do
|
|
|
|
Annex.changeState $ \s -> s
|
|
|
|
{ Annex.repo = g'
|
|
|
|
-- Start a separate queue for any changes made
|
|
|
|
-- with the modified repo.
|
|
|
|
, Annex.repoqueue = Nothing
|
|
|
|
}
|
2016-04-06 19:33:29 +00:00
|
|
|
a
|
withAltRepo needs a separate queue of changes
The queue could potentially contain changes from before withAltRepo, and
get flushed inside the call, which would apply the changes to the modified
repo.
Or, changes could be queued in withAltRepo that were intended to affect
the modified repo, but don't get flushed until later.
I don't know of any cases where either happens, but better safe than sorry.
Note that this affect withIndexFile, which is used in git-annex branch
updates. So, it potentially makes things slower. Should not be by much;
the overhead consists only of querying the current queue a couple of times,
and potentially flushing changes queued within withAltRepo earlier, that
could have maybe been bundled with other later changes.
Notice in particular that the existing queue is not flushed when calling
withAltRepo. So eg when git annex add needs to stage files in the index,
it will still bundle them together efficiently.
2016-06-03 17:48:14 +00:00
|
|
|
void $ tryNonAsync Annex.Queue.flush
|
|
|
|
Annex.changeState $ \s -> s
|
|
|
|
{ Annex.repo = unmodrepo g (Annex.repo s)
|
|
|
|
, Annex.repoqueue = Just q
|
|
|
|
}
|
|
|
|
either E.throw return v
|