Mercurial > public > mercurial-scm > hg
annotate purge.py @ 2377:626779aba9bb
The meaning of the directories on the command line is now explained correctly
author | Marco Barisione <marco@barisione.org> |
---|---|
date | Tue, 16 May 2006 22:36:31 +0200 |
parents | 52cfb9864257 |
children | 6e5d40ec862d |
rev | line source |
---|---|
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): |
29 def __init__(self, act=True, abort_on_err=False): | |
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 | |
35 | |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
36 def purge(self, ui, repo, dirs=None): |
2364 | 37 self._repo = repo |
38 self._ui = ui | |
39 self._hg_root = self._split_path(repo.root) | |
40 | |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
41 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
|
42 dirs = [repo.root] |
2364 | 43 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
44 for path in dirs: |
2364 | 45 path = os.path.abspath(path) |
46 for root, dirs, files in os.walk(path, topdown=False): | |
47 if '.hg' in self._split_path(root): | |
48 # Skip files in the .hg directory. | |
49 # Note that if the repository is in a directory | |
50 # called .hg this command does not work. | |
51 continue | |
52 for name in files: | |
53 self._remove_file(os.path.join(root, name)) | |
54 if not os.listdir(root): | |
55 # Remove this directory if it is empty. | |
56 self._remove_dir(root) | |
57 | |
58 self._repo = None | |
59 self._ui = None | |
60 self._hg_root = None | |
61 | |
62 def _error(self, msg): | |
63 if self._abort_on_err: | |
64 raise util.Abort(msg) | |
65 else: | |
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
66 self._ui.warn(_('warning: %s\n') % msg) |
2364 | 67 |
68 def _remove_file(self, name): | |
69 relative_name = self._relative_name(name) | |
70 # dirstate.state() requires a path relative to the root | |
71 # directory. | |
72 if self._repo.dirstate.state(relative_name) != '?': | |
73 return | |
2371
e39300cdb8ff
Use self._ui.note(...) instead of if self._ui.verbose: self._ui.status(...)
demian@gaudron.lan
parents:
2370
diff
changeset
|
74 self._ui.note(name + '\n') |
2364 | 75 if self._act: |
76 try: | |
77 os.remove(name) | |
78 except OSError, e: | |
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
79 self._error(_('%s cannot be removed') % name) |
2364 | 80 |
81 def _remove_dir(self, name): | |
2371
e39300cdb8ff
Use self._ui.note(...) instead of if self._ui.verbose: self._ui.status(...)
demian@gaudron.lan
parents:
2370
diff
changeset
|
82 self._ui.note(name + '\n') |
2364 | 83 if self._act: |
84 try: | |
85 os.rmdir(name) | |
86 except OSError, e: | |
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
87 self._error(_('%s cannot be removed') % name) |
2364 | 88 |
2375 | 89 def _relative_name(self, path): |
90 ''' | |
91 Returns "path" but relative to the root directory of the | |
92 repository and with '\\' replaced with '/'. | |
93 This is needed because this is the format required by | |
94 self._repo.dirstate.state(). | |
95 ''' | |
96 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
|
97 # 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
|
98 return self._join_path(splitted_path).replace('\\', '/') |
2364 | 99 |
100 def _split_path(self, path): | |
2375 | 101 ''' |
102 Retruns a list of the single files/directories in "path". | |
103 For instance: | |
104 '/home/user/test' -> ['/', 'home', 'user', 'test'] | |
105 'C:\\Mercurial' -> ['C:\\', 'Mercurial'] | |
106 ''' | |
2364 | 107 ret = [] |
108 while True: | |
109 head, tail = os.path.split(path) | |
110 if tail: | |
111 ret.append(tail) | |
112 if head == path: | |
113 ret.append(head) | |
114 break | |
115 path = head | |
116 ret.reverse() | |
117 return ret | |
118 | |
119 def _join_path(self, splitted_path): | |
2375 | 120 ''' |
121 Joins a list returned by _split_path(). | |
122 ''' | |
2364 | 123 ret = '' |
124 for part in splitted_path: | |
125 if ret: | |
126 ret = os.path.join(ret, part) | |
127 else: | |
128 ret = part | |
129 return ret | |
130 | |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
131 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
132 def purge(ui, repo, *dirs, **opts): |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
133 '''removes files not tracked by mercurial |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
134 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
135 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
|
136 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
|
137 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
138 This means that purge will delete: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
139 - 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
|
140 - 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
|
141 a pattern in a ".hgignore" file |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
142 - Empty directories: infact Mercurial ignores directories unless they |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
143 contain files under source control managment |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
144 But it will leave untouched: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
145 - Unmodified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
146 - Modified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
147 - New files added to the repository (with "hg add") |
2364 | 148 |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
149 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
|
150 directories are considered. |
2364 | 151 |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
152 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
|
153 forgot to add to the repository. If you only want to print the list of |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
154 files that this program would delete use the -vn options. |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
155 ''' |
2372
449906e17576
Changset de893ad6bd17 wrongly reversed the meaning of --nothing
demian@gaudron.lan
parents:
2371
diff
changeset
|
156 act = not opts['nothing'] |
2370
de893ad6bd17
Command line options are read in a saner way
demian@gaudron.lan
parents:
2369
diff
changeset
|
157 abort_on_err = bool(opts['abort_on_err']) |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
158 p = Purge(act, abort_on_err) |
2377
626779aba9bb
The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents:
2376
diff
changeset
|
159 p.purge(ui, repo, dirs) |
2364 | 160 |
161 | |
162 cmdtable = { | |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
163 'purge': (purge, |
2376
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
164 [('a', 'abort-on-err', None, _('abort if an error occurs')), |
52cfb9864257
Marked strings for translation
Marco Barisione <marco@barisione.org>
parents:
2375
diff
changeset
|
165 ('n', 'nothing', None, _('do nothing on files, useful with --verbose')), |
2364 | 166 ], |
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 _('hg purge [OPTIONS] [DIR]')) |
2364 | 168 } |