author | Christian Ebert <blacktrash@gmx.net> |
Sun, 25 Jan 2009 12:21:57 +0100 | |
changeset 7686 | fa584d23e3cc |
parent 7605 | 3e592067515d |
child 7998 | e2c55c4a25e2 |
permissions | -rw-r--r-- |
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): |
7605
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
35 |
'''removes files not tracked by Mercurial |
6573 | 36 |
|
7605
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
37 |
Delete files not known to Mercurial. This is useful to test local and |
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
38 |
uncommitted changes in an otherwise-clean source tree. |
6573 | 39 |
|
40 |
This means that purge will delete: |
|
41 |
- Unknown files: files marked with "?" by "hg status" |
|
42 |
- Empty directories: in fact Mercurial ignores directories unless they |
|
43 |
contain files under source control managment |
|
44 |
But it will leave untouched: |
|
7605
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
45 |
- Modified and unmodified tracked files |
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
46 |
- Ignored files (unless --all is specified) |
6573 | 47 |
- New files added to the repository (with "hg add") |
48 |
||
49 |
If directories are given on the command line, only files in these |
|
50 |
directories are considered. |
|
51 |
||
7605
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
52 |
Be careful with purge, as you could irreversibly delete some files you |
6573 | 53 |
forgot to add to the repository. If you only want to print the list of |
7605
3e592067515d
1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents:
7570
diff
changeset
|
54 |
files that this program would delete, use the --print option. |
6573 | 55 |
''' |
56 |
act = not opts['print'] |
|
6757 | 57 |
eol = '\n' |
58 |
if opts['print0']: |
|
59 |
eol = '\0' |
|
60 |
act = False # --print0 implies --print |
|
2364 | 61 |
|
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
62 |
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
|
63 |
if act: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
64 |
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
|
65 |
remove_func(repo.wjoin(name)) |
7280
810ca383da9c
remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
6762
diff
changeset
|
66 |
except OSError: |
6757 | 67 |
m = _('%s cannot be removed') % name |
68 |
if opts['abort_on_err']: |
|
69 |
raise util.Abort(m) |
|
70 |
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
|
71 |
else: |
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
72 |
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
|
73 |
|
4153
af72395580e8
Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents:
4152
diff
changeset
|
74 |
directories = [] |
6582
5acbdd3941c4
walk: remove remaining users of cmdutils.matchpats
Matt Mackall <mpm@selenic.com>
parents:
6574
diff
changeset
|
75 |
match = cmdutil.match(repo, dirs, opts) |
6588
10c23c1d5f33
walk: use match.dir in statwalk
Matt Mackall <mpm@selenic.com>
parents:
6582
diff
changeset
|
76 |
match.dir = directories.append |
6754 | 77 |
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
|
78 |
|
6762 | 79 |
for f in util.sort(status[4] + status[5]): |
6754 | 80 |
ui.note(_('Removing file %s\n') % f) |
81 |
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
|
82 |
|
6762 | 83 |
for f in util.sort(directories)[::-1]: |
4463
a73cf208b2a0
purge: add --include and --exclude options
Emanuele Aina <em@nerd.ocracy.org>
parents:
4311
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
remove(os.rmdir, f) |
2364 | 87 |
|
88 |
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
|
89 |
'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
|
90 |
(purge, |
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
91 |
[('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
|
92 |
('', 'all', None, _('purge ignored files too')), |
2382
b429566d1994
Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2381
diff
changeset
|
93 |
('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
|
94 |
('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
|
95 |
' (implies -p)')), |
5147
c80af96943aa
refactor options from cmdtable
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
4957
diff
changeset
|
96 |
] + 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
|
97 |
_('hg purge [OPTION]... [DIR]...')) |
2364 | 98 |
} |