Mercurial > hg > mercurial-crew-with-dirclash
annotate contrib/purge/purge.py @ 4418:0532491f7476
MQ: tidy up if a qpush is interrupted.
This aborts the transaction, makes sure the dirtied dirstate isn't
written out, and cleans up the working directory.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Tue, 08 May 2007 12:16:02 -0700 |
parents | 05c703c281ff |
children | f9dc36b1bdd5 |
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 | |
21 import os | |
22 | |
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
23 def _(s): |
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
24 return s |
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
25 |
2364 | 26 class Purge(object): |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
27 def __init__(self, act=True, abort_on_err=False, eol='\n'): |
2364 | 28 self._repo = None |
29 self._ui = None | |
30 self._hg_root = None | |
31 self._act = act | |
32 self._abort_on_err = abort_on_err | |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
33 self._eol = eol |
2364 | 34 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
35 def purge(self, ui, repo, dirs=None): |
2364 | 36 self._repo = repo |
37 self._ui = ui | |
38 self._hg_root = self._split_path(repo.root) | |
39 | |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
40 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
|
41 dirs = [repo.root] |
2364 | 42 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
43 for path in dirs: |
2364 | 44 path = os.path.abspath(path) |
45 for root, dirs, files in os.walk(path, topdown=False): | |
46 if '.hg' in self._split_path(root): | |
47 # Skip files in the .hg directory. | |
48 # Note that if the repository is in a directory | |
49 # called .hg this command does not work. | |
50 continue | |
51 for name in files: | |
52 self._remove_file(os.path.join(root, name)) | |
53 if not os.listdir(root): | |
54 # Remove this directory if it is empty. | |
55 self._remove_dir(root) | |
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 | |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
73 self._ui.note(_('Removing file %s\n') % name) |
2364 | 74 if self._act: |
75 try: | |
76 os.remove(name) | |
77 except OSError, e: | |
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
78 self._error(_('%s cannot be removed') % name) |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
79 else: |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
80 self._ui.write('%s%s' % (name, self._eol)) |
2364 | 81 |
82 def _remove_dir(self, name): | |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
83 self._ui.note(_('Removing directory %s\n') % name) |
2364 | 84 if self._act: |
85 try: | |
86 os.rmdir(name) | |
87 except OSError, e: | |
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
88 self._error(_('%s cannot be removed') % name) |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
89 else: |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
90 self._ui.write('%s%s' % (name, self._eol)) |
2364 | 91 |
2375 | 92 def _relative_name(self, path): |
93 ''' | |
94 Returns "path" but relative to the root directory of the | |
95 repository and with '\\' replaced with '/'. | |
96 This is needed because this is the format required by | |
97 self._repo.dirstate.state(). | |
98 ''' | |
99 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
|
100 # 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
|
101 return self._join_path(splitted_path).replace('\\', '/') |
2364 | 102 |
103 def _split_path(self, path): | |
2375 | 104 ''' |
2381
ab7a438294fc
Rewritten install instructions for hg-purge to match new situation, fixed typos.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2379
diff
changeset
|
105 Returns a list of the single files/directories in "path". |
2375 | 106 For instance: |
107 '/home/user/test' -> ['/', 'home', 'user', 'test'] | |
108 'C:\\Mercurial' -> ['C:\\', 'Mercurial'] | |
109 ''' | |
2364 | 110 ret = [] |
111 while True: | |
112 head, tail = os.path.split(path) | |
113 if tail: | |
114 ret.append(tail) | |
115 if head == path: | |
116 ret.append(head) | |
117 break | |
118 path = head | |
119 ret.reverse() | |
120 return ret | |
121 | |
122 def _join_path(self, splitted_path): | |
2375 | 123 ''' |
124 Joins a list returned by _split_path(). | |
125 ''' | |
2364 | 126 ret = '' |
127 for part in splitted_path: | |
128 if ret: | |
129 ret = os.path.join(ret, part) | |
130 else: | |
131 ret = part | |
132 return ret | |
133 | |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
134 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
135 def purge(ui, repo, *dirs, **opts): |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
136 '''removes files not tracked by mercurial |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
137 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
138 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
|
139 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
|
140 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
141 This means that purge will delete: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
142 - 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
|
143 - 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
|
144 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
|
145 - 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
|
146 contain files under source control managment |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
147 But it will leave untouched: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
148 - Unmodified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
149 - Modified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
150 - New files added to the repository (with "hg add") |
2364 | 151 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
152 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
|
153 directories are considered. |
2364 | 154 |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
155 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
|
156 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
|
157 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
|
158 ''' |
2378
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
159 act = not opts['print'] |
2370
de893ad6bd17
Command line options are read in a saner way
demian@gaudron.lan
parents:
2369
diff
changeset
|
160 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
|
161 eol = opts['print0'] and '\0' or '\n' |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
162 if eol == '\0': |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
163 # --print0 implies --print |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
164 act = False |
6e5d40ec862d
Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents:
2377
diff
changeset
|
165 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
|
166 p.purge(ui, repo, dirs) |
2364 | 167 |
168 | |
169 cmdtable = { | |
2382
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
170 'purge': |
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 [('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
|
173 ('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
|
174 ('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
|
175 ' (implies -p)'))], |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
176 _('hg purge [OPTION]... [DIR]...')) |
2364 | 177 } |