Show More
@@ -1,130 +1,128 b'' | |||||
1 | # Copyright (C) 2006 - Marco Barisione <marco@barisione.org> |
|
1 | # Copyright (C) 2006 - Marco Barisione <marco@barisione.org> | |
2 | # |
|
2 | # | |
3 | # This is a small extension for Mercurial (http://www.selenic.com/mercurial) |
|
3 | # This is a small extension for Mercurial (http://www.selenic.com/mercurial) | |
4 | # that removes files not known to mercurial |
|
4 | # that removes files not known to mercurial | |
5 | # |
|
5 | # | |
6 | # This program is free software; you can redistribute it and/or modify |
|
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 |
|
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 |
|
8 | # the Free Software Foundation; either version 2 of the License, or | |
9 | # (at your option) any later version. |
|
9 | # (at your option) any later version. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
18 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
19 |
|
19 | |||
20 | from mercurial import hg, util |
|
20 | from mercurial import hg, util | |
21 | from mercurial.i18n import _ |
|
21 | from mercurial.i18n import _ | |
22 | import os |
|
22 | import os | |
23 |
|
23 | |||
24 | class Purge(object): |
|
24 | class Purge(object): | |
25 | def __init__(self, act=True, abort_on_err=False, eol='\n'): |
|
25 | def __init__(self, act=True, abort_on_err=False, eol='\n'): | |
26 | self._repo = None |
|
26 | self._repo = None | |
27 | self._ui = None |
|
27 | self._ui = None | |
28 | self._act = act |
|
28 | self._act = act | |
29 | self._abort_on_err = abort_on_err |
|
29 | self._abort_on_err = abort_on_err | |
30 | self._eol = eol |
|
30 | self._eol = eol | |
31 |
|
31 | |||
32 | def purge(self, ui, repo, dirs=None): |
|
32 | def purge(self, ui, repo, dirs=None): | |
33 | self._repo = repo |
|
33 | self._repo = repo | |
34 | self._ui = ui |
|
34 | self._ui = ui | |
35 |
|
35 | |||
36 | directories = [] |
|
36 | directories = [] | |
37 | files = [] |
|
37 | files = [] | |
38 | for src, f, st in repo.dirstate.statwalk(files=dirs, ignored=True, |
|
38 | for src, f, st in repo.dirstate.statwalk(files=dirs, ignored=True, | |
39 | directories=True): |
|
39 | directories=True): | |
40 | if src == 'd': |
|
40 | if src == 'd': | |
41 | directories.append(f) |
|
41 | directories.append(f) | |
42 | elif src == 'f' and f not in repo.dirstate: |
|
42 | elif src == 'f' and f not in repo.dirstate: | |
43 | files.append(f) |
|
43 | files.append(f) | |
44 |
|
44 | |||
45 | directories.sort() |
|
45 | directories.sort() | |
46 |
|
46 | |||
47 | for f in files: |
|
47 | for f in files: | |
48 | self._remove_file(f) |
|
48 | self._remove_file(f) | |
49 |
|
49 | |||
50 | for f in directories[::-1]: |
|
50 | for f in directories[::-1]: | |
51 | if not os.listdir(repo.wjoin(f)): |
|
51 | if not os.listdir(repo.wjoin(f)): | |
52 | self._remove_dir(f) |
|
52 | self._remove_dir(f) | |
53 |
|
53 | |||
54 | self._repo = None |
|
54 | self._repo = None | |
55 | self._ui = None |
|
55 | self._ui = None | |
56 |
|
56 | |||
57 | def _error(self, msg): |
|
57 | def _error(self, msg): | |
58 | if self._abort_on_err: |
|
58 | if self._abort_on_err: | |
59 | raise util.Abort(msg) |
|
59 | raise util.Abort(msg) | |
60 | else: |
|
60 | else: | |
61 | self._ui.warn(_('warning: %s\n') % msg) |
|
61 | self._ui.warn(_('warning: %s\n') % msg) | |
62 |
|
62 | |||
63 | def _remove_file(self, name): |
|
63 | def _remove_file(self, name): | |
64 | # dirstate.state() requires a path relative to the root |
|
64 | if name in self._repo.dirstate: | |
65 | # directory. |
|
|||
66 | if self._repo.dirstate.state(name) != '?': |
|
|||
67 | return |
|
65 | return | |
68 | self._ui.note(_('Removing file %s\n') % name) |
|
66 | self._ui.note(_('Removing file %s\n') % name) | |
69 | if self._act: |
|
67 | if self._act: | |
70 | try: |
|
68 | try: | |
71 | os.remove(self._repo.wjoin(name)) |
|
69 | os.remove(self._repo.wjoin(name)) | |
72 | except OSError, e: |
|
70 | except OSError, e: | |
73 | self._error(_('%s cannot be removed') % name) |
|
71 | self._error(_('%s cannot be removed') % name) | |
74 | else: |
|
72 | else: | |
75 | self._ui.write('%s%s' % (name, self._eol)) |
|
73 | self._ui.write('%s%s' % (name, self._eol)) | |
76 |
|
74 | |||
77 | def _remove_dir(self, name): |
|
75 | def _remove_dir(self, name): | |
78 | self._ui.note(_('Removing directory %s\n') % name) |
|
76 | self._ui.note(_('Removing directory %s\n') % name) | |
79 | if self._act: |
|
77 | if self._act: | |
80 | try: |
|
78 | try: | |
81 | os.rmdir(self._repo.wjoin(name)) |
|
79 | os.rmdir(self._repo.wjoin(name)) | |
82 | except OSError, e: |
|
80 | except OSError, e: | |
83 | self._error(_('%s cannot be removed') % name) |
|
81 | self._error(_('%s cannot be removed') % name) | |
84 | else: |
|
82 | else: | |
85 | self._ui.write('%s%s' % (name, self._eol)) |
|
83 | self._ui.write('%s%s' % (name, self._eol)) | |
86 |
|
84 | |||
87 |
|
85 | |||
88 | def purge(ui, repo, *dirs, **opts): |
|
86 | def purge(ui, repo, *dirs, **opts): | |
89 | '''removes files not tracked by mercurial |
|
87 | '''removes files not tracked by mercurial | |
90 |
|
88 | |||
91 | Delete files not known to mercurial, this is useful to test local and |
|
89 | Delete files not known to mercurial, this is useful to test local and | |
92 | uncommitted changes in the otherwise clean source tree. |
|
90 | uncommitted changes in the otherwise clean source tree. | |
93 |
|
91 | |||
94 | This means that purge will delete: |
|
92 | This means that purge will delete: | |
95 | - Unknown files: files marked with "?" by "hg status" |
|
93 | - Unknown files: files marked with "?" by "hg status" | |
96 | - Ignored files: files usually ignored by Mercurial because they match |
|
94 | - Ignored files: files usually ignored by Mercurial because they match | |
97 | a pattern in a ".hgignore" file |
|
95 | a pattern in a ".hgignore" file | |
98 | - Empty directories: in fact Mercurial ignores directories unless they |
|
96 | - Empty directories: in fact Mercurial ignores directories unless they | |
99 | contain files under source control managment |
|
97 | contain files under source control managment | |
100 | But it will leave untouched: |
|
98 | But it will leave untouched: | |
101 | - Unmodified tracked files |
|
99 | - Unmodified tracked files | |
102 | - Modified tracked files |
|
100 | - Modified tracked files | |
103 | - New files added to the repository (with "hg add") |
|
101 | - New files added to the repository (with "hg add") | |
104 |
|
102 | |||
105 | If directories are given on the command line, only files in these |
|
103 | If directories are given on the command line, only files in these | |
106 | directories are considered. |
|
104 | directories are considered. | |
107 |
|
105 | |||
108 | Be careful with purge, you could irreversibly delete some files you |
|
106 | Be careful with purge, you could irreversibly delete some files you | |
109 | forgot to add to the repository. If you only want to print the list of |
|
107 | forgot to add to the repository. If you only want to print the list of | |
110 | files that this program would delete use the --print option. |
|
108 | files that this program would delete use the --print option. | |
111 | ''' |
|
109 | ''' | |
112 | act = not opts['print'] |
|
110 | act = not opts['print'] | |
113 | abort_on_err = bool(opts['abort_on_err']) |
|
111 | abort_on_err = bool(opts['abort_on_err']) | |
114 | eol = opts['print0'] and '\0' or '\n' |
|
112 | eol = opts['print0'] and '\0' or '\n' | |
115 | if eol == '\0': |
|
113 | if eol == '\0': | |
116 | # --print0 implies --print |
|
114 | # --print0 implies --print | |
117 | act = False |
|
115 | act = False | |
118 | p = Purge(act, abort_on_err, eol) |
|
116 | p = Purge(act, abort_on_err, eol) | |
119 | p.purge(ui, repo, dirs) |
|
117 | p.purge(ui, repo, dirs) | |
120 |
|
118 | |||
121 |
|
119 | |||
122 | cmdtable = { |
|
120 | cmdtable = { | |
123 | 'purge': |
|
121 | 'purge': | |
124 | (purge, |
|
122 | (purge, | |
125 | [('a', 'abort-on-err', None, _('abort if an error occurs')), |
|
123 | [('a', 'abort-on-err', None, _('abort if an error occurs')), | |
126 | ('p', 'print', None, _('print the file names instead of deleting them')), |
|
124 | ('p', 'print', None, _('print the file names instead of deleting them')), | |
127 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs' |
|
125 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs' | |
128 | ' (implies -p)'))], |
|
126 | ' (implies -p)'))], | |
129 | _('hg purge [OPTION]... [DIR]...')) |
|
127 | _('hg purge [OPTION]... [DIR]...')) | |
130 | } |
|
128 | } |
General Comments 0
You need to be logged in to leave comments.
Login now