summaryrefslogtreecommitdiff
path: root/Git/CheckAttr.hs
blob: 5c747a951964c95ab2ae8b66647c2e4551f7bf1f (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
{- git check-attr interface
 -
 - Copyright 2010, 2011 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Git.CheckAttr where

import Common
import Git
import Git.Command
import qualified Git.Version

{- Efficiently looks up a gitattributes value for each file in a list. -}
lookup :: String -> [FilePath] -> Repo -> IO [(FilePath, String)]
lookup attr files repo = do
	cwd <- getCurrentDirectory
	(_, r) <- pipeBoth "git" (toCommand params) $
		join "\0" $ input cwd
	return $ zip files $ map attrvalue $ lines r
	where
		params = gitCommandLine 
				[ Param "check-attr"
				, Param attr
				, Params "-z --stdin"
				] repo

		{- Before git 1.7.7, git check-attr worked best with
		 - absolute filenames; using them worked around some bugs
		 - with relative filenames.
		 - 
		 - With newer git, git check-attr chokes on some absolute
		 - filenames, and the bugs that necessitated them were fixed,
		 - so use relative filenames. -}
		oldgit = Git.Version.older "1.7.7"
		input cwd
			| oldgit = map (absPathFrom cwd) files
			| otherwise = map (relPathDirToFile cwd . absPathFrom cwd) files
		attrvalue l = end bits !! 0
			where
				bits = split sep l
				sep = ": " ++ attr ++ ": "