Mercurial > hg > mercurial-crew-with-dirclash
annotate contrib/purge/purge.py @ 4147:691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
author | Emanuele Aina <faina.mail@tiscali.it> |
---|---|
date | Tue, 06 Mar 2007 17:45:40 -0300 |
parents | d250076824e3 |
children | de85ff0aaac5 |
rev | line source |
---|---|
2364 | 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 is free software; you can redistribute it and/or modify | |
7 # it under the terms of the GNU General Public License as published by | |
8 # the Free Software Foundation; either version 2 of the License, or | |
9 # (at your option) any later version. | |
10 # | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU General Public License for more details. | |
15 # | |
16 # You should have received a copy of the GNU General Public License | |
17 # along with this program; if not, write to the Free Software | |
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
19 | |
20 from mercurial import hg, util | |
4121
d250076824e3
Use the mercurial i18n infrastructure in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
4120
diff
changeset
|
21 from mercurial.i18n import _ |
2364 | 22 import os |
23 | |
24 class Purge(object): | |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
25 def __init__(self, act=True, abort_on_err=False, eol='\n'): |
2364 | 26 self._repo = None |
27 self._ui = None | |
28 self._hg_root = None | |
29 self._act = act | |
30 self._abort_on_err = abort_on_err | |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
31 self._eol = eol |
2364 | 32 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
33 def purge(self, ui, repo, dirs=None): |
2364 | 34 self._repo = repo |
35 self._ui = ui | |
36 self._hg_root = self._split_path(repo.root) | |
4147
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
37 |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
38 directories = [] |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
39 files = [] |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
40 for src, f, st in repo.dirstate.statwalk(files=dirs, ignored=True, |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
41 directories=True): |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
42 if src == 'd': |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
43 directories.append(f) |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
44 elif src == 'f' and f not in repo.dirstate: |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
45 files.append(f) |
2364 | 46 |
4147
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
47 directories.sort() |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
48 |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
49 for f in files: |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
50 self._remove_file(os.path.join(repo.root, f)) |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
51 |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
52 for f in directories[::-1]: |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
53 f = os.path.join(repo.root, f) |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
54 if not os.listdir(f): |
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
55 self._remove_dir(f) |
2364 | 56 |
57 self._repo = None | |
58 self._ui = None | |
59 self._hg_root = None | |
60 | |
61 def _error(self, msg): | |
62 if self._abort_on_err: | |
63 raise util.Abort(msg) | |
64 else: | |
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
65 self._ui.warn(_('warning: %s\n') % msg) |
2364 | 66 |
67 def _remove_file(self, name): | |
68 relative_name = self._relative_name(name) | |
69 # dirstate.state() requires a path relative to the root | |
70 # directory. | |
71 if self._repo.dirstate.state(relative_name) != '?': | |
72 return | |
4120
f9dc36b1bdd5
Print relative names in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
3937
diff
changeset
|
73 self._ui.note(_('Removing file %s\n') % relative_name) |
2364 | 74 if self._act: |
75 try: | |
76 os.remove(name) | |
77 except OSError, e: | |
4120
f9dc36b1bdd5
Print relative names in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
3937
diff
changeset
|
78 self._error(_('%s cannot be removed') % relative_name) |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
79 else: |
4120
f9dc36b1bdd5
Print relative names in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
3937
diff
changeset
|
80 self._ui.write('%s%s' % (relative_name, self._eol)) |
2364 | 81 |
82 def _remove_dir(self, name): | |
4120
f9dc36b1bdd5
Print relative names in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
3937
diff
changeset
|
83 relative_name = self._relative_name(name) |
f9dc36b1bdd5
Print relative names in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
3937
diff
changeset
|
84 self._ui.note(_('Removing directory %s\n') % relative_name) |
2364 | 85 if self._act: |
86 try: | |
87 os.rmdir(name) | |
88 except OSError, e: | |
4120
f9dc36b1bdd5
Print relative names in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
3937
diff
changeset
|
89 self._error(_('%s cannot be removed') % relative_name) |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
90 else: |
4120
f9dc36b1bdd5
Print relative names in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
3937
diff
changeset
|
91 self._ui.write('%s%s' % (relative_name, self._eol)) |
2364 | 92 |
2375 | 93 def _relative_name(self, path): |
94 ''' | |
95 Returns "path" but relative to the root directory of the | |
96 repository and with '\\' replaced with '/'. | |
97 This is needed because this is the format required by | |
98 self._repo.dirstate.state(). | |
99 ''' | |
100 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
|
101 # 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
|
102 return self._join_path(splitted_path).replace('\\', '/') |
2364 | 103 |
104 def _split_path(self, path): | |
2375 | 105 ''' |
2381
ab7a438294fc
Rewritten install instructions for hg-purge to match new situation, fixed typos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2379
diff
changeset
|
106 Returns a list of the single files/directories in "path". |
2375 | 107 For instance: |
108 '/home/user/test' -> ['/', 'home', 'user', 'test'] | |
109 'C:\\Mercurial' -> ['C:\\', 'Mercurial'] | |
110 ''' | |
2364 | 111 ret = [] |
112 while True: | |
113 head, tail = os.path.split(path) | |
114 if tail: | |
115 ret.append(tail) | |
116 if head == path: | |
117 ret.append(head) | |
118 break | |
119 path = head | |
120 ret.reverse() | |
121 return ret | |
122 | |
123 def _join_path(self, splitted_path): | |
2375 | 124 ''' |
125 Joins a list returned by _split_path(). | |
126 ''' | |
2364 | 127 ret = '' |
128 for part in splitted_path: | |
129 if ret: | |
130 ret = os.path.join(ret, part) | |
131 else: | |
132 ret = part | |
133 return ret | |
134 | |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
135 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
136 def purge(ui, repo, *dirs, **opts): |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
137 '''removes files not tracked by mercurial |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
138 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
139 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
|
140 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
|
141 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
142 This means that purge will delete: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
143 - 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
|
144 - 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
|
145 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
|
146 - 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
|
147 contain files under source control managment |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
148 But it will leave untouched: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
149 - Unmodified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
150 - Modified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
151 - New files added to the repository (with "hg add") |
2364 | 152 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
153 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
|
154 directories are considered. |
2364 | 155 |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 ''' |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
160 act = not opts['print'] |
2370
de893ad6bd17
Command line options are read in a saner way
demian@gaudron.lan
parents:
2369
diff
changeset
|
161 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
|
162 eol = opts['print0'] and '\0' or '\n' |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
163 if eol == '\0': |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
164 # --print0 implies --print |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
165 act = False |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
166 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
|
167 p.purge(ui, repo, dirs) |
2364 | 168 |
169 | |
170 cmdtable = { | |
2382
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
171 'purge': |
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 [('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
|
174 ('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
|
175 ('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
|
176 ' (implies -p)'))], |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
177 _('hg purge [OPTION]... [DIR]...')) |
2364 | 178 } |