Mercurial > hg
annotate purge.py @ 2372:449906e17576
Changset de893ad6bd17 wrongly reversed the meaning of --nothing
author | demian@gaudron.lan |
---|---|
date | Tue, 16 May 2006 18:55:22 +0200 |
parents | e39300cdb8ff |
children | 61976a27aa2b |
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 | |
25 class Purge(object): | |
26 def __init__(self, act=True, abort_on_err=False): | |
27 self._repo = None | |
28 self._ui = None | |
29 self._hg_root = None | |
30 self._act = act | |
31 self._abort_on_err = abort_on_err | |
32 | |
33 def purge(self, ui, repo, paths=None): | |
34 self._repo = repo | |
35 self._ui = ui | |
36 self._hg_root = self._split_path(repo.root) | |
37 | |
38 if not paths: | |
39 paths = [repo.root] | |
40 | |
41 for path in paths: | |
42 path = os.path.abspath(path) | |
43 for root, dirs, files in os.walk(path, topdown=False): | |
44 if '.hg' in self._split_path(root): | |
45 # Skip files in the .hg directory. | |
46 # Note that if the repository is in a directory | |
47 # called .hg this command does not work. | |
48 continue | |
49 for name in files: | |
50 self._remove_file(os.path.join(root, name)) | |
51 if not os.listdir(root): | |
52 # Remove this directory if it is empty. | |
53 self._remove_dir(root) | |
54 | |
55 self._repo = None | |
56 self._ui = None | |
57 self._hg_root = None | |
58 | |
59 def _error(self, msg): | |
60 if self._abort_on_err: | |
61 raise util.Abort(msg) | |
62 else: | |
63 ui.warn('warning: ' + msg + '\n') | |
64 | |
65 def _remove_file(self, name): | |
66 relative_name = self._relative_name(name) | |
67 # dirstate.state() requires a path relative to the root | |
68 # directory. | |
69 if self._repo.dirstate.state(relative_name) != '?': | |
70 return | |
2371
e39300cdb8ff
Use self._ui.note(...) instead of if self._ui.verbose: self._ui.status(...)
demian@gaudron.lan
parents:
2370
diff
changeset
|
71 self._ui.note(name + '\n') |
2364 | 72 if self._act: |
73 try: | |
74 os.remove(name) | |
75 except OSError, e: | |
76 error('"%s" cannot be removed' % name) | |
77 | |
78 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
|
79 self._ui.note(name + '\n') |
2364 | 80 if self._act: |
81 try: | |
82 os.rmdir(name) | |
83 except OSError, e: | |
84 error('"%s" cannot be removed' % name) | |
85 | |
86 def _relative_name(self, name): | |
87 splitted_path = self._split_path(name)[len(self._hg_root):] | |
88 return self._join_path(splitted_path) | |
89 | |
90 def _split_path(self, path): | |
91 ret = [] | |
92 while True: | |
93 head, tail = os.path.split(path) | |
94 if tail: | |
95 ret.append(tail) | |
96 if head == path: | |
97 ret.append(head) | |
98 break | |
99 path = head | |
100 ret.reverse() | |
101 return ret | |
102 | |
103 def _join_path(self, splitted_path): | |
104 ret = '' | |
105 for part in splitted_path: | |
106 if ret: | |
107 ret = os.path.join(ret, part) | |
108 else: | |
109 ret = part | |
110 return ret | |
111 | |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
112 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
113 def purge(ui, repo, *paths, **opts): |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
114 '''removes files not tracked by mercurial |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
115 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
116 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
|
117 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
|
118 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
119 This means that purge will delete: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
120 - Unknown files: files marked with "?" by "hg status" |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
121 - Ignored files: files usually ignored by Mercurial because they match a |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
122 pattern in a ".hgignore" file |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
123 - 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
|
124 contain files under source control managment |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
125 But it will leave untouched: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
126 - Unmodified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
127 - Modified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
128 - New files added to the repository (with "hg add") |
2364 | 129 |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
130 If names are given, only files matching the names are considered, else |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
131 all files in the repository directory are considered. |
2364 | 132 |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
133 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
|
134 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
|
135 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
|
136 ''' |
2372
449906e17576
Changset de893ad6bd17 wrongly reversed the meaning of --nothing
demian@gaudron.lan
parents:
2371
diff
changeset
|
137 act = not opts['nothing'] |
2370
de893ad6bd17
Command line options are read in a saner way
demian@gaudron.lan
parents:
2369
diff
changeset
|
138 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
|
139 p = Purge(act, abort_on_err) |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
140 p.purge(ui, repo, paths) |
2364 | 141 |
142 | |
143 cmdtable = { | |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
144 'purge': (purge, |
2364 | 145 [('a', 'abort-on-err', None, 'abort if an error occurs'), |
146 ('n', 'nothing', None, 'do nothing on files, useful with --verbose'), | |
147 ], | |
148 'hg purge [OPTIONS] [NAME]') | |
149 } |