2012-06-06 04:03:08 +00:00
|
|
|
{- git-update-index library
|
|
|
|
-
|
|
|
|
- Copyright 2011, 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-06-10 16:50:06 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
2012-06-06 04:03:08 +00:00
|
|
|
module Git.UpdateIndex (
|
|
|
|
Streamer,
|
2012-06-08 04:29:39 +00:00
|
|
|
pureStreamer,
|
|
|
|
streamUpdateIndex,
|
|
|
|
lsTree,
|
|
|
|
updateIndexLine,
|
|
|
|
stageSymlink
|
2012-06-06 04:03:08 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import System.Cmd.Utils
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
2012-06-06 18:26:15 +00:00
|
|
|
import Git.Types
|
2012-06-06 04:03:08 +00:00
|
|
|
import Git.Command
|
2012-06-06 18:26:15 +00:00
|
|
|
import Git.FilePath
|
2012-06-08 04:29:39 +00:00
|
|
|
import Git.HashObject
|
2012-06-06 04:03:08 +00:00
|
|
|
|
|
|
|
{- Streamers are passed a callback and should feed it lines in the form
|
|
|
|
- read by update-index, and generated by ls-tree. -}
|
|
|
|
type Streamer = (String -> IO ()) -> IO ()
|
|
|
|
|
2012-06-08 04:29:39 +00:00
|
|
|
{- A streamer with a precalculated value. -}
|
|
|
|
pureStreamer :: String -> Streamer
|
2012-06-10 16:50:06 +00:00
|
|
|
pureStreamer !s = \streamer -> streamer s
|
2012-06-08 04:29:39 +00:00
|
|
|
|
2012-06-06 04:03:08 +00:00
|
|
|
{- Streams content into update-index from a list of Streamers. -}
|
2012-06-08 04:29:39 +00:00
|
|
|
streamUpdateIndex :: Repo -> [Streamer] -> IO ()
|
|
|
|
streamUpdateIndex repo as = do
|
2012-06-06 04:03:08 +00:00
|
|
|
(p, h) <- hPipeTo "git" (toCommand $ gitCommandLine params repo)
|
|
|
|
fileEncoding h
|
|
|
|
forM_ as (stream h)
|
|
|
|
hClose h
|
|
|
|
forceSuccess p
|
|
|
|
where
|
|
|
|
params = map Param ["update-index", "-z", "--index-info"]
|
|
|
|
stream h a = a (streamer h)
|
|
|
|
streamer h s = do
|
|
|
|
hPutStr h s
|
|
|
|
hPutStr h "\0"
|
|
|
|
|
2012-06-08 04:29:39 +00:00
|
|
|
{- A streamer that adds the current tree for a ref. Useful for eg, copying
|
|
|
|
- and modifying branches. -}
|
|
|
|
lsTree :: Ref -> Repo -> Streamer
|
|
|
|
lsTree (Ref x) repo streamer = mapM_ streamer =<< pipeNullSplit params repo
|
2012-06-06 04:03:08 +00:00
|
|
|
where
|
|
|
|
params = map Param ["ls-tree", "-z", "-r", "--full-tree", x]
|
2012-06-06 18:26:15 +00:00
|
|
|
|
|
|
|
{- Generates a line suitable to be fed into update-index, to add
|
|
|
|
- a given file with a given sha. -}
|
2012-06-08 04:29:39 +00:00
|
|
|
updateIndexLine :: Sha -> BlobType -> TopFilePath -> String
|
|
|
|
updateIndexLine sha filetype file =
|
2012-06-06 18:26:15 +00:00
|
|
|
show filetype ++ " blob " ++ show sha ++ "\t" ++ getTopFilePath file
|
2012-06-08 04:29:39 +00:00
|
|
|
|
|
|
|
{- A streamer that adds a symlink to the index. -}
|
|
|
|
stageSymlink :: FilePath -> String -> Repo -> IO Streamer
|
|
|
|
stageSymlink file linktext repo = do
|
|
|
|
line <- updateIndexLine
|
|
|
|
<$> hashObject BlobObject linktext repo
|
|
|
|
<*> pure SymlinkBlob
|
|
|
|
<*> toTopFilePath file repo
|
|
|
|
return $ pureStreamer line
|