Mercurial > hg
annotate hgext/purge.py @ 37141:eb4a23382e18
locate: avoid using "rev" variable name for nodeid
Also, drop silly "ctx = repo[ctx.node()]".
Differential Revision: https://phab.mercurial-scm.org/D2961
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Wed, 28 Mar 2018 15:30:09 -0700 |
parents | 5a3f8da663e5 |
children | 7fea205fd5dc |
rev | line source |
---|---|
2364 | 1 # Copyright (C) 2006 - Marco Barisione <marco@barisione.org> |
2 # | |
26421
4b0fc75f9403
urls: bulk-change primary website URLs
Matt Mackall <mpm@selenic.com>
parents:
25186
diff
changeset
|
3 # This is a small extension for Mercurial (https://mercurial-scm.org/) |
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''' |
28382
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
26 from __future__ import absolute_import |
8873
e872ef2e6758
help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8866
diff
changeset
|
27 |
28382
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
28 import os |
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
29 |
29205
a0939666b836
py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents:
28382
diff
changeset
|
30 from mercurial.i18n import _ |
28382
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
31 from mercurial import ( |
32375
04baab18d60a
commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32337
diff
changeset
|
32 cmdutil, |
28382
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
33 error, |
36571
5a3f8da663e5
purge: apply byteskwargs to opts, fixing all python3 issues here
Augie Fackler <augie@google.com>
parents:
32375
diff
changeset
|
34 pycompat, |
32337
46ba2cdda476
registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
29841
diff
changeset
|
35 registrar, |
28382
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
36 scmutil, |
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
37 util, |
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
38 ) |
2364 | 39 |
14310
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
40 cmdtable = {} |
32337
46ba2cdda476
registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
29841
diff
changeset
|
41 command = registrar.command(cmdtable) |
29841
d5883fd055c6
extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents:
29222
diff
changeset
|
42 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
25186
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
22920
diff
changeset
|
43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
22920
diff
changeset
|
44 # be specifying the version(s) of Mercurial they are tested with, or |
80c5b2666a96
extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents:
22920
diff
changeset
|
45 # leave the attribute unspecified. |
29841
d5883fd055c6
extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents:
29222
diff
changeset
|
46 testedwith = 'ships-with-hg-core' |
14310
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
47 |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
48 @command('purge|clean', |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
49 [('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
|
50 ('', 'all', None, _('purge ignored files too')), |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
51 ('', 'dirs', None, _('purge empty directories')), |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
52 ('', 'files', None, _('purge files')), |
14310
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
53 ('p', 'print', None, _('print filenames instead of deleting them')), |
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
54 ('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
|
55 ' (implies -p/--print)')), |
32375
04baab18d60a
commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32337
diff
changeset
|
56 ] + cmdutil.walkopts, |
14310
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
57 _('hg purge [OPTION]... [DIR]...')) |
6573 | 58 def purge(ui, repo, *dirs, **opts): |
7605
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
59 '''removes files not tracked by Mercurial |
6573 | 60 |
9270
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
61 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
|
62 and uncommitted changes in an otherwise-clean source tree. |
6573 | 63 |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
64 This means that purge will delete the following by default: |
9215
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
65 |
10973
49a07f441496
Use hg role in help strings
Martin Geisler <mg@aragost.com>
parents:
9270
diff
changeset
|
66 - 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
|
67 - Empty directories: in fact Mercurial ignores directories unless |
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
68 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
|
69 |
6573 | 70 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
|
71 |
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
72 - Modified and unmodified tracked files |
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
73 - Ignored files (unless --all is specified) |
10973
49a07f441496
Use hg role in help strings
Martin Geisler <mg@aragost.com>
parents:
9270
diff
changeset
|
74 - New files added to the repository (with :hg:`add`) |
6573 | 75 |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
76 The --files and --dirs options can be used to direct purge to delete |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
77 only files, only directories, or both. If neither option is given, |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
78 both will be deleted. |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
79 |
6573 | 80 If directories are given on the command line, only files in these |
81 directories are considered. | |
82 | |
9270
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 option. |
6573 | 87 ''' |
36571
5a3f8da663e5
purge: apply byteskwargs to opts, fixing all python3 issues here
Augie Fackler <augie@google.com>
parents:
32375
diff
changeset
|
88 opts = pycompat.byteskwargs(opts) |
29222
ed4bd789fc55
purge: use opts.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29205
diff
changeset
|
89 act = not opts.get('print') |
6757 | 90 eol = '\n' |
29222
ed4bd789fc55
purge: use opts.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29205
diff
changeset
|
91 if opts.get('print0'): |
6757 | 92 eol = '\0' |
93 act = False # --print0 implies --print | |
29222
ed4bd789fc55
purge: use opts.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29205
diff
changeset
|
94 removefiles = opts.get('files') |
ed4bd789fc55
purge: use opts.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29205
diff
changeset
|
95 removedirs = opts.get('dirs') |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
96 if not removefiles and not removedirs: |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
97 removefiles = True |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
98 removedirs = True |
2364 | 99 |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
100 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
|
101 if act: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
102 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
|
103 remove_func(repo.wjoin(name)) |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6762
diff
changeset
|
104 except OSError: |
6757 | 105 m = _('%s cannot be removed') % name |
29222
ed4bd789fc55
purge: use opts.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29205
diff
changeset
|
106 if opts.get('abort_on_err'): |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26421
diff
changeset
|
107 raise error.Abort(m) |
6757 | 108 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
|
109 else: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
110 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
|
111 |
14671
35c2cc322ba8
scmutil: switch match users to supplying contexts
Matt Mackall <mpm@selenic.com>
parents:
14322
diff
changeset
|
112 match = scmutil.match(repo[None], dirs, opts) |
22265
fe22d86a8992
purge: avoid full walks when directories aren't purged
Siddharth Agarwal <sid0@fb.com>
parents:
21994
diff
changeset
|
113 if removedirs: |
fe22d86a8992
purge: avoid full walks when directories aren't purged
Siddharth Agarwal <sid0@fb.com>
parents:
21994
diff
changeset
|
114 directories = [] |
fe22d86a8992
purge: avoid full walks when directories aren't purged
Siddharth Agarwal <sid0@fb.com>
parents:
21994
diff
changeset
|
115 match.explicitdir = match.traversedir = directories.append |
29222
ed4bd789fc55
purge: use opts.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29205
diff
changeset
|
116 status = repo.status(match=match, ignored=opts.get('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
|
117 |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
118 if removefiles: |
22920
e049338d1a7b
purge: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@gmail.com>
parents:
22265
diff
changeset
|
119 for f in sorted(status.unknown + status.ignored): |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
120 if act: |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
121 ui.note(_('removing file %s\n') % f) |
21983
52d34d5415c9
purge: prefer util.unlink instead over own removefile
Christian Ebert <blacktrash@gmx.net>
parents:
21853
diff
changeset
|
122 remove(util.unlink, 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
|
123 |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
124 if removedirs: |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
125 for f in sorted(directories, reverse=True): |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
126 if match(f) and not os.listdir(repo.wjoin(f)): |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
127 if act: |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
128 ui.note(_('removing directory %s\n') % f) |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
129 remove(os.rmdir, f) |