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