aboutsummaryrefslogtreecommitdiff
path: root/Types/RefSpec.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-05-14 15:31:38 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-05-14 15:31:38 -0400
commit9ef82700e53d82b38aed603c5c8033d09fe3cf3f (patch)
tree0cf4f7521c9f7c0d96b4a360f616348946728cd2 /Types/RefSpec.hs
parent76e69deadf246c95ac4a49b7b72af49a742d235b (diff)
unused: Add --used option, which can specify a set of refs to consider used, rather than the default of considering all refs used.
Diffstat (limited to 'Types/RefSpec.hs')
-rw-r--r--Types/RefSpec.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/Types/RefSpec.hs b/Types/RefSpec.hs
new file mode 100644
index 000000000..42f4c6226
--- /dev/null
+++ b/Types/RefSpec.hs
@@ -0,0 +1,44 @@
+{- This is not the same as git's fetch/push refspecs.
+ -
+ - Copyright 2015 Joey Hess <id@joeyh.name>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+module Types.RefSpec where
+
+import Common
+import Utility.Glob
+import Git.Types
+
+import Data.Either
+
+type RefSpec = [RefSpecPart]
+
+data RefSpecPart = AddRef Ref | AddMatching Glob | RemoveMatching Glob
+
+allRefSpec :: RefSpec
+allRefSpec = [AddMatching $ compileGlob "*" CaseSensative]
+
+parseRefSpec :: String -> Either String RefSpec
+parseRefSpec v = case partitionEithers (map mk $ split ":" v) of
+ ([],refspec) -> Right refspec
+ (e:_,_) -> Left e
+ where
+ mk ('+':s)
+ | any (`elem` s) "*?" =
+ Right $ AddMatching $ compileGlob s CaseSensative
+ | otherwise = Right $ AddRef $ Ref s
+ mk ('-':s) = Right $ RemoveMatching $ compileGlob s CaseSensative
+ mk s = Left $ "bad refspec item \"" ++ s ++ "\" (expected + or - prefix)"
+
+applyRefSpec :: RefSpec -> [Ref] -> [Ref]
+applyRefSpec refspec rs = go [] refspec
+ where
+ go c [] = reverse c
+ go c (AddRef r : rest) = go (r:c) rest
+ go c (AddMatching g : rest) =
+ let add = filter (matchGlob g . fromRef) rs
+ in go (add ++ c) rest
+ go c (RemoveMatching g : rest) =
+ go (filter (not . matchGlob g . fromRef) c) rest