summaryrefslogtreecommitdiff
path: root/Git
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-12-30 16:48:26 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-12-30 16:48:26 -0400
commit26040d64195a9fe39a3e7fc04d06d3180ab2497a (patch)
treebce5027aab8741b154fae4a50e544c6ce4221795 /Git
parent5d17da5eb321d08e801e31c67f1bd9d748cc593d (diff)
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.
Diffstat (limited to 'Git')
-rw-r--r--Git/Ref.hs16
1 files changed, 14 insertions, 2 deletions
diff --git a/Git/Ref.hs b/Git/Ref.hs
index 0197ae789..3341cf648 100644
--- a/Git/Ref.hs
+++ b/Git/Ref.hs
@@ -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"