Show More
@@ -1,100 +1,98 b'' | |||
|
1 | 1 | # Copyright (C) 2006 - Marco Barisione <marco@barisione.org> |
|
2 | 2 | # |
|
3 | 3 | # This is a small extension for Mercurial (http://www.selenic.com/mercurial) |
|
4 | 4 | # that removes files not known to mercurial |
|
5 | 5 | # |
|
6 | 6 | # This program was inspired by the "cvspurge" script contained in CVS utilities |
|
7 | 7 | # (http://www.red-bean.com/cvsutils/). |
|
8 | 8 | # |
|
9 | 9 | # To enable the "purge" extension put these lines in your ~/.hgrc: |
|
10 | 10 | # [extensions] |
|
11 | 11 | # hgext.purge = |
|
12 | 12 | # |
|
13 | 13 | # For help on the usage of "hg purge" use: |
|
14 | 14 | # hg help purge |
|
15 | 15 | # |
|
16 | 16 | # This program is free software; you can redistribute it and/or modify |
|
17 | 17 | # it under the terms of the GNU General Public License as published by |
|
18 | 18 | # the Free Software Foundation; either version 2 of the License, or |
|
19 | 19 | # (at your option) any later version. |
|
20 | 20 | # |
|
21 | 21 | # This program is distributed in the hope that it will be useful, |
|
22 | 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
23 | 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
24 | 24 | # GNU General Public License for more details. |
|
25 | 25 | # |
|
26 | 26 | # You should have received a copy of the GNU General Public License |
|
27 | 27 | # along with this program; if not, write to the Free Software |
|
28 | 28 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
29 | 29 | |
|
30 | 30 | from mercurial import util, commands, cmdutil |
|
31 | 31 | from mercurial.i18n import _ |
|
32 | 32 | import os |
|
33 | 33 | |
|
34 | 34 | def purge(ui, repo, *dirs, **opts): |
|
35 |
'''removes files not tracked by |
|
|
35 | '''removes files not tracked by Mercurial | |
|
36 | 36 | |
|
37 |
Delete files not known to |
|
|
38 |
uncommitted changes in |
|
|
37 | Delete files not known to Mercurial. This is useful to test local and | |
|
38 | uncommitted changes in an otherwise-clean source tree. | |
|
39 | 39 | |
|
40 | 40 | This means that purge will delete: |
|
41 | 41 | - Unknown files: files marked with "?" by "hg status" |
|
42 | - Ignored files: files usually ignored by Mercurial because they match | |
|
43 | a pattern in a ".hgignore" file | |
|
44 | 42 | - Empty directories: in fact Mercurial ignores directories unless they |
|
45 | 43 | contain files under source control managment |
|
46 | 44 | But it will leave untouched: |
|
47 |
- |
|
|
48 | - Modified tracked files | |
|
45 | - Modified and unmodified tracked files | |
|
46 | - Ignored files (unless --all is specified) | |
|
49 | 47 | - New files added to the repository (with "hg add") |
|
50 | 48 | |
|
51 | 49 | If directories are given on the command line, only files in these |
|
52 | 50 | directories are considered. |
|
53 | 51 | |
|
54 | Be careful with purge, you could irreversibly delete some files you | |
|
52 | Be careful with purge, as you could irreversibly delete some files you | |
|
55 | 53 | 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. | |
|
54 | files that this program would delete, use the --print option. | |
|
57 | 55 | ''' |
|
58 | 56 | act = not opts['print'] |
|
59 | 57 | eol = '\n' |
|
60 | 58 | if opts['print0']: |
|
61 | 59 | eol = '\0' |
|
62 | 60 | act = False # --print0 implies --print |
|
63 | 61 | |
|
64 | 62 | def remove(remove_func, name): |
|
65 | 63 | if act: |
|
66 | 64 | try: |
|
67 | 65 | remove_func(repo.wjoin(name)) |
|
68 | 66 | except OSError: |
|
69 | 67 | m = _('%s cannot be removed') % name |
|
70 | 68 | if opts['abort_on_err']: |
|
71 | 69 | raise util.Abort(m) |
|
72 | 70 | ui.warn(_('warning: %s\n') % m) |
|
73 | 71 | else: |
|
74 | 72 | ui.write('%s%s' % (name, eol)) |
|
75 | 73 | |
|
76 | 74 | directories = [] |
|
77 | 75 | match = cmdutil.match(repo, dirs, opts) |
|
78 | 76 | match.dir = directories.append |
|
79 | 77 | status = repo.status(match=match, ignored=opts['all'], unknown=True) |
|
80 | 78 | |
|
81 | 79 | for f in util.sort(status[4] + status[5]): |
|
82 | 80 | ui.note(_('Removing file %s\n') % f) |
|
83 | 81 | remove(os.remove, f) |
|
84 | 82 | |
|
85 | 83 | for f in util.sort(directories)[::-1]: |
|
86 | 84 | if match(f) and not os.listdir(repo.wjoin(f)): |
|
87 | 85 | ui.note(_('Removing directory %s\n') % f) |
|
88 | 86 | remove(os.rmdir, f) |
|
89 | 87 | |
|
90 | 88 | cmdtable = { |
|
91 | 89 | 'purge|clean': |
|
92 | 90 | (purge, |
|
93 | 91 | [('a', 'abort-on-err', None, _('abort if an error occurs')), |
|
94 | 92 | ('', 'all', None, _('purge ignored files too')), |
|
95 | 93 | ('p', 'print', None, _('print the file names instead of deleting them')), |
|
96 | 94 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs' |
|
97 | 95 | ' (implies -p)')), |
|
98 | 96 | ] + commands.walkopts, |
|
99 | 97 | _('hg purge [OPTION]... [DIR]...')) |
|
100 | 98 | } |
General Comments 0
You need to be logged in to leave comments.
Login now