##// END OF EJS Templates
purge: access status fields by name rather than index
Martin von Zweigbergk -
r22920:e049338d default
parent child Browse files
Show More
@@ -1,115 +1,115 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://mercurial.selenic.com/)
3 # This is a small extension for Mercurial (http://mercurial.selenic.com/)
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
6 # This program was inspired by the "cvspurge" script contained in CVS
7 # utilities (http://www.red-bean.com/cvsutils/).
7 # utilities (http://www.red-bean.com/cvsutils/).
8 #
8 #
9 # For help on the usage of "hg purge" use:
9 # For help on the usage of "hg purge" use:
10 # hg help purge
10 # hg help purge
11 #
11 #
12 # This program is free software; you can redistribute it and/or modify
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
15 # (at your option) any later version.
16 #
16 #
17 # This program is distributed in the hope that it will be useful,
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
20 # GNU General Public License for more details.
21 #
21 #
22 # You should have received a copy of the GNU General Public License
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, see <http://www.gnu.org/licenses/>.
23 # along with this program; if not, see <http://www.gnu.org/licenses/>.
24
24
25 '''command to delete untracked files from the working directory'''
25 '''command to delete untracked files from the working directory'''
26
26
27 from mercurial import util, commands, cmdutil, scmutil
27 from mercurial import util, commands, cmdutil, scmutil
28 from mercurial.i18n import _
28 from mercurial.i18n import _
29 import os
29 import os
30
30
31 cmdtable = {}
31 cmdtable = {}
32 command = cmdutil.command(cmdtable)
32 command = cmdutil.command(cmdtable)
33 testedwith = 'internal'
33 testedwith = 'internal'
34
34
35 @command('purge|clean',
35 @command('purge|clean',
36 [('a', 'abort-on-err', None, _('abort if an error occurs')),
36 [('a', 'abort-on-err', None, _('abort if an error occurs')),
37 ('', 'all', None, _('purge ignored files too')),
37 ('', 'all', None, _('purge ignored files too')),
38 ('', 'dirs', None, _('purge empty directories')),
38 ('', 'dirs', None, _('purge empty directories')),
39 ('', 'files', None, _('purge files')),
39 ('', 'files', None, _('purge files')),
40 ('p', 'print', None, _('print filenames instead of deleting them')),
40 ('p', 'print', None, _('print filenames instead of deleting them')),
41 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
41 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
42 ' (implies -p/--print)')),
42 ' (implies -p/--print)')),
43 ] + commands.walkopts,
43 ] + commands.walkopts,
44 _('hg purge [OPTION]... [DIR]...'))
44 _('hg purge [OPTION]... [DIR]...'))
45 def purge(ui, repo, *dirs, **opts):
45 def purge(ui, repo, *dirs, **opts):
46 '''removes files not tracked by Mercurial
46 '''removes files not tracked by Mercurial
47
47
48 Delete files not known to Mercurial. This is useful to test local
48 Delete files not known to Mercurial. This is useful to test local
49 and uncommitted changes in an otherwise-clean source tree.
49 and uncommitted changes in an otherwise-clean source tree.
50
50
51 This means that purge will delete the following by default:
51 This means that purge will delete the following by default:
52
52
53 - Unknown files: files marked with "?" by :hg:`status`
53 - Unknown files: files marked with "?" by :hg:`status`
54 - Empty directories: in fact Mercurial ignores directories unless
54 - Empty directories: in fact Mercurial ignores directories unless
55 they contain files under source control management
55 they contain files under source control management
56
56
57 But it will leave untouched:
57 But it will leave untouched:
58
58
59 - Modified and unmodified tracked files
59 - Modified and unmodified tracked files
60 - Ignored files (unless --all is specified)
60 - Ignored files (unless --all is specified)
61 - New files added to the repository (with :hg:`add`)
61 - New files added to the repository (with :hg:`add`)
62
62
63 The --files and --dirs options can be used to direct purge to delete
63 The --files and --dirs options can be used to direct purge to delete
64 only files, only directories, or both. If neither option is given,
64 only files, only directories, or both. If neither option is given,
65 both will be deleted.
65 both will be deleted.
66
66
67 If directories are given on the command line, only files in these
67 If directories are given on the command line, only files in these
68 directories are considered.
68 directories are considered.
69
69
70 Be careful with purge, as you could irreversibly delete some files
70 Be careful with purge, as you could irreversibly delete some files
71 you forgot to add to the repository. If you only want to print the
71 you forgot to add to the repository. If you only want to print the
72 list of files that this program would delete, use the --print
72 list of files that this program would delete, use the --print
73 option.
73 option.
74 '''
74 '''
75 act = not opts['print']
75 act = not opts['print']
76 eol = '\n'
76 eol = '\n'
77 if opts['print0']:
77 if opts['print0']:
78 eol = '\0'
78 eol = '\0'
79 act = False # --print0 implies --print
79 act = False # --print0 implies --print
80 removefiles = opts['files']
80 removefiles = opts['files']
81 removedirs = opts['dirs']
81 removedirs = opts['dirs']
82 if not removefiles and not removedirs:
82 if not removefiles and not removedirs:
83 removefiles = True
83 removefiles = True
84 removedirs = True
84 removedirs = True
85
85
86 def remove(remove_func, name):
86 def remove(remove_func, name):
87 if act:
87 if act:
88 try:
88 try:
89 remove_func(repo.wjoin(name))
89 remove_func(repo.wjoin(name))
90 except OSError:
90 except OSError:
91 m = _('%s cannot be removed') % name
91 m = _('%s cannot be removed') % name
92 if opts['abort_on_err']:
92 if opts['abort_on_err']:
93 raise util.Abort(m)
93 raise util.Abort(m)
94 ui.warn(_('warning: %s\n') % m)
94 ui.warn(_('warning: %s\n') % m)
95 else:
95 else:
96 ui.write('%s%s' % (name, eol))
96 ui.write('%s%s' % (name, eol))
97
97
98 match = scmutil.match(repo[None], dirs, opts)
98 match = scmutil.match(repo[None], dirs, opts)
99 if removedirs:
99 if removedirs:
100 directories = []
100 directories = []
101 match.explicitdir = match.traversedir = directories.append
101 match.explicitdir = match.traversedir = directories.append
102 status = repo.status(match=match, ignored=opts['all'], unknown=True)
102 status = repo.status(match=match, ignored=opts['all'], unknown=True)
103
103
104 if removefiles:
104 if removefiles:
105 for f in sorted(status[4] + status[5]):
105 for f in sorted(status.unknown + status.ignored):
106 if act:
106 if act:
107 ui.note(_('removing file %s\n') % f)
107 ui.note(_('removing file %s\n') % f)
108 remove(util.unlink, f)
108 remove(util.unlink, f)
109
109
110 if removedirs:
110 if removedirs:
111 for f in sorted(directories, reverse=True):
111 for f in sorted(directories, reverse=True):
112 if match(f) and not os.listdir(repo.wjoin(f)):
112 if match(f) and not os.listdir(repo.wjoin(f)):
113 if act:
113 if act:
114 ui.note(_('removing directory %s\n') % f)
114 ui.note(_('removing directory %s\n') % f)
115 remove(os.rmdir, f)
115 remove(os.rmdir, f)
General Comments 0
You need to be logged in to leave comments. Login now