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