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