Show More
@@ -1,137 +1,134 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']) |
|
|||
60 | abort_on_err = bool(opts['abort_on_err']) |
|
59 | abort_on_err = bool(opts['abort_on_err']) | |
61 | eol = opts['print0'] and '\0' or '\n' |
|
60 | eol = opts['print0'] and '\0' or '\n' | |
62 | if eol == '\0': |
|
61 | if eol == '\0': | |
63 | # --print0 implies --print |
|
62 | # --print0 implies --print | |
64 | act = False |
|
63 | act = False | |
65 | force = bool(opts['force']) |
|
64 | force = bool(opts['force']) | |
66 |
|
65 | |||
67 | def error(msg): |
|
66 | def error(msg): | |
68 | if abort_on_err: |
|
67 | if abort_on_err: | |
69 | raise util.Abort(msg) |
|
68 | raise util.Abort(msg) | |
70 | else: |
|
69 | else: | |
71 | ui.warn(_('warning: %s\n') % msg) |
|
70 | ui.warn(_('warning: %s\n') % msg) | |
72 |
|
71 | |||
73 | def remove(remove_func, name): |
|
72 | def remove(remove_func, name): | |
74 | if act: |
|
73 | if act: | |
75 | try: |
|
74 | try: | |
76 | remove_func(os.path.join(repo.root, name)) |
|
75 | remove_func(os.path.join(repo.root, name)) | |
77 | except OSError, e: |
|
76 | except OSError, e: | |
78 | error(_('%s cannot be removed') % name) |
|
77 | error(_('%s cannot be removed') % name) | |
79 | else: |
|
78 | else: | |
80 | ui.write('%s%s' % (name, eol)) |
|
79 | ui.write('%s%s' % (name, eol)) | |
81 |
|
80 | |||
82 | if not force: |
|
81 | if not force: | |
83 | _check_fs(ui, repo) |
|
82 | _check_fs(ui, repo) | |
84 |
|
83 | |||
85 | directories = [] |
|
84 | directories = [] | |
86 | files = [] |
|
85 | files = [] | |
87 | match = cmdutil.match(repo, dirs, opts) |
|
86 | match = cmdutil.match(repo, dirs, opts) | |
88 | match.dir = directories.append |
|
87 | match.dir = directories.append | |
89 | for src, f, st in repo.dirstate.statwalk(match, ignored=ignored): |
|
88 | status = repo.status(match=match, ignored=opts['all'], unknown=True) | |
90 | if src == 'f' and f not in repo.dirstate: |
|
89 | files = status[4] + status[5] | |
91 | files.append(f) |
|
90 | files.sort() | |
92 |
|
||||
93 | directories.sort() |
|
91 | directories.sort() | |
94 |
|
92 | |||
95 | for f in files: |
|
93 | for f in files: | |
96 | if f not in repo.dirstate: |
|
|||
97 |
|
|
94 | ui.note(_('Removing file %s\n') % f) | |
98 |
|
|
95 | remove(os.remove, f) | |
99 |
|
96 | |||
100 | for f in directories[::-1]: |
|
97 | for f in directories[::-1]: | |
101 | if match(f) and not os.listdir(repo.wjoin(f)): |
|
98 | if match(f) and not os.listdir(repo.wjoin(f)): | |
102 | ui.note(_('Removing directory %s\n') % f) |
|
99 | ui.note(_('Removing directory %s\n') % f) | |
103 | remove(os.rmdir, f) |
|
100 | remove(os.rmdir, f) | |
104 |
|
101 | |||
105 | def _check_fs(ui, repo): |
|
102 | def _check_fs(ui, repo): | |
106 | """Abort if there is the chance of having problems with name-mangling fs |
|
103 | """Abort if there is the chance of having problems with name-mangling fs | |
107 |
|
104 | |||
108 | In a name mangling filesystem (e.g. a case insensitive one) |
|
105 | In a name mangling filesystem (e.g. a case insensitive one) | |
109 | dirstate.walk() can yield filenames different from the ones |
|
106 | dirstate.walk() can yield filenames different from the ones | |
110 | stored in the dirstate. This already confuses the status and |
|
107 | stored in the dirstate. This already confuses the status and | |
111 | add commands, but with purge this may cause data loss. |
|
108 | add commands, but with purge this may cause data loss. | |
112 |
|
109 | |||
113 | To prevent this, this function will abort if there are uncommitted |
|
110 | To prevent this, this function will abort if there are uncommitted | |
114 | changes. |
|
111 | changes. | |
115 | """ |
|
112 | """ | |
116 |
|
113 | |||
117 | # We can't use (files, match) to do a partial walk here - we wouldn't |
|
114 | # We can't use (files, match) to do a partial walk here - we wouldn't | |
118 | # notice a modified README file if the user ran "hg purge readme" |
|
115 | # notice a modified README file if the user ran "hg purge readme" | |
119 | modified, added, removed, deleted = repo.status()[:4] |
|
116 | modified, added, removed, deleted = repo.status()[:4] | |
120 | if modified or added or removed or deleted: |
|
117 | if modified or added or removed or deleted: | |
121 | if not util.checkcase(repo.path) and not ui.quiet: |
|
118 | if not util.checkcase(repo.path) and not ui.quiet: | |
122 | ui.warn(_("Purging on case-insensitive filesystems is not " |
|
119 | ui.warn(_("Purging on case-insensitive filesystems is not " | |
123 | "fully supported.\n")) |
|
120 | "fully supported.\n")) | |
124 | raise util.Abort(_("outstanding uncommitted changes")) |
|
121 | raise util.Abort(_("outstanding uncommitted changes")) | |
125 |
|
122 | |||
126 | cmdtable = { |
|
123 | cmdtable = { | |
127 | 'purge|clean': |
|
124 | 'purge|clean': | |
128 | (purge, |
|
125 | (purge, | |
129 | [('a', 'abort-on-err', None, _('abort if an error occurs')), |
|
126 | [('a', 'abort-on-err', None, _('abort if an error occurs')), | |
130 | ('', 'all', None, _('purge ignored files too')), |
|
127 | ('', 'all', None, _('purge ignored files too')), | |
131 | ('f', 'force', None, _('purge even when there are uncommitted changes')), |
|
128 | ('f', 'force', None, _('purge even when there are uncommitted changes')), | |
132 | ('p', 'print', None, _('print the file names instead of deleting them')), |
|
129 | ('p', 'print', None, _('print the file names instead of deleting them')), | |
133 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs' |
|
130 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs' | |
134 | ' (implies -p)')), |
|
131 | ' (implies -p)')), | |
135 | ] + commands.walkopts, |
|
132 | ] + commands.walkopts, | |
136 | _('hg purge [OPTION]... [DIR]...')) |
|
133 | _('hg purge [OPTION]... [DIR]...')) | |
137 | } |
|
134 | } |
General Comments 0
You need to be logged in to leave comments.
Login now