##// END OF EJS Templates
purge: use cmdutil.matchpats
Matt Mackall -
r6574:76af1dff default
parent child Browse files
Show More
@@ -1,145 +1,142 b''
1 # Copyright (C) 2006 - Marco Barisione <marco@barisione.org>
1 # Copyright (C) 2006 - Marco Barisione <marco@barisione.org>
2 #
2 #
3 # This is a small extension for Mercurial (http://www.selenic.com/mercurial)
3 # This is a small extension for Mercurial (http://www.selenic.com/mercurial)
4 # that removes files not known to mercurial
4 # that removes files not known to mercurial
5 #
5 #
6 # This program was inspired by the "cvspurge" script contained in CVS utilities
6 # This program was inspired by the "cvspurge" script contained in CVS utilities
7 # (http://www.red-bean.com/cvsutils/).
7 # (http://www.red-bean.com/cvsutils/).
8 #
8 #
9 # To enable the "purge" extension put these lines in your ~/.hgrc:
9 # To enable the "purge" extension put these lines in your ~/.hgrc:
10 # [extensions]
10 # [extensions]
11 # hgext.purge =
11 # hgext.purge =
12 #
12 #
13 # For help on the usage of "hg purge" use:
13 # For help on the usage of "hg purge" use:
14 # hg help purge
14 # hg help purge
15 #
15 #
16 # This program is free software; you can redistribute it and/or modify
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
19 # (at your option) any later version.
20 #
20 #
21 # This program is distributed in the hope that it will be useful,
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
24 # GNU General Public License for more details.
25 #
25 #
26 # You should have received a copy of the GNU General Public License
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29
29
30 from mercurial import util, commands
30 from mercurial import util, commands, cmdutil
31 from mercurial.i18n import _
31 from mercurial.i18n import _
32 import os
32 import os
33
33
34 def purge(ui, repo, *dirs, **opts):
34 def purge(ui, repo, *dirs, **opts):
35 '''removes files not tracked by mercurial
35 '''removes files not tracked by mercurial
36
36
37 Delete files not known to mercurial, this is useful to test local and
37 Delete files not known to mercurial, this is useful to test local and
38 uncommitted changes in the otherwise clean source tree.
38 uncommitted changes in the otherwise clean source tree.
39
39
40 This means that purge will delete:
40 This means that purge will delete:
41 - Unknown files: files marked with "?" by "hg status"
41 - Unknown files: files marked with "?" by "hg status"
42 - Ignored files: files usually ignored by Mercurial because they match
42 - Ignored files: files usually ignored by Mercurial because they match
43 a pattern in a ".hgignore" file
43 a pattern in a ".hgignore" file
44 - Empty directories: in fact Mercurial ignores directories unless they
44 - Empty directories: in fact Mercurial ignores directories unless they
45 contain files under source control managment
45 contain files under source control managment
46 But it will leave untouched:
46 But it will leave untouched:
47 - Unmodified tracked files
47 - Unmodified tracked files
48 - Modified tracked files
48 - Modified tracked files
49 - New files added to the repository (with "hg add")
49 - New files added to the repository (with "hg add")
50
50
51 If directories are given on the command line, only files in these
51 If directories are given on the command line, only files in these
52 directories are considered.
52 directories are considered.
53
53
54 Be careful with purge, you could irreversibly delete some files you
54 Be careful with purge, you could irreversibly delete some files you
55 forgot to add to the repository. If you only want to print the list of
55 forgot to add to the repository. If you only want to print the list of
56 files that this program would delete use the --print option.
56 files that this program would delete use the --print option.
57 '''
57 '''
58 act = not opts['print']
58 act = not opts['print']
59 ignored = bool(opts['all'])
59 ignored = bool(opts['all'])
60 abort_on_err = bool(opts['abort_on_err'])
60 abort_on_err = bool(opts['abort_on_err'])
61 eol = opts['print0'] and '\0' or '\n'
61 eol = opts['print0'] and '\0' or '\n'
62 if eol == '\0':
62 if eol == '\0':
63 # --print0 implies --print
63 # --print0 implies --print
64 act = False
64 act = False
65 force = bool(opts['force'])
65 force = bool(opts['force'])
66 include = opts['include']
67 exclude = opts['exclude']
68
66
69 def error(msg):
67 def error(msg):
70 if abort_on_err:
68 if abort_on_err:
71 raise util.Abort(msg)
69 raise util.Abort(msg)
72 else:
70 else:
73 ui.warn(_('warning: %s\n') % msg)
71 ui.warn(_('warning: %s\n') % msg)
74
72
75 def remove(remove_func, name):
73 def remove(remove_func, name):
76 if act:
74 if act:
77 try:
75 try:
78 remove_func(os.path.join(repo.root, name))
76 remove_func(os.path.join(repo.root, name))
79 except OSError, e:
77 except OSError, e:
80 error(_('%s cannot be removed') % name)
78 error(_('%s cannot be removed') % name)
81 else:
79 else:
82 ui.write('%s%s' % (name, eol))
80 ui.write('%s%s' % (name, eol))
83
81
84 if not force:
82 if not force:
85 _check_fs(ui, repo)
83 _check_fs(ui, repo)
86
84
87 directories = []
85 directories = []
88 files = []
86 files = []
89 missing = []
87 missing = []
90 roots, match, anypats = util.cmdmatcher(repo.root, repo.getcwd(), dirs,
88 roots, match, anypats = cmdutil.matchpats(repo, dirs, opts)
91 include, exclude)
92 for src, f, st in repo.dirstate.statwalk(roots, match,
89 for src, f, st in repo.dirstate.statwalk(roots, match,
93 ignored=ignored, directories=True):
90 ignored=ignored, directories=True):
94 if src == 'd':
91 if src == 'd':
95 directories.append(f)
92 directories.append(f)
96 elif src == 'm':
93 elif src == 'm':
97 missing.append(f)
94 missing.append(f)
98 elif src == 'f' and f not in repo.dirstate:
95 elif src == 'f' and f not in repo.dirstate:
99 files.append(f)
96 files.append(f)
100
97
101 directories.sort()
98 directories.sort()
102
99
103 for f in files:
100 for f in files:
104 if f not in repo.dirstate:
101 if f not in repo.dirstate:
105 ui.note(_('Removing file %s\n') % f)
102 ui.note(_('Removing file %s\n') % f)
106 remove(os.remove, f)
103 remove(os.remove, f)
107
104
108 for f in directories[::-1]:
105 for f in directories[::-1]:
109 if match(f) and not os.listdir(repo.wjoin(f)):
106 if match(f) and not os.listdir(repo.wjoin(f)):
110 ui.note(_('Removing directory %s\n') % f)
107 ui.note(_('Removing directory %s\n') % f)
111 remove(os.rmdir, f)
108 remove(os.rmdir, f)
112
109
113 def _check_fs(ui, repo):
110 def _check_fs(ui, repo):
114 """Abort if there is the chance of having problems with name-mangling fs
111 """Abort if there is the chance of having problems with name-mangling fs
115
112
116 In a name mangling filesystem (e.g. a case insensitive one)
113 In a name mangling filesystem (e.g. a case insensitive one)
117 dirstate.walk() can yield filenames different from the ones
114 dirstate.walk() can yield filenames different from the ones
118 stored in the dirstate. This already confuses the status and
115 stored in the dirstate. This already confuses the status and
119 add commands, but with purge this may cause data loss.
116 add commands, but with purge this may cause data loss.
120
117
121 To prevent this, this function will abort if there are uncommitted
118 To prevent this, this function will abort if there are uncommitted
122 changes.
119 changes.
123 """
120 """
124
121
125 # We can't use (files, match) to do a partial walk here - we wouldn't
122 # We can't use (files, match) to do a partial walk here - we wouldn't
126 # notice a modified README file if the user ran "hg purge readme"
123 # notice a modified README file if the user ran "hg purge readme"
127 modified, added, removed, deleted = repo.status()[:4]
124 modified, added, removed, deleted = repo.status()[:4]
128 if modified or added or removed or deleted:
125 if modified or added or removed or deleted:
129 if not util.checkfolding(repo.path) and not ui.quiet:
126 if not util.checkfolding(repo.path) and not ui.quiet:
130 ui.warn(_("Purging on name mangling filesystems is not "
127 ui.warn(_("Purging on name mangling filesystems is not "
131 "fully supported.\n"))
128 "fully supported.\n"))
132 raise util.Abort(_("outstanding uncommitted changes"))
129 raise util.Abort(_("outstanding uncommitted changes"))
133
130
134 cmdtable = {
131 cmdtable = {
135 'purge|clean':
132 'purge|clean':
136 (purge,
133 (purge,
137 [('a', 'abort-on-err', None, _('abort if an error occurs')),
134 [('a', 'abort-on-err', None, _('abort if an error occurs')),
138 ('', 'all', None, _('purge ignored files too')),
135 ('', 'all', None, _('purge ignored files too')),
139 ('f', 'force', None, _('purge even when there are uncommitted changes')),
136 ('f', 'force', None, _('purge even when there are uncommitted changes')),
140 ('p', 'print', None, _('print the file names instead of deleting them')),
137 ('p', 'print', None, _('print the file names instead of deleting them')),
141 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
138 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
142 ' (implies -p)')),
139 ' (implies -p)')),
143 ] + commands.walkopts,
140 ] + commands.walkopts,
144 _('hg purge [OPTION]... [DIR]...'))
141 _('hg purge [OPTION]... [DIR]...'))
145 }
142 }
General Comments 0
You need to be logged in to leave comments. Login now