hgext/purge.py
author Cédric Duval <cedricduval@free.fr>
Mon, 22 Jun 2009 15:48:08 +0200
changeset 8894 868670dbc237
parent 8873 e872ef2e6758
child 8934 9dda4c73fc3b
permissions -rw-r--r--
extensions: improve the consistency of synopses Trying as much as possible to consistently: - use a present tense predicate followed by a direct object - verb referring directly to the functionality provided (ie. not "add command that does this" but simple "do that") - keep simple and to the point, leaving details for the long help (width is tight, possibly even more so for translations) Thanks to timeless, Martin Geisler, Rafael Villar Burke, Dan Villiom Podlaski Christiansen and others for the helpful suggestions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     1
# Copyright (C) 2006 - Marco Barisione <marco@barisione.org>
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     2
#
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     3
# This is a small extension for Mercurial (http://www.selenic.com/mercurial)
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     4
# that removes files not known to mercurial
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     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
# 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
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    12
# This program is free software; you can redistribute it and/or modify
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    13
# it under the terms of the GNU General Public License as published by
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    14
# the Free Software Foundation; either version 2 of the License, or
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    15
# (at your option) any later version.
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    16
#
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    17
# This program is distributed in the hope that it will be useful,
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    20
# GNU General Public License for more details.
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    21
#
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    22
# You should have received a copy of the GNU General Public License
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    23
# along with this program; if not, write to the Free Software
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    25
8894
868670dbc237 extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents: 8873
diff changeset
    26
'''delete files not tracked from the working directory'''
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8866
diff changeset
    27
6574
76af1dff402a purge: use cmdutil.matchpats
Matt Mackall <mpm@selenic.com>
parents: 6573
diff changeset
    28
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
    29
from mercurial.i18n import _
8043
b777dd8f7836 purge: remove read-only files under Windows (issue583)
Patrick Mezard <pmezard@gmail.com>
parents: 7998
diff changeset
    30
import os, stat
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    31
6573
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    32
def purge(ui, repo, *dirs, **opts):
7605
3e592067515d 1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 7570
diff changeset
    33
    '''removes files not tracked by Mercurial
6573
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    34
7998
e2c55c4a25e2 purge: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7605
diff changeset
    35
    Delete files not known to Mercurial. This is useful to test local
e2c55c4a25e2 purge: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7605
diff changeset
    36
    and uncommitted changes in an otherwise-clean source tree.
6573
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    37
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    38
    This means that purge will delete:
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    39
     - Unknown files: files marked with "?" by "hg status"
7998
e2c55c4a25e2 purge: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7605
diff changeset
    40
     - Empty directories: in fact Mercurial ignores directories unless
8617
7af21dfae9d5 purge: fix spelling error
Cédric Duval <cedricduval@free.fr>
parents: 8209
diff changeset
    41
       they contain files under source control management
6573
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    42
    But it will leave untouched:
7605
3e592067515d 1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 7570
diff changeset
    43
     - Modified and unmodified tracked files
3e592067515d 1 file changed, 7 insertions(+), 9 deletions(-)
Benjamin Pollack <benjamin@bitquabit.com>
parents: 7570
diff changeset
    44
     - Ignored files (unless --all is specified)
6573
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    45
     - New files added to the repository (with "hg add")
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    46
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    47
    If directories are given on the command line, only files in these
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    48
    directories are considered.
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    49
7998
e2c55c4a25e2 purge: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7605
diff changeset
    50
    Be careful with purge, as you could irreversibly delete some files
e2c55c4a25e2 purge: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7605
diff changeset
    51
    you forgot to add to the repository. If you only want to print the
e2c55c4a25e2 purge: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7605
diff changeset
    52
    list of files that this program would delete, use the --print
e2c55c4a25e2 purge: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7605
diff changeset
    53
    option.
6573
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    54
    '''
44cd348e6529 purge: eliminate dopurge
Matt Mackall <mpm@selenic.com>
parents: 6212
diff changeset
    55
    act = not opts['print']
6757
55c71226eceb purge: cleanup
Matt Mackall <mpm@selenic.com>
parents: 6754
diff changeset
    56
    eol = '\n'
55c71226eceb purge: cleanup
Matt Mackall <mpm@selenic.com>
parents: 6754
diff changeset
    57
    if opts['print0']:
55c71226eceb purge: cleanup
Matt Mackall <mpm@selenic.com>
parents: 6754
diff changeset
    58
        eol = '\0'
55c71226eceb purge: cleanup
Matt Mackall <mpm@selenic.com>
parents: 6754
diff changeset
    59
        act = False # --print0 implies --print
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    60
4153
af72395580e8 Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents: 4152
diff changeset
    61
    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
    62
        if act:
af72395580e8 Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents: 4152
diff changeset
    63
            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
    64
                remove_func(repo.wjoin(name))
7280
810ca383da9c remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6762
diff changeset
    65
            except OSError:
6757
55c71226eceb purge: cleanup
Matt Mackall <mpm@selenic.com>
parents: 6754
diff changeset
    66
                m = _('%s cannot be removed') % name
55c71226eceb purge: cleanup
Matt Mackall <mpm@selenic.com>
parents: 6754
diff changeset
    67
                if opts['abort_on_err']:
55c71226eceb purge: cleanup
Matt Mackall <mpm@selenic.com>
parents: 6754
diff changeset
    68
                    raise util.Abort(m)
55c71226eceb purge: cleanup
Matt Mackall <mpm@selenic.com>
parents: 6754
diff changeset
    69
                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
    70
        else:
af72395580e8 Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents: 4152
diff changeset
    71
            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
    72
8043
b777dd8f7836 purge: remove read-only files under Windows (issue583)
Patrick Mezard <pmezard@gmail.com>
parents: 7998
diff changeset
    73
    def removefile(path):
8044
c1e2b7407dc3 purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents: 8043
diff changeset
    74
        try:
c1e2b7407dc3 purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents: 8043
diff changeset
    75
            os.remove(path)
c1e2b7407dc3 purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents: 8043
diff changeset
    76
        except OSError:
c1e2b7407dc3 purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents: 8043
diff changeset
    77
            # 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
    78
            s = os.stat(path)
c1e2b7407dc3 purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents: 8043
diff changeset
    79
            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
    80
                raise
c1e2b7407dc3 purge: fix b777dd8f7836 (remove read-only files)
Patrick Mezard <pmezard@gmail.com>
parents: 8043
diff changeset
    81
            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
    82
            os.remove(path)
8043
b777dd8f7836 purge: remove read-only files under Windows (issue583)
Patrick Mezard <pmezard@gmail.com>
parents: 7998
diff changeset
    83
4153
af72395580e8 Delete the Purge class, refactoring Purge.purge() in dopurge()
Emanuele Aina <faina.mail@tiscali.it>
parents: 4152
diff changeset
    84
    directories = []
6582
5acbdd3941c4 walk: remove remaining users of cmdutils.matchpats
Matt Mackall <mpm@selenic.com>
parents: 6574
diff changeset
    85
    match = cmdutil.match(repo, dirs, opts)
6588
10c23c1d5f33 walk: use match.dir in statwalk
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
    86
    match.dir = directories.append
6754
0b700faaef32 purge: use status
Matt Mackall <mpm@selenic.com>
parents: 6746
diff changeset
    87
    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
    88
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8076
diff changeset
    89
    for f in sorted(status[4] + status[5]):
6754
0b700faaef32 purge: use status
Matt Mackall <mpm@selenic.com>
parents: 6746
diff changeset
    90
        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
    91
        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
    92
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8076
diff changeset
    93
    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
    94
        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
    95
            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
    96
            remove(os.rmdir, f)
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    97
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    98
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
    99
    '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
   100
        (purge,
b429566d1994 Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2381
diff changeset
   101
         [('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
   102
          ('',  'all', None, _('purge ignored files too')),
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8617
diff changeset
   103
          ('p', 'print', None, _('print filenames instead of deleting them')),
2382
b429566d1994 Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2381
diff changeset
   104
          ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
8076
5ec526c1a32f help texts: write command line switches as -a/--abc
Martin Geisler <mg@lazybytes.net>
parents: 8044
diff changeset
   105
                                  ' (implies -p/--print)')),
5147
c80af96943aa refactor options from cmdtable
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 4957
diff changeset
   106
         ] + 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
   107
         _('hg purge [OPTION]... [DIR]...'))
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
   108
}