summaryrefslogtreecommitdiff
path: root/Git/LsTree.hs
blob: 4a6c509f94608edab97baf818a83d051e3d0a525 (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
{- git ls-tree interface
 -
 - Copyright 2011 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Git.LsTree (
	TreeItem(..),
	lsTree
) where

import Numeric
import Control.Applicative
import Data.Char
import System.Posix.Types

import Git
import Utility.SafeCommand

type Treeish = String

data TreeItem = TreeItem
	{ mode :: FileMode
	, objtype :: String
	, sha :: String
	, file :: FilePath
	} deriving Show

{- Lists the contents of a Treeish -}
lsTree :: Repo -> Treeish -> IO [TreeItem]
lsTree repo t = map parseLsTree <$>
	pipeNullSplit repo [Params "ls-tree --full-tree -z -r --", File t]

{- Parses a line of ls-tree output.
 - (The --long format is not currently supported.) -}
parseLsTree :: String -> TreeItem
parseLsTree l = TreeItem m o s f
	where
		-- l = <mode> SP <type> SP <sha> TAB <file>
		-- Since everything until the file is fixed-width,
		-- do not need to split on words.
		(m, past_m) = head $ readOct l
		(o, past_o) = splitAt 4 $ space past_m
		(s, past_s) = splitAt shaSize $ space past_o
		f = decodeGitFile $ space past_s
		space (sp:rest)
			| isSpace sp = rest
			| otherwise = parseerr
		space [] = parseerr
		parseerr = "ls-tree parse error: " ++ l