o ]LbA@slddlmZmZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z mZmZmZm Z mZddlmZedZd Zd ZejZd d Zd\ddZddZddZ  d\ddZ         d]ddZd^ddZd^ddZ d^ddZ!dd Z"d_d!d"Z#Gd#d$d$e$Z%Gd%d&d&e%Z&Gd'd(d(e%Z'Gd)d*d*e%Z(d+d,Z)Gd-d.d.e%Z*Gd/d0d0e$Z+Gd1d2d2e%Z,Gd3d4d4e%Z-Gd5d6d6e%Z.d7d8Z/Gd9d:d:e%Z0Gd;d<dd>e%Z2Gd?d@d@e%Z3d^dAdBZ4dCdDZ5dEdFZ6dGdHZ7dIdJZ8dKZ9dLdMZ:dNdOZ;dPdQZdVdWZ?dXdYZ@daAd`dZd[ZBdS)a)absolute_importprint_functionN)_)open)encodingerrorpathutilpolicypycompatutil) stringutildirstate) reglobpathrelglobrelpathrelrerootgloblistfile listfile0setinclude subinclude rootfilesinrrcCs.tj|}z|jWSty|jYSw)zZcompile the regexp with the best available regexp engine and return a matcher function)r recompile test_matchAttributeErrormatch)regexmr$1/usr/lib/python3/dist-packages/mercurial/match.py _rematcher2s   r&Fc Csg}g}|D]E\}}} |dkrC|durtd||j|||d|rB|jD]} || j|||d} t| | |d} || q(q|||| fq||fS)zFReturns the kindpats list with the 'set' patterns expanded to matchersrNs"fileset expression with no contextbadfn)rProgrammingErrorappend matchfilesetsubstatesubprefixdirmatcher) cwdkindpatsctx listsubreposr(matchersotherkindpatsourcesubpathsmpmr$r$r% _expandsets=s"  r;c Csg}g}|D]G\}}}|dkrEtt|}t|}t||}t|} | dgd|gf} t||| } | r=| d7} || | fq||||fq||fS)z_Returns the list of subinclude matcher args and the kindpats without the subincludes in it.rs include:%s/)r dirnamer normpathpconvertjoin canonpathr*) r0root relmatchersr4r5r6r7 sourcerootpathnewroot matcherargsprefixr$r$r%_expandsubincludeUs   rJcCs*|D]\}}}|dks|dvrdSqdS)zQChecks whether the kindspats match everything, as e.g. 'relpath:.' does. r<rFTr$r0r5r6r7r$r$r%_kindpatsalwaysmatchns rLc Csng}t|||||d\}}|r||||d} || |r"|||s)t|dSt|dkr3|dSt|S)Nr1r2r(r'rr)r;r*extend nevermatcherlen unionmatcher) matcherclsrCr/r0r1r2r(r3fmsr#r$r$r%_buildkindpatsmatcherxs$      rTrc  stj|sJtj|t|}t} | r%|jj fdd} |rE| |||||| } t | r9t | }nt t ||| ||| d}nt | }|rd| |d|||| } t t||| ||dd}t||}|r| |d|||| } t t||| ||dd}t||}|S)a build an object to match a set of file patterns arguments: root - the canonical root of the tree you're matching against cwd - the current working directory, if relevant patterns - patterns to find include - patterns to include (unless they are excluded) exclude - patterns to exclude (even if they are included) default - if a pattern in patterns has no explicit type, assume this one auditor - optional path auditor ctx - optional changecontext listsubrepos - if True, recurse into subrepositories warn - optional function used for printing warnings badfn - optional bad() callback for this matcher instead of the default icasefs - make a matcher for wdir on case insensitive filesystems, which normalizes the given patterns to the case in the filesystem a pattern is one of: 'glob:' - a glob relative to cwd 're:' - a regular expression 'path:' - a path relative to repository root, which is matched recursively 'rootfilesin:' - a path relative to repository root, which is matched non-recursively (will not match subdirectories) 'relglob:' - an unrooted glob (*.c matches C files in all dirs) 'relpath:' - a path relative to cwd 'relre:' - a regexp that needn't match the start of a name 'set:' - a fileset expression 'include:' - a file of patterns to read and include 'subinclude:' - a file of patterns to match against files under the same directory '' - a pattern of the specified default type >>> def _match(root, *args, **kwargs): ... return match(util.localpath(root), *args, **kwargs) Usually a patternmatcher is returned: >>> _match(b'/foo', b'.', [b're:.*\.c$', b'path:foo/a', b'*.py']) Combining 'patterns' with 'include' (resp. 'exclude') gives an intersectionmatcher (resp. a differencematcher): >>> type(_match(b'/foo', b'.', [b're:.*\.c$'], include=[b'path:lib'])) >>> type(_match(b'/foo', b'.', [b're:.*\.c$'], exclude=[b'path:build'])) Notice that, if 'patterns' is empty, an alwaysmatcher is returned: >>> _match(b'/foo', b'.', []) The 'default' argument determines which kind of pattern is assumed if a pattern has no prefix: >>> _match(b'/foo', b'.', [b'.*\.c$'], default=b're') >>> _match(b'/foo', b'.', [b'main.py'], default=b'relpath') >>> _match(b'/foo', b'.', [b'main.py'], default=b're') The primary use of matchers is to check whether a value (usually a file name) matches againset one of the patterns given at initialization. There are two ways of doing this check. >>> m = _match(b'/foo', b'', [b're:.*\.c$', b'relpath:a']) 1. Calling the matcher with a file name returns True if any pattern matches that file name: >>> m(b'a') True >>> m(b'main.c') True >>> m(b'test.py') False 2. Using the exact() method only returns True if the file name matches one of the exact patterns (i.e. not re: or glob: patterns): >>> m.exact(b'a') True >>> m.exact(b'main.c') False c snt||||||}g}|D]'\}} } |dvr,| } | } | | kr,| vr,||| | f||| | fq |S)N)rr) _donormalizer*) patternsdefaultrCr/auditorwarnkpr0r5patsr7pr dsnormalizer$r% normalizeszmatch..normalizerMrN)osrFisabsrAr localpathrUreporr_rL alwaysmatcherrTpatternmatcherincludematcherintersectmatchersdifferencematcher)rCr/rVincludeexcluderWrXr1r2rYr(icasefsr_r0r#imemr$r]r%r!s\`     r!cCs t||dS)Nr') exactmatcher)filesr(r$r$r%exact9s rpcCt|SN)rdr'r$r$r%always=rscCrqrr)rOr'r$r$r%neverArtrucCst|}||_|S)zWMake a copy of the given matcher, replacing its bad method with the given one. )copybad)r!r(r#r$r$r%badmatchEs rxc sg}fdd|DD]\}}|tvrtj||||d}n|dvr't|}n|dvrpzt|} |dkr;| d} n| } dd| D} WntyWt t d |wt | ||||D] \} } } | | | |fqaq |d krz*tj|t|} t| |}t |||||D]\} } } | | | | p|fqWn8t j y}z t d ||jfd }~wty}z|r|t d |t|jfWYd }~nd }~wwq | ||dfq |S)zConvert 'kind:pat' from the patterns list to tuples with kind and normalized and rooted patterns and with listfiles expanded.csg|]}t|qSr$ _patsplit).0r\rWr$r% Rz _donormalize..)rX)rrrr)rrrcSsg|]}|r|qSr$r$r{fr$r$r%r}^sunable to read file list (%s)rs%s: %sNs*skipping unreadable pattern file '%s': %s r<)cwdrelativepatternkindsr rBr r?readfilesplit splitlinesEnvironmentErrorrAbortrrUr*r`rFrArbreadpatternfilemessageIOErrorr forcebytestrstrerror)rVrWrCr/rXrYr0r5r6rokr\r7fullpath includepatsinstr$r|r%rUNsf       rUc@seZdZdddZddZddZdZedd Zd d Z ed d Z ddZ ddZ ddZ ddZddZddZddZddZdS) basematcherNcCs|dur ||_dSdSrr)rwselfr(r$r$r%__init__s zbasematcher.__init__cCs ||Srr)matchfn)rfnr$r$r%__call__ zbasematcher.__call__cCdS)zoCallback from dirstate.walk for each explicit file that can't be found/accessed, with an error message.Nr$rrmsgr$r$r%rwszbasematcher.badcCsgSrrr$rr$r$r%_filesszbasematcher._filescC|jS)zExplicitly listed files or patterns or roots: if no patterns or .always(): empty list, if exact: list exact files, if not .anypats(): list all files and dirs, else: optimal rootsrrr$r$r%roszbasematcher.filescC t|jSrr)setrrr$r$r%_fileset zbasematcher._filesetcC ||jvS)z!Returns True if f is in .files().)rrrr$r$r%rprzbasematcher.exactcCrNFr$rr$r$r%rzbasematcher.matchfncCr)aDecides whether a directory should be visited based on whether it has potential matches in it or one of its subdirectories. This is based on the match's primary, included, and excluded patterns. Returns the string 'all' if the given directory and all subdirectories should be visited. Otherwise returns True or False indicating whether the given directory should be visited. Tr$rdirr$r$r%visitdirs zbasematcher.visitdircCr)a]Decides whether a directory should be visited based on whether it has potential matches in it or one of its subdirectories, and potentially lists which subdirectories of that directory should be visited. This is based on the match's primary, included, and excluded patterns. This function is very similar to 'visitdir', and the following mapping can be applied: visitdir | visitchildrenlist ----------+------------------- False | set() 'all' | 'all' True | 'this' OR non-empty set of subdirs -or files- to visit Example: Assume matchers ['path:foo/bar', 'rootfilesin:qux'], we would return the following values (assuming the implementation of visitchildrenset is capable of recognizing this; some implementations are not). '' -> {'foo', 'qux'} 'baz' -> set() 'foo' -> {'bar'} # Ideally this would be 'all', but since the prefix nature of matchers # is applied to the entire matcher, we have to downgrade this to # 'this' due to the non-prefix 'rootfilesin'-kind matcher being mixed # in. 'foo/bar' -> 'this' 'qux' -> 'this' Important: Most matchers do not know if they're representing files or directories. They see ['path:dir/f'] and don't know whether 'f' is a file or a directory, so visitchildrenset('dir') for most matchers will return {'f'}, but if the matcher knows it's a file (like exactmatcher does), it may return 'this'. Do not rely on the return being a set indicating that there are no files in this dir to investigate (or equivalently that if there are files to investigate in 'dir' that it will always return 'this'). thisr$rr$r$r%visitchildrensets)zbasematcher.visitchildrensetcCr)zcMatcher will match everything and .files() will be empty -- optimization might be possible.Fr$rr$r$r%rszbasematcher.alwayscCr)zcMatcher will match exactly the list of files in .files() -- optimization might be possible.Fr$rr$r$r%isexactrzbasematcher.isexactcCr)z_Matcher will match the paths in .files() recursively -- optimization might be possible.Fr$rr$r$r%rIrzbasematcher.prefixcCs| o| o| S)z`None of .always(), .isexact(), and .prefix() is true -- optimizations will be difficult.)rsrrIrr$r$r%anypatsszbasematcher.anypatsrr)__name__ __module__ __qualname__rrrw traversedir propertycacherrorrprrrrsrrIrr$r$r$r%rs$    + rcJeZdZdZdfdd ZddZddZd d Zd d Zd dZ Z S)rdzMatches everything.Nctt||dSrr)superrdrr __class__r$r%rzalwaysmatcher.__init__cCrNTr$rr$r$r%rsrzalwaysmatcher.alwayscCrrr$rr$r$r%rrzalwaysmatcher.matchfncCrNallr$rr$r$r%rrzalwaysmatcher.visitdircCrrr$rr$r$r%rrzalwaysmatcher.visitchildrensetcCr)Nzr$rr$r$r%__repr__rzalwaysmatcher.__repr__rr) rrr__doc__rrsrrrr __classcell__r$r$rr%rdsrdcr)rOzMatches nothing.Ncrrr)rrOrrrr$r%rrznevermatcher.__init__cCrrr$rr$r$r%rrznevermatcher.isexactcCrrr$rr$r$r%rIrznevermatcher.prefixcCrrr$rr$r$r%rrznevermatcher.visitdircCstSrr)rrr$r$r%r znevermatcher.visitchildrensetcCr)Nzr$rr$r$r%r#rznevermatcher.__repr__rr) rrrrrrrIrrrrr$r$rr%rO srOcs0eZdZdZdfdd ZejddZZS)predicatematcherz/A matcher adapter for a simple boolean functionNcs tt||||_||_dSrr)rrrr _predrepr)rpredfnpredreprr(rr$r%r*s zpredicatematcher.__init__cCs t|jp t|j}d|S)Ns)r buildreprrr bytereprr)rsr$r$r%r/szpredicatematcher.__repr__NN) rrrrrr strmethodrrr$r$rr%r's rcs|t}|dkr dSvrdS|dkr"tfddtDSdvr(dStjr0tdnd tfd dDS) zDReturns True if `path` (or any parent of `path`) is in `prefix_set`.rFTc3s|]}|vVqdSrrr$)r{ parentdir) prefix_setr$r% Bs z)path_or_parents_in_set..r<r=/c3s*|]}|ot|kVqdSrr startswithrP)r{pf)rFslr$r%rRs()rPanyr finddirsr ispy3ord)rFrlr$)rFrrr%path_or_parents_in_set7s  rcsTeZdZdZdfdd ZeddZddZd d Zd d Z e j d dZ Z S)rea(Matches a set of (kind, pat, source) against a 'root' directory. >>> kindpats = [ ... (b're', br'.*\.c$', b''), ... (b'path', b'foo/a', b''), ... (b'relpath', b'b', b''), ... (b'glob', b'*.h', b''), ... ] >>> m = patternmatcher(b'foo', kindpats) >>> m(b'main.c') # matches re:.*\.c$ True >>> m(b'b.txt') False >>> m(b'foo/a') # matches path:foo/a True >>> m(b'a') # does not match path:b, since 'root' is 'foo' False >>> m(b'b') # matches relpath:b, since 'root' is 'foo' True >>> m(b'lib.h') # matches glob:*.h True >>> m.files() ['', 'foo/a', 'b', ''] >>> m.exact(b'foo/a') True >>> m.exact(b'b') True >>> m.exact(b'lib.h') # exact matches are for (rel)path kinds False Ncs<tt||t||_t||_t|d|\|_|_dS)N$) rrer_explicitfilesr_prefix _buildmatch_patsr)rrCr0r(rr$r%rvs  zpatternmatcher.__init__cCtt|jSrrrr dirsrrr$r$r%_dirs}zpatternmatcher._dirscCs*|jr ||jvr dS||jvpt||jSr)rrrrrr$r$r%rszpatternmatcher.visitdircCs0||}|dur dS|stS|dksJdS)NTrr)rr)rrretr$r$r%rs  zpatternmatcher.visitchildrensetcCrrr)rrr$r$r%rIrzpatternmatcher.prefixcCdt|jS)Nsr bytestrrrr$r$r%rrzpatternmatcher.__repr__rr)rrrrrrrrrrIrrrrr$r$rr%reUs   rec@s2eZdZd ddZddZeddZdd ZdS) _dirchildrenNcCs,i|_|pg|_|j}|D]}||q dSrr)r _onlyincludeaddpath)rpaths onlyincluderrr$r$r%rs   z_dirchildren.__init__cCsN|dkrdS|j}tj}||D]\}}||jvrq||t|qdSNr<)rr_findsplitdirsr setdefaultradd)rrFr findsplitdirsdbr$r$r%rs z_dirchildren.addpathccsjt|}|d}|dkr*|d|||d|fV|}|dd|}|dksd|d|fVdS)Nr=rrr<)rPrfind)rFoldposposr$r$r%rs z_dirchildren._findsplitdirscCs|j|tSrr)rgetr)rrFr$r$r%rsz_dirchildren.getrr)rrrrr staticmethodrrr$r$r$r%rs    rcsHeZdZd fdd ZddZeddZdd Zej d d Z Z S) rfNcshtt||tdur||_t|d|\|_|_t||_t |\}}}t ||_ t ||_ ||_ dS)N(?:/|$))rrfrrustmod _kindpatsrrrr_rootsdirsandparentsr_rootsr_parents)rrCr0r(rootsrparentsrr$r%rs    zincludematcher.__init__cCs4|jr ||jvr dS||jvp||jvpt||jSr)rrrrrrr$r$r%rs  zincludematcher.visitdircCstt|j|j|j|jdS)N)r)r itertoolschainrrrrr$r$r%_allparentschildrensz"includematcher._allparentschildrencCsZ|jr ||jvr dSd|jvs||jvst||jrdS||jvr*|j|p)tStS)Nrr<r)rrrrrrrrrr$r$r%rs    zincludematcher.visitchildrensetcCr)Nsrrr$r$r%rrzincludematcher.__repr__rr) rrrrrrrrrrrrr$r$rr%rfs  rfcsreZdZdZdfdd ZejZeddZ ddZ ed d Z ed d Z d dZ ddZejddZZS)rnaMatches the input files exactly. They are interpreted as paths, not patterns (so no kind-prefixes). >>> m = exactmatcher([b'a.txt', br're:.*\.c$']) >>> m(b'a.txt') True >>> m(b'b.txt') False Input files that would be matched are exactly those returned by .files() >>> m.files() ['a.txt', 're:.*\\.c$'] So pattern 're:.*\.c$' is not considered as a regex, but as a file name >>> m(b'main.c') False >>> m(br're:.*\.c$') True Ncs2tt||t|tr||_dSt||_dSrr)rrnr isinstancelistr)rror(rr$r%rs  zexactmatcher.__init__cCrrrrrr$r$r%rrzexactmatcher._dirscCrrr)rrr$r$r%rrzexactmatcher.visitdircCs|j|jdhBS)z2A memoized set of candidates for visitchildrenset.r<)rrrr$r$r%_visitchildrenset_candidates"sz)exactmatcher._visitchildrenset_candidatescCr)z:A memoized sorted list of candidates for visitchildrenset.)sortedrrr$r$r%#_sorted_visitchildrenset_candidates's z0exactmatcher._sorted_visitchildrenset_candidatescs|jr||jvr tS|dkr|j}n3|j}|d}t||}|tt t dd}tj|||d}t |fdd|||D}dd|D}|sQJ|S)Nr<r=r)locsh|]}|dqSrrr$r{cdlenr$r% Az0exactmatcher.visitchildrenset..cSsh|]}d|vr|qS)r=r$r r$r$r%rGr) rrrrr bisect bisect_leftr strtolocalchrrrP)rr candidatesrfirstdnextlastrr$rr%r,s zexactmatcher.visitchildrensetcCrrr$rr$r$r%rMrzexactmatcher.isexactcC d|jS)Nsrrr$r$r%rPrzexactmatcher.__repr__rr)rrrrrrrprrrrrr rrrrrrr$r$rr%rns   !rncsZeZdZdZfddZddZeddZdd Zd d Z d d Z e j ddZ ZS)rhzComposes two matchers by matching if the first matches and the second does not. The second matcher's non-matching-attributes (bad, traversedir) are ignored. c.tt|||_||_|j|_|j|_dSrr)rrhr_m1_m2rwrrm1m2rr$r%r\  zdifferencematcher.__init__cCs||o || Srrrrrr$r$r%rcszdifferencematcher.matchfncs*rfddjDSjS)Ncg|]}|r|qSr$r$rrr$r%r}irz,differencematcher._files..)rrrorr$rr%rfs zdifferencematcher._filescCs<|j|dkr dS|j|s|j|St|j|S)NrF)rrrboolrr$r$r%rqs   zdifferencematcher.visitdircCs>|j|}|dkr tS|j|}|s|S|dvrdS|S)Nr)rrr)rrrr)rrm2_setm1_setr$r$r%rys  z"differencematcher.visitchildrensetcC |jSrr)rrrr$r$r%rrzdifferencematcher.isexactcCd|j|jfS)Ns r"rr$r$r%rrzdifferencematcher.__repr__)rrrrrrrrrrrrrrrr$r$rr%rhUs   rhcCs^|dus|dur |p |S|rt|}|j|_|j|_|S|r*t|}|St||S)zComposes two matchers by matching if both of them match. The second matcher's non-matching-attributes (bad, traversedir) are ignored. N)rsrvrwrintersectionmatcher)rr r#r$r$r%rgs   rgcs^eZdZfddZeddZddZddZd d Zd d Z d dZ e j ddZ ZS)r)crrr)rr)rrrrwrrrr$r%rr!zintersectionmatcher.__init__csR|r|j|j}|s|}fdd|DS|j|jS)Ncr#r$r$rr r$r%r}rz.intersectionmatcher._files..)rrrro)rrr$r*r%rs  zintersectionmatcher._filescCs||o ||Srrr"rr$r$r%rrzintersectionmatcher.matchfncCs4|j|}|dkr|j|St|o|j|Sr)rrrr$)rrvisit1r$r$r%rs  zintersectionmatcher.visitdircCsz|j|}|s tS|j|}|stS|dkr|S|dkr"|S|dks*|dkr,dSt|tr6t|ts8J||S)Nrr)rrrrr intersection)rrr&r%r$r$r%rs   z$intersectionmatcher.visitchildrensetcCs|jo |jSrr)rrsrrr$r$r%rsrzintersectionmatcher.alwayscCs|jp |jSrr)rrrrr$r$r%rrzintersectionmatcher.isexactcCr()Ns"r"rr$r$r%rrzintersectionmatcher.__repr__)rrrrrrrrrrsrrrrrr$r$rr%r)s   r)cs^eZdZdZfddZddZddZdd Zd d Zd d Z ddZ e j ddZ ZS) subdirmatcheraAdapt a matcher to work on a subdirectory only. The paths are remapped to remove/insert the path as needed: >>> from . import pycompat >>> m1 = match(util.localpath(b'/root'), b'', [b'a.txt', b'sub/b.txt'], auditor=lambda name: None) >>> m2 = subdirmatcher(b'sub', m1) >>> m2(b'a.txt') False >>> m2(b'b.txt') True >>> m2.matchfn(b'a.txt') False >>> m2.matchfn(b'b.txt') True >>> m2.files() ['b.txt'] >>> m2.exact(b'b.txt') True >>> def bad(f, msg): ... print(pycompat.sysstr(b"%s: %s" % (f, msg))) >>> m1.bad = bad >>> m2.bad(b'x.txt', b'No such file') sub/x.txt: No such file csdtt||_||_||_fdd|jD|_|r0t fdd|jD|_dSdS)Ncs.g|]}|dr|tddqS)r=rNrrrFr$r%r} s  z*subdirmatcher.__init__..c3s|]}|kVqdSrrr$rr.r$r%rz)subdirmatcher.__init__..) rr-r_path_matcherrs_alwaysrrIr)rrFmatcherrr.r%rs  zsubdirmatcher.__init__cCs|j|jd||dSNr=)r1rwr0rr$r$r%rwszsubdirmatcher.badcCs|j|jd|Sr4)r1rr0rr$r$r%rszsubdirmatcher.matchfncC*|dkr|j}n|jd|}|j|SNr<r=)r0r1rrr$r$r%r! zsubdirmatcher.visitdircCr5r6)r0r1rrr$r$r%r(r7zsubdirmatcher.visitchildrensetcCrrr)r2rr$r$r%rs/rzsubdirmatcher.alwayscCs|jo|j Srr)r1rIr2rr$r$r%rI2szsubdirmatcher.prefixcCr()Ns#)r0r1rr$r$r%r5szsubdirmatcher.__repr__)rrrrrrwrrrrsrIrrrrr$r$rr%r-s r-cspeZdZdZdfdd ZeddZddZed d Zd d Z d dZ ddZ ddZ e jddZZS)r.aAdapt a matcher to work on a parent directory. The matcher's non-matching-attributes (bad, traversedir) are ignored. The prefix path should usually be the relative path from the root of this matcher to the root of the wrapped matcher. >>> m1 = match(util.localpath(b'/root/d/e'), b'f', [b'../a.txt', b'b.txt'], auditor=lambda name: None) >>> m2 = prefixdirmatcher(b'd/e', m1) >>> m2(b'a.txt') False >>> m2(b'd/e/a.txt') True >>> m2(b'd/e/b.txt') False >>> m2.files() ['d/e/a.txt', 'd/e/f/b.txt'] >>> m2.exact(b'd/e/a.txt') True >>> m2.visitdir(b'd') True >>> m2.visitdir(b'd/e') True >>> m2.visitdir(b'd/e/f') True >>> m2.visitdir(b'd/e/g') False >>> m2.visitdir(b'd/ef') False Ncs8tt|||std||_|d|_||_dS)Nsprefix path must not be emptyr=)rr.rrr)r0 _pathprefixr1)rrFr3r(rr$r%r]s    zprefixdirmatcher.__init__csfddjjDS)Ncsg|]}j|qSr$)r8rrr$r%r}gr~z+prefixdirmatcher._files..)r1rrr$rr%reszprefixdirmatcher._filescCs*||jsdS|j|t|jdSr)rr8r1rrPrr$r$r%ris zprefixdirmatcher.matchfncCrrr)rr rr0rr$r$r% _pathdirsnrzprefixdirmatcher._pathdirscCsF||jkr |jdS||jr|j|t|jdS||jvSr)r0r1rrr8rPr9rr$r$r%rrs    zprefixdirmatcher.visitdircCsP||jkr |jdS||jr|j|t|jdS||jvr%dStS)Nr<r)r0r1rrr8rPr9rrr$r$r%rys    z!prefixdirmatcher.visitchildrensetcCr'rr)r1rrr$r$r%rrzprefixdirmatcher.isexactcCr'rr)r1rIrr$r$r%rIrzprefixdirmatcher.prefixcCsdt|j|jfS)Ns&)r rr0r1rr$r$r%rs zprefixdirmatcher.__repr__rr)rrrrrrrrr9rrrrIrrrrr$r$rr%r.=s   r.csFeZdZdZfddZddZddZdd Zej d d Z Z S) rQzA matcher that is the union of several matchers. The non-matching-attributes (bad, traversedir) are taken from the first matcher. cs(|d}tt||j|_||_dS)Nr)rrQrr _matchers)rr3rrr$r%rs zunionmatcher.__init__cCs|jD] }||r dSqdSNTFr:)rrr!r$r$r%rs zunionmatcher.matchfncCs6d}|jD]}||}|dkr|S||O}q|S)NFr)r:r)rrrr#vr$r$r%rs   zunionmatcher.visitdircCslt}d}|jD]'}||}|sq|dkr|S|s |dkr#d}qt|ts*J||}q|r4dS|S)NFrrT)rr:rrunion)rrr=thisr#r>r$r$r%rs     zunionmatcher.visitchildrensetcCr)Nsr<rr$r$r%rrzunionmatcher.__repr__) rrrrrrrrrrrrr$r$rr%rQs  rQcCst||dS)aIf pattern is 'kind:pat' with a known kind, return kind. >>> patkind(br're:.*\.c$') 're' >>> patkind(b'glob:*.c') 'glob' >>> patkind(b'relpath:test.py') 'relpath' >>> patkind(b'main.py') >>> patkind(b'main.py', default=b're') 're' rry)patternrWr$r$r%patkinds rBcCs0d|vr|dd\}}|tvr||fS||fS)zPSplit a string into the optional pattern kind prefix and the actual pattern.:r)rallpatternkinds)rArWr5r6r$r$r%rzs rzc sRdtd}d}tjjj}fdd}kr'd}d7|dvr5||||7}n|dkrZ|dkrUd7|dkrPd7|d 7}n|d 7}n|d 7}n|d krc|d 7}n|dkrֈ}|kr{||ddvr{|d7}|kr||ddkr|d7}|kr||ddks|kr|d7}n|dd}|d|dddkrd|dd}n |dddkrd|}d||f}nM|dkr|d7}|d7}n@|dkr|r|d7}|d8}n1|dkr|r|d7}n&|dkr|}|rd7||||7}n||||7}n||||7}ks|S)a Convert an extended glob string to a regexp string. >>> from . import pycompat >>> def bprint(s): ... print(pycompat.sysstr(s)) >>> bprint(_globre(br'?')) . >>> bprint(_globre(br'*')) [^/]* >>> bprint(_globre(br'**')) .* >>> bprint(_globre(br'**/a')) (?:.*/)?a >>> bprint(_globre(br'a/**/b')) a/(?:.*/)?b >>> bprint(_globre(br'[a*?!^][^b][!c]')) [a*?!^][\^b][^c] >>> bprint(_globre(br'{a,b}')) (?:a|b) >>> bprint(_globre(br'.\*\?')) \.\*\? rr<csko dS)Nrr$r$inr6r$r%peeksz_globre..peekrs*?[{},\*r=s(?:.*/)?.*[^/]*?.[s!]]s\[\s\\!^Ns%s[%s]{s(?:}),|)rPr r regexbytesescapemaprreplace) r6resgroupescaperHr jstuffr\r$rEr%_globresj               2r_cCs|s|dvrdS|dkr|S|dvr |dkrdStj|dS|dkr:|dkr.d}|d Stj|d}|d S|d krYt|}|d rSd |td d |Sd||S|dkrh|drd|Sd |S|dvrrt||Std||f)z|Convert a (normalized) pattern of any kind into a regular expression. globsuffix is appended to the regexp of globs.)rrr<rrrrMrrr=s[^/]+$rrKrJNs(?:|.*/)rrRrrsnot a regex pattern: %s:%s)r r reescaper_rrPrr))r5r6 globsuffixescapedglobrer$r$r%_regex2s4     rfcsgt||\}rifdd}|d}|rPtdd|DrDdd|Dfdd }d ttt}|n t||\}}|td kr\|d fS|fd dfS)zlReturn regexp string and a matcher function for kindpats. globsuffix is appended to the regexp of globs.csXD]'\}}||r)|}|durt|}||<||t|dr)dSqdSr;)rrr!rP)rrIrHmf) subincludes submatchersr$r%matchsubinclude_s   z$_buildmatch..matchsubincluder<css|] \}}}|dkVqdS)rNr$r{rr\rr$r$r%rosz_buildmatch..cSsh|]\}}}|qSr$r$rkr$r$r%rpr~z_buildmatch..cs2|d}|dkr|d|}|vSd}|vS)Nr=rrM)r)rrFr)rr$r%rgrs  z_buildmatch..mfsrootfilesin: %srrctfddDS)Nc3|]}|VqdSrrr$)r{rgrr$r%rr/z0_buildmatch....rrn) matchfuncsrnr%r~z_buildmatch..) rJr*allr pprintrr _buildregexmatchrP)r0rcrCrjr"rgr$)rrprhrir%rVs$      ri NcCs d|S)z5gather multiple regular expressions into a single onerW)rA)regexpsr$r$r% _joinregexesrrvc szzzg}fdd|D}t|}d}d}t|D]4\}}t|} | tkr/td| } t| || tkrF|||} |t| |}d}|| d7}q|dkr\t|fdd} n||d} |t| d d|Dfd d} || fWSt jy|D]0\} }}z tt | |Wqt jy|rttd || |fttd | |fwttd w)aKBuild a match function from a list of kinds and kindpats, return regexp string and a matcher function. Test too large input >>> _buildregexmatch([ ... (b'relglob', b'?' * MAX_RE_SIZE, b'') ... ], b'$') Traceback (most recent call last): ... Abort: matcher pattern is too long (20009 bytes) csg|] \}}}t||qSr$)rfrk)rcr$r%r}sz$_buildregexmatch..rs&matcher pattern is too long (%d bytes)rcs t|Srr)r$r)r3r$r%rqs z"_buildregexmatch..NcSsg|]}t|qSr$)r&)r{gr$r$r%r}rcrl)Nc3rmrrr$)r{r#rwr$r%rr/z5_buildregexmatch....rorw) allmatchersrwr%rqr~s%s: invalid pattern (%s): %ssinvalid pattern (%s): %ssinvalid pattern) rv enumeraterP MAX_RE_SIZErrrr*r&rrf)r0rc allgroupsru fullregexpstartidx groupsizeidxr= piecesizerr[funcrr\rr$)ryrcr3r%rtsL        rtcCsg}g}|D]X\}}}|dvr9g}|dD]}d|vs(d|vs(d|vs(d|vr*n||q|d|q|dvrI|dkrCd }||q|d vrY|dkrSd }||q|d q||fS) a1Returns roots and directories corresponding to each pattern. This calculates the roots and directories exactly matching the patterns and returns a tuple of (roots, dirs) for each. It does not return other directories which may also need to be considered, like the parent directories. rar=rNrSrIrL)rrrMr<r)rr*rA)r0r=rr5r6r7rCr\r$r$r%_patternrootsanddirss(     rcCst|\}}|S)zFReturns root directories to match recursively from the given patterns.)r)r0rrr$r$r%rs rcCs<t|\}}t}|t||t||||fS)aReturns roots and exact directories from patterns. `roots` are directories to match recursively, `dirs` should be matched non-recursively, and `parents` are the implicitly required directories to walk to items in either roots or dirs. Returns a tuple of (roots, dirs, parents). >>> r = _rootsdirsandparents( ... [(b'glob', b'g/h/*', b''), (b'glob', b'g/h', b''), ... (b'glob', b'g*', b'')]) >>> print(r[0:2], sorted(r[2])) # the set has an unstable output (['g/h', 'g/h', ''], []) ['', 'g'] >>> r = _rootsdirsandparents( ... [(b'rootfilesin', b'g/h', b''), (b'rootfilesin', b'', b'')]) >>> print(r[0:2], sorted(r[2])) # the set has an unstable output ([], ['g/h', '']) ['', 'g'] >>> r = _rootsdirsandparents( ... [(b'relpath', b'r', b''), (b'path', b'p/p', b''), ... (b'path', b'', b'')]) >>> print(r[0:2], sorted(r[2])) # the set has an unstable output (['r', 'p/p', ''], []) ['', 'p'] >>> r = _rootsdirsandparents( ... [(b'relglob', b'rg*', b''), (b're', b're/', b''), ... (b'relre', b'rr', b'')]) >>> print(r[0:2], sorted(r[2])) # the set has an unstable output (['', '', ''], []) [''] )rrupdater r)r0r=rr\r$r$r%rs  rcCsdd|D}t|S)zReturns the potential explicit filenames from the patterns. >>> _explicitfiles([(b'path', b'foo/bar', b'')]) ['foo/bar'] >>> _explicitfiles([(b'rootfilesin', b'foo/bar', b'')]) [] cSsg|] }|ddvr|qS)rrr$)r{rZr$r$r%r}sz"_explicitfiles..)r)r0filabler$r$r%rs rcCs"|D] \}}}|dvrdSqdS)z:Whether all the patterns match a prefix (i.e. recursively)r`FTr$rKr$r$r%r#s rc Cs~ddddddd}d}g}t|d}tt|dd D]\}}d |vrAts+tjd at|} | r;|d | d}| d d }| }|sHq| drr|dd  } z|| }Wnt yp|rn|td|| fYnwq|} t|D]*\} } | | r| } |t| d }n| | dr| } |t| dd }nqy|r|| |||fq|| |q||S)a.parse a pattern file, returning a list of patterns. These patterns should be given to compile() to be validated and converted into a match function. trailing white space is dropped. the escape character is backslash. comments start with #. empty lines are skipped. lines can be of the following formats: syntax: regexp # defaults following lines to non-rooted regexps syntax: glob # defaults following lines to non-rooted globs re:pattern # non-rooted regular expression glob:pattern # non-rooted glob rootglob:pat # rooted glob (same root as ^ in regexps) pattern # pattern of the current default type if sourceinfo is set, returns a list of tuples: (pattern, lineno, originalline). This is useful to debug ignore patterns. srelre:srelglob:s rootglob:rr)rsregexprrrrsrbr)start#s((?:^|[^\\])(?:\\\\)*)#.*Ns\#ssyntax:s!%s: ignoring invalid syntax '%s' rC)rrzr iterfile _commentrerrsearchendrYrstriprstripKeyErrorrr iteritemsrPr*close) filepathrY sourceinfosyntaxessyntaxrVfplinenoliner#r linesyntaxrelsr$r$r%r.sb        r)NFN) NNNrNNFNNFrrr)F)C __future__rrrrvrr`ri18nrr rrrr r r utilsr importrustrrDrrr&r;rJrLrTr!rprsrurxrUobjectrrdrOrrrerrfrnrhrgr)r-r.rQrBrzr_rfrr{rvrtrrrrrrrr$r$r$r%s          %   4tE%=YE=RS 4 T$06,