Commits

Stefan Saasen  committed 27c00c5

Derive the git directory from the given GitRepository

  • Participants
  • Parent commits e5a6e74

Comments (0)

Files changed (3)

File src/Git/Common.hs

 
 data GitRepository = GitRepository {
     getName         :: String
-   ,getGitDirectory :: FilePath
 } deriving (Show, Eq)
 
 -- | Return a hex representation of the given Integral.

File src/Git/Remote.hs

 clone url =
     case parseRemote $ C.pack url of
         Just remote -> let gitRepoName = repositoryName remote
-                       in clone' (GitRepository gitRepoName (gitRepoName </> ".git")) remote
+                       in clone' (GitRepository gitRepoName) remote
         _           -> putStrLn $ "Invalid URL" ++ url
 
 

File src/Git/Store/ObjectStore.hs

 createGitRepositoryFromPackfile target packFile = do
     pack <- packRead packFile
     let repoName = getName target
-        repo = GitRepository repoName (repoName </> ".git")
+        repo = GitRepository repoName
     unpackPackfile repo pack
     updateHead repo pack
 
 
 
 readHead :: GitRepository -> IO ObjectId
-readHead GitRepository{..}  = do
-    ref <- C.readFile (getGitDirectory </> "HEAD")
+readHead repo = do
+    let gitDir = getGitDirectory repo
+    ref <- C.readFile (gitDir </> "HEAD")
     -- TODO check if valid HEAD
     let unwrappedRef = C.unpack $ strip $ head $ tail $ C.split ':' ref
-    obj <- C.readFile (getGitDirectory </> unwrappedRef)
+    obj <- C.readFile (gitDir </> unwrappedRef)
     return $ C.unpack $ strip obj
   where strip = C.takeWhile (not . isSpace) . C.dropWhile isSpace
 
 
 -- ref: refs/heads/master
 createSymRef :: GitRepository -> String -> String -> IO ()
-createSymRef GitRepository{..} symName ref =
-        writeFile (getGitDirectory </> symName) $ "ref: " ++ ref ++ "\n"
+createSymRef repo symName ref =
+        writeFile (getGitDirectory repo </> symName) $ "ref: " ++ ref ++ "\n"
 
 
 createRef :: GitRepository -> String -> String -> IO ()
-createRef GitRepository{..} ref sha = do
+createRef repo ref sha = do
     let (path, name) = splitFileName ref
-        dir          = getGitDirectory </> path
+        dir          = (getGitDirectory repo) </> path
     _ <- createDirectoryIfMissing True dir
     writeFile (dir </> name) (sha ++ "\n")
 
 pathForPack :: GitRepository -> FilePath
-pathForPack GitRepository{..} = getGitDirectory </> "objects" </> "pack"
+pathForPack repo = (getGitDirectory repo) </> "objects" </> "pack"
 
 pathForObject :: String -> String -> (FilePath, String)
 pathForObject repoName sha | length sha == 40 = (repoName </> ".git" </> "objects" </> pre, rest)
 
 toHex :: C.ByteString -> String
 toHex bytes = C.unpack bytes >>= printf "%02x"
+
+getGitDirectory :: GitRepository -> FilePath
+getGitDirectory = (</> ".git") . getName
+