##// END OF EJS Templates
purge: use opts.get()...
Gregory Szorc -
r29222:ed4bd789 default
parent child Browse files
Show More
@@ -1,127 +1,127 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 (https://mercurial-scm.org/)
3 # This is a small extension for Mercurial (https://mercurial-scm.org/)
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 from __future__ import absolute_import
26 from __future__ import absolute_import
27
27
28 import os
28 import os
29
29
30 from mercurial.i18n import _
30 from mercurial.i18n import _
31 from mercurial import (
31 from mercurial import (
32 cmdutil,
32 cmdutil,
33 commands,
33 commands,
34 error,
34 error,
35 scmutil,
35 scmutil,
36 util,
36 util,
37 )
37 )
38
38
39 cmdtable = {}
39 cmdtable = {}
40 command = cmdutil.command(cmdtable)
40 command = cmdutil.command(cmdtable)
41 # Note for extension authors: ONLY specify testedwith = 'internal' for
41 # Note for extension authors: ONLY specify testedwith = 'internal' for
42 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
42 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
43 # be specifying the version(s) of Mercurial they are tested with, or
43 # be specifying the version(s) of Mercurial they are tested with, or
44 # leave the attribute unspecified.
44 # leave the attribute unspecified.
45 testedwith = 'internal'
45 testedwith = 'internal'
46
46
47 @command('purge|clean',
47 @command('purge|clean',
48 [('a', 'abort-on-err', None, _('abort if an error occurs')),
48 [('a', 'abort-on-err', None, _('abort if an error occurs')),
49 ('', 'all', None, _('purge ignored files too')),
49 ('', 'all', None, _('purge ignored files too')),
50 ('', 'dirs', None, _('purge empty directories')),
50 ('', 'dirs', None, _('purge empty directories')),
51 ('', 'files', None, _('purge files')),
51 ('', 'files', None, _('purge files')),
52 ('p', 'print', None, _('print filenames instead of deleting them')),
52 ('p', 'print', None, _('print filenames instead of deleting them')),
53 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
53 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
54 ' (implies -p/--print)')),
54 ' (implies -p/--print)')),
55 ] + commands.walkopts,
55 ] + commands.walkopts,
56 _('hg purge [OPTION]... [DIR]...'))
56 _('hg purge [OPTION]... [DIR]...'))
57 def purge(ui, repo, *dirs, **opts):
57 def purge(ui, repo, *dirs, **opts):
58 '''removes files not tracked by Mercurial
58 '''removes files not tracked by Mercurial
59
59
60 Delete files not known to Mercurial. This is useful to test local
60 Delete files not known to Mercurial. This is useful to test local
61 and uncommitted changes in an otherwise-clean source tree.
61 and uncommitted changes in an otherwise-clean source tree.
62
62
63 This means that purge will delete the following by default:
63 This means that purge will delete the following by default:
64
64
65 - Unknown files: files marked with "?" by :hg:`status`
65 - Unknown files: files marked with "?" by :hg:`status`
66 - Empty directories: in fact Mercurial ignores directories unless
66 - Empty directories: in fact Mercurial ignores directories unless
67 they contain files under source control management
67 they contain files under source control management
68
68
69 But it will leave untouched:
69 But it will leave untouched:
70
70
71 - Modified and unmodified tracked files
71 - Modified and unmodified tracked files
72 - Ignored files (unless --all is specified)
72 - Ignored files (unless --all is specified)
73 - New files added to the repository (with :hg:`add`)
73 - New files added to the repository (with :hg:`add`)
74
74
75 The --files and --dirs options can be used to direct purge to delete
75 The --files and --dirs options can be used to direct purge to delete
76 only files, only directories, or both. If neither option is given,
76 only files, only directories, or both. If neither option is given,
77 both will be deleted.
77 both will be deleted.
78
78
79 If directories are given on the command line, only files in these
79 If directories are given on the command line, only files in these
80 directories are considered.
80 directories are considered.
81
81
82 Be careful with purge, as you could irreversibly delete some files
82 Be careful with purge, as you could irreversibly delete some files
83 you forgot to add to the repository. If you only want to print the
83 you forgot to add to the repository. If you only want to print the
84 list of files that this program would delete, use the --print
84 list of files that this program would delete, use the --print
85 option.
85 option.
86 '''
86 '''
87 act = not opts['print']
87 act = not opts.get('print')
88 eol = '\n'
88 eol = '\n'
89 if opts['print0']:
89 if opts.get('print0'):
90 eol = '\0'
90 eol = '\0'
91 act = False # --print0 implies --print
91 act = False # --print0 implies --print
92 removefiles = opts['files']
92 removefiles = opts.get('files')
93 removedirs = opts['dirs']
93 removedirs = opts.get('dirs')
94 if not removefiles and not removedirs:
94 if not removefiles and not removedirs:
95 removefiles = True
95 removefiles = True
96 removedirs = True
96 removedirs = True
97
97
98 def remove(remove_func, name):
98 def remove(remove_func, name):
99 if act:
99 if act:
100 try:
100 try:
101 remove_func(repo.wjoin(name))
101 remove_func(repo.wjoin(name))
102 except OSError:
102 except OSError:
103 m = _('%s cannot be removed') % name
103 m = _('%s cannot be removed') % name
104 if opts['abort_on_err']:
104 if opts.get('abort_on_err'):
105 raise error.Abort(m)
105 raise error.Abort(m)
106 ui.warn(_('warning: %s\n') % m)
106 ui.warn(_('warning: %s\n') % m)
107 else:
107 else:
108 ui.write('%s%s' % (name, eol))
108 ui.write('%s%s' % (name, eol))
109
109
110 match = scmutil.match(repo[None], dirs, opts)
110 match = scmutil.match(repo[None], dirs, opts)
111 if removedirs:
111 if removedirs:
112 directories = []
112 directories = []
113 match.explicitdir = match.traversedir = directories.append
113 match.explicitdir = match.traversedir = directories.append
114 status = repo.status(match=match, ignored=opts['all'], unknown=True)
114 status = repo.status(match=match, ignored=opts.get('all'), unknown=True)
115
115
116 if removefiles:
116 if removefiles:
117 for f in sorted(status.unknown + status.ignored):
117 for f in sorted(status.unknown + status.ignored):
118 if act:
118 if act:
119 ui.note(_('removing file %s\n') % f)
119 ui.note(_('removing file %s\n') % f)
120 remove(util.unlink, f)
120 remove(util.unlink, f)
121
121
122 if removedirs:
122 if removedirs:
123 for f in sorted(directories, reverse=True):
123 for f in sorted(directories, reverse=True):
124 if match(f) and not os.listdir(repo.wjoin(f)):
124 if match(f) and not os.listdir(repo.wjoin(f)):
125 if act:
125 if act:
126 ui.note(_('removing directory %s\n') % f)
126 ui.note(_('removing directory %s\n') % f)
127 remove(os.rmdir, f)
127 remove(os.rmdir, f)
General Comments 0
You need to be logged in to leave comments. Login now