Mercurial > public > mercurial-scm > hg-stable
annotate purge.py @ 2369:9da3dd62c827
Purge.from_command is now a function called purge
author | demian@gaudron.lan |
---|---|
date | Tue, 16 May 2006 13:37:48 +0200 |
parents | f368a1c302d5 |
children | de893ad6bd17 |
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 | |
71 if self._ui.verbose: | |
72 self._ui.status(name + '\n') | |
73 if self._act: | |
74 try: | |
75 os.remove(name) | |
76 except OSError, e: | |
77 error('"%s" cannot be removed' % name) | |
78 | |
79 def _remove_dir(self, name): | |
80 if self._ui.verbose: | |
81 self._ui.status(name + '\n') | |
82 if self._act: | |
83 try: | |
84 os.rmdir(name) | |
85 except OSError, e: | |
86 error('"%s" cannot be removed' % name) | |
87 | |
88 def _relative_name(self, name): | |
89 splitted_path = self._split_path(name)[len(self._hg_root):] | |
90 return self._join_path(splitted_path) | |
91 | |
92 def _split_path(self, path): | |
93 ret = [] | |
94 while True: | |
95 head, tail = os.path.split(path) | |
96 if tail: | |
97 ret.append(tail) | |
98 if head == path: | |
99 ret.append(head) | |
100 break | |
101 path = head | |
102 ret.reverse() | |
103 return ret | |
104 | |
105 def _join_path(self, splitted_path): | |
106 ret = '' | |
107 for part in splitted_path: | |
108 if ret: | |
109 ret = os.path.join(ret, part) | |
110 else: | |
111 ret = part | |
112 return ret | |
113 | |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
114 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
115 def purge(ui, repo, *paths, **opts): |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
116 '''removes files not tracked by mercurial |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
117 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
118 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
|
119 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
|
120 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
121 This means that purge will delete: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
122 - 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
|
123 - 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
|
124 pattern in a ".hgignore" file |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
125 - 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
|
126 contain files under source control managment |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
127 But it will leave untouched: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
128 - Unmodified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
129 - Modified tracked files |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
130 - New files added to the repository (with "hg add") |
2364 | 131 |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
132 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
|
133 all files in the repository directory are considered. |
2364 | 134 |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 ''' |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
139 act = True |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
140 if opts['nothing']: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
141 act = False |
2364 | 142 |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
143 abort_on_err = True |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
144 if not opts['abort_on_err']: |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
145 abort_on_err = False |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
146 |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
147 p = Purge(act, abort_on_err) |
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
148 p.purge(ui, repo, paths) |
2364 | 149 |
150 | |
151 cmdtable = { | |
2369
9da3dd62c827
Purge.from_command is now a function called purge
demian@gaudron.lan
parents:
2364
diff
changeset
|
152 'purge': (purge, |
2364 | 153 [('a', 'abort-on-err', None, 'abort if an error occurs'), |
154 ('n', 'nothing', None, 'do nothing on files, useful with --verbose'), | |
155 ], | |
156 'hg purge [OPTIONS] [NAME]') | |
157 } |