Show More
@@ -1,98 +1,99 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 | 35 | '''removes files not tracked by Mercurial |
|
36 | 36 | |
|
37 |
Delete files not known to Mercurial. This is useful to test local |
|
|
38 | uncommitted changes in an otherwise-clean source tree. | |
|
37 | Delete files not known to Mercurial. This is useful to test local | |
|
38 | and 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 |
- Empty directories: in fact Mercurial ignores directories unless |
|
|
43 | contain files under source control managment | |
|
42 | - Empty directories: in fact Mercurial ignores directories unless | |
|
43 | they contain files under source control managment | |
|
44 | 44 | But it will leave untouched: |
|
45 | 45 | - Modified and unmodified tracked files |
|
46 | 46 | - Ignored files (unless --all is specified) |
|
47 | 47 | - New files added to the repository (with "hg add") |
|
48 | 48 | |
|
49 | 49 | If directories are given on the command line, only files in these |
|
50 | 50 | directories are considered. |
|
51 | 51 | |
|
52 |
Be careful with purge, as you could irreversibly delete some files |
|
|
53 |
forgot to add to the repository. If you only want to print the |
|
|
54 |
files that this program would delete, use the --print |
|
|
52 | Be careful with purge, as you could irreversibly delete some files | |
|
53 | you forgot to add to the repository. If you only want to print the | |
|
54 | list of files that this program would delete, use the --print | |
|
55 | option. | |
|
55 | 56 | ''' |
|
56 | 57 | act = not opts['print'] |
|
57 | 58 | eol = '\n' |
|
58 | 59 | if opts['print0']: |
|
59 | 60 | eol = '\0' |
|
60 | 61 | act = False # --print0 implies --print |
|
61 | 62 | |
|
62 | 63 | def remove(remove_func, name): |
|
63 | 64 | if act: |
|
64 | 65 | try: |
|
65 | 66 | remove_func(repo.wjoin(name)) |
|
66 | 67 | except OSError: |
|
67 | 68 | m = _('%s cannot be removed') % name |
|
68 | 69 | if opts['abort_on_err']: |
|
69 | 70 | raise util.Abort(m) |
|
70 | 71 | ui.warn(_('warning: %s\n') % m) |
|
71 | 72 | else: |
|
72 | 73 | ui.write('%s%s' % (name, eol)) |
|
73 | 74 | |
|
74 | 75 | directories = [] |
|
75 | 76 | match = cmdutil.match(repo, dirs, opts) |
|
76 | 77 | match.dir = directories.append |
|
77 | 78 | status = repo.status(match=match, ignored=opts['all'], unknown=True) |
|
78 | 79 | |
|
79 | 80 | for f in util.sort(status[4] + status[5]): |
|
80 | 81 | ui.note(_('Removing file %s\n') % f) |
|
81 | 82 | remove(os.remove, f) |
|
82 | 83 | |
|
83 | 84 | for f in util.sort(directories)[::-1]: |
|
84 | 85 | if match(f) and not os.listdir(repo.wjoin(f)): |
|
85 | 86 | ui.note(_('Removing directory %s\n') % f) |
|
86 | 87 | remove(os.rmdir, f) |
|
87 | 88 | |
|
88 | 89 | cmdtable = { |
|
89 | 90 | 'purge|clean': |
|
90 | 91 | (purge, |
|
91 | 92 | [('a', 'abort-on-err', None, _('abort if an error occurs')), |
|
92 | 93 | ('', 'all', None, _('purge ignored files too')), |
|
93 | 94 | ('p', 'print', None, _('print the file names instead of deleting them')), |
|
94 | 95 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs' |
|
95 | 96 | ' (implies -p)')), |
|
96 | 97 | ] + commands.walkopts, |
|
97 | 98 | _('hg purge [OPTION]... [DIR]...')) |
|
98 | 99 | } |
General Comments 0
You need to be logged in to leave comments.
Login now