1 # Copyright (C) 2006 - Marco Barisione <marco@barisione.org>
3 # This is a small extension for Mercurial (http://mercurial.selenic.com/)
4 # that removes files not known to mercurial
6 # This program was inspired by the "cvspurge" script contained in CVS
7 # utilities (http://www.red-bean.com/cvsutils/).
9 # For help on the usage of "hg purge" use:
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
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 '''command to delete untracked files from the working directory'''
28 from mercurial import util, commands, cmdutil
29 from mercurial.i18n import _
32 def purge(ui, repo, *dirs, **opts):
33 '''removes files not tracked by Mercurial
35 Delete files not known to Mercurial. This is useful to test local
36 and uncommitted changes in an otherwise-clean source tree.
38 This means that purge will delete:
40 - Unknown files: files marked with "?" by "hg status"
41 - Empty directories: in fact Mercurial ignores directories unless
42 they contain files under source control management
44 But it will leave untouched:
46 - Modified and unmodified tracked files
47 - Ignored files (unless --all is specified)
48 - New files added to the repository (with "hg add")
50 If directories are given on the command line, only files in these
51 directories are considered.
53 Be careful with purge, as you could irreversibly delete some files
54 you forgot to add to the repository. If you only want to print the
55 list of files that this program would delete, use the --print
58 act = not opts['print']
62 act = False # --print0 implies --print
64 def remove(remove_func, name):
67 remove_func(repo.wjoin(name))
69 m = _('%s cannot be removed') % name
70 if opts['abort_on_err']:
72 ui.warn(_('warning: %s\n') % m)
74 ui.write('%s%s' % (name, eol))
80 # read-only files cannot be unlinked under Windows
82 if (s.st_mode & stat.S_IWRITE) != 0:
84 os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE)
88 match = cmdutil.match(repo, dirs, opts)
89 match.dir = directories.append
90 status = repo.status(match=match, ignored=opts['all'], unknown=True)
92 for f in sorted(status[4] + status[5]):
93 ui.note(_('Removing file %s\n') % f)
96 for f in sorted(directories, reverse=True):
97 if match(f) and not os.listdir(repo.wjoin(f)):
98 ui.note(_('Removing directory %s\n') % f)
104 [('a', 'abort-on-err', None, _('abort if an error occurs')),
105 ('', 'all', None, _('purge ignored files too')),
106 ('p', 'print', None, _('print filenames instead of deleting them')),
107 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
108 ' (implies -p/--print)')),
109 ] + commands.walkopts,
110 _('hg purge [OPTION]... [DIR]...'))