d8fb97806c
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.
82 lines
2.8 KiB
Haskell
82 lines
2.8 KiB
Haskell
{- running git commands
|
|
-
|
|
- Copyright 2010, 2011 Joey Hess <joey@kitenet.net>
|
|
-
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
-}
|
|
|
|
module Git.Command where
|
|
|
|
import qualified Data.Text.Lazy as L
|
|
import qualified Data.Text.Lazy.IO as L
|
|
|
|
import Common
|
|
import Git
|
|
import Git.Types
|
|
|
|
{- Constructs a git command line operating on the specified repo. -}
|
|
gitCommandLine :: [CommandParam] -> Repo -> [CommandParam]
|
|
gitCommandLine params repo@(Repo { location = Dir _ } ) =
|
|
-- force use of specified repo via --git-dir and --work-tree
|
|
[ Param ("--git-dir=" ++ gitDir repo)
|
|
, Param ("--work-tree=" ++ workTree repo)
|
|
] ++ params
|
|
gitCommandLine _ repo = assertLocal repo $ error "internal"
|
|
|
|
{- Runs git in the specified repo. -}
|
|
runBool :: String -> [CommandParam] -> Repo -> IO Bool
|
|
runBool subcommand params repo = assertLocal repo $
|
|
boolSystem "git" $ gitCommandLine (Param subcommand : params) repo
|
|
|
|
{- Runs git in the specified repo, throwing an error if it fails. -}
|
|
run :: String -> [CommandParam] -> Repo -> IO ()
|
|
run subcommand params repo = assertLocal repo $
|
|
unlessM (runBool subcommand params repo) $
|
|
error $ "git " ++ subcommand ++ " " ++ show params ++ " failed"
|
|
|
|
{- Runs a git subcommand and returns its output, lazily.
|
|
-
|
|
- Note that this leaves the git process running, and so zombies will
|
|
- result unless reap is called.
|
|
-}
|
|
pipeRead :: [CommandParam] -> Repo -> IO String
|
|
pipeRead params repo = assertLocal repo $ do
|
|
(_, h) <- hPipeFrom "git" $ toCommand $ gitCommandLine params repo
|
|
fileEncoding h
|
|
hGetContents h
|
|
|
|
{- Runs a git subcommand, feeding it input.
|
|
- You should call either getProcessStatus or forceSuccess on the PipeHandle. -}
|
|
pipeWrite :: [CommandParam] -> L.Text -> Repo -> IO PipeHandle
|
|
pipeWrite params s repo = assertLocal repo $ do
|
|
(p, h) <- hPipeTo "git" (toCommand $ gitCommandLine params repo)
|
|
L.hPutStr h s
|
|
hClose h
|
|
return p
|
|
|
|
{- Runs a git subcommand, feeding it input, and returning its output.
|
|
- You should call either getProcessStatus or forceSuccess on the PipeHandle. -}
|
|
pipeWriteRead :: [CommandParam] -> String -> Repo -> IO (PipeHandle, String)
|
|
pipeWriteRead params s repo = assertLocal repo $ do
|
|
(p, from, to) <- hPipeBoth "git" (toCommand $ gitCommandLine params repo)
|
|
fileEncoding to
|
|
fileEncoding from
|
|
hPutStr to s
|
|
hClose to
|
|
c <- hGetContents from
|
|
return (p, c)
|
|
|
|
{- Reads null terminated output of a git command (as enabled by the -z
|
|
- parameter), and splits it. -}
|
|
pipeNullSplit :: [CommandParam] -> Repo -> IO [String]
|
|
pipeNullSplit params repo =
|
|
filter (not . null) . split sep <$> pipeRead params repo
|
|
where
|
|
sep = "\0"
|
|
|
|
{- Reaps any zombie git processes. -}
|
|
reap :: IO ()
|
|
reap = do
|
|
-- throws an exception when there are no child processes
|
|
r <- catchDefaultIO (getAnyProcessStatus False True) Nothing
|
|
maybe (return ()) (const reap) r
|