purge.py
changeset 2378 6e5d40ec862d
parent 2377 626779aba9bb
equal deleted inserted replaced
2377:626779aba9bb 2378:6e5d40ec862d
    24 
    24 
    25 def _(s):
    25 def _(s):
    26     return s
    26     return s
    27 
    27 
    28 class Purge(object):
    28 class Purge(object):
    29     def __init__(self, act=True, abort_on_err=False):
    29     def __init__(self, act=True, abort_on_err=False, eol='\n'):
    30         self._repo = None
    30         self._repo = None
    31         self._ui = None
    31         self._ui = None
    32         self._hg_root = None
    32         self._hg_root = None
    33         self._act = act
    33         self._act = act
    34         self._abort_on_err = abort_on_err
    34         self._abort_on_err = abort_on_err
       
    35         self._eol = eol
    35 
    36 
    36     def purge(self, ui, repo, dirs=None):
    37     def purge(self, ui, repo, dirs=None):
    37         self._repo = repo
    38         self._repo = repo
    38         self._ui = ui
    39         self._ui = ui
    39         self._hg_root = self._split_path(repo.root)
    40         self._hg_root = self._split_path(repo.root)
    69         relative_name = self._relative_name(name)
    70         relative_name = self._relative_name(name)
    70         # dirstate.state() requires a path relative to the root
    71         # dirstate.state() requires a path relative to the root
    71         # directory.
    72         # directory.
    72         if self._repo.dirstate.state(relative_name) != '?':
    73         if self._repo.dirstate.state(relative_name) != '?':
    73             return
    74             return
    74         self._ui.note(name + '\n')
    75         self._ui.note(_('Removing file %s\n') % name)
    75         if self._act:
    76         if self._act:
    76             try:
    77             try:
    77                 os.remove(name)
    78                 os.remove(name)
    78             except OSError, e:
    79             except OSError, e:
    79                 self._error(_('%s cannot be removed') % name)
    80                 self._error(_('%s cannot be removed') % name)
       
    81         else:
       
    82             self._ui.write('%s%s' % (name, self._eol))
    80 
    83 
    81     def _remove_dir(self, name):
    84     def _remove_dir(self, name):
    82         self._ui.note(name + '\n')
    85         self._ui.note(_('Removing directory %s\n') % name)
    83         if self._act:
    86         if self._act:
    84             try:
    87             try:
    85                 os.rmdir(name)
    88                 os.rmdir(name)
    86             except OSError, e:
    89             except OSError, e:
    87                 self._error(_('%s cannot be removed') % name)
    90                 self._error(_('%s cannot be removed') % name)
       
    91         else:
       
    92             self._ui.write('%s%s' % (name, self._eol))
    88 
    93 
    89     def _relative_name(self, path):
    94     def _relative_name(self, path):
    90         '''
    95         '''
    91         Returns "path" but relative to the root directory of the
    96         Returns "path" but relative to the root directory of the
    92         repository and with '\\' replaced with '/'.
    97         repository and with '\\' replaced with '/'.
   151 
   156 
   152     Be careful with purge, you could irreversibly delete some files you
   157     Be careful with purge, you could irreversibly delete some files you
   153     forgot to add to the repository. If you only want to print the list of
   158     forgot to add to the repository. If you only want to print the list of
   154     files that this program would delete use the -vn options.
   159     files that this program would delete use the -vn options.
   155     '''
   160     '''
   156     act = not opts['nothing']
   161     act = not opts['print']
   157     abort_on_err = bool(opts['abort_on_err'])
   162     abort_on_err = bool(opts['abort_on_err'])
   158     p = Purge(act, abort_on_err)
   163     eol = opts['print0'] and '\0' or '\n'
       
   164     if eol == '\0':
       
   165         # --print0 implies --print
       
   166         act = False
       
   167     p = Purge(act, abort_on_err, eol)
   159     p.purge(ui, repo, dirs)
   168     p.purge(ui, repo, dirs)
   160 
   169 
   161 
   170 
   162 cmdtable = {
   171 cmdtable = {
   163     'purge':    (purge,
   172     'purge':    (purge,
   164                  [('a', 'abort-on-err', None, _('abort if an error occurs')),
   173                  [('a', 'abort-on-err', None, _('abort if an error occurs')),
   165                   ('n', 'nothing',      None, _('do nothing on files, useful with --verbose')),
   174                   ('p', 'print',        None, _('print the file names instead of deleting them')),
       
   175                   ('0', 'print0',       None, _('end filenames with NUL, for use with xargs (implies -p)')),
   166                  ],
   176                  ],
   167                  _('hg purge [OPTIONS] [DIR]'))
   177                  _('hg purge [OPTIONS] [DIR]'))
   168 }
   178 }