##// END OF EJS Templates
move walk and matchpats from commands to cmdutil.
Vadim Gelfer -
r2882:cf98cd70 default
parent child Browse files
Show More
@@ -8,6 +8,7 b''
8 from demandload import demandload
8 from demandload import demandload
9 from node import *
9 from node import *
10 from i18n import gettext as _
10 from i18n import gettext as _
11 demandload(globals(), 'util')
11 demandload(globals(), 'os sys')
12 demandload(globals(), 'os sys')
12
13
13 def make_filename(repo, pat, node,
14 def make_filename(repo, pat, node,
@@ -66,3 +67,26 b' def make_file(repo, pat, node=None,'
66 return open(make_filename(repo, pat, node, total, seqno, revwidth,
67 return open(make_filename(repo, pat, node, total, seqno, revwidth,
67 pathname),
68 pathname),
68 mode)
69 mode)
70
71 def matchpats(repo, pats=[], opts={}, head=''):
72 cwd = repo.getcwd()
73 if not pats and cwd:
74 opts['include'] = [os.path.join(cwd, i) for i in opts['include']]
75 opts['exclude'] = [os.path.join(cwd, x) for x in opts['exclude']]
76 cwd = ''
77 return util.cmdmatcher(repo.root, cwd, pats or ['.'], opts.get('include'),
78 opts.get('exclude'), head)
79
80 def makewalk(repo, pats, opts, node=None, head='', badmatch=None):
81 files, matchfn, anypats = matchpats(repo, pats, opts, head)
82 exact = dict(zip(files, files))
83 def walk():
84 for src, fn in repo.walk(node=node, files=files, match=matchfn,
85 badmatch=badmatch):
86 yield src, fn, util.pathto(repo.getcwd(), fn), fn in exact
87 return files, matchfn, walk()
88
89 def walk(repo, pats, opts, node=None, head='', badmatch=None):
90 files, matchfn, results = makewalk(repo, pats, opts, node, head, badmatch)
91 for r in results:
92 yield r
@@ -50,29 +50,6 b' def logmessage(opts):'
50 (logfile, inst.strerror))
50 (logfile, inst.strerror))
51 return message
51 return message
52
52
53 def matchpats(repo, pats=[], opts={}, head=''):
54 cwd = repo.getcwd()
55 if not pats and cwd:
56 opts['include'] = [os.path.join(cwd, i) for i in opts['include']]
57 opts['exclude'] = [os.path.join(cwd, x) for x in opts['exclude']]
58 cwd = ''
59 return util.cmdmatcher(repo.root, cwd, pats or ['.'], opts.get('include'),
60 opts.get('exclude'), head)
61
62 def makewalk(repo, pats, opts, node=None, head='', badmatch=None):
63 files, matchfn, anypats = matchpats(repo, pats, opts, head)
64 exact = dict(zip(files, files))
65 def walk():
66 for src, fn in repo.walk(node=node, files=files, match=matchfn,
67 badmatch=badmatch):
68 yield src, fn, util.pathto(repo.getcwd(), fn), fn in exact
69 return files, matchfn, walk()
70
71 def walk(repo, pats, opts, node=None, head='', badmatch=None):
72 files, matchfn, results = makewalk(repo, pats, opts, node, head, badmatch)
73 for r in results:
74 yield r
75
76 def walkchangerevs(ui, repo, pats, opts):
53 def walkchangerevs(ui, repo, pats, opts):
77 '''Iterate over files and the revs they changed in.
54 '''Iterate over files and the revs they changed in.
78
55
@@ -115,7 +92,7 b' def walkchangerevs(ui, repo, pats, opts)'
115 windowsize *= 2
92 windowsize *= 2
116
93
117
94
118 files, matchfn, anypats = matchpats(repo, pats, opts)
95 files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)
119 follow = opts.get('follow') or opts.get('follow_first')
96 follow = opts.get('follow') or opts.get('follow_first')
120
97
121 if repo.changelog.count() == 0:
98 if repo.changelog.count() == 0:
@@ -653,7 +630,7 b' def add(ui, repo, *pats, **opts):'
653 """
630 """
654
631
655 names = []
632 names = []
656 for src, abs, rel, exact in walk(repo, pats, opts):
633 for src, abs, rel, exact in cmdutil.walk(repo, pats, opts):
657 if exact:
634 if exact:
658 if ui.verbose:
635 if ui.verbose:
659 ui.status(_('adding %s\n') % rel)
636 ui.status(_('adding %s\n') % rel)
@@ -682,7 +659,7 b' def addremove(ui, repo, *pats, **opts):'
682
659
683 def addremove_lock(ui, repo, pats, opts, wlock=None):
660 def addremove_lock(ui, repo, pats, opts, wlock=None):
684 add, remove = [], []
661 add, remove = [], []
685 for src, abs, rel, exact in walk(repo, pats, opts):
662 for src, abs, rel, exact in cmdutil.walk(repo, pats, opts):
686 if src == 'f' and repo.dirstate.state(abs) == '?':
663 if src == 'f' and repo.dirstate.state(abs) == '?':
687 add.append(abs)
664 add.append(abs)
688 if ui.verbose or not exact:
665 if ui.verbose or not exact:
@@ -736,7 +713,8 b' def annotate(ui, repo, *pats, **opts):'
736
713
737 ctx = repo.changectx(opts['rev'] or repo.dirstate.parents()[0])
714 ctx = repo.changectx(opts['rev'] or repo.dirstate.parents()[0])
738
715
739 for src, abs, rel, exact in walk(repo, pats, opts, node=ctx.node()):
716 for src, abs, rel, exact in cmdutil.walk(repo, pats, opts,
717 node=ctx.node()):
740 fctx = ctx.filectx(abs)
718 fctx = ctx.filectx(abs)
741 if not opts['text'] and util.binary(fctx.data()):
719 if not opts['text'] and util.binary(fctx.data()):
742 ui.write(_("%s: binary file\n") % ((pats and rel) or abs))
720 ui.write(_("%s: binary file\n") % ((pats and rel) or abs))
@@ -791,7 +769,7 b' def archive(ui, repo, dest, **opts):'
791 dest = cmdutil.make_filename(repo, dest, node)
769 dest = cmdutil.make_filename(repo, dest, node)
792 if os.path.realpath(dest) == repo.root:
770 if os.path.realpath(dest) == repo.root:
793 raise util.Abort(_('repository root cannot be destination'))
771 raise util.Abort(_('repository root cannot be destination'))
794 dummy, matchfn, dummy = matchpats(repo, [], opts)
772 dummy, matchfn, dummy = cmdutil.matchpats(repo, [], opts)
795 kind = opts.get('type') or 'files'
773 kind = opts.get('type') or 'files'
796 prefix = opts['prefix']
774 prefix = opts['prefix']
797 if dest == '-':
775 if dest == '-':
@@ -903,7 +881,8 b' def cat(ui, repo, file1, *pats, **opts):'
903 %p root-relative path name of file being printed
881 %p root-relative path name of file being printed
904 """
882 """
905 ctx = repo.changectx(opts['rev'] or "-1")
883 ctx = repo.changectx(opts['rev'] or "-1")
906 for src, abs, rel, exact in walk(repo, (file1,) + pats, opts, ctx.node()):
884 for src, abs, rel, exact in cmdutil.walk(repo, (file1,) + pats, opts,
885 ctx.node()):
907 fp = cmdutil.make_file(repo, opts['output'], ctx.node(), pathname=abs)
886 fp = cmdutil.make_file(repo, opts['output'], ctx.node(), pathname=abs)
908 fp.write(ctx.filectx(abs).data())
887 fp.write(ctx.filectx(abs).data())
909
888
@@ -967,7 +946,7 b' def commit(ui, repo, *pats, **opts):'
967
946
968 if opts['addremove']:
947 if opts['addremove']:
969 addremove_lock(ui, repo, pats, opts)
948 addremove_lock(ui, repo, pats, opts)
970 fns, match, anypats = matchpats(repo, pats, opts)
949 fns, match, anypats = cmdutil.matchpats(repo, pats, opts)
971 if pats:
950 if pats:
972 modified, added, removed = repo.status(files=fns, match=match)[:3]
951 modified, added, removed = repo.status(files=fns, match=match)[:3]
973 files = modified + added + removed
952 files = modified + added + removed
@@ -1124,7 +1103,7 b' def docopy(ui, repo, pats, opts, wlock):'
1124 copylist = []
1103 copylist = []
1125 for pat in pats:
1104 for pat in pats:
1126 srcs = []
1105 srcs = []
1127 for tag, abssrc, relsrc, exact in walk(repo, [pat], opts):
1106 for tag, abssrc, relsrc, exact in cmdutil.walk(repo, [pat], opts):
1128 origsrc = okaytocopy(abssrc, relsrc, exact)
1107 origsrc = okaytocopy(abssrc, relsrc, exact)
1129 if origsrc:
1108 if origsrc:
1130 srcs.append((origsrc, abssrc, relsrc, exact))
1109 srcs.append((origsrc, abssrc, relsrc, exact))
@@ -1341,7 +1320,7 b' def debugrename(ui, repo, file, rev=None'
1341
1320
1342 def debugwalk(ui, repo, *pats, **opts):
1321 def debugwalk(ui, repo, *pats, **opts):
1343 """show how files match on given patterns"""
1322 """show how files match on given patterns"""
1344 items = list(walk(repo, pats, opts))
1323 items = list(cmdutil.walk(repo, pats, opts))
1345 if not items:
1324 if not items:
1346 return
1325 return
1347 fmt = '%%s %%-%ds %%-%ds %%s' % (
1326 fmt = '%%s %%-%ds %%-%ds %%s' % (
@@ -1370,7 +1349,7 b' def diff(ui, repo, *pats, **opts):'
1370 """
1349 """
1371 node1, node2 = revpair(ui, repo, opts['rev'])
1350 node1, node2 = revpair(ui, repo, opts['rev'])
1372
1351
1373 fns, matchfn, anypats = matchpats(repo, pats, opts)
1352 fns, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)
1374
1353
1375 patch.diff(repo, node1, node2, fns, match=matchfn,
1354 patch.diff(repo, node1, node2, fns, match=matchfn,
1376 opts=ui.diffopts(opts))
1355 opts=ui.diffopts(opts))
@@ -1423,7 +1402,7 b' def forget(ui, repo, *pats, **opts):'
1423 """
1402 """
1424 ui.warn(_("(the forget command is deprecated; use revert instead)\n"))
1403 ui.warn(_("(the forget command is deprecated; use revert instead)\n"))
1425 forget = []
1404 forget = []
1426 for src, abs, rel, exact in walk(repo, pats, opts):
1405 for src, abs, rel, exact in cmdutil.walk(repo, pats, opts):
1427 if repo.dirstate.state(abs) == 'a':
1406 if repo.dirstate.state(abs) == 'a':
1428 forget.append(abs)
1407 forget.append(abs)
1429 if ui.verbose or not exact:
1408 if ui.verbose or not exact:
@@ -1847,8 +1826,8 b' def locate(ui, repo, *pats, **opts):'
1847 else:
1826 else:
1848 node = None
1827 node = None
1849
1828
1850 for src, abs, rel, exact in walk(repo, pats, opts, node=node,
1829 for src, abs, rel, exact in cmdutil.walk(repo, pats, opts, node=node,
1851 head='(?:.*/|)'):
1830 head='(?:.*/|)'):
1852 if not node and repo.dirstate.state(abs) == '?':
1831 if not node and repo.dirstate.state(abs) == '?':
1853 continue
1832 continue
1854 if opts['fullpath']:
1833 if opts['fullpath']:
@@ -2244,12 +2223,12 b' def remove(ui, repo, *pats, **opts):'
2244 names = []
2223 names = []
2245 if not opts['after'] and not pats:
2224 if not opts['after'] and not pats:
2246 raise util.Abort(_('no files specified'))
2225 raise util.Abort(_('no files specified'))
2247 files, matchfn, anypats = matchpats(repo, pats, opts)
2226 files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)
2248 exact = dict.fromkeys(files)
2227 exact = dict.fromkeys(files)
2249 mardu = map(dict.fromkeys, repo.status(files=files, match=matchfn))[:5]
2228 mardu = map(dict.fromkeys, repo.status(files=files, match=matchfn))[:5]
2250 modified, added, removed, deleted, unknown = mardu
2229 modified, added, removed, deleted, unknown = mardu
2251 remove, forget = [], []
2230 remove, forget = [], []
2252 for src, abs, rel, exact in walk(repo, pats, opts):
2231 for src, abs, rel, exact in cmdutil.walk(repo, pats, opts):
2253 reason = None
2232 reason = None
2254 if abs not in deleted and opts['after']:
2233 if abs not in deleted and opts['after']:
2255 reason = _('is still present')
2234 reason = _('is still present')
@@ -2356,15 +2335,16 b' def revert(ui, repo, *pats, **opts):'
2356
2335
2357 # walk dirstate.
2336 # walk dirstate.
2358
2337
2359 for src, abs, rel, exact in walk(repo, pats, opts, badmatch=mf.has_key):
2338 for src, abs, rel, exact in cmdutil.walk(repo, pats, opts,
2339 badmatch=mf.has_key):
2360 names[abs] = (rel, exact)
2340 names[abs] = (rel, exact)
2361 if src == 'b':
2341 if src == 'b':
2362 target_only[abs] = True
2342 target_only[abs] = True
2363
2343
2364 # walk target manifest.
2344 # walk target manifest.
2365
2345
2366 for src, abs, rel, exact in walk(repo, pats, opts, node=node,
2346 for src, abs, rel, exact in cmdutil.walk(repo, pats, opts, node=node,
2367 badmatch=names.has_key):
2347 badmatch=names.has_key):
2368 if abs in names: continue
2348 if abs in names: continue
2369 names[abs] = (rel, exact)
2349 names[abs] = (rel, exact)
2370 target_only[abs] = True
2350 target_only[abs] = True
@@ -2576,7 +2556,7 b' def status(ui, repo, *pats, **opts):'
2576
2556
2577 all = opts['all']
2557 all = opts['all']
2578
2558
2579 files, matchfn, anypats = matchpats(repo, pats, opts)
2559 files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)
2580 cwd = (pats and repo.getcwd()) or ''
2560 cwd = (pats and repo.getcwd()) or ''
2581 modified, added, removed, deleted, unknown, ignored, clean = [
2561 modified, added, removed, deleted, unknown, ignored, clean = [
2582 [util.pathto(cwd, x) for x in n]
2562 [util.pathto(cwd, x) for x in n]
General Comments 0
You need to be logged in to leave comments. Login now