##// END OF EJS Templates
Refactor matchpats and walk...
mpm@selenic.com -
r742:092937de default
parent child Browse files
Show More
@@ -39,40 +39,16 b' def relpath(repo, args):'
39 for x in args]
39 for x in args]
40 return args
40 return args
41
41
42 def matchpats(ui, cwd, pats = [], opts = {}, emptyok = True):
42 def matchpats(cwd, pats = [], opts = {}, head = ''):
43 if not pats and not emptyok:
43 return util.matcher(cwd, pats, opts.get('include'),
44 raise Abort('at least one file name or pattern required')
44 opts.get('exclude'), head)
45 head = ''
46 if opts.get('rootless'): head = '(?:.*/|)'
47 def reify(name, tail):
48 if name.startswith('re:'):
49 return name[3:]
50 elif name.startswith('glob:'):
51 return head + util.globre(name[5:], '', tail)
52 elif name.startswith('path:'):
53 return '^' + re.escape(name[5:]) + '$'
54 return head + util.globre(name, '', tail)
55 cwdsep = cwd + os.sep
56 def under(fn):
57 if not cwd or fn.startswith(cwdsep): return True
58 def matchfn(pats, tail, ifempty = util.always):
59 if not pats: return ifempty
60 pat = '(?:%s)' % '|'.join([reify(p, tail) for p in pats])
61 if cwd: pat = re.escape(cwd + os.sep) + pat
62 ui.debug('regexp: %s\n' % pat)
63 return re.compile(pat).match
64 patmatch = matchfn(pats, '$')
65 incmatch = matchfn(opts.get('include'), '(?:/|$)', under)
66 excmatch = matchfn(opts.get('exclude'), '(?:/|$)', util.never)
67 return lambda fn: (incmatch(fn) and not excmatch(fn) and
68 (fn.endswith('/') or patmatch(fn)))
69
45
70 def walk(repo, pats, opts, emptyok = True):
46 def walk(repo, pats, opts, head = ''):
71 cwd = repo.getcwd()
47 cwd = repo.getcwd()
48 c = 0
72 if cwd: c = len(cwd) + 1
49 if cwd: c = len(cwd) + 1
73 for src, fn in repo.walk(match = matchpats(repo.ui, cwd, pats, opts, emptyok)):
50 for src, fn in repo.walk(match = matchpats(cwd, pats, opts, head)):
74 if cwd: yield src, fn, fn[c:]
51 yield src, fn, fn[c:]
75 else: yield src, fn, fn
76
52
77 revrangesep = ':'
53 revrangesep = ':'
78
54
@@ -709,10 +685,10 b' def init(ui, source=None):'
709
685
710 def locate(ui, repo, *pats, **opts):
686 def locate(ui, repo, *pats, **opts):
711 """locate files matching specific patterns"""
687 """locate files matching specific patterns"""
688 end = '\n'
712 if opts['print0']: end = '\0'
689 if opts['print0']: end = '\0'
713 else: end = '\n'
690
714 opts['rootless'] = True
691 for src, abs, rel in walk(repo, pats, opts, '(?:.*/|)'):
715 for src, abs, rel in walk(repo, pats, opts):
716 if repo.dirstate.state(abs) == '?': continue
692 if repo.dirstate.state(abs) == '?': continue
717 if opts['fullpath']:
693 if opts['fullpath']:
718 ui.write(os.path.join(repo.root, abs), end)
694 ui.write(os.path.join(repo.root, abs), end)
@@ -998,8 +974,7 b' def status(ui, repo, *pats, **opts):'
998 R = removed
974 R = removed
999 ? = not tracked'''
975 ? = not tracked'''
1000
976
1001 (c, a, d, u) = repo.changes(match = matchpats(ui, repo.getcwd(),
977 (c, a, d, u) = repo.changes(match = matchpats(repo.getcwd(), pats, opts))
1002 pats, opts))
1003 (c, a, d, u) = map(lambda x: relfilter(repo, x), (c, a, d, u))
978 (c, a, d, u) = map(lambda x: relfilter(repo, x), (c, a, d, u))
1004
979
1005 for f in c:
980 for f in c:
@@ -79,6 +79,37 b" def globre(pat, head = '^', tail = '$'):"
79 res += re.escape(c)
79 res += re.escape(c)
80 return head + res + tail
80 return head + res + tail
81
81
82 def matcher(cwd, pats, inc, exc, head = ''):
83 def regex(name, tail):
84 '''convert a pattern into a regular expression'''
85 if name.startswith('re:'):
86 return name[3:]
87 elif name.startswith('path:'):
88 return '^' + re.escape(name[5:]) + '$'
89 elif name.startswith('glob:'):
90 return head + globre(name[5:], '', tail)
91 return head + globre(name, '', tail)
92
93 def under(fn):
94 """check if fn is under our cwd"""
95 return not cwd or fn.startswith(cwdsep)
96
97 def matchfn(pats, tail):
98 """build a matching function from a set of patterns"""
99 if pats:
100 pat = '(?:%s)' % '|'.join([regex(p, tail) for p in pats])
101 if cwd:
102 pat = re.escape(cwd + os.sep) + pat
103 return re.compile(pat).match
104
105 cwdsep = cwd + os.sep
106 patmatch = matchfn(pats, '$') or (lambda fn: True)
107 incmatch = matchfn(inc, '(?:/|$)') or under
108 excmatch = matchfn(exc, '(?:/|$)') or (lambda fn: False)
109
110 return lambda fn: (incmatch(fn) and not excmatch(fn) and
111 (fn.endswith('/') or patmatch(fn)))
112
82 def system(cmd, errprefix=None):
113 def system(cmd, errprefix=None):
83 """execute a shell command that must succeed"""
114 """execute a shell command that must succeed"""
84 rc = os.system(cmd)
115 rc = os.system(cmd)
General Comments 0
You need to be logged in to leave comments. Login now