2015-05-14 19:31:38 +00:00
|
|
|
{- This is not the same as git's fetch/push refspecs.
|
|
|
|
-
|
|
|
|
- Copyright 2015 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-05-14 19:31:38 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Types.RefSpec where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Utility.Glob
|
|
|
|
import Git.Types
|
|
|
|
|
|
|
|
import Data.Either
|
|
|
|
|
|
|
|
type RefSpec = [RefSpecPart]
|
|
|
|
|
2015-07-07 21:13:50 +00:00
|
|
|
data RefSpecPart
|
|
|
|
= AddRef Ref
|
|
|
|
| AddMatching Glob
|
|
|
|
| AddRefLog
|
|
|
|
| RemoveMatching Glob
|
2015-05-14 19:31:38 +00:00
|
|
|
|
|
|
|
allRefSpec :: RefSpec
|
2020-12-15 16:39:34 +00:00
|
|
|
allRefSpec = [AddMatching $ compileGlob "*" CaseSensative (GlobFilePath False)]
|
2015-05-14 19:31:38 +00:00
|
|
|
|
|
|
|
parseRefSpec :: String -> Either String RefSpec
|
2017-01-31 22:40:42 +00:00
|
|
|
parseRefSpec v = case partitionEithers (map mk $ splitc ':' v) of
|
2015-05-14 19:31:38 +00:00
|
|
|
([],refspec) -> Right refspec
|
|
|
|
(e:_,_) -> Left e
|
|
|
|
where
|
|
|
|
mk ('+':s)
|
|
|
|
| any (`elem` s) "*?" =
|
2020-12-15 16:39:34 +00:00
|
|
|
Right $ AddMatching $ compileGlob s CaseSensative (GlobFilePath False)
|
2020-04-06 21:14:49 +00:00
|
|
|
| otherwise = Right $ AddRef $ Ref $ encodeBS s
|
2020-12-15 16:39:34 +00:00
|
|
|
mk ('-':s) = Right $ RemoveMatching $ compileGlob s CaseSensative (GlobFilePath False)
|
2015-07-07 21:13:50 +00:00
|
|
|
mk "reflog" = Right AddRefLog
|
2015-05-14 19:31:38 +00:00
|
|
|
mk s = Left $ "bad refspec item \"" ++ s ++ "\" (expected + or - prefix)"
|
|
|
|
|
2015-07-07 21:13:50 +00:00
|
|
|
applyRefSpec :: Monad m => RefSpec -> [Ref] -> m [Sha] -> m [Ref]
|
|
|
|
applyRefSpec refspec rs getreflog = go [] refspec
|
2015-05-14 19:31:38 +00:00
|
|
|
where
|
2015-07-07 21:13:50 +00:00
|
|
|
go c [] = return (reverse c)
|
2015-05-14 19:31:38 +00:00
|
|
|
go c (AddRef r : rest) = go (r:c) rest
|
|
|
|
go c (AddMatching g : rest) =
|
2020-04-07 15:54:27 +00:00
|
|
|
let add = filter (matchGlob g . fromRef) rs
|
2015-05-14 19:31:38 +00:00
|
|
|
in go (add ++ c) rest
|
2015-07-07 21:13:50 +00:00
|
|
|
go c (AddRefLog : rest) = do
|
|
|
|
reflog <- getreflog
|
|
|
|
go (reflog ++ c) rest
|
2015-05-14 19:31:38 +00:00
|
|
|
go c (RemoveMatching g : rest) =
|
2020-04-07 15:54:27 +00:00
|
|
|
go (filter (not . matchGlob g . fromRef) c) rest
|