##// END OF EJS Templates
purge: avoid duplicate output for --print (issue4092)...
anuraggoel -
r20565:f1c3ba16 stable
parent child Browse files
Show More
@@ -1,110 +1,112
1 1 # Copyright (C) 2006 - Marco Barisione <marco@barisione.org>
2 2 #
3 3 # This is a small extension for Mercurial (http://mercurial.selenic.com/)
4 4 # that removes files not known to mercurial
5 5 #
6 6 # This program was inspired by the "cvspurge" script contained in CVS
7 7 # utilities (http://www.red-bean.com/cvsutils/).
8 8 #
9 9 # For help on the usage of "hg purge" use:
10 10 # hg help purge
11 11 #
12 12 # This program is free software; you can redistribute it and/or modify
13 13 # it under the terms of the GNU General Public License as published by
14 14 # the Free Software Foundation; either version 2 of the License, or
15 15 # (at your option) any later version.
16 16 #
17 17 # This program is distributed in the hope that it will be useful,
18 18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 20 # GNU General Public License for more details.
21 21 #
22 22 # You should have received a copy of the GNU General Public License
23 23 # along with this program; if not, see <http://www.gnu.org/licenses/>.
24 24
25 25 '''command to delete untracked files from the working directory'''
26 26
27 27 from mercurial import util, commands, cmdutil, scmutil
28 28 from mercurial.i18n import _
29 29 import os, stat
30 30
31 31 cmdtable = {}
32 32 command = cmdutil.command(cmdtable)
33 33 testedwith = 'internal'
34 34
35 35 @command('purge|clean',
36 36 [('a', 'abort-on-err', None, _('abort if an error occurs')),
37 37 ('', 'all', None, _('purge ignored files too')),
38 38 ('p', 'print', None, _('print filenames instead of deleting them')),
39 39 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
40 40 ' (implies -p/--print)')),
41 41 ] + commands.walkopts,
42 42 _('hg purge [OPTION]... [DIR]...'))
43 43 def purge(ui, repo, *dirs, **opts):
44 44 '''removes files not tracked by Mercurial
45 45
46 46 Delete files not known to Mercurial. This is useful to test local
47 47 and uncommitted changes in an otherwise-clean source tree.
48 48
49 49 This means that purge will delete:
50 50
51 51 - Unknown files: files marked with "?" by :hg:`status`
52 52 - Empty directories: in fact Mercurial ignores directories unless
53 53 they contain files under source control management
54 54
55 55 But it will leave untouched:
56 56
57 57 - Modified and unmodified tracked files
58 58 - Ignored files (unless --all is specified)
59 59 - New files added to the repository (with :hg:`add`)
60 60
61 61 If directories are given on the command line, only files in these
62 62 directories are considered.
63 63
64 64 Be careful with purge, as you could irreversibly delete some files
65 65 you forgot to add to the repository. If you only want to print the
66 66 list of files that this program would delete, use the --print
67 67 option.
68 68 '''
69 69 act = not opts['print']
70 70 eol = '\n'
71 71 if opts['print0']:
72 72 eol = '\0'
73 73 act = False # --print0 implies --print
74 74
75 75 def remove(remove_func, name):
76 76 if act:
77 77 try:
78 78 remove_func(repo.wjoin(name))
79 79 except OSError:
80 80 m = _('%s cannot be removed') % name
81 81 if opts['abort_on_err']:
82 82 raise util.Abort(m)
83 83 ui.warn(_('warning: %s\n') % m)
84 84 else:
85 85 ui.write('%s%s' % (name, eol))
86 86
87 87 def removefile(path):
88 88 try:
89 89 os.remove(path)
90 90 except OSError:
91 91 # read-only files cannot be unlinked under Windows
92 92 s = os.stat(path)
93 93 if (s.st_mode & stat.S_IWRITE) != 0:
94 94 raise
95 95 os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE)
96 96 os.remove(path)
97 97
98 98 directories = []
99 99 match = scmutil.match(repo[None], dirs, opts)
100 100 match.explicitdir = match.traversedir = directories.append
101 101 status = repo.status(match=match, ignored=opts['all'], unknown=True)
102 102
103 103 for f in sorted(status[4] + status[5]):
104 ui.note(_('removing file %s\n') % f)
104 if act:
105 ui.note(_('removing file %s\n') % f)
105 106 remove(removefile, f)
106 107
107 108 for f in sorted(directories, reverse=True):
108 109 if match(f) and not os.listdir(repo.wjoin(f)):
109 ui.note(_('removing directory %s\n') % f)
110 if act:
111 ui.note(_('removing directory %s\n') % f)
110 112 remove(os.rmdir, f)
@@ -1,218 +1,218
1 1 $ cat <<EOF >> $HGRCPATH
2 2 > [extensions]
3 3 > purge =
4 4 > EOF
5 5
6 6 init
7 7
8 8 $ hg init t
9 9 $ cd t
10 10
11 11 setup
12 12
13 13 $ echo r1 > r1
14 14 $ hg ci -qAmr1 -d'0 0'
15 15 $ mkdir directory
16 16 $ echo r2 > directory/r2
17 17 $ hg ci -qAmr2 -d'1 0'
18 18 $ echo 'ignored' > .hgignore
19 19 $ hg ci -qAmr3 -d'2 0'
20 20
21 21 delete an empty directory
22 22
23 23 $ mkdir empty_dir
24 $ hg purge -p
24 $ hg purge -p -v
25 25 empty_dir
26 26 $ hg purge -v
27 27 removing directory empty_dir
28 28 $ ls
29 29 directory
30 30 r1
31 31
32 32 delete an untracked directory
33 33
34 34 $ mkdir untracked_dir
35 35 $ touch untracked_dir/untracked_file1
36 36 $ touch untracked_dir/untracked_file2
37 37 $ hg purge -p
38 38 untracked_dir/untracked_file1
39 39 untracked_dir/untracked_file2
40 40 $ hg purge -v
41 41 removing file untracked_dir/untracked_file1
42 42 removing file untracked_dir/untracked_file2
43 43 removing directory untracked_dir
44 44 $ ls
45 45 directory
46 46 r1
47 47
48 48 delete an untracked file
49 49
50 50 $ touch untracked_file
51 51 $ touch untracked_file_readonly
52 52 $ python <<EOF
53 53 > import os, stat
54 54 > f= 'untracked_file_readonly'
55 55 > os.chmod(f, stat.S_IMODE(os.stat(f).st_mode) & ~stat.S_IWRITE)
56 56 > EOF
57 57 $ hg purge -p
58 58 untracked_file
59 59 untracked_file_readonly
60 60 $ hg purge -v
61 61 removing file untracked_file
62 62 removing file untracked_file_readonly
63 63 $ ls
64 64 directory
65 65 r1
66 66
67 67 delete an untracked file in a tracked directory
68 68
69 69 $ touch directory/untracked_file
70 70 $ hg purge -p
71 71 directory/untracked_file
72 72 $ hg purge -v
73 73 removing file directory/untracked_file
74 74 $ ls
75 75 directory
76 76 r1
77 77
78 78 delete nested directories
79 79
80 80 $ mkdir -p untracked_directory/nested_directory
81 81 $ hg purge -p
82 82 untracked_directory/nested_directory
83 83 $ hg purge -v
84 84 removing directory untracked_directory/nested_directory
85 85 removing directory untracked_directory
86 86 $ ls
87 87 directory
88 88 r1
89 89
90 90 delete nested directories from a subdir
91 91
92 92 $ mkdir -p untracked_directory/nested_directory
93 93 $ cd directory
94 94 $ hg purge -p
95 95 untracked_directory/nested_directory
96 96 $ hg purge -v
97 97 removing directory untracked_directory/nested_directory
98 98 removing directory untracked_directory
99 99 $ cd ..
100 100 $ ls
101 101 directory
102 102 r1
103 103
104 104 delete only part of the tree
105 105
106 106 $ mkdir -p untracked_directory/nested_directory
107 107 $ touch directory/untracked_file
108 108 $ cd directory
109 109 $ hg purge -p ../untracked_directory
110 110 untracked_directory/nested_directory
111 111 $ hg purge -v ../untracked_directory
112 112 removing directory untracked_directory/nested_directory
113 113 removing directory untracked_directory
114 114 $ cd ..
115 115 $ ls
116 116 directory
117 117 r1
118 118 $ ls directory/untracked_file
119 119 directory/untracked_file
120 120 $ rm directory/untracked_file
121 121
122 122 skip ignored files if --all not specified
123 123
124 124 $ touch ignored
125 125 $ hg purge -p
126 126 $ hg purge -v
127 127 $ ls
128 128 directory
129 129 ignored
130 130 r1
131 131 $ hg purge -p --all
132 132 ignored
133 133 $ hg purge -v --all
134 134 removing file ignored
135 135 $ ls
136 136 directory
137 137 r1
138 138
139 139 abort with missing files until we support name mangling filesystems
140 140
141 141 $ touch untracked_file
142 142 $ rm r1
143 143
144 144 hide error messages to avoid changing the output when the text changes
145 145
146 146 $ hg purge -p 2> /dev/null
147 147 untracked_file
148 148 $ hg st
149 149 ! r1
150 150 ? untracked_file
151 151
152 152 $ hg purge -p
153 153 untracked_file
154 154 $ hg purge -v 2> /dev/null
155 155 removing file untracked_file
156 156 $ hg st
157 157 ! r1
158 158
159 159 $ hg purge -v
160 160 $ hg revert --all --quiet
161 161 $ hg st -a
162 162
163 163 tracked file in ignored directory (issue621)
164 164
165 165 $ echo directory >> .hgignore
166 166 $ hg ci -m 'ignore directory'
167 167 $ touch untracked_file
168 168 $ hg purge -p
169 169 untracked_file
170 170 $ hg purge -v
171 171 removing file untracked_file
172 172
173 173 skip excluded files
174 174
175 175 $ touch excluded_file
176 176 $ hg purge -p -X excluded_file
177 177 $ hg purge -v -X excluded_file
178 178 $ ls
179 179 directory
180 180 excluded_file
181 181 r1
182 182 $ rm excluded_file
183 183
184 184 skip files in excluded dirs
185 185
186 186 $ mkdir excluded_dir
187 187 $ touch excluded_dir/file
188 188 $ hg purge -p -X excluded_dir
189 189 $ hg purge -v -X excluded_dir
190 190 $ ls
191 191 directory
192 192 excluded_dir
193 193 r1
194 194 $ ls excluded_dir
195 195 file
196 196 $ rm -R excluded_dir
197 197
198 198 skip excluded empty dirs
199 199
200 200 $ mkdir excluded_dir
201 201 $ hg purge -p -X excluded_dir
202 202 $ hg purge -v -X excluded_dir
203 203 $ ls
204 204 directory
205 205 excluded_dir
206 206 r1
207 207 $ rmdir excluded_dir
208 208
209 209 skip patterns
210 210
211 211 $ mkdir .svn
212 212 $ touch .svn/foo
213 213 $ mkdir directory/.svn
214 214 $ touch directory/.svn/foo
215 215 $ hg purge -p -X .svn -X '*/.svn'
216 216 $ hg purge -p -X re:.*.svn
217 217
218 218 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now