git-annex/Utility/Glob.hs
Joey Hess 0a4479b8ec
Avoid backtraces on expected failures when built with ghc 8; only use backtraces for unexpected errors.
ghc 8 added backtraces on uncaught errors. This is great, but git-annex was
using error in many places for a error message targeted at the user, in
some known problem case. A backtrace only confuses such a message, so omit it.

Notably, commands like git annex drop that failed due to eg, numcopies,
used to use error, so had a backtrace.

This commit was sponsored by Ethan Aubin.
2016-11-15 21:29:54 -04:00

42 lines
942 B
Haskell

{- file globbing
-
- Copyright 2014 Joey Hess <id@joeyh.name>
-
- License: BSD-2-clause
-}
module Utility.Glob (
Glob,
GlobCase(..),
compileGlob,
matchGlob
) where
import Utility.Exception
import System.Path.WildMatch
import "regex-tdfa" Text.Regex.TDFA
import "regex-tdfa" Text.Regex.TDFA.String
newtype Glob = Glob Regex
data GlobCase = CaseSensative | CaseInsensative
{- Compiles a glob to a regex, that can be repeatedly used. -}
compileGlob :: String -> GlobCase -> Glob
compileGlob glob globcase = Glob $
case compile (defaultCompOpt {caseSensitive = casesentitive}) defaultExecOpt regex of
Right r -> r
Left _ -> giveup $ "failed to compile regex: " ++ regex
where
regex = '^':wildToRegex glob
casesentitive = case globcase of
CaseSensative -> True
CaseInsensative -> False
matchGlob :: Glob -> String -> Bool
matchGlob (Glob regex) val =
case execute regex val of
Right (Just _) -> True
_ -> False