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