summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Branch.hs20
-rw-r--r--GitRepo.hs23
2 files changed, 24 insertions, 19 deletions
diff --git a/Branch.hs b/Branch.hs
index 2cd658fe8..4f204929e 100644
--- a/Branch.hs
+++ b/Branch.hs
@@ -68,16 +68,19 @@ genIndex g = do
{- Runs an action using the branch's index file. -}
withIndex :: Annex a -> Annex a
-withIndex a = do
+withIndex = withIndex' False
+withIndex' :: Bool -> Annex a -> Annex a
+withIndex' bootstrapping a = do
g <- Annex.gitRepo
let f = index g
- liftIO $ Git.useIndex f
+ reset <- liftIO $ Git.useIndex f
- e <- liftIO $ doesFileExist f
- unless e $ liftIO $ genIndex g
+ unless bootstrapping $ do
+ e <- liftIO $ doesFileExist f
+ unless e $ liftIO $ genIndex g
r <- a
- liftIO $ Git.useDefaultIndex
+ liftIO reset
return r
withIndexUpdate :: Annex a -> Annex a
@@ -121,11 +124,8 @@ create = do
inorigin <- refexists origin
if inorigin
then liftIO $ Git.run g "branch" [Param name, Param origin]
- else liftIO $ do
- let f = index g
- liftIO $ Git.useIndex f
- GitUnionMerge.commit g "branch created" fullname []
- liftIO $ Git.useDefaultIndex
+ else withIndex' True $
+ liftIO $ GitUnionMerge.commit g "branch created" fullname []
where
origin = "origin/" ++ name
refexists ref = do
diff --git a/GitRepo.hs b/GitRepo.hs
index d1f122fba..4a6c4d763 100644
--- a/GitRepo.hs
+++ b/GitRepo.hs
@@ -60,7 +60,6 @@ module GitRepo (
repoAbsPath,
reap,
useIndex,
- useDefaultIndex,
hashObject,
getSha,
shaSize,
@@ -79,6 +78,7 @@ import System.Cmd.Utils
import IO (bracket_)
import Data.String.Utils
import System.IO
+import IO (try)
import qualified Data.Map as Map hiding (map, split)
import Network.URI
import Data.Maybe
@@ -88,7 +88,7 @@ import Codec.Binary.UTF8.String (encode)
import Text.Printf
import Data.List (isInfixOf, isPrefixOf, isSuffixOf)
import System.Exit
-import System.Posix.Env (setEnv, unsetEnv)
+import System.Posix.Env (setEnv, unsetEnv, getEnv)
import Utility
@@ -391,13 +391,18 @@ reap = do
r <- catch (getAnyProcessStatus False True) (\_ -> return Nothing)
maybe (return ()) (const reap) r
-{- Forces git to use the specified index file. -}
-useIndex :: FilePath -> IO ()
-useIndex index = setEnv "GIT_INDEX_FILE" index True
-
-{- Undoes useIndex -}
-useDefaultIndex :: IO ()
-useDefaultIndex = unsetEnv "GIT_INDEX_FILE"
+{- Forces git to use the specified index file.
+ - Returns an action that will reset back to the default
+ - index file. -}
+useIndex :: FilePath -> IO (IO ())
+useIndex index = do
+ res <- try $ getEnv var
+ setEnv var index True
+ return $ reset res
+ where
+ var = "GIT_INDEX_FILE"
+ reset (Right (Just v)) = setEnv var v True
+ reset _ = unsetEnv var
{- Injects some content into git, returning its hash. -}
hashObject :: Repo -> String -> IO String