summaryrefslogtreecommitdiff
path: root/GitRepo.hs
blob: fece797858e9e1df9581e2f4042814040ded891c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{- git repository handling -}

module GitRepo where

import Directory
import System.Directory
import Data.String.Utils

{- Returns the path to the current repository's .git directory.
 - (For a bare repository, that is the root of the repository.) -}
gitDir :: IO String
gitDir = do
	repo <- repoTop
	bare <- isBareRepo repo
	if (bare)
		then return repo
		else return $ repo ++ "/.git"

{- Finds the top of the current git repository, which may be in a parent
 - directory. -}
repoTop :: IO String
repoTop = do
	dir <- getCurrentDirectory
	top <- seekUp dir isRepoTop
	case top of
		(Just dir) -> return dir
		Nothing -> error "Not in a git repository."

seekUp :: String -> (String -> IO Bool) -> IO (Maybe String)
seekUp dir want = do
	ok <- want dir
	if ok
		then return (Just dir)
		else case (parentDir dir) of
			(Just d) -> seekUp d want
			Nothing -> return Nothing

parentDir :: String -> Maybe String
parentDir dir =
	if length dirs > 0
	then Just ("/" ++ (join "/" $ take ((length dirs) - 1) dirs))
	else Nothing
		where
			dirs = filter (\x -> length x > 0) $ split "/" dir

isRepoTop dir = do
	r <- isGitRepo dir
	b <- isBareRepo dir
	return (r || b)

isGitRepo dir = gitSignature dir ".git" ".git/config"
isBareRepo dir = gitSignature dir "objects" "config"
	
gitSignature dir subdir file = do
	s <- (doesDirectoryExist (dir ++ "/" ++ subdir))
	f <- (doesFileExist (dir ++ "/" ++ file))
	return (s && f)