contrib/purge/purge.py
author Emanuele Aina <faina.mail@tiscali.it>
Tue, 06 Mar 2007 17:45:41 -0300
changeset 4148 de85ff0aaac5
parent 4147 691f9168a815
child 4149 e59286f15189
permissions -rw-r--r--
Reduce the absolute/relative path conversion in the purge extension
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
#
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     6
# This program is free software; you can redistribute it and/or modify
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     7
# it under the terms of the GNU General Public License as published by
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     8
# the Free Software Foundation; either version 2 of the License, or
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
     9
# (at your option) any later version.
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    10
#
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    11
# This program is distributed in the hope that it will be useful,
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    14
# GNU General Public License for more details.
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    15
#
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    16
# You should have received a copy of the GNU General Public License
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    17
# along with this program; if not, write to the Free Software
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    18
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    19
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    20
from mercurial import hg, util
4121
d250076824e3 Use the mercurial i18n infrastructure in the purge extension
Emanuele Aina <em@nerd.ocracy.org>
parents: 4120
diff changeset
    21
from mercurial.i18n import _
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    22
import os
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    23
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    24
class Purge(object):
2378
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
    25
    def __init__(self, act=True, abort_on_err=False, eol='\n'):
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    26
        self._repo = None
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    27
        self._ui = None
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    28
        self._act = act
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    29
        self._abort_on_err = abort_on_err
2378
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
    30
        self._eol = eol
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    31
2377
626779aba9bb The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents: 2376
diff changeset
    32
    def purge(self, ui, repo, dirs=None):
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    33
        self._repo = repo
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    34
        self._ui = ui
4147
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    35
        
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    36
        directories = []
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    37
        files = []
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    38
        for src, f, st in repo.dirstate.statwalk(files=dirs, ignored=True,
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    39
                                                 directories=True):
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    40
            if   src == 'd':
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    41
                directories.append(f)
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    42
            elif src == 'f' and f not in repo.dirstate:
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    43
                files.append(f)
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    44
4147
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    45
        directories.sort()
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    46
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    47
        for f in files:
4148
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    48
            self._remove_file(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
    49
691f9168a815 Make the purge extension use the statwalk walker from the dirstate object
Emanuele Aina <faina.mail@tiscali.it>
parents: 4121
diff changeset
    50
        for f in directories[::-1]:
4148
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    51
            if not os.listdir(repo.wjoin(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
    52
                self._remove_dir(f)
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    53
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    54
        self._repo = None
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    55
        self._ui = None
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    56
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    57
    def _error(self, msg):
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    58
        if self._abort_on_err:
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    59
            raise util.Abort(msg)
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    60
        else:
2376
52cfb9864257 Marked strings for translation
Marco Barisione <marco@barisione.org>
parents: 2375
diff changeset
    61
            self._ui.warn(_('warning: %s\n') % msg)
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    62
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    63
    def _remove_file(self, name):
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    64
        # dirstate.state() requires a path relative to the root
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    65
        # directory.
4148
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    66
        if self._repo.dirstate.state(name) != '?':
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    67
            return
4148
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    68
        self._ui.note(_('Removing file %s\n') % name)
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    69
        if self._act:
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    70
            try:
4148
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    71
                os.remove(self._repo.wjoin(name))
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    72
            except OSError, e:
4148
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    73
                self._error(_('%s cannot be removed') % name)
2378
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
    74
        else:
4148
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    75
            self._ui.write('%s%s' % (name, self._eol))
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    76
4148
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    77
    def _remove_dir(self, name):
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    78
        self._ui.note(_('Removing directory %s\n') % name)
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    79
        if self._act:
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    80
            try:
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    81
                os.rmdir(self._repo.wjoin(name))
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    82
            except OSError, e:
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    83
                self._error(_('%s cannot be removed') % name)
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    84
        else:
de85ff0aaac5 Reduce the absolute/relative path conversion in the purge extension
Emanuele Aina <faina.mail@tiscali.it>
parents: 4147
diff changeset
    85
            self._ui.write('%s%s' % (name, self._eol))
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
    86
2369
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    87
2377
626779aba9bb The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents: 2376
diff changeset
    88
def purge(ui, repo, *dirs, **opts):
2369
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    89
    '''removes files not tracked by mercurial
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    90
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    91
    Delete files not known to mercurial, this is useful to test local and
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    92
    uncommitted changes in the otherwise clean source tree.
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    93
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    94
    This means that purge will delete:
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    95
     - Unknown files: files marked with "?" by "hg status"
2377
626779aba9bb The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents: 2376
diff changeset
    96
     - Ignored files: files usually ignored by Mercurial because they match
626779aba9bb The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents: 2376
diff changeset
    97
       a pattern in a ".hgignore" file
2381
ab7a438294fc Rewritten install instructions for hg-purge to match new situation, fixed typos.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2379
diff changeset
    98
     - Empty directories: in fact Mercurial ignores directories unless they
2369
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
    99
       contain files under source control managment
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
   100
    But it will leave untouched:
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
   101
     - Unmodified tracked files
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
   102
     - Modified tracked files
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
   103
     - New files added to the repository (with "hg add")
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
   104
2377
626779aba9bb The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents: 2376
diff changeset
   105
    If directories are given on the command line, only files in these
626779aba9bb The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents: 2376
diff changeset
   106
    directories are considered.
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
   107
2369
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
   108
    Be careful with purge, you could irreversibly delete some files you
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
   109
    forgot to add to the repository. If you only want to print the list of
2381
ab7a438294fc Rewritten install instructions for hg-purge to match new situation, fixed typos.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2379
diff changeset
   110
    files that this program would delete use the --print option.
2369
9da3dd62c827 Purge.from_command is now a function called purge
demian@gaudron.lan
parents: 2364
diff changeset
   111
    '''
2378
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
   112
    act = not opts['print']
2370
de893ad6bd17 Command line options are read in a saner way
demian@gaudron.lan
parents: 2369
diff changeset
   113
    abort_on_err = bool(opts['abort_on_err'])
2378
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
   114
    eol = opts['print0'] and '\0' or '\n'
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
   115
    if eol == '\0':
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
   116
        # --print0 implies --print
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
   117
        act = False
6e5d40ec862d Removed --nothing, added --print and --print0
Marco Barisione <marco@barisione.org>
parents: 2377
diff changeset
   118
    p = Purge(act, abort_on_err, eol)
2377
626779aba9bb The meaning of the directories on the command line is now explained correctly
Marco Barisione <marco@barisione.org>
parents: 2376
diff changeset
   119
    p.purge(ui, repo, dirs)
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
   120
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
   121
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
   122
cmdtable = {
2382
b429566d1994 Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2381
diff changeset
   123
    'purge':
b429566d1994 Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2381
diff changeset
   124
        (purge,
b429566d1994 Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2381
diff changeset
   125
         [('a', 'abort-on-err', None, _('abort if an error occurs')),
b429566d1994 Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2381
diff changeset
   126
          ('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
   127
          ('0', 'print0', None, _('end filenames with NUL, for use with xargs'
b429566d1994 Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2381
diff changeset
   128
                                  ' (implies -p)'))],
b429566d1994 Make indentation of purge's cmdtable match to mercurial/commands.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2381
diff changeset
   129
         _('hg purge [OPTION]... [DIR]...'))
2364
f368a1c302d5 Initial commit
demian@gaudron.lan
parents:
diff changeset
   130
}