comparison hgext/purge.py @ 4311:1043e4b27ab9

Move back the purge extension in hgext
author Emanuele Aina <em@nerd.ocracy.org>
date Tue, 27 Feb 2007 09:05:36 +0100
parents contrib/purge/purge.py@c8919eb0f315
children a73cf208b2a0
comparison
equal deleted inserted replaced
4310:c8919eb0f315 4311:1043e4b27ab9
1 # Copyright (C) 2006 - Marco Barisione <marco@barisione.org>
2 #
3 # This is a small extension for Mercurial (http://www.selenic.com/mercurial)
4 # that removes files not known to mercurial
5 #
6 # This program was inspired by the "cvspurge" script contained in CVS utilities
7 # (http://www.red-bean.com/cvsutils/).
8 #
9 # To enable the "purge" extension put these lines in your ~/.hgrc:
10 # [extensions]
11 # hgext.purge =
12 #
13 # For help on the usage of "hg purge" use:
14 # hg help purge
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29
30 from mercurial import hg, util
31 from mercurial.i18n import _
32 import os
33
34 def dopurge(ui, repo, dirs=None, act=True, abort_on_err=False, eol='\n',
35 force=False):
36 def error(msg):
37 if abort_on_err:
38 raise util.Abort(msg)
39 else:
40 ui.warn(_('warning: %s\n') % msg)
41
42 def remove(remove_func, name):
43 if act:
44 try:
45 remove_func(os.path.join(repo.root, name))
46 except OSError, e:
47 error(_('%s cannot be removed') % name)
48 else:
49 ui.write('%s%s' % (name, eol))
50
51 directories = []
52 files = []
53 missing = []
54 roots, match, anypats = util.cmdmatcher(repo.root, repo.getcwd(), dirs)
55 for src, f, st in repo.dirstate.statwalk(files=roots, match=match,
56 ignored=True, directories=True):
57 if src == 'd':
58 directories.append(f)
59 elif src == 'm':
60 missing.append(f)
61 elif src == 'f' and f not in repo.dirstate:
62 files.append(f)
63
64 _check_missing(ui, repo, missing, force)
65
66 directories.sort()
67
68 for f in files:
69 if f not in repo.dirstate:
70 ui.note(_('Removing file %s\n') % f)
71 remove(os.remove, f)
72
73 for f in directories[::-1]:
74 if not os.listdir(repo.wjoin(f)):
75 ui.note(_('Removing directory %s\n') % f)
76 remove(os.rmdir, f)
77
78 def _check_missing(ui, repo, missing, force=False):
79 """Abort if there is the chance of having problems with name-mangling fs
80
81 In a name mangling filesystem (e.g. a case insensitive one)
82 dirstate.walk() can yield filenames different from the ones
83 stored in the dirstate. This already confuses the status and
84 add commands, but with purge this may cause data loss.
85
86 To prevent this, _check_missing will abort if there are missing
87 files. The force option will let the user skip the check if he
88 knows it is safe.
89
90 Even with the force option this function will check if any of the
91 missing files is still available in the working dir: if so there
92 may be some problem with the underlying filesystem, so it
93 aborts unconditionally."""
94
95 found = [f for f in missing if util.lexists(repo.wjoin(f))]
96
97 if found:
98 if not ui.quiet:
99 ui.warn(_("The following tracked files weren't listed by the "
100 "filesystem, but could still be found:\n"))
101 for f in found:
102 ui.warn("%s\n" % f)
103 if util.checkfolding(repo.path):
104 ui.warn(_("This is probably due to a case-insensitive "
105 "filesystem\n"))
106 raise util.Abort(_("purging on name mangling filesystems is not "
107 "yet fully supported"))
108
109 if missing and not force:
110 raise util.Abort(_("there are missing files in the working dir and "
111 "purge still has problems with them due to name "
112 "mangling filesystems. "
113 "Use --force if you know what you are doing"))
114
115
116 def purge(ui, repo, *dirs, **opts):
117 '''removes files not tracked by mercurial
118
119 Delete files not known to mercurial, this is useful to test local and
120 uncommitted changes in the otherwise clean source tree.
121
122 This means that purge will delete:
123 - Unknown files: files marked with "?" by "hg status"
124 - Ignored files: files usually ignored by Mercurial because they match
125 a pattern in a ".hgignore" file
126 - Empty directories: in fact Mercurial ignores directories unless they
127 contain files under source control managment
128 But it will leave untouched:
129 - Unmodified tracked files
130 - Modified tracked files
131 - New files added to the repository (with "hg add")
132
133 If directories are given on the command line, only files in these
134 directories are considered.
135
136 Be careful with purge, you could irreversibly delete some files you
137 forgot to add to the repository. If you only want to print the list of
138 files that this program would delete use the --print option.
139 '''
140 act = not opts['print']
141 abort_on_err = bool(opts['abort_on_err'])
142 eol = opts['print0'] and '\0' or '\n'
143 if eol == '\0':
144 # --print0 implies --print
145 act = False
146 force = bool(opts['force'])
147 dopurge(ui, repo, dirs, act, abort_on_err, eol, force)
148
149
150 cmdtable = {
151 'purge':
152 (purge,
153 [('a', 'abort-on-err', None, _('abort if an error occurs')),
154 ('f', 'force', None, _('purge even when missing files are detected')),
155 ('p', 'print', None, _('print the file names instead of deleting them')),
156 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
157 ' (implies -p)'))],
158 _('hg purge [OPTION]... [DIR]...'))
159 }