Mercurial > hg
annotate hgext/purge.py @ 6574:76af1dff402a
purge: use cmdutil.matchpats
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 12 May 2008 11:37:07 -0500 |
parents | 44cd348e6529 |
children | 5acbdd3941c4 |
rev | line source |
---|---|
2364 | 1 # Copyright (C) 2006 - Marco Barisione <marco@barisione.org> |
2 # | |
3 # This is a small extension for Mercurial (http://www.selenic.com/mercurial) | |
4 # that removes files not known to mercurial | |
5 # | |
4154
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
6 # This program was inspired by the "cvspurge" script contained in CVS utilities |
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
7 # (http://www.red-bean.com/cvsutils/). |
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 # To enable the "purge" extension put these lines in your ~/.hgrc: |
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
10 # [extensions] |
4311
1043e4b27ab9
Move back the purge extension in hgext
Emanuele Aina <em@nerd.ocracy.org>
parents:
4310
diff
changeset
|
11 # hgext.purge = |
4154
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
12 # |
15cd36db4230
Delete the README for purge, putting the useful informations in comments
Emanuele Aina <em@nerd.ocracy.org>
parents:
4153
diff
changeset
|
13 # 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
|
14 # 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
|
15 # |
2364 | 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 | |
18 # the Free Software Foundation; either version 2 of the License, or | |
19 # (at your option) any later version. | |
20 # | |
21 # This program is distributed in the hope that it will be useful, | |
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 # GNU General Public License for more details. | |
25 # | |
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 | |
28 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
29 | |
6574
76af1dff402a
purge: use cmdutil.matchpats
Matt Mackall <mpm@selenic.com>
parents:
6573
diff
changeset
|
30 from mercurial import util, commands, cmdutil |
4121
d250076824e3
Use the mercurial i18n infrastructure in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents:
4120
diff
changeset
|
31 from mercurial.i18n import _ |
2364 | 32 import os |
33 | |
6573 | 34 def purge(ui, repo, *dirs, **opts): |
35 '''removes files not tracked by mercurial | |
36 | |
37 Delete files not known to mercurial, this is useful to test local and | |
38 uncommitted changes in the otherwise clean source tree. | |
39 | |
40 This means that purge will delete: | |
41 - Unknown files: files marked with "?" by "hg status" | |
42 - Ignored files: files usually ignored by Mercurial because they match | |
43 a pattern in a ".hgignore" file | |
44 - Empty directories: in fact Mercurial ignores directories unless they | |
45 contain files under source control managment | |
46 But it will leave untouched: | |
47 - Unmodified tracked files | |
48 - Modified tracked files | |
49 - New files added to the repository (with "hg add") | |
50 | |
51 If directories are given on the command line, only files in these | |
52 directories are considered. | |
53 | |
54 Be careful with purge, you could irreversibly delete some files you | |
55 forgot to add to the repository. If you only want to print the list of | |
56 files that this program would delete use the --print option. | |
57 ''' | |
58 act = not opts['print'] | |
59 ignored = bool(opts['all']) | |
60 abort_on_err = bool(opts['abort_on_err']) | |
61 eol = opts['print0'] and '\0' or '\n' | |
62 if eol == '\0': | |
63 # --print0 implies --print | |
64 act = False | |
65 force = bool(opts['force']) | |
66 | |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
67 def error(msg): |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
68 if abort_on_err: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
69 raise util.Abort(msg) |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
70 else: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
71 ui.warn(_('warning: %s\n') % msg) |
2364 | 72 |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
73 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
|
74 if act: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
75 try: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
76 remove_func(os.path.join(repo.root, name)) |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
77 except OSError, e: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
78 error(_('%s cannot be removed') % name) |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
79 else: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
80 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
|
81 |
5517
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
82 if not force: |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
83 _check_fs(ui, repo) |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
84 |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
85 directories = [] |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
86 files = [] |
4310
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
87 missing = [] |
6574
76af1dff402a
purge: use cmdutil.matchpats
Matt Mackall <mpm@selenic.com>
parents:
6573
diff
changeset
|
88 roots, match, anypats = cmdutil.matchpats(repo, dirs, opts) |
6573 | 89 for src, f, st in repo.dirstate.statwalk(roots, match, |
4691
ca4971347e0a
purge: don't delete ignored files if --all is not specified
Emanuele Aina <em@nerd.ocracy.org>
parents:
4516
diff
changeset
|
90 ignored=ignored, directories=True): |
4155
4c714ed245d6
purge.py: fix invocation of statwalk
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
4154
diff
changeset
|
91 if src == 'd': |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
92 directories.append(f) |
4310
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
93 elif src == 'm': |
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
94 missing.append(f) |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
95 elif src == 'f' and f not in repo.dirstate: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
96 files.append(f) |
2364 | 97 |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
98 directories.sort() |
4147
691f9168a815
Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents:
4121
diff
changeset
|
99 |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
100 for f in files: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
101 if f not in repo.dirstate: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
102 ui.note(_('Removing file %s\n') % f) |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
103 remove(os.remove, 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
|
104 |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
105 for f in directories[::-1]: |
4463
a73cf208b2a0
purge: add --include and --exclude options
Emanuele Aina <em@nerd.ocracy.org>
parents:
4311
diff
changeset
|
106 if match(f) and not os.listdir(repo.wjoin(f)): |
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
107 ui.note(_('Removing directory %s\n') % f) |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
108 remove(os.rmdir, f) |
2364 | 109 |
5517
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
110 def _check_fs(ui, repo): |
4310
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
111 """Abort if there is the chance of having problems with name-mangling fs |
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
112 |
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
113 In a name mangling filesystem (e.g. a case insensitive one) |
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
114 dirstate.walk() can yield filenames different from the ones |
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
115 stored in the dirstate. This already confuses the status and |
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
116 add commands, but with purge this may cause data loss. |
4516
96d8a56d4ef9
Removed trailing whitespace and tabs from python files
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4463
diff
changeset
|
117 |
5517
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
118 To prevent this, this function will abort if there are uncommitted |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
119 changes. |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
120 """ |
4310
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
121 |
5517
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
122 # We can't use (files, match) to do a partial walk here - we wouldn't |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
123 # notice a modified README file if the user ran "hg purge readme" |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
124 modified, added, removed, deleted = repo.status()[:4] |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
125 if modified or added or removed or deleted: |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
126 if not util.checkfolding(repo.path) and not ui.quiet: |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
127 ui.warn(_("Purging on name mangling filesystems is not " |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
128 "fully supported.\n")) |
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
129 raise util.Abort(_("outstanding uncommitted changes")) |
4310
c8919eb0f315
purge: abort with missing files avoiding problems with name-mangling fs
Emanuele Aina <em@nerd.ocracy.org>
parents:
4155
diff
changeset
|
130 |
2364 | 131 cmdtable = { |
4695
c3da7b6cc975
purge: add the clean alias inspired by git-clean and svn-clean
Emanuele Aina <em@nerd.ocracy.org>
parents:
4691
diff
changeset
|
132 'purge|clean': |
2382
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
133 (purge, |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
134 [('a', 'abort-on-err', None, _('abort if an error occurs')), |
4691
ca4971347e0a
purge: don't delete ignored files if --all is not specified
Emanuele Aina <em@nerd.ocracy.org>
parents:
4516
diff
changeset
|
135 ('', 'all', None, _('purge ignored files too')), |
5517
98d5f9b95699
purge: simplify safety net for case mangling filesystems
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
5147
diff
changeset
|
136 ('f', 'force', None, _('purge even when there are uncommitted changes')), |
2382
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
137 ('p', 'print', None, _('print the file names instead of deleting them')), |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
138 ('0', 'print0', None, _('end filenames with NUL, for use with xargs' |
4463
a73cf208b2a0
purge: add --include and --exclude options
Emanuele Aina <em@nerd.ocracy.org>
parents:
4311
diff
changeset
|
139 ' (implies -p)')), |
5147
c80af96943aa
refactor options from cmdtable
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
4957
diff
changeset
|
140 ] + commands.walkopts, |
2382
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
141 _('hg purge [OPTION]... [DIR]...')) |
2364 | 142 } |