1{- git index file stuff 2 - 3 - Copyright 2011-2018 Joey Hess <id@joeyh.name> 4 - 5 - Licensed under the GNU AGPL version 3 or higher. 6 -} 7 8{-# LANGUAGE OverloadedStrings #-} 9 10module Git.Index where 11 12import Common 13import Git 14import Utility.Env 15import Utility.Env.Set 16 17import qualified System.FilePath.ByteString as P 18 19indexEnv :: String 20indexEnv = "GIT_INDEX_FILE" 21 22{- Gets value to set GIT_INDEX_FILE to. Input should be absolute path, 23 - or relative to the CWD. 24 - 25 - When relative, GIT_INDEX_FILE is interpreted by git as being 26 - relative to the top of the work tree of the git repository, 27 - not to the CWD. Worse, other environment variables (GIT_WORK_TREE) 28 - or git options (--work-tree) or configuration (core.worktree) 29 - can change what the relative path is interpreted relative to. 30 - 31 - So, an absolute path is the only safe option for this to return. 32 -} 33indexEnvVal :: RawFilePath -> IO String 34indexEnvVal p = fromRawFilePath <$> absPath p 35 36{- Forces git to use the specified index file. 37 - 38 - Returns an action that will reset back to the default 39 - index file. 40 - 41 - Warning: Not thread safe. 42 -} 43override :: RawFilePath -> Repo -> IO (IO ()) 44override index _r = do 45 res <- getEnv var 46 val <- indexEnvVal index 47 setEnv var val True 48 return $ reset res 49 where 50 var = "GIT_INDEX_FILE" 51 reset (Just v) = setEnv indexEnv v True 52 reset _ = unsetEnv var 53 54{- The normal index file. Does not check GIT_INDEX_FILE. -} 55indexFile :: Repo -> RawFilePath 56indexFile r = localGitDir r P.</> "index" 57 58{- The index file git will currently use, checking GIT_INDEX_FILE. -} 59currentIndexFile :: Repo -> IO RawFilePath 60currentIndexFile r = maybe (indexFile r) toRawFilePath <$> getEnv indexEnv 61 62{- Git locks the index by creating this file. -} 63indexFileLock :: RawFilePath -> RawFilePath 64indexFileLock f = f <> ".lock" 65