##// END OF EJS Templates
1 file changed, 7 insertions(+), 9 deletions(-)...
Benjamin Pollack -
r7605:3e592067 default
parent child Browse files
Show More
@@ -1,100 +1,98 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, cmdutil
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 an 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
43 a pattern in a ".hgignore" file
44 - Empty directories: in fact Mercurial ignores directories unless they
42 - Empty directories: in fact Mercurial ignores directories unless they
45 contain files under source control managment
43 contain files under source control managment
46 But it will leave untouched:
44 But it will leave untouched:
47 - Unmodified tracked files
45 - Modified and unmodified tracked files
48 - Modified tracked files
46 - Ignored files (unless --all is specified)
49 - New files added to the repository (with "hg add")
47 - New files added to the repository (with "hg add")
50
48
51 If directories are given on the command line, only files in these
49 If directories are given on the command line, only files in these
52 directories are considered.
50 directories are considered.
53
51
54 Be careful with purge, you could irreversibly delete some files you
52 Be careful with purge, as you could irreversibly delete some files you
55 forgot to add to the repository. If you only want to print the list of
53 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.
54 files that this program would delete, use the --print option.
57 '''
55 '''
58 act = not opts['print']
56 act = not opts['print']
59 eol = '\n'
57 eol = '\n'
60 if opts['print0']:
58 if opts['print0']:
61 eol = '\0'
59 eol = '\0'
62 act = False # --print0 implies --print
60 act = False # --print0 implies --print
63
61
64 def remove(remove_func, name):
62 def remove(remove_func, name):
65 if act:
63 if act:
66 try:
64 try:
67 remove_func(repo.wjoin(name))
65 remove_func(repo.wjoin(name))
68 except OSError:
66 except OSError:
69 m = _('%s cannot be removed') % name
67 m = _('%s cannot be removed') % name
70 if opts['abort_on_err']:
68 if opts['abort_on_err']:
71 raise util.Abort(m)
69 raise util.Abort(m)
72 ui.warn(_('warning: %s\n') % m)
70 ui.warn(_('warning: %s\n') % m)
73 else:
71 else:
74 ui.write('%s%s' % (name, eol))
72 ui.write('%s%s' % (name, eol))
75
73
76 directories = []
74 directories = []
77 match = cmdutil.match(repo, dirs, opts)
75 match = cmdutil.match(repo, dirs, opts)
78 match.dir = directories.append
76 match.dir = directories.append
79 status = repo.status(match=match, ignored=opts['all'], unknown=True)
77 status = repo.status(match=match, ignored=opts['all'], unknown=True)
80
78
81 for f in util.sort(status[4] + status[5]):
79 for f in util.sort(status[4] + status[5]):
82 ui.note(_('Removing file %s\n') % f)
80 ui.note(_('Removing file %s\n') % f)
83 remove(os.remove, f)
81 remove(os.remove, f)
84
82
85 for f in util.sort(directories)[::-1]:
83 for f in util.sort(directories)[::-1]:
86 if match(f) and not os.listdir(repo.wjoin(f)):
84 if match(f) and not os.listdir(repo.wjoin(f)):
87 ui.note(_('Removing directory %s\n') % f)
85 ui.note(_('Removing directory %s\n') % f)
88 remove(os.rmdir, f)
86 remove(os.rmdir, f)
89
87
90 cmdtable = {
88 cmdtable = {
91 'purge|clean':
89 'purge|clean':
92 (purge,
90 (purge,
93 [('a', 'abort-on-err', None, _('abort if an error occurs')),
91 [('a', 'abort-on-err', None, _('abort if an error occurs')),
94 ('', 'all', None, _('purge ignored files too')),
92 ('', 'all', None, _('purge ignored files too')),
95 ('p', 'print', None, _('print the file names instead of deleting them')),
93 ('p', 'print', None, _('print the file names instead of deleting them')),
96 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
94 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
97 ' (implies -p)')),
95 ' (implies -p)')),
98 ] + commands.walkopts,
96 ] + commands.walkopts,
99 _('hg purge [OPTION]... [DIR]...'))
97 _('hg purge [OPTION]... [DIR]...'))
100 }
98 }
General Comments 0
You need to be logged in to leave comments. Login now