summaryrefslogtreecommitdiff
path: root/Git
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-06-06 12:52:21 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-06-06 12:52:21 -0400
commit4b32ea793d4e747c3e6cff92041e35061a78c410 (patch)
tree32e316e5a6466bce986c25305944f881562f729c /Git
parent27cfeca4ea8f2aa326e7d8416401c319133491db (diff)
parent455fca65bfb9ca4270fa7f89986d09ee62188d43 (diff)
Merge branch 'master' into watch
Diffstat (limited to 'Git')
-rw-r--r--Git/CatFile.hs8
-rw-r--r--Git/HashObject.hs18
-rw-r--r--Git/Types.hs15
-rw-r--r--Git/UnionMerge.hs17
4 files changed, 36 insertions, 22 deletions
diff --git a/Git/CatFile.hs b/Git/CatFile.hs
index c598d7aa4..d5b367945 100644
--- a/Git/CatFile.hs
+++ b/Git/CatFile.hs
@@ -21,6 +21,7 @@ import Common
import Git
import Git.Sha
import Git.Command
+import Git.Types
import qualified Utility.CoProcess as CoProcess
type CatFileHandle = CoProcess.CoProcessHandle
@@ -52,7 +53,7 @@ catObject h object = CoProcess.query h send receive
case words header of
[sha, objtype, size]
| length sha == shaSize &&
- validobjtype objtype ->
+ isJust (readObjectType objtype) ->
case reads size of
[(bytes, "")] -> readcontent bytes from
_ -> dne
@@ -67,8 +68,3 @@ catObject h object = CoProcess.query h send receive
error "missing newline from git cat-file"
return $ L.fromChunks [content]
dne = return L.empty
- validobjtype t
- | t == "blob" = True
- | t == "commit" = True
- | t == "tree" = True
- | otherwise = False
diff --git a/Git/HashObject.hs b/Git/HashObject.hs
index 617e5ac28..b052413fd 100644
--- a/Git/HashObject.hs
+++ b/Git/HashObject.hs
@@ -9,7 +9,9 @@ module Git.HashObject where
import Common
import Git
+import Git.Sha
import Git.Command
+import Git.Types
import qualified Utility.CoProcess as CoProcess
type HashObjectHandle = CoProcess.CoProcessHandle
@@ -24,11 +26,23 @@ hashObjectStart = CoProcess.start "git" . toCommand . gitCommandLine
hashObjectStop :: HashObjectHandle -> IO ()
hashObjectStop = CoProcess.stop
-{- Injects a file into git, returning the shas of the objects. -}
+{- Injects a file into git, returning the Sha of the object. -}
hashFile :: HashObjectHandle -> FilePath -> IO Sha
hashFile h file = CoProcess.query h send receive
where
send to = do
fileEncoding to
hPutStrLn to file
- receive from = Ref <$> hGetLine from
+ receive from = getSha "hash-object" $ hGetLine from
+
+{- Injects some content into git, returning its Sha. -}
+hashObject :: Repo -> ObjectType -> String -> IO Sha
+hashObject repo objtype content = getSha subcmd $ do
+ (h, s) <- pipeWriteRead (map Param params) content repo
+ length s `seq` do
+ forceSuccess h
+ reap -- XXX unsure why this is needed
+ return s
+ where
+ subcmd = "hash-object"
+ params = [subcmd, "-t", show objtype, "-w", "--stdin"]
diff --git a/Git/Types.hs b/Git/Types.hs
index deb14ebd4..64d418a04 100644
--- a/Git/Types.hs
+++ b/Git/Types.hs
@@ -48,3 +48,18 @@ instance Show Ref where
type Branch = Ref
type Sha = Ref
type Tag = Ref
+
+{- Types of objects that can be stored in git. -}
+data ObjectType = BlobObject | CommitObject | TreeObject
+
+instance Show ObjectType where
+ show BlobObject = "blob"
+ show CommitObject = "commit"
+ show TreeObject = "tree"
+
+readObjectType :: String -> Maybe ObjectType
+readObjectType "blob" = Just BlobObject
+readObjectType "commit" = Just CommitObject
+readObjectType "tree" = Just TreeObject
+readObjectType _ = Nothing
+
diff --git a/Git/UnionMerge.hs b/Git/UnionMerge.hs
index 9ff820dc9..822e6abbf 100644
--- a/Git/UnionMerge.hs
+++ b/Git/UnionMerge.hs
@@ -10,7 +10,6 @@ module Git.UnionMerge (
merge_index
) where
-import System.Cmd.Utils
import qualified Data.Text.Lazy as L
import qualified Data.Text.Lazy.Encoding as L
import qualified Data.Set as S
@@ -21,6 +20,8 @@ import Git.Sha
import Git.CatFile
import Git.Command
import Git.UpdateIndex
+import Git.HashObject
+import Git.Types
{- Performs a union merge between two branches, staging it in the index.
- Any previously staged changes in the index will be lost.
@@ -72,7 +73,7 @@ mergeFile :: String -> FilePath -> CatFileHandle -> Repo -> IO (Maybe String)
mergeFile info file h repo = case filter (/= nullSha) [Ref asha, Ref bsha] of
[] -> return Nothing
(sha:[]) -> use sha
- shas -> use =<< either return (hashObject repo . unlines) =<<
+ shas -> use =<< either return (hashObject repo BlobObject . unlines) =<<
calcMerge . zip shas <$> mapM getcontents shas
where
[_colonmode, _bmode, asha, bsha, _status] = words info
@@ -80,18 +81,6 @@ mergeFile info file h repo = case filter (/= nullSha) [Ref asha, Ref bsha] of
L.decodeUtf8 <$> catObject h s
use sha = return $ Just $ update_index_line sha file
-{- Injects some content into git, returning its Sha. -}
-hashObject :: Repo -> String -> IO Sha
-hashObject repo content = getSha subcmd $ do
- (h, s) <- pipeWriteRead (map Param params) content repo
- length s `seq` do
- forceSuccess h
- reap -- XXX unsure why this is needed
- return s
- where
- subcmd = "hash-object"
- params = [subcmd, "-w", "--stdin"]
-
{- Calculates a union merge between a list of refs, with contents.
-
- When possible, reuses the content of an existing ref, rather than