Mercurial > hg
annotate hgext/purge.py @ 46366:135056e8b5a8
purge: add a --confirm option
The options provide a prompt to the user before permanent deletion are made.
The prompt is currently not aware of directory deletion. I'll fix this in the
next changesets.
Differential Revision: https://phab.mercurial-scm.org/D9818
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 18 Jan 2021 10:24:03 +0100 |
parents | 89a2afe31e82 |
children | bb3a5c0df06b |
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 |
29205
a0939666b836
py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents:
28382
diff
changeset
|
28 from mercurial.i18n import _ |
28382
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
29 from mercurial import ( |
32375
04baab18d60a
commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32337
diff
changeset
|
30 cmdutil, |
39463
7fea205fd5dc
merge: move purge logic from extension
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36571
diff
changeset
|
31 merge as mergemod, |
36571
5a3f8da663e5
purge: apply byteskwargs to opts, fixing all python3 issues here
Augie Fackler <augie@google.com>
parents:
32375
diff
changeset
|
32 pycompat, |
32337
46ba2cdda476
registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
29841
diff
changeset
|
33 registrar, |
28382
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
34 scmutil, |
27996f78a64c
purge: use absolute_import
timeless <timeless@mozdev.org>
parents:
26587
diff
changeset
|
35 ) |
2364 | 36 |
14310
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
37 cmdtable = {} |
32337
46ba2cdda476
registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
29841
diff
changeset
|
38 command = registrar.command(cmdtable) |
29841
d5883fd055c6
extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents:
29222
diff
changeset
|
39 # 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
|
40 # 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
|
41 # 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
|
42 # leave the attribute unspecified. |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
43 testedwith = b'ships-with-hg-core' |
14310
c16ec14d44b6
purge: use cmdutil.command decorator
Adrian Buehlmann <adrian@cadifra.com>
parents:
10973
diff
changeset
|
44 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
45 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
46 @command( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
47 b'purge|clean', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
48 [ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
49 (b'a', b'abort-on-err', None, _(b'abort if an error occurs')), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
50 (b'', b'all', None, _(b'purge ignored files too')), |
44289
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
51 (b'i', b'ignored', None, _(b'purge only ignored files')), |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
52 (b'', b'dirs', None, _(b'purge empty directories')), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
53 (b'', b'files', None, _(b'purge files')), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
54 (b'p', b'print', None, _(b'print filenames instead of deleting them')), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
55 ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
56 b'0', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
57 b'print0', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
58 None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
59 _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
60 b'end filenames with NUL, for use with xargs' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
61 b' (implies -p/--print)' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
62 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
63 ), |
46366
135056e8b5a8
purge: add a --confirm option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45942
diff
changeset
|
64 (b'', b'confirm', None, _(b'ask before permanently deleting files')), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
65 ] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
66 + cmdutil.walkopts, |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
67 _(b'hg purge [OPTION]... [DIR]...'), |
45133
81859d38e009
purge: classify as a "working directory management" command in help
Martin von Zweigbergk <martinvonz@google.com>
parents:
44289
diff
changeset
|
68 helpcategory=command.CATEGORY_WORKING_DIRECTORY, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
69 ) |
6573 | 70 def purge(ui, repo, *dirs, **opts): |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45133
diff
changeset
|
71 """removes files not tracked by Mercurial |
6573 | 72 |
9270
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
73 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
|
74 and uncommitted changes in an otherwise-clean source tree. |
6573 | 75 |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
76 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
|
77 |
10973
49a07f441496
Use hg role in help strings
Martin Geisler <mg@aragost.com>
parents:
9270
diff
changeset
|
78 - 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
|
79 - Empty directories: in fact Mercurial ignores directories unless |
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
80 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
|
81 |
6573 | 82 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
|
83 |
f6a880fa9cd7
purge: fix formatting of lists with proper reST markup
Martin Geisler <mg@lazybytes.net>
parents:
9072
diff
changeset
|
84 - Modified and unmodified tracked files |
44289
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
85 - Ignored files (unless -i or --all is specified) |
10973
49a07f441496
Use hg role in help strings
Martin Geisler <mg@aragost.com>
parents:
9270
diff
changeset
|
86 - New files added to the repository (with :hg:`add`) |
6573 | 87 |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
88 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
|
89 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
|
90 both will be deleted. |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
91 |
6573 | 92 If directories are given on the command line, only files in these |
93 directories are considered. | |
94 | |
9270
00cc7fa0c0c6
purge: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents:
9215
diff
changeset
|
95 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
|
96 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
|
97 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
|
98 option. |
45942
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
45133
diff
changeset
|
99 """ |
36571
5a3f8da663e5
purge: apply byteskwargs to opts, fixing all python3 issues here
Augie Fackler <augie@google.com>
parents:
32375
diff
changeset
|
100 opts = pycompat.byteskwargs(opts) |
44289
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
101 cmdutil.check_at_most_one_arg(opts, b'all', b'ignored') |
39463
7fea205fd5dc
merge: move purge logic from extension
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36571
diff
changeset
|
102 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
103 act = not opts.get(b'print') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
104 eol = b'\n' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
105 if opts.get(b'print0'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
106 eol = b'\0' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
107 act = False # --print0 implies --print |
44289
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
108 if opts.get(b'all', False): |
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
109 ignored = True |
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
110 unknown = True |
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
111 else: |
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
112 ignored = opts.get(b'ignored', False) |
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
113 unknown = not ignored |
39463
7fea205fd5dc
merge: move purge logic from extension
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36571
diff
changeset
|
114 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
115 removefiles = opts.get(b'files') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
116 removedirs = opts.get(b'dirs') |
46366
135056e8b5a8
purge: add a --confirm option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45942
diff
changeset
|
117 confirm = opts.get(b'confirm') |
39463
7fea205fd5dc
merge: move purge logic from extension
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36571
diff
changeset
|
118 |
21853
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
119 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
|
120 removefiles = True |
8127b9e798b1
purge: add options for deleting only files or only directories
Ben Kehoe <benk@berkeley.edu>
parents:
20565
diff
changeset
|
121 removedirs = True |
2364 | 122 |
39463
7fea205fd5dc
merge: move purge logic from extension
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36571
diff
changeset
|
123 match = scmutil.match(repo[None], dirs, opts) |
4151
337010e50dcd
Use nested functions instead of object methods
Emanuele Aina <faina.mail@tiscali.it>
parents:
4150
diff
changeset
|
124 |
39463
7fea205fd5dc
merge: move purge logic from extension
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36571
diff
changeset
|
125 paths = mergemod.purge( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
126 repo, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
127 match, |
44289
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
128 unknown=unknown, |
9f8eddd2723f
purge: add -i flag to delete ignored files instead of untracked files
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
43077
diff
changeset
|
129 ignored=ignored, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
130 removeemptydirs=removedirs, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
131 removefiles=removefiles, |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
132 abortonerror=opts.get(b'abort_on_err'), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
133 noop=not act, |
46366
135056e8b5a8
purge: add a --confirm option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45942
diff
changeset
|
134 confirm=confirm, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40293
diff
changeset
|
135 ) |
4147
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
136 |
39463
7fea205fd5dc
merge: move purge logic from extension
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36571
diff
changeset
|
137 for path in paths: |
7fea205fd5dc
merge: move purge logic from extension
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36571
diff
changeset
|
138 if not act: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
139 ui.write(b'%s%s' % (path, eol)) |