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