##// END OF EJS Templates
purge: use status
Matt Mackall -
r6754:0b700faa default
parent child Browse files
Show More
@@ -1,137 +1,134 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 37 Delete files not known to mercurial, this is useful to test local and
38 38 uncommitted changes in the 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 42 - Ignored files: files usually ignored by Mercurial because they match
43 43 a pattern in a ".hgignore" file
44 44 - Empty directories: in fact Mercurial ignores directories unless they
45 45 contain files under source control managment
46 46 But it will leave untouched:
47 47 - Unmodified tracked files
48 48 - Modified tracked files
49 49 - New files added to the repository (with "hg add")
50 50
51 51 If directories are given on the command line, only files in these
52 52 directories are considered.
53 53
54 54 Be careful with purge, you could irreversibly delete some files you
55 55 forgot to add to the repository. If you only want to print the list of
56 56 files that this program would delete use the --print option.
57 57 '''
58 58 act = not opts['print']
59 ignored = bool(opts['all'])
60 59 abort_on_err = bool(opts['abort_on_err'])
61 60 eol = opts['print0'] and '\0' or '\n'
62 61 if eol == '\0':
63 62 # --print0 implies --print
64 63 act = False
65 64 force = bool(opts['force'])
66 65
67 66 def error(msg):
68 67 if abort_on_err:
69 68 raise util.Abort(msg)
70 69 else:
71 70 ui.warn(_('warning: %s\n') % msg)
72 71
73 72 def remove(remove_func, name):
74 73 if act:
75 74 try:
76 75 remove_func(os.path.join(repo.root, name))
77 76 except OSError, e:
78 77 error(_('%s cannot be removed') % name)
79 78 else:
80 79 ui.write('%s%s' % (name, eol))
81 80
82 81 if not force:
83 82 _check_fs(ui, repo)
84 83
85 84 directories = []
86 85 files = []
87 86 match = cmdutil.match(repo, dirs, opts)
88 87 match.dir = directories.append
89 for src, f, st in repo.dirstate.statwalk(match, ignored=ignored):
90 if src == 'f' and f not in repo.dirstate:
91 files.append(f)
92
88 status = repo.status(match=match, ignored=opts['all'], unknown=True)
89 files = status[4] + status[5]
90 files.sort()
93 91 directories.sort()
94 92
95 93 for f in files:
96 if f not in repo.dirstate:
97 ui.note(_('Removing file %s\n') % f)
98 remove(os.remove, f)
94 ui.note(_('Removing file %s\n') % f)
95 remove(os.remove, f)
99 96
100 97 for f in directories[::-1]:
101 98 if match(f) and not os.listdir(repo.wjoin(f)):
102 99 ui.note(_('Removing directory %s\n') % f)
103 100 remove(os.rmdir, f)
104 101
105 102 def _check_fs(ui, repo):
106 103 """Abort if there is the chance of having problems with name-mangling fs
107 104
108 105 In a name mangling filesystem (e.g. a case insensitive one)
109 106 dirstate.walk() can yield filenames different from the ones
110 107 stored in the dirstate. This already confuses the status and
111 108 add commands, but with purge this may cause data loss.
112 109
113 110 To prevent this, this function will abort if there are uncommitted
114 111 changes.
115 112 """
116 113
117 114 # We can't use (files, match) to do a partial walk here - we wouldn't
118 115 # notice a modified README file if the user ran "hg purge readme"
119 116 modified, added, removed, deleted = repo.status()[:4]
120 117 if modified or added or removed or deleted:
121 118 if not util.checkcase(repo.path) and not ui.quiet:
122 119 ui.warn(_("Purging on case-insensitive filesystems is not "
123 120 "fully supported.\n"))
124 121 raise util.Abort(_("outstanding uncommitted changes"))
125 122
126 123 cmdtable = {
127 124 'purge|clean':
128 125 (purge,
129 126 [('a', 'abort-on-err', None, _('abort if an error occurs')),
130 127 ('', 'all', None, _('purge ignored files too')),
131 128 ('f', 'force', None, _('purge even when there are uncommitted changes')),
132 129 ('p', 'print', None, _('print the file names instead of deleting them')),
133 130 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
134 131 ' (implies -p)')),
135 132 ] + commands.walkopts,
136 133 _('hg purge [OPTION]... [DIR]...'))
137 134 }
General Comments 0
You need to be logged in to leave comments. Login now