comparison hgext/purge.py @ 4462:a73cf208b2a0

purge: add --include and --exclude options
author Emanuele Aina <em@nerd.ocracy.org>
date Fri, 11 May 2007 17:05:44 +0200
parents 1043e4b27ab9
children 96d8a56d4ef9
comparison
equal deleted inserted replaced
4461:12e4d9524951 4462:a73cf208b2a0
30 from mercurial import hg, util 30 from mercurial import hg, util
31 from mercurial.i18n import _ 31 from mercurial.i18n import _
32 import os 32 import os
33 33
34 def dopurge(ui, repo, dirs=None, act=True, abort_on_err=False, eol='\n', 34 def dopurge(ui, repo, dirs=None, act=True, abort_on_err=False, eol='\n',
35 force=False): 35 force=False, include=None, exclude=None):
36 def error(msg): 36 def error(msg):
37 if abort_on_err: 37 if abort_on_err:
38 raise util.Abort(msg) 38 raise util.Abort(msg)
39 else: 39 else:
40 ui.warn(_('warning: %s\n') % msg) 40 ui.warn(_('warning: %s\n') % msg)
49 ui.write('%s%s' % (name, eol)) 49 ui.write('%s%s' % (name, eol))
50 50
51 directories = [] 51 directories = []
52 files = [] 52 files = []
53 missing = [] 53 missing = []
54 roots, match, anypats = util.cmdmatcher(repo.root, repo.getcwd(), dirs) 54 roots, match, anypats = util.cmdmatcher(repo.root, repo.getcwd(), dirs,
55 include, exclude)
55 for src, f, st in repo.dirstate.statwalk(files=roots, match=match, 56 for src, f, st in repo.dirstate.statwalk(files=roots, match=match,
56 ignored=True, directories=True): 57 ignored=True, directories=True):
57 if src == 'd': 58 if src == 'd':
58 directories.append(f) 59 directories.append(f)
59 elif src == 'm': 60 elif src == 'm':
69 if f not in repo.dirstate: 70 if f not in repo.dirstate:
70 ui.note(_('Removing file %s\n') % f) 71 ui.note(_('Removing file %s\n') % f)
71 remove(os.remove, f) 72 remove(os.remove, f)
72 73
73 for f in directories[::-1]: 74 for f in directories[::-1]:
74 if not os.listdir(repo.wjoin(f)): 75 if match(f) and not os.listdir(repo.wjoin(f)):
75 ui.note(_('Removing directory %s\n') % f) 76 ui.note(_('Removing directory %s\n') % f)
76 remove(os.rmdir, f) 77 remove(os.rmdir, f)
77 78
78 def _check_missing(ui, repo, missing, force=False): 79 def _check_missing(ui, repo, missing, force=False):
79 """Abort if there is the chance of having problems with name-mangling fs 80 """Abort if there is the chance of having problems with name-mangling fs
142 eol = opts['print0'] and '\0' or '\n' 143 eol = opts['print0'] and '\0' or '\n'
143 if eol == '\0': 144 if eol == '\0':
144 # --print0 implies --print 145 # --print0 implies --print
145 act = False 146 act = False
146 force = bool(opts['force']) 147 force = bool(opts['force'])
147 dopurge(ui, repo, dirs, act, abort_on_err, eol, force) 148 include = opts['include']
149 exclude = opts['exclude']
150 dopurge(ui, repo, dirs, act, abort_on_err, eol, force, include, exclude)
148 151
149 152
150 cmdtable = { 153 cmdtable = {
151 'purge': 154 'purge':
152 (purge, 155 (purge,
153 [('a', 'abort-on-err', None, _('abort if an error occurs')), 156 [('a', 'abort-on-err', None, _('abort if an error occurs')),
154 ('f', 'force', None, _('purge even when missing files are detected')), 157 ('f', 'force', None, _('purge even when missing files are detected')),
155 ('p', 'print', None, _('print the file names instead of deleting them')), 158 ('p', 'print', None, _('print the file names instead of deleting them')),
156 ('0', 'print0', None, _('end filenames with NUL, for use with xargs' 159 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
157 ' (implies -p)'))], 160 ' (implies -p)')),
161 ('I', 'include', [], _('include names matching the given patterns')),
162 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
158 _('hg purge [OPTION]... [DIR]...')) 163 _('hg purge [OPTION]... [DIR]...'))
159 } 164 }