author | Matt Mackall <mpm@selenic.com> |
Sat, 18 Nov 2006 11:34:19 -0600 | |
changeset 3674 | 9103dab96093 |
parent 2401 | dbeaa4369121 |
child 3943 | 05c703c281ff |
permissions | -rw-r--r-- |
2364 | 1 |
#!/usr/bin/env python |
2 |
# |
|
3 |
# Copyright (C) 2006 - Marco Barisione <marco@barisione.org> |
|
4 |
# |
|
5 |
# This is a small extension for Mercurial (http://www.selenic.com/mercurial) |
|
6 |
# that removes files not known to mercurial |
|
7 |
# |
|
8 |
# This program is free software; you can redistribute it and/or modify |
|
9 |
# it under the terms of the GNU General Public License as published by |
|
10 |
# the Free Software Foundation; either version 2 of the License, or |
|
11 |
# (at your option) any later version. |
|
12 |
# |
|
13 |
# This program is distributed in the hope that it will be useful, |
|
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
# GNU General Public License for more details. |
|
17 |
# |
|
18 |
# You should have received a copy of the GNU General Public License |
|
19 |
# along with this program; if not, write to the Free Software |
|
20 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
||
22 |
from mercurial import hg, util |
|
23 |
import os |
|
24 |
||
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
25 |
def _(s): |
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
26 |
return s |
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
27 |
|
2364 | 28 |
class Purge(object): |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
29 |
def __init__(self, act=True, abort_on_err=False, eol='\n'): |
2364 | 30 |
self._repo = None |
31 |
self._ui = None |
|
32 |
self._hg_root = None |
|
33 |
self._act = act |
|
34 |
self._abort_on_err = abort_on_err |
|
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
35 |
self._eol = eol |
2364 | 36 |
|
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
37 |
def purge(self, ui, repo, dirs=None): |
2364 | 38 |
self._repo = repo |
39 |
self._ui = ui |
|
40 |
self._hg_root = self._split_path(repo.root) |
|
41 |
||
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
42 |
if not dirs: |
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
43 |
dirs = [repo.root] |
2364 | 44 |
|
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
45 |
for path in dirs: |
2364 | 46 |
path = os.path.abspath(path) |
47 |
for root, dirs, files in os.walk(path, topdown=False): |
|
48 |
if '.hg' in self._split_path(root): |
|
49 |
# Skip files in the .hg directory. |
|
50 |
# Note that if the repository is in a directory |
|
51 |
# called .hg this command does not work. |
|
52 |
continue |
|
53 |
for name in files: |
|
54 |
self._remove_file(os.path.join(root, name)) |
|
55 |
if not os.listdir(root): |
|
56 |
# Remove this directory if it is empty. |
|
57 |
self._remove_dir(root) |
|
58 |
||
59 |
self._repo = None |
|
60 |
self._ui = None |
|
61 |
self._hg_root = None |
|
62 |
||
63 |
def _error(self, msg): |
|
64 |
if self._abort_on_err: |
|
65 |
raise util.Abort(msg) |
|
66 |
else: |
|
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
67 |
self._ui.warn(_('warning: %s\n') % msg) |
2364 | 68 |
|
69 |
def _remove_file(self, name): |
|
70 |
relative_name = self._relative_name(name) |
|
71 |
# dirstate.state() requires a path relative to the root |
|
72 |
# directory. |
|
73 |
if self._repo.dirstate.state(relative_name) != '?': |
|
74 |
return |
|
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
75 |
self._ui.note(_('Removing file %s\n') % name) |
2364 | 76 |
if self._act: |
77 |
try: |
|
78 |
os.remove(name) |
|
79 |
except OSError, e: |
|
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
80 |
self._error(_('%s cannot be removed') % name) |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
81 |
else: |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
82 |
self._ui.write('%s%s' % (name, self._eol)) |
2364 | 83 |
|
84 |
def _remove_dir(self, name): |
|
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
85 |
self._ui.note(_('Removing directory %s\n') % name) |
2364 | 86 |
if self._act: |
87 |
try: |
|
88 |
os.rmdir(name) |
|
89 |
except OSError, e: |
|
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
90 |
self._error(_('%s cannot be removed') % name) |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
91 |
else: |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
92 |
self._ui.write('%s%s' % (name, self._eol)) |
2364 | 93 |
|
2375 | 94 |
def _relative_name(self, path): |
95 |
''' |
|
96 |
Returns "path" but relative to the root directory of the |
|
97 |
repository and with '\\' replaced with '/'. |
|
98 |
This is needed because this is the format required by |
|
99 |
self._repo.dirstate.state(). |
|
100 |
''' |
|
101 |
splitted_path = self._split_path(path)[len(self._hg_root):] |
|
2374
ffc2ed61061b
Even on Windows self._repo.dirstate.state() wants '/' in paths
demian@gaudron.lan
parents:
2373
diff
changeset
|
102 |
# Even on Windows self._repo.dirstate.state() wants '/'. |
ffc2ed61061b
Even on Windows self._repo.dirstate.state() wants '/' in paths
demian@gaudron.lan
parents:
2373
diff
changeset
|
103 |
return self._join_path(splitted_path).replace('\\', '/') |
2364 | 104 |
|
105 |
def _split_path(self, path): |
|
2375 | 106 |
''' |
2381
ab7a438294fc
Rewritten install instructions for hg-purge to match new situation, fixed typos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2379
diff
changeset
|
107 |
Returns a list of the single files/directories in "path". |
2375 | 108 |
For instance: |
109 |
'/home/user/test' -> ['/', 'home', 'user', 'test'] |
|
110 |
'C:\\Mercurial' -> ['C:\\', 'Mercurial'] |
|
111 |
''' |
|
2364 | 112 |
ret = [] |
113 |
while True: |
|
114 |
head, tail = os.path.split(path) |
|
115 |
if tail: |
|
116 |
ret.append(tail) |
|
117 |
if head == path: |
|
118 |
ret.append(head) |
|
119 |
break |
|
120 |
path = head |
|
121 |
ret.reverse() |
|
122 |
return ret |
|
123 |
||
124 |
def _join_path(self, splitted_path): |
|
2375 | 125 |
''' |
126 |
Joins a list returned by _split_path(). |
|
127 |
''' |
|
2364 | 128 |
ret = '' |
129 |
for part in splitted_path: |
|
130 |
if ret: |
|
131 |
ret = os.path.join(ret, part) |
|
132 |
else: |
|
133 |
ret = part |
|
134 |
return ret |
|
135 |
||
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
136 |
|
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
137 |
def purge(ui, repo, *dirs, **opts): |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
138 |
'''removes files not tracked by mercurial |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
139 |
|
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
140 |
Delete files not known to mercurial, this is useful to test local and |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
141 |
uncommitted changes in the otherwise clean source tree. |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
142 |
|
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
143 |
This means that purge will delete: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
144 |
- Unknown files: files marked with "?" by "hg status" |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
145 |
- Ignored files: files usually ignored by Mercurial because they match |
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
146 |
a pattern in a ".hgignore" file |
2381
ab7a438294fc
Rewritten install instructions for hg-purge to match new situation, fixed typos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2379
diff
changeset
|
147 |
- Empty directories: in fact Mercurial ignores directories unless they |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
148 |
contain files under source control managment |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
149 |
But it will leave untouched: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
150 |
- Unmodified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
151 |
- Modified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
152 |
- New files added to the repository (with "hg add") |
2364 | 153 |
|
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
154 |
If directories are given on the command line, only files in these |
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
155 |
directories are considered. |
2364 | 156 |
|
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
157 |
Be careful with purge, you could irreversibly delete some files you |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
158 |
forgot to add to the repository. If you only want to print the list of |
2381
ab7a438294fc
Rewritten install instructions for hg-purge to match new situation, fixed typos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2379
diff
changeset
|
159 |
files that this program would delete use the --print option. |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
160 |
''' |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
161 |
act = not opts['print'] |
2370
de893ad6bd17
Command line options are read in a saner way
demian@gaudron.lan
parents:
2369
diff
changeset
|
162 |
abort_on_err = bool(opts['abort_on_err']) |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
163 |
eol = opts['print0'] and '\0' or '\n' |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
164 |
if eol == '\0': |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
165 |
# --print0 implies --print |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
166 |
act = False |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
167 |
p = Purge(act, abort_on_err, eol) |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
168 |
p.purge(ui, repo, dirs) |
2364 | 169 |
|
170 |
||
171 |
cmdtable = { |
|
2382
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
172 |
'purge': |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
173 |
(purge, |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
174 |
[('a', 'abort-on-err', None, _('abort if an error occurs')), |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
175 |
('p', 'print', None, _('print the file names instead of deleting them')), |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
176 |
('0', 'print0', None, _('end filenames with NUL, for use with xargs' |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
177 |
' (implies -p)'))], |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
178 |
_('hg purge [OPTION]... [DIR]...')) |
2364 | 179 |
} |