##// END OF EJS Templates
purge: simplify safety net for case mangling filesystems...
Alexis S. L. Carvalho -
r5517:98d5f9b9 default
parent child Browse files
Show More
@@ -1,167 +1,152 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://www.selenic.com/mercurial)
3 # This is a small extension for Mercurial (http://www.selenic.com/mercurial)
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 # To enable the "purge" extension put these lines in your ~/.hgrc:
9 # To enable the "purge" extension put these lines in your ~/.hgrc:
10 # [extensions]
10 # [extensions]
11 # hgext.purge =
11 # hgext.purge =
12 #
12 #
13 # For help on the usage of "hg purge" use:
13 # For help on the usage of "hg purge" use:
14 # hg help purge
14 # hg help purge
15 #
15 #
16 # This program is free software; you can redistribute it and/or modify
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
19 # (at your option) any later version.
20 #
20 #
21 # This program is distributed in the hope that it will be useful,
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
24 # GNU General Public License for more details.
25 #
25 #
26 # You should have received a copy of the GNU General Public License
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29
29
30 from mercurial import hg, util, commands
30 from mercurial import hg, util, commands
31 from mercurial.i18n import _
31 from mercurial.i18n import _
32 import os
32 import os
33
33
34 def dopurge(ui, repo, dirs=None, act=True, ignored=False,
34 def dopurge(ui, repo, dirs=None, act=True, ignored=False,
35 abort_on_err=False, eol='\n',
35 abort_on_err=False, eol='\n',
36 force=False, include=None, exclude=None):
36 force=False, include=None, exclude=None):
37 def error(msg):
37 def error(msg):
38 if abort_on_err:
38 if abort_on_err:
39 raise util.Abort(msg)
39 raise util.Abort(msg)
40 else:
40 else:
41 ui.warn(_('warning: %s\n') % msg)
41 ui.warn(_('warning: %s\n') % msg)
42
42
43 def remove(remove_func, name):
43 def remove(remove_func, name):
44 if act:
44 if act:
45 try:
45 try:
46 remove_func(os.path.join(repo.root, name))
46 remove_func(os.path.join(repo.root, name))
47 except OSError, e:
47 except OSError, e:
48 error(_('%s cannot be removed') % name)
48 error(_('%s cannot be removed') % name)
49 else:
49 else:
50 ui.write('%s%s' % (name, eol))
50 ui.write('%s%s' % (name, eol))
51
51
52 if not force:
53 _check_fs(ui, repo)
54
52 directories = []
55 directories = []
53 files = []
56 files = []
54 missing = []
57 missing = []
55 roots, match, anypats = util.cmdmatcher(repo.root, repo.getcwd(), dirs,
58 roots, match, anypats = util.cmdmatcher(repo.root, repo.getcwd(), dirs,
56 include, exclude)
59 include, exclude)
57 for src, f, st in repo.dirstate.statwalk(files=roots, match=match,
60 for src, f, st in repo.dirstate.statwalk(files=roots, match=match,
58 ignored=ignored, directories=True):
61 ignored=ignored, directories=True):
59 if src == 'd':
62 if src == 'd':
60 directories.append(f)
63 directories.append(f)
61 elif src == 'm':
64 elif src == 'm':
62 missing.append(f)
65 missing.append(f)
63 elif src == 'f' and f not in repo.dirstate:
66 elif src == 'f' and f not in repo.dirstate:
64 files.append(f)
67 files.append(f)
65
68
66 _check_missing(ui, repo, missing, force)
67
68 directories.sort()
69 directories.sort()
69
70
70 for f in files:
71 for f in files:
71 if f not in repo.dirstate:
72 if f not in repo.dirstate:
72 ui.note(_('Removing file %s\n') % f)
73 ui.note(_('Removing file %s\n') % f)
73 remove(os.remove, f)
74 remove(os.remove, f)
74
75
75 for f in directories[::-1]:
76 for f in directories[::-1]:
76 if match(f) and not os.listdir(repo.wjoin(f)):
77 if match(f) and not os.listdir(repo.wjoin(f)):
77 ui.note(_('Removing directory %s\n') % f)
78 ui.note(_('Removing directory %s\n') % f)
78 remove(os.rmdir, f)
79 remove(os.rmdir, f)
79
80
80 def _check_missing(ui, repo, missing, force=False):
81 def _check_fs(ui, repo):
81 """Abort if there is the chance of having problems with name-mangling fs
82 """Abort if there is the chance of having problems with name-mangling fs
82
83
83 In a name mangling filesystem (e.g. a case insensitive one)
84 In a name mangling filesystem (e.g. a case insensitive one)
84 dirstate.walk() can yield filenames different from the ones
85 dirstate.walk() can yield filenames different from the ones
85 stored in the dirstate. This already confuses the status and
86 stored in the dirstate. This already confuses the status and
86 add commands, but with purge this may cause data loss.
87 add commands, but with purge this may cause data loss.
87
88
88 To prevent this, _check_missing will abort if there are missing
89 To prevent this, this function will abort if there are uncommitted
89 files. The force option will let the user skip the check if he
90 changes.
90 knows it is safe.
91 """
91
92 Even with the force option this function will check if any of the
93 missing files is still available in the working dir: if so there
94 may be some problem with the underlying filesystem, so it
95 aborts unconditionally."""
96
97 found = [f for f in missing if util.lexists(repo.wjoin(f))]
98
92
99 if found:
93 # We can't use (files, match) to do a partial walk here - we wouldn't
100 if not ui.quiet:
94 # notice a modified README file if the user ran "hg purge readme"
101 ui.warn(_("The following tracked files weren't listed by the "
95 modified, added, removed, deleted = repo.status()[:4]
102 "filesystem, but could still be found:\n"))
96 if modified or added or removed or deleted:
103 for f in found:
97 if not util.checkfolding(repo.path) and not ui.quiet:
104 ui.warn("%s\n" % f)
98 ui.warn(_("Purging on name mangling filesystems is not "
105 if util.checkfolding(repo.path):
99 "fully supported.\n"))
106 ui.warn(_("This is probably due to a case-insensitive "
100 raise util.Abort(_("outstanding uncommitted changes"))
107 "filesystem\n"))
108 raise util.Abort(_("purging on name mangling filesystems is not "
109 "yet fully supported"))
110
111 if missing and not force:
112 raise util.Abort(_("there are missing files in the working dir and "
113 "purge still has problems with them due to name "
114 "mangling filesystems. "
115 "Use --force if you know what you are doing"))
116
101
117
102
118 def purge(ui, repo, *dirs, **opts):
103 def purge(ui, repo, *dirs, **opts):
119 '''removes files not tracked by mercurial
104 '''removes files not tracked by mercurial
120
105
121 Delete files not known to mercurial, this is useful to test local and
106 Delete files not known to mercurial, this is useful to test local and
122 uncommitted changes in the otherwise clean source tree.
107 uncommitted changes in the otherwise clean source tree.
123
108
124 This means that purge will delete:
109 This means that purge will delete:
125 - Unknown files: files marked with "?" by "hg status"
110 - Unknown files: files marked with "?" by "hg status"
126 - Ignored files: files usually ignored by Mercurial because they match
111 - Ignored files: files usually ignored by Mercurial because they match
127 a pattern in a ".hgignore" file
112 a pattern in a ".hgignore" file
128 - Empty directories: in fact Mercurial ignores directories unless they
113 - Empty directories: in fact Mercurial ignores directories unless they
129 contain files under source control managment
114 contain files under source control managment
130 But it will leave untouched:
115 But it will leave untouched:
131 - Unmodified tracked files
116 - Unmodified tracked files
132 - Modified tracked files
117 - Modified tracked files
133 - New files added to the repository (with "hg add")
118 - New files added to the repository (with "hg add")
134
119
135 If directories are given on the command line, only files in these
120 If directories are given on the command line, only files in these
136 directories are considered.
121 directories are considered.
137
122
138 Be careful with purge, you could irreversibly delete some files you
123 Be careful with purge, you could irreversibly delete some files you
139 forgot to add to the repository. If you only want to print the list of
124 forgot to add to the repository. If you only want to print the list of
140 files that this program would delete use the --print option.
125 files that this program would delete use the --print option.
141 '''
126 '''
142 act = not opts['print']
127 act = not opts['print']
143 ignored = bool(opts['all'])
128 ignored = bool(opts['all'])
144 abort_on_err = bool(opts['abort_on_err'])
129 abort_on_err = bool(opts['abort_on_err'])
145 eol = opts['print0'] and '\0' or '\n'
130 eol = opts['print0'] and '\0' or '\n'
146 if eol == '\0':
131 if eol == '\0':
147 # --print0 implies --print
132 # --print0 implies --print
148 act = False
133 act = False
149 force = bool(opts['force'])
134 force = bool(opts['force'])
150 include = opts['include']
135 include = opts['include']
151 exclude = opts['exclude']
136 exclude = opts['exclude']
152 dopurge(ui, repo, dirs, act, ignored, abort_on_err,
137 dopurge(ui, repo, dirs, act, ignored, abort_on_err,
153 eol, force, include, exclude)
138 eol, force, include, exclude)
154
139
155
140
156 cmdtable = {
141 cmdtable = {
157 'purge|clean':
142 'purge|clean':
158 (purge,
143 (purge,
159 [('a', 'abort-on-err', None, _('abort if an error occurs')),
144 [('a', 'abort-on-err', None, _('abort if an error occurs')),
160 ('', 'all', None, _('purge ignored files too')),
145 ('', 'all', None, _('purge ignored files too')),
161 ('f', 'force', None, _('purge even when missing files are detected')),
146 ('f', 'force', None, _('purge even when there are uncommitted changes')),
162 ('p', 'print', None, _('print the file names instead of deleting them')),
147 ('p', 'print', None, _('print the file names instead of deleting them')),
163 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
148 ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
164 ' (implies -p)')),
149 ' (implies -p)')),
165 ] + commands.walkopts,
150 ] + commands.walkopts,
166 _('hg purge [OPTION]... [DIR]...'))
151 _('hg purge [OPTION]... [DIR]...'))
167 }
152 }
@@ -1,133 +1,140 b''
1 #!/bin/sh
1 #!/bin/sh
2
2
3 cat <<EOF >> $HGRCPATH
3 cat <<EOF >> $HGRCPATH
4 [extensions]
4 [extensions]
5 hgext.purge=
5 hgext.purge=
6 EOF
6 EOF
7
7
8 echo % init
8 echo % init
9 hg init t
9 hg init t
10 cd t
10 cd t
11
11
12 echo % setup
12 echo % setup
13 echo r1 > r1
13 echo r1 > r1
14 hg ci -qAmr1 -d'0 0'
14 hg ci -qAmr1 -d'0 0'
15 mkdir directory
15 mkdir directory
16 echo r2 > directory/r2
16 echo r2 > directory/r2
17 hg ci -qAmr2 -d'1 0'
17 hg ci -qAmr2 -d'1 0'
18 echo 'ignored' > .hgignore
18 echo 'ignored' > .hgignore
19 hg ci -qAmr3 -d'2 0'
19 hg ci -qAmr3 -d'2 0'
20
20
21 echo % delete an empty directory
21 echo % delete an empty directory
22 mkdir empty_dir
22 mkdir empty_dir
23 hg purge -p
23 hg purge -p
24 hg purge -v
24 hg purge -v
25 ls
25 ls
26
26
27 echo % delete an untracked directory
27 echo % delete an untracked directory
28 mkdir untracked_dir
28 mkdir untracked_dir
29 touch untracked_dir/untracked_file1
29 touch untracked_dir/untracked_file1
30 touch untracked_dir/untracked_file2
30 touch untracked_dir/untracked_file2
31 hg purge -p
31 hg purge -p
32 hg purge -v
32 hg purge -v
33 ls
33 ls
34
34
35 echo % delete an untracked file
35 echo % delete an untracked file
36 touch untracked_file
36 touch untracked_file
37 hg purge -p
37 hg purge -p
38 hg purge -v
38 hg purge -v
39 ls
39 ls
40
40
41 echo % delete an untracked file in a tracked directory
41 echo % delete an untracked file in a tracked directory
42 touch directory/untracked_file
42 touch directory/untracked_file
43 hg purge -p
43 hg purge -p
44 hg purge -v
44 hg purge -v
45 ls
45 ls
46
46
47 echo % delete nested directories
47 echo % delete nested directories
48 mkdir -p untracked_directory/nested_directory
48 mkdir -p untracked_directory/nested_directory
49 hg purge -p
49 hg purge -p
50 hg purge -v
50 hg purge -v
51 ls
51 ls
52
52
53 echo % delete nested directories from a subdir
53 echo % delete nested directories from a subdir
54 mkdir -p untracked_directory/nested_directory
54 mkdir -p untracked_directory/nested_directory
55 cd directory
55 cd directory
56 hg purge -p
56 hg purge -p
57 hg purge -v
57 hg purge -v
58 cd ..
58 cd ..
59 ls
59 ls
60
60
61 echo % delete only part of the tree
61 echo % delete only part of the tree
62 mkdir -p untracked_directory/nested_directory
62 mkdir -p untracked_directory/nested_directory
63 touch directory/untracked_file
63 touch directory/untracked_file
64 cd directory
64 cd directory
65 hg purge -p ../untracked_directory
65 hg purge -p ../untracked_directory
66 hg purge -v ../untracked_directory
66 hg purge -v ../untracked_directory
67 cd ..
67 cd ..
68 ls
68 ls
69 ls directory/untracked_file
69 ls directory/untracked_file
70 rm directory/untracked_file
70 rm directory/untracked_file
71
71
72 echo % skip ignored files if --all not specified
72 echo % skip ignored files if --all not specified
73 touch ignored
73 touch ignored
74 hg purge -p
74 hg purge -p
75 hg purge -v
75 hg purge -v
76 ls
76 ls
77 hg purge -p --all
77 hg purge -p --all
78 hg purge -v --all
78 hg purge -v --all
79 ls
79 ls
80
80
81 echo % abort with missing files until we support name mangling filesystems
81 echo % abort with missing files until we support name mangling filesystems
82 touch untracked_file
82 touch untracked_file
83 rm r1
83 rm r1
84 # hide error messages to avoid changing the output when the text changes
84 # hide error messages to avoid changing the output when the text changes
85 hg purge -p 2> /dev/null
85 hg purge -p 2> /dev/null
86 if [ $? -ne 0 ]; then
86 if [ $? -ne 0 ]; then
87 echo "refused to run"
87 echo "refused to run"
88 fi
88 fi
89 if [ -f untracked_file ]; then
89 if [ -f untracked_file ]; then
90 echo "untracked_file still around"
90 echo "untracked_file still around"
91 fi
91 fi
92 hg purge -p --force
92 hg purge -p --force
93 hg purge -v 2> /dev/null
93 hg purge -v 2> /dev/null
94 if [ $? -ne 0 ]; then
94 if [ $? -ne 0 ]; then
95 echo "refused to run"
95 echo "refused to run"
96 fi
96 fi
97 if [ -f untracked_file ]; then
97 if [ -f untracked_file ]; then
98 echo "untracked_file still around"
98 echo "untracked_file still around"
99 fi
99 fi
100 hg purge -v --force
100 hg purge -v --force
101 hg revert --all --quiet
101 hg revert --all --quiet
102 ls
102 ls
103
103
104 echo '% tracked file in ignored directory (issue621)'
105 echo directory >> .hgignore
106 hg ci -m 'ignore directory'
107 touch untracked_file
108 hg purge -p
109 hg purge -v
110
104 echo % skip excluded files
111 echo % skip excluded files
105 touch excluded_file
112 touch excluded_file
106 hg purge -p -X excluded_file
113 hg purge -p -X excluded_file
107 hg purge -v -X excluded_file
114 hg purge -v -X excluded_file
108 ls
115 ls
109 rm excluded_file
116 rm excluded_file
110
117
111 echo % skip files in excluded dirs
118 echo % skip files in excluded dirs
112 mkdir excluded_dir
119 mkdir excluded_dir
113 touch excluded_dir/file
120 touch excluded_dir/file
114 hg purge -p -X excluded_dir
121 hg purge -p -X excluded_dir
115 hg purge -v -X excluded_dir
122 hg purge -v -X excluded_dir
116 ls
123 ls
117 ls excluded_dir
124 ls excluded_dir
118 rm -R excluded_dir
125 rm -R excluded_dir
119
126
120 echo % skip excluded empty dirs
127 echo % skip excluded empty dirs
121 mkdir excluded_dir
128 mkdir excluded_dir
122 hg purge -p -X excluded_dir
129 hg purge -p -X excluded_dir
123 hg purge -v -X excluded_dir
130 hg purge -v -X excluded_dir
124 ls
131 ls
125 rmdir excluded_dir
132 rmdir excluded_dir
126
133
127 echo % skip patterns
134 echo % skip patterns
128 mkdir .svn
135 mkdir .svn
129 touch .svn/foo
136 touch .svn/foo
130 mkdir directory/.svn
137 mkdir directory/.svn
131 touch directory/.svn/foo
138 touch directory/.svn/foo
132 hg purge -p -X .svn -X '*/.svn'
139 hg purge -p -X .svn -X '*/.svn'
133 hg purge -p -X re:.*.svn
140 hg purge -p -X re:.*.svn
@@ -1,75 +1,78 b''
1 % init
1 % init
2 % setup
2 % setup
3 % delete an empty directory
3 % delete an empty directory
4 empty_dir
4 empty_dir
5 Removing directory empty_dir
5 Removing directory empty_dir
6 directory
6 directory
7 r1
7 r1
8 % delete an untracked directory
8 % delete an untracked directory
9 untracked_dir/untracked_file1
9 untracked_dir/untracked_file1
10 untracked_dir/untracked_file2
10 untracked_dir/untracked_file2
11 Removing file untracked_dir/untracked_file1
11 Removing file untracked_dir/untracked_file1
12 Removing file untracked_dir/untracked_file2
12 Removing file untracked_dir/untracked_file2
13 Removing directory untracked_dir
13 Removing directory untracked_dir
14 directory
14 directory
15 r1
15 r1
16 % delete an untracked file
16 % delete an untracked file
17 untracked_file
17 untracked_file
18 Removing file untracked_file
18 Removing file untracked_file
19 directory
19 directory
20 r1
20 r1
21 % delete an untracked file in a tracked directory
21 % delete an untracked file in a tracked directory
22 directory/untracked_file
22 directory/untracked_file
23 Removing file directory/untracked_file
23 Removing file directory/untracked_file
24 directory
24 directory
25 r1
25 r1
26 % delete nested directories
26 % delete nested directories
27 untracked_directory/nested_directory
27 untracked_directory/nested_directory
28 Removing directory untracked_directory/nested_directory
28 Removing directory untracked_directory/nested_directory
29 Removing directory untracked_directory
29 Removing directory untracked_directory
30 directory
30 directory
31 r1
31 r1
32 % delete nested directories from a subdir
32 % delete nested directories from a subdir
33 untracked_directory/nested_directory
33 untracked_directory/nested_directory
34 Removing directory untracked_directory/nested_directory
34 Removing directory untracked_directory/nested_directory
35 Removing directory untracked_directory
35 Removing directory untracked_directory
36 directory
36 directory
37 r1
37 r1
38 % delete only part of the tree
38 % delete only part of the tree
39 untracked_directory/nested_directory
39 untracked_directory/nested_directory
40 Removing directory untracked_directory/nested_directory
40 Removing directory untracked_directory/nested_directory
41 Removing directory untracked_directory
41 Removing directory untracked_directory
42 directory
42 directory
43 r1
43 r1
44 directory/untracked_file
44 directory/untracked_file
45 % skip ignored files if --all not specified
45 % skip ignored files if --all not specified
46 directory
46 directory
47 ignored
47 ignored
48 r1
48 r1
49 ignored
49 ignored
50 Removing file ignored
50 Removing file ignored
51 directory
51 directory
52 r1
52 r1
53 % abort with missing files until we support name mangling filesystems
53 % abort with missing files until we support name mangling filesystems
54 refused to run
54 refused to run
55 untracked_file still around
55 untracked_file still around
56 untracked_file
56 untracked_file
57 refused to run
57 refused to run
58 untracked_file still around
58 untracked_file still around
59 Removing file untracked_file
59 Removing file untracked_file
60 directory
60 directory
61 r1
61 r1
62 % tracked file in ignored directory (issue621)
63 untracked_file
64 Removing file untracked_file
62 % skip excluded files
65 % skip excluded files
63 directory
66 directory
64 excluded_file
67 excluded_file
65 r1
68 r1
66 % skip files in excluded dirs
69 % skip files in excluded dirs
67 directory
70 directory
68 excluded_dir
71 excluded_dir
69 r1
72 r1
70 file
73 file
71 % skip excluded empty dirs
74 % skip excluded empty dirs
72 directory
75 directory
73 excluded_dir
76 excluded_dir
74 r1
77 r1
75 % skip patterns
78 % skip patterns
General Comments 0
You need to be logged in to leave comments. Login now