2012-06-06 04:03:08 +00:00
|
|
|
{- git-update-index library
|
|
|
|
-
|
2013-05-12 22:18:48 +00:00
|
|
|
- Copyright 2011-2013 Joey Hess <joey@kitenet.net>
|
2012-06-06 04:03:08 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-05-12 22:18:48 +00:00
|
|
|
{-# LANGUAGE BangPatterns, CPP #-}
|
2012-06-10 16:50:06 +00:00
|
|
|
|
2012-06-06 04:03:08 +00:00
|
|
|
module Git.UpdateIndex (
|
|
|
|
Streamer,
|
2012-06-08 04:29:39 +00:00
|
|
|
pureStreamer,
|
|
|
|
streamUpdateIndex,
|
2014-02-18 21:38:23 +00:00
|
|
|
streamUpdateIndex',
|
|
|
|
startUpdateIndex,
|
|
|
|
stopUpdateIndex,
|
2012-06-08 04:29:39 +00:00
|
|
|
lsTree,
|
2014-03-04 19:00:19 +00:00
|
|
|
lsSubTree,
|
2012-06-08 04:29:39 +00:00
|
|
|
updateIndexLine,
|
2013-10-22 16:58:04 +00:00
|
|
|
stageFile,
|
2012-06-10 17:05:58 +00:00
|
|
|
unstageFile,
|
2012-06-08 04:29:39 +00:00
|
|
|
stageSymlink
|
2012-06-06 04:03:08 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
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-10 17:05:58 +00:00
|
|
|
import Git.Sha
|
2012-06-06 04:03:08 +00:00
|
|
|
|
2014-02-18 21:38:23 +00:00
|
|
|
import Control.Exception (bracket)
|
|
|
|
import System.Process (std_in)
|
|
|
|
|
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 ()
|
2014-02-18 21:38:23 +00:00
|
|
|
streamUpdateIndex repo as = bracket (startUpdateIndex repo) stopUpdateIndex $
|
|
|
|
(\h -> forM_ as $ streamUpdateIndex' h)
|
|
|
|
|
|
|
|
data UpdateIndexHandle = UpdateIndexHandle ProcessHandle Handle
|
|
|
|
|
|
|
|
streamUpdateIndex' :: UpdateIndexHandle -> Streamer -> IO ()
|
|
|
|
streamUpdateIndex' (UpdateIndexHandle _ h) a = a $ \s -> do
|
|
|
|
hPutStr h s
|
|
|
|
hPutStr h "\0"
|
|
|
|
|
|
|
|
startUpdateIndex :: Repo -> IO UpdateIndexHandle
|
|
|
|
startUpdateIndex repo = do
|
|
|
|
(Just h, _, _, p) <- createProcess (gitCreateProcess params repo)
|
|
|
|
{ std_in = CreatePipe }
|
2012-08-25 00:50:39 +00:00
|
|
|
fileEncoding h
|
2014-02-18 21:38:23 +00:00
|
|
|
return $ UpdateIndexHandle p h
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
params = map Param ["update-index", "-z", "--index-info"]
|
2014-02-18 21:38:23 +00:00
|
|
|
|
|
|
|
stopUpdateIndex :: UpdateIndexHandle -> IO Bool
|
|
|
|
stopUpdateIndex (UpdateIndexHandle p h) = do
|
|
|
|
hClose h
|
|
|
|
checkSuccessProcess p
|
2012-06-06 04:03:08 +00:00
|
|
|
|
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
|
2012-10-04 22:47:31 +00:00
|
|
|
lsTree (Ref x) repo streamer = do
|
|
|
|
(s, cleanup) <- pipeNullSplit params repo
|
|
|
|
mapM_ streamer s
|
|
|
|
void $ cleanup
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
params = map Param ["ls-tree", "-z", "-r", "--full-tree", x]
|
2014-03-04 19:00:19 +00:00
|
|
|
lsSubTree :: Ref -> FilePath -> Repo -> Streamer
|
|
|
|
lsSubTree (Ref x) p repo streamer = do
|
|
|
|
(s, cleanup) <- pipeNullSplit params repo
|
|
|
|
mapM_ streamer s
|
|
|
|
void $ cleanup
|
|
|
|
where
|
|
|
|
params = map Param ["ls-tree", "-z", "-r", "--full-tree", x, p]
|
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 =
|
2014-02-19 05:09:17 +00:00
|
|
|
show filetype ++ " blob " ++ fromRef sha ++ "\t" ++ indexPath file
|
2012-06-08 04:29:39 +00:00
|
|
|
|
2013-10-22 16:58:04 +00:00
|
|
|
stageFile :: Sha -> BlobType -> FilePath -> Repo -> IO Streamer
|
|
|
|
stageFile sha filetype file repo = do
|
|
|
|
p <- toTopFilePath file repo
|
|
|
|
return $ pureStreamer $ updateIndexLine sha filetype p
|
|
|
|
|
2012-06-10 17:05:58 +00:00
|
|
|
{- A streamer that removes a file from the index. -}
|
|
|
|
unstageFile :: FilePath -> Repo -> IO Streamer
|
|
|
|
unstageFile file repo = do
|
|
|
|
p <- toTopFilePath file repo
|
2014-02-19 05:09:17 +00:00
|
|
|
return $ pureStreamer $ "0 " ++ fromRef nullSha ++ "\t" ++ indexPath p
|
2012-06-10 17:05:58 +00:00
|
|
|
|
2012-06-08 04:29:39 +00:00
|
|
|
{- A streamer that adds a symlink to the index. -}
|
2012-06-10 23:58:34 +00:00
|
|
|
stageSymlink :: FilePath -> Sha -> Repo -> IO Streamer
|
|
|
|
stageSymlink file sha repo = do
|
2012-06-13 01:13:15 +00:00
|
|
|
!line <- updateIndexLine
|
2012-06-10 23:58:34 +00:00
|
|
|
<$> pure sha
|
2012-06-08 04:29:39 +00:00
|
|
|
<*> pure SymlinkBlob
|
|
|
|
<*> toTopFilePath file repo
|
|
|
|
return $ pureStreamer line
|
2013-05-12 22:18:48 +00:00
|
|
|
|
|
|
|
indexPath :: TopFilePath -> InternalGitPath
|
|
|
|
indexPath = toInternalGitPath . getTopFilePath
|