Commits

Stefan Saasen committed 0ef9bc8

Introduce 'readSymRef' function in the ObjectStore instead of reading the HEAD symbolic ref directly

  • Participants
  • Parent commits 352f24e

Comments (0)

Files changed (2)

File src/Git/Repository.hs

 import qualified Data.ByteString as B
 import Text.Printf                                          (printf)
 import Git.Common                                           (GitRepository(..), ObjectId, WithRepository)
+import Numeric                                              (readOct)
 import Git.Store.Object
 import Git.Store.ObjectStore
 import Git.Store.Index
 import System.FilePath
 import System.Directory
-import Data.Char                                            (isSpace)
 import System.Posix.Files
 import Control.Monad.Reader
-import Numeric                                              (readOct)
 
 -- | Updates files in the working tree to match the given <tree-ish>
 checkoutHead :: WithRepository ()
 toHex bytes = C.unpack bytes >>= printf "%02x"
 
 readHead :: WithRepository ObjectId
-readHead = do
-    repo <- ask
-    let gitDir = getGitDirectory repo
-    ref <- liftIO $ C.readFile (gitDir </> "HEAD")
-    -- TODO check if valid HEAD
-    let unwrappedRef = C.unpack $ strip $ head $ tail $ C.split ':' ref
-    obj <- liftIO $ C.readFile (gitDir </> unwrappedRef)
-    return $ C.unpack $ strip obj
-  where strip = C.takeWhile (not . isSpace) . C.dropWhile isSpace
-
+readHead = readSymRef "HEAD"
 

File src/Git/Store/ObjectStore.hs

   , updateHead
   , readTree
   , readObject
+  , readSymRef
   , createRef
   , getGitDirectory
 ) where
 -- FIXME -> don't use isJust/fromJust
 import Data.Maybe                                           (isJust, fromJust, isNothing)
 import Text.Printf                                          (printf)
+import Data.Char                                            (isSpace)
 import Git.Pack.Packfile
 import Git.Pack.Delta                                       (patch)
 import Git.Common                                           (GitRepository(..), ObjectId, WithRepository, Ref(..))
         repo <- ask
         liftIO $ writeFile (getGitDirectory repo </> symName) $ "ref: " ++ ref ++ "\n"
 
-
+readSymRef :: String -> WithRepository ObjectId
+readSymRef name = do
+    repo <- ask
+    let gitDir = getGitDirectory repo
+    ref <- liftIO $ C.readFile (gitDir </> name)
+    let unwrappedRef = C.unpack $ strip $ head $ tail $ C.split ':' ref
+    obj <- liftIO $ C.readFile (gitDir </> unwrappedRef)
+    return $ C.unpack (strip obj)
+  where strip = C.takeWhile (not . isSpace) . C.dropWhile isSpace
 
 pathForPack :: GitRepository -> FilePath
 pathForPack repo = getGitDirectory repo </> "objects" </> "pack"