Show More
@@ -1,141 +1,138 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 was inspired by the "cvspurge" script contained in CVS utilities |
|
6 | # This program was inspired by the "cvspurge" script contained in CVS utilities | |
7 | # (http://www.red-bean.com/cvsutils/). |
|
7 | # (http://www.red-bean.com/cvsutils/). | |
8 | # |
|
8 | # | |
9 | # To enable the "purge" extension put these lines in your ~/.hgrc: |
|
9 | # To enable the "purge" extension put these lines in your ~/.hgrc: | |
10 | # [extensions] |
|
10 | # [extensions] | |
11 | # hgext.purge = |
|
11 | # hgext.purge = | |
12 | # |
|
12 | # | |
13 | # For help on the usage of "hg purge" use: |
|
13 | # For help on the usage of "hg purge" use: | |
14 | # hg help purge |
|
14 | # hg help purge | |
15 | # |
|
15 | # | |
16 | # This program is free software; you can redistribute it and/or modify |
|
16 | # This program is free software; you can redistribute it and/or modify | |
17 | # it under the terms of the GNU General Public License as published by |
|
17 | # it under the terms of the GNU General Public License as published by | |
18 | # the Free Software Foundation; either version 2 of the License, or |
|
18 | # the Free Software Foundation; either version 2 of the License, or | |
19 | # (at your option) any later version. |
|
19 | # (at your option) any later version. | |
20 | # |
|
20 | # | |
21 | # This program is distributed in the hope that it will be useful, |
|
21 | # This program is distributed in the hope that it will be useful, | |
22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | # GNU General Public License for more details. |
|
24 | # GNU General Public License for more details. | |
25 | # |
|
25 | # | |
26 | # You should have received a copy of the GNU General Public License |
|
26 | # You should have received a copy of the GNU General Public License | |
27 | # along with this program; if not, write to the Free Software |
|
27 | # along with this program; if not, write to the Free Software | |
28 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
28 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
29 |
|
29 | |||
30 | from mercurial import util, commands, cmdutil |
|
30 | from mercurial import util, commands, cmdutil | |
31 | from mercurial.i18n import _ |
|
31 | from mercurial.i18n import _ | |
32 | import os |
|
32 | import os | |
33 |
|
33 | |||
34 | def purge(ui, repo, *dirs, **opts): |
|
34 | def purge(ui, repo, *dirs, **opts): | |
35 | '''removes files not tracked by mercurial |
|
35 | '''removes files not tracked by mercurial | |
36 |
|
36 | |||
37 | Delete files not known to mercurial, this is useful to test local and |
|
37 | Delete files not known to mercurial, this is useful to test local and | |
38 | uncommitted changes in the otherwise clean source tree. |
|
38 | uncommitted changes in the otherwise clean source tree. | |
39 |
|
39 | |||
40 | This means that purge will delete: |
|
40 | This means that purge will delete: | |
41 | - Unknown files: files marked with "?" by "hg status" |
|
41 | - Unknown files: files marked with "?" by "hg status" | |
42 | - Ignored files: files usually ignored by Mercurial because they match |
|
42 | - Ignored files: files usually ignored by Mercurial because they match | |
43 | a pattern in a ".hgignore" file |
|
43 | a pattern in a ".hgignore" file | |
44 | - Empty directories: in fact Mercurial ignores directories unless they |
|
44 | - Empty directories: in fact Mercurial ignores directories unless they | |
45 | contain files under source control managment |
|
45 | contain files under source control managment | |
46 | But it will leave untouched: |
|
46 | But it will leave untouched: | |
47 | - Unmodified tracked files |
|
47 | - Unmodified tracked files | |
48 | - Modified tracked files |
|
48 | - Modified tracked files | |
49 | - New files added to the repository (with "hg add") |
|
49 | - New files added to the repository (with "hg add") | |
50 |
|
50 | |||
51 | If directories are given on the command line, only files in these |
|
51 | If directories are given on the command line, only files in these | |
52 | directories are considered. |
|
52 | directories are considered. | |
53 |
|
53 | |||
54 | Be careful with purge, you could irreversibly delete some files you |
|
54 | Be careful with purge, you could irreversibly delete some files you | |
55 | forgot to add to the repository. If you only want to print the list of |
|
55 | forgot to add to the repository. If you only want to print the list of | |
56 | files that this program would delete use the --print option. |
|
56 | files that this program would delete use the --print option. | |
57 | ''' |
|
57 | ''' | |
58 | act = not opts['print'] |
|
58 | act = not opts['print'] | |
59 | ignored = bool(opts['all']) |
|
59 | ignored = bool(opts['all']) | |
60 | abort_on_err = bool(opts['abort_on_err']) |
|
60 | abort_on_err = bool(opts['abort_on_err']) | |
61 | eol = opts['print0'] and '\0' or '\n' |
|
61 | eol = opts['print0'] and '\0' or '\n' | |
62 | if eol == '\0': |
|
62 | if eol == '\0': | |
63 | # --print0 implies --print |
|
63 | # --print0 implies --print | |
64 | act = False |
|
64 | act = False | |
65 | force = bool(opts['force']) |
|
65 | force = bool(opts['force']) | |
66 |
|
66 | |||
67 | def error(msg): |
|
67 | def error(msg): | |
68 | if abort_on_err: |
|
68 | if abort_on_err: | |
69 | raise util.Abort(msg) |
|
69 | raise util.Abort(msg) | |
70 | else: |
|
70 | else: | |
71 | ui.warn(_('warning: %s\n') % msg) |
|
71 | ui.warn(_('warning: %s\n') % msg) | |
72 |
|
72 | |||
73 | def remove(remove_func, name): |
|
73 | def remove(remove_func, name): | |
74 | if act: |
|
74 | if act: | |
75 | try: |
|
75 | try: | |
76 | remove_func(os.path.join(repo.root, name)) |
|
76 | remove_func(os.path.join(repo.root, name)) | |
77 | except OSError, e: |
|
77 | except OSError, e: | |
78 | error(_('%s cannot be removed') % name) |
|
78 | error(_('%s cannot be removed') % name) | |
79 | else: |
|
79 | else: | |
80 | ui.write('%s%s' % (name, eol)) |
|
80 | ui.write('%s%s' % (name, eol)) | |
81 |
|
81 | |||
82 | if not force: |
|
82 | if not force: | |
83 | _check_fs(ui, repo) |
|
83 | _check_fs(ui, repo) | |
84 |
|
84 | |||
85 | directories = [] |
|
85 | directories = [] | |
86 | files = [] |
|
86 | files = [] | |
87 | missing = [] |
|
|||
88 | match = cmdutil.match(repo, dirs, opts) |
|
87 | match = cmdutil.match(repo, dirs, opts) | |
89 | match.dir = directories.append |
|
88 | match.dir = directories.append | |
90 | for src, f, st in repo.dirstate.statwalk(match.files(), match, |
|
89 | for src, f, st in repo.dirstate.statwalk(match.files(), match, | |
91 | ignored=ignored): |
|
90 | ignored=ignored): | |
92 | if src == 'm': |
|
91 | if src == 'f' and f not in repo.dirstate: | |
93 | missing.append(f) |
|
|||
94 | elif src == 'f' and f not in repo.dirstate: |
|
|||
95 | files.append(f) |
|
92 | files.append(f) | |
96 |
|
93 | |||
97 | directories.sort() |
|
94 | directories.sort() | |
98 |
|
95 | |||
99 | for f in files: |
|
96 | for f in files: | |
100 | if f not in repo.dirstate: |
|
97 | if f not in repo.dirstate: | |
101 | ui.note(_('Removing file %s\n') % f) |
|
98 | ui.note(_('Removing file %s\n') % f) | |
102 | remove(os.remove, f) |
|
99 | remove(os.remove, f) | |
103 |
|
100 | |||
104 | for f in directories[::-1]: |
|
101 | for f in directories[::-1]: | |
105 | if match(f) and not os.listdir(repo.wjoin(f)): |
|
102 | if match(f) and not os.listdir(repo.wjoin(f)): | |
106 | ui.note(_('Removing directory %s\n') % f) |
|
103 | ui.note(_('Removing directory %s\n') % f) | |
107 | remove(os.rmdir, f) |
|
104 | remove(os.rmdir, f) | |
108 |
|
105 | |||
109 | def _check_fs(ui, repo): |
|
106 | def _check_fs(ui, repo): | |
110 | """Abort if there is the chance of having problems with name-mangling fs |
|
107 | """Abort if there is the chance of having problems with name-mangling fs | |
111 |
|
108 | |||
112 | In a name mangling filesystem (e.g. a case insensitive one) |
|
109 | In a name mangling filesystem (e.g. a case insensitive one) | |
113 | dirstate.walk() can yield filenames different from the ones |
|
110 | dirstate.walk() can yield filenames different from the ones | |
114 | stored in the dirstate. This already confuses the status and |
|
111 | stored in the dirstate. This already confuses the status and | |
115 | add commands, but with purge this may cause data loss. |
|
112 | add commands, but with purge this may cause data loss. | |
116 |
|
113 | |||
117 | To prevent this, this function will abort if there are uncommitted |
|
114 | To prevent this, this function will abort if there are uncommitted | |
118 | changes. |
|
115 | changes. | |
119 | """ |
|
116 | """ | |
120 |
|
117 | |||
121 | # We can't use (files, match) to do a partial walk here - we wouldn't |
|
118 | # We can't use (files, match) to do a partial walk here - we wouldn't | |
122 | # notice a modified README file if the user ran "hg purge readme" |
|
119 | # notice a modified README file if the user ran "hg purge readme" | |
123 | modified, added, removed, deleted = repo.status()[:4] |
|
120 | modified, added, removed, deleted = repo.status()[:4] | |
124 | if modified or added or removed or deleted: |
|
121 | if modified or added or removed or deleted: | |
125 | if not util.checkfolding(repo.path) and not ui.quiet: |
|
122 | if not util.checkfolding(repo.path) and not ui.quiet: | |
126 | ui.warn(_("Purging on name mangling filesystems is not " |
|
123 | ui.warn(_("Purging on name mangling filesystems is not " | |
127 | "fully supported.\n")) |
|
124 | "fully supported.\n")) | |
128 | raise util.Abort(_("outstanding uncommitted changes")) |
|
125 | raise util.Abort(_("outstanding uncommitted changes")) | |
129 |
|
126 | |||
130 | cmdtable = { |
|
127 | cmdtable = { | |
131 | 'purge|clean': |
|
128 | 'purge|clean': | |
132 | (purge, |
|
129 | (purge, | |
133 | [('a', 'abort-on-err', None, _('abort if an error occurs')), |
|
130 | [('a', 'abort-on-err', None, _('abort if an error occurs')), | |
134 | ('', 'all', None, _('purge ignored files too')), |
|
131 | ('', 'all', None, _('purge ignored files too')), | |
135 | ('f', 'force', None, _('purge even when there are uncommitted changes')), |
|
132 | ('f', 'force', None, _('purge even when there are uncommitted changes')), | |
136 | ('p', 'print', None, _('print the file names instead of deleting them')), |
|
133 | ('p', 'print', None, _('print the file names instead of deleting them')), | |
137 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs' |
|
134 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs' | |
138 | ' (implies -p)')), |
|
135 | ' (implies -p)')), | |
139 | ] + commands.walkopts, |
|
136 | ] + commands.walkopts, | |
140 | _('hg purge [OPTION]... [DIR]...')) |
|
137 | _('hg purge [OPTION]... [DIR]...')) | |
141 | } |
|
138 | } |
General Comments 0
You need to be logged in to leave comments.
Login now