add base, under

The describe function was only intended to generate a human-visible
description of a branch, but taking the base of a branch is a useful
operation to be able to do no matter the human-visible representation.

Converting a branch like refs/heads/master to refs/heads/origin/master
is also a useful operation, and under can do that.
This commit is contained in:
Joey Hess 2011-12-30 16:48:26 -04:00
parent 5d17da5eb3
commit 26040d6419

View file

@ -13,14 +13,26 @@ import Common
import Git
import Git.Command
{- Converts a fully qualified git ref into a user-visible version. -}
{- Converts a fully qualified git ref into a user-visible string. -}
describe :: Ref -> String
describe = remove "refs/heads/" . remove "refs/remotes/" . show
describe = show . base
{- Often git refs are fully qualified (eg: refs/heads/master).
- Converts such a fully qualified ref into a base ref (eg: master). -}
base :: Ref -> Ref
base = Ref . remove "refs/heads/" . remove "refs/remotes/" . show
where
remove prefix s
| prefix `isPrefixOf` s = drop (length prefix) s
| otherwise = s
{- Given a directory such as "refs/remotes/origin", and a ref such as
- refs/heads/master, yields a version of that ref under the directory,
- such as refs/remotes/origin/master. -}
under :: String -> Ref -> Ref
under dir r = Ref $ dir </> show (base r)
{- Checks if a ref exists. -}
exists :: Ref -> Repo -> IO Bool
exists ref = runBool "show-ref"