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