Mercurial > hg
annotate hgext/purge.py @ 20182:04036798ebed
branches: avoid unnecessary changectx.branch() calls
This requires reading from the changelog, which can be costly over NFS.
Note that this does not totally remove reading from the changelog; we
still do that when calling changectx.closesbranch(). That call will be
removed in a later patch.
Running hg branches on the PyPy repo (with 996) over a busy NFS server,
before this change:
$ time hg --profile branches > /dev/null
CallCount Recursive Total(s) Inline(s) module:lineno(function)
2042 0 2.2827 2.2827 <open>
2036 0 0.9840 0.9840 <method 'close' of 'file' objects>
2036 0 0.0464 0.0464 <method 'read' of 'file' objects>
5233 0 0.1985 0.0453 mercurial.repoview:161(changelog)
10462 0 0.0791 0.0314 mercurial.changelog:133(tip)
5233 0 0.0388 0.0176 mercurial.localrepo:26(__get__)
10462 0 0.0250 0.0126 <len>
5233 0 0.0059 0.0039 mercurial.repoview:112(filterrevs)
10462 0 0.0029 0.0029 <hash>
2034 0 0.0444 0.0444 <method 'seek' of 'file' objects>
5340 0 0.0390 0.0390 mercurial.revlog:296(rev)
2582 0 0.0371 0.0371 <zlib.decompress>
3155 0 0.1963 0.0366 mercurial.context:202(__init__)
3155 0 0.1238 0.0306 mercurial.repoview:161(changelog)
3155 0 0.0261 0.0080 mercurial.changelog:183(rev)
9465 0 0.0061 0.0061 <isinstance>
1096 0 0.0023 0.0023 <binascii.unhexlify>
4251 0 0.0014 0.0014 <len>
2059 0 3.7341 0.0332 mercurial.changelog:270(read)
2059 0 3.6304 0.0307 mercurial.revlog:907(revision)
2057 0 0.0262 0.0137 mercurial.changelog:28(decodeextra)
4118 0 0.0094 0.0094 <method 'split' of 'str' objects>
4118 0 0.0270 0.0048 mercurial.encoding:61(tolocal)
2059 0 0.0040 0.0040 <method 'index' of 'str' objects>
10462 0 0.0791 0.0314 mercurial.changelog:133(tip)
10462 0 0.0289 0.0207 mercurial.changelog:190(node)
10462 0 0.0188 0.0091 <len>
52433 20932 0.0478 0.0310 <len>
20932 0 0.0221 0.0168 mercurial.revlog:262(__len__)
2059 0 3.6304 0.0307 mercurial.revlog:907(revision)
real 0m4.361s
user 0m0.986s
sys 0m0.237s
After this change:
$ time hg --profile branches > /dev/null
CallCount Recursive Total(s) Inline(s) module:lineno(function)
1069 0 1.1098 1.1098 <open>
1063 0 0.4865 0.4865 <method 'close' of 'file' objects>
4122 0 0.1811 0.0404 mercurial.repoview:161(changelog)
8240 0 0.0712 0.0272 mercurial.changelog:133(tip)
4122 0 0.0378 0.0177 mercurial.localrepo:26(__get__)
8240 0 0.0221 0.0115 <len>
4122 0 0.0057 0.0033 mercurial.repoview:112(filterrevs)
8240 0 0.0025 0.0025 <hash>
3029 0 0.1979 0.0371 mercurial.context:202(__init__)
3029 0 0.1278 0.0310 mercurial.repoview:161(changelog)
3029 0 0.0230 0.0081 mercurial.changelog:183(rev)
9087 0 0.0061 0.0061 <isinstance>
1096 0 0.0026 0.0026 <binascii.unhexlify>
4125 0 0.0014 0.0014 <len>
4229 0 0.0337 0.0337 mercurial.revlog:296(rev)
1061 0 0.0296 0.0296 <method 'seek' of 'file' objects>
1063 0 0.0292 0.0292 <method 'read' of 'file' objects>
8240 0 0.0712 0.0272 mercurial.changelog:133(tip)
8240 0 0.0271 0.0196 mercurial.changelog:190(node)
8240 0 0.0169 0.0083 <len>
40476 16488 0.0422 0.0271 <len>
16488 0 0.0193 0.0152 mercurial.revlog:262(__len__)
1342 0 0.0241 0.0241 <zlib.decompress>
9445 0 0.0336 0.0224 mercurial.changelog:190(node)
9445 0 0.0112 0.0112 mercurial.revlog:317(node)
1074 0 1.9102 0.0224 mercurial.changelog:270(read)
1074 0 1.8397 0.0202 mercurial.revlog:907(revision)
1073 0 0.0187 0.0099 mercurial.changelog:28(decodeextra)
2148 0 0.0061 0.0061 <method 'split' of 'str' objects>
2148 0 0.0184 0.0034 mercurial.encoding:61(tolocal)
real 0m2.402s
user 0m0.735s
sys 0m0.177s
author | Brodie Rao <brodie@sf.io> |
---|---|
date | Fri, 15 Nov 2013 23:18:08 -0500 |
parents | 31c863bd21e8 |
children | f1c3ba167b0c |
rev | line source |
---|---|
2364 | 1 # Copyright (C) 2006 - Marco Barisione <marco@barisione.org> |
2 # | |
8936
1de6e7e1bb9f
change wiki/bts URLs to point to new hostname
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8934
diff
changeset
|
3 # This is a small extension for Mercurial (http://mercurial.selenic.com/) |
2364 | 4 # that removes files not known to mercurial |
5 # | |
9270
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
6 # This program was inspired by the "cvspurge" script contained in CVS |
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
7 # utilities (http://www.red-bean.com/cvsutils/). |
4154
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
8 # |
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
9 # For help on the usage of "hg purge" use: |
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
10 # hg help purge |
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
11 # |
2364 | 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 | |
14 # the Free Software Foundation; either version 2 of the License, or | |
15 # (at your option) any later version. | |
16 # | |
17 # This program is distributed in the hope that it will be useful, | |
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 # GNU General Public License for more details. | |
21 # | |
22 # You should have received a copy of the GNU General Public License | |
15782
7de7630053cb
Remove FSF mailing address from GPL headers
Martin Geisler <mg@aragost.com>
parents:
14671
diff
changeset
|
23 # along with this program; if not, see <http://www.gnu.org/licenses/>. |
2364 | 24 |
8934
9dda4c73fc3b
extensions: change descriptions for extensions providing a few commands
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8894
diff
changeset
|
25 '''command to delete untracked files from the working directory''' |
8873
e872ef2e6758
help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8866
diff
changeset
|
26 |
14322
a90131b85fd8
scmutil: drop aliases in cmdutil for match functions
Matt Mackall <mpm@selenic.com>
parents:
14310
diff
changeset
|
27 from mercurial import util, commands, cmdutil, scmutil |
4121
d250076824e3
Use the mercurial i18n infrastructure in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
4120
diff
changeset
|
28 from mercurial.i18n import _ |
8043
b777dd8f7836
purge: remove read-only files under Windows (issue583)
Patrick Mezard <pmezard@gmail.com>
parents:
7998
diff
changeset
|
29 import os, stat |
2364 | 30 |
14310
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
31 cmdtable = {} |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
32 command = cmdutil.command(cmdtable) |
16743
38caf405d010
hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents:
15782
diff
changeset
|
33 testedwith = 'internal' |
14310
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
34 |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
35 @command('purge|clean', |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
36 [('a', 'abort-on-err', None, _('abort if an error occurs')), |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
37 ('', 'all', None, _('purge ignored files too')), |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
38 ('p', 'print', None, _('print filenames instead of deleting them')), |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
39 ('0', 'print0', None, _('end filenames with NUL, for use with xargs' |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
40 ' (implies -p/--print)')), |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
41 ] + commands.walkopts, |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
42 _('hg purge [OPTION]... [DIR]...')) |
6573 | 43 def purge(ui, repo, *dirs, **opts): |
7605
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
44 '''removes files not tracked by Mercurial |
6573 | 45 |
9270
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
46 Delete files not known to Mercurial. This is useful to test local |
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
47 and uncommitted changes in an otherwise-clean source tree. |
6573 | 48 |
49 This means that purge will delete: | |
9215
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
50 |
10973
49a07f441496
Use hg role in help strings
Martin Geisler <mg@aragost.com>
parents:
9270
diff
changeset
|
51 - Unknown files: files marked with "?" by :hg:`status` |
9270
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
52 - Empty directories: in fact Mercurial ignores directories unless |
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
53 they contain files under source control management |
9215
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
54 |
6573 | 55 But it will leave untouched: |
9215
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
56 |
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
57 - Modified and unmodified tracked files |
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
58 - Ignored files (unless --all is specified) |
10973
49a07f441496
Use hg role in help strings
Martin Geisler <mg@aragost.com>
parents:
9270
diff
changeset
|
59 - New files added to the repository (with :hg:`add`) |
6573 | 60 |
61 If directories are given on the command line, only files in these | |
62 directories are considered. | |
63 | |
9270
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
64 Be careful with purge, as you could irreversibly delete some files |
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
65 you forgot to add to the repository. If you only want to print the |
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
66 list of files that this program would delete, use the --print |
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
67 option. |
6573 | 68 ''' |
69 act = not opts['print'] | |
6757 | 70 eol = '\n' |
71 if opts['print0']: | |
72 eol = '\0' | |
73 act = False # --print0 implies --print | |
2364 | 74 |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
75 def remove(remove_func, name): |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
76 if act: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
77 try: |
7570
e05aa73ce2b7
use repo.wjoin(f) instead of os.path.join(repo.root, f)
Martin Geisler <mg@daimi.au.dk>
parents:
7280
diff
changeset
|
78 remove_func(repo.wjoin(name)) |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6762
diff
changeset
|
79 except OSError: |
6757 | 80 m = _('%s cannot be removed') % name |
81 if opts['abort_on_err']: | |
82 raise util.Abort(m) | |
83 ui.warn(_('warning: %s\n') % m) | |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
84 else: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
85 ui.write('%s%s' % (name, eol)) |
4151
337010e50dcd
Use nested functions instead of object methods
Emanuele Aina <faina.mail@tiscali.it>
parents:
4150
diff
changeset
|
86 |
8043
b777dd8f7836
purge: remove read-only files under Windows (issue583)
Patrick Mezard <pmezard@gmail.com>
parents:
7998
diff
changeset
|
87 def removefile(path): |
8044
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
88 try: |
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
89 os.remove(path) |
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
90 except OSError: |
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
91 # read-only files cannot be unlinked under Windows |
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
92 s = os.stat(path) |
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
93 if (s.st_mode & stat.S_IWRITE) != 0: |
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
94 raise |
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
95 os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE) |
c1e2b7407dc3
purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents:
8043
diff
changeset
|
96 os.remove(path) |
8043
b777dd8f7836
purge: remove read-only files under Windows (issue583)
Patrick Mezard <pmezard@gmail.com>
parents:
7998
diff
changeset
|
97 |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
98 directories = [] |
14671
35c2cc322ba8
scmutil: switch match users to supplying contexts
Matt Mackall <mpm@selenic.com>
parents:
14322
diff
changeset
|
99 match = scmutil.match(repo[None], dirs, opts) |
19139
31c863bd21e8
purge: hook into match.explicitdir and traversedir
Siddharth Agarwal <sid0@fb.com>
parents:
16930
diff
changeset
|
100 match.explicitdir = match.traversedir = directories.append |
6754 | 101 status = repo.status(match=match, ignored=opts['all'], unknown=True) |
4147
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
102 |
8209
a1a5a57efe90
replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents:
8076
diff
changeset
|
103 for f in sorted(status[4] + status[5]): |
16930
9efe4a95c099
purge: lowercase messages
Martin Geisler <mg@aragost.com>
parents:
16743
diff
changeset
|
104 ui.note(_('removing file %s\n') % f) |
8043
b777dd8f7836
purge: remove read-only files under Windows (issue583)
Patrick Mezard <pmezard@gmail.com>
parents:
7998
diff
changeset
|
105 remove(removefile, f) |
4147
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
106 |
8209
a1a5a57efe90
replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents:
8076
diff
changeset
|
107 for f in sorted(directories, reverse=True): |
4463
a73cf208b2a0
purge: add --include and --exclude options
Emanuele Aina <em@nerd.ocracy.org>
parents:
4311
diff
changeset
|
108 if match(f) and not os.listdir(repo.wjoin(f)): |
16930
9efe4a95c099
purge: lowercase messages
Martin Geisler <mg@aragost.com>
parents:
16743
diff
changeset
|
109 ui.note(_('removing directory %s\n') % f) |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
110 remove(os.rmdir, f) |