2011-12-13 19:22:43 +00:00
|
|
|
{- git check-attr interface
|
|
|
|
-
|
2012-02-14 03:42:44 +00:00
|
|
|
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
|
2011-12-13 19:22:43 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Git.CheckAttr where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
2011-12-14 19:56:11 +00:00
|
|
|
import Git.Command
|
2011-12-22 18:59:25 +00:00
|
|
|
import qualified Git.Version
|
2012-02-20 19:20:36 +00:00
|
|
|
import qualified Utility.CoProcess as CoProcess
|
2011-12-13 19:22:43 +00:00
|
|
|
|
2012-02-20 19:20:36 +00:00
|
|
|
type CheckAttrHandle = (CoProcess.CoProcessHandle, [Attr], String)
|
2012-02-14 03:42:44 +00:00
|
|
|
|
|
|
|
type Attr = String
|
|
|
|
|
|
|
|
{- Starts git check-attr running to look up the specified gitattributes
|
|
|
|
- values and return a handle. -}
|
|
|
|
checkAttrStart :: [Attr] -> Repo -> IO CheckAttrHandle
|
|
|
|
checkAttrStart attrs repo = do
|
2011-12-13 19:22:43 +00:00
|
|
|
cwd <- getCurrentDirectory
|
2012-02-20 19:20:36 +00:00
|
|
|
h <- CoProcess.start "git" $ toCommand $
|
2012-02-14 03:42:44 +00:00
|
|
|
gitCommandLine params repo
|
2012-02-20 19:20:36 +00:00
|
|
|
CoProcess.query h fileEncoding fileEncoding
|
|
|
|
return (h, attrs, cwd)
|
2011-12-13 19:22:43 +00:00
|
|
|
where
|
2012-02-14 03:42:44 +00:00
|
|
|
params =
|
2012-02-15 17:59:09 +00:00
|
|
|
[ Param "check-attr"
|
|
|
|
, Params "-z --stdin"
|
|
|
|
] ++ map Param attrs ++
|
|
|
|
[ Param "--" ]
|
2011-12-22 18:59:25 +00:00
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
{- Stops git check-attr. -}
|
|
|
|
checkAttrStop :: CheckAttrHandle -> IO ()
|
2012-02-20 19:20:36 +00:00
|
|
|
checkAttrStop (h, _, _) = CoProcess.stop h
|
2012-02-14 03:42:44 +00:00
|
|
|
|
|
|
|
{- Gets an attribute of a file. -}
|
|
|
|
checkAttr :: CheckAttrHandle -> Attr -> FilePath -> IO String
|
2012-02-20 19:20:36 +00:00
|
|
|
checkAttr (h, attrs, cwd) want file = do
|
|
|
|
pairs <- CoProcess.query h send receive
|
2012-02-14 03:42:44 +00:00
|
|
|
let vals = map snd $ filter (\(attr, _) -> attr == want) pairs
|
|
|
|
case vals of
|
|
|
|
[v] -> return v
|
|
|
|
_ -> error $ "unable to determine " ++ want ++ " attribute of " ++ file
|
|
|
|
where
|
2012-02-20 19:20:36 +00:00
|
|
|
send to = hPutStr to $ file' ++ "\0"
|
|
|
|
receive from = forM attrs $ \attr -> do
|
|
|
|
l <- hGetLine from
|
|
|
|
return (attr, attrvalue attr l)
|
2011-12-22 18:59:25 +00:00
|
|
|
{- 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"
|
2012-02-14 03:42:44 +00:00
|
|
|
file'
|
|
|
|
| oldgit = absPathFrom cwd file
|
|
|
|
| otherwise = relPathDirToFile cwd $ absPathFrom cwd file
|
|
|
|
attrvalue attr l = end bits !! 0
|
support all filename encodings with ghc 7.4
Under ghc 7.4, this seems to be able to handle all filename encodings
again. Including filename encodings that do not match the LANG setting.
I think this will not work with earlier versions of ghc, it uses some ghc
internals.
Turns out that ghc 7.4 has a special filesystem encoding that it uses when
reading/writing filenames (as FilePaths). This encoding is documented
to allow "arbitrary undecodable bytes to be round-tripped through it".
So, to get FilePaths from eg, git ls-files, set the Handle that is reading
from git to use this encoding. Then things basically just work.
However, I have not found a way to make Text read using this encoding.
Text really does assume unicode. So I had to switch back to using String
when reading/writing data to git. Which is a pity, because it's some
percent slower, but at least it works.
Note that stdout and stderr also have to be set to this encoding, or
printing out filenames that contain undecodable bytes causes a crash.
IMHO this is a misfeature in ghc, that the user can pass you a filename,
which you can readFile, etc, but that default, putStr of filename may
cause a crash!
Git.CheckAttr gave me special trouble, because the filenames I got back
from git, after feeding them in, had further encoding breakage.
Rather than try to deal with that, I just zip up the input filenames
with the attributes. Which must be returned in the same order queried
for this to work.
Also of note is an apparent GHC bug I worked around in Git.CheckAttr. It
used to forkProcess and feed git from the child process. Unfortunatly,
after this forkProcess, accessing the `files` variable from the parent
returns []. Not the value that was passed into the function. This screams
of a bad bug, that's clobbering a variable, but for now I just avoid
forkProcess there to work around it. That forkProcess was itself only added
because of a ghc bug, #624389. I've confirmed that the test case for that
bug doesn't reproduce it with ghc 7.4. So that's ok, except for the new ghc
bug I have not isolated and reported. Why does this simple bit of code
magnet the ghc bugs? :)
Also, the symlink touching code is currently broken, when used on utf-8
filenames in a non-utf-8 locale, or probably on any filename containing
undecodable bytes, and I temporarily commented it out.
2012-02-03 19:12:41 +00:00
|
|
|
where
|
2011-12-13 19:22:43 +00:00
|
|
|
bits = split sep l
|
|
|
|
sep = ": " ++ attr ++ ": "
|