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