summaryrefslogtreecommitdiff
path: root/Git/Ref.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-12-12 18:23:24 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-12-12 18:30:33 -0400
commit543d0d250104c1f5908e1b7b258d36d95488a029 (patch)
tree7342d994ac8932ed92b1e14108fdc6bb8e84d84e /Git/Ref.hs
parentda95cbadca5b7ef3058b91a384d5f3a48cc39039 (diff)
split out Git/Ref.hs
Diffstat (limited to 'Git/Ref.hs')
-rw-r--r--Git/Ref.hs47
1 files changed, 47 insertions, 0 deletions
diff --git a/Git/Ref.hs b/Git/Ref.hs
new file mode 100644
index 000000000..723bea681
--- /dev/null
+++ b/Git/Ref.hs
@@ -0,0 +1,47 @@
+{- git ref stuff
+ -
+ - Copyright 2011 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+module Git.Ref where
+
+import qualified Data.ByteString.Lazy.Char8 as L
+
+import Common
+import Git
+
+{- Converts a fully qualified git ref into a user-visible version. -}
+describe :: Ref -> String
+describe = remove "refs/heads/" . remove "refs/remotes/" . show
+ where
+ remove prefix s
+ | prefix `isPrefixOf` s = drop (length prefix) s
+ | otherwise = s
+
+{- Checks if a ref exists. -}
+exists :: Ref -> Repo -> IO Bool
+exists ref = runBool "show-ref"
+ [Param "--verify", Param "-q", Param $ show ref]
+
+{- Get the sha of a fully qualified git ref, if it exists. -}
+sha :: Branch -> Repo -> IO (Maybe Sha)
+sha branch repo = process . L.unpack <$> showref repo
+ where
+ showref = pipeRead [Param "show-ref",
+ Param "--hash", -- get the hash
+ Param $ show branch]
+ process [] = Nothing
+ process s = Just $ Ref $ firstLine s
+
+{- List of (refs, branches) matching a given ref spec.
+ - Duplicate refs are filtered out. -}
+matching :: Ref -> Repo -> IO [(Git.Ref, Git.Branch)]
+matching ref repo = do
+ r <- Git.pipeRead [Param "show-ref", Param $ show ref] repo
+ return $ nubBy uref $ map (gen . words . L.unpack) (L.lines r)
+ where
+ gen l = (Git.Ref $ head l, Git.Ref $ last l)
+ uref (a, _) (b, _) = a == b
+