[PATCH 05 of 13] Reduce the absolute/relative path conversion in the purge extension

Emanuele Aina faina.mail at tiscali.it
Tue Feb 27 07:06:05 UTC 2007


# HG changeset patch
# User Emanuele Aina <em at nerd.ocracy.org>
# Date 1172563536 -3600
# Node ID 87be54dfa3b211aedb6c08aaad7bc39f11377920
# Parent  94ddfaa09ed610b36e4c7114b5406416f220c451
Reduce the absolute/relative path conversion in the purge extension

diff --git a/contrib/purge/purge.py b/contrib/purge/purge.py
--- a/contrib/purge/purge.py
+++ b/contrib/purge/purge.py
@@ -27,7 +27,6 @@ class Purge(object):
     def __init__(self, act=True, abort_on_err=False, eol='\n'):
         self._repo = None
         self._ui = None
-        self._hg_root = None
         self._act = act
         self._abort_on_err = abort_on_err
         self._eol = eol
@@ -35,7 +34,6 @@ class Purge(object):
     def purge(self, ui, repo, dirs=None):
         self._repo = repo
         self._ui = ui
-        self._hg_root = self._split_path(repo.root)
 
         if not dirs:
             dirs = [repo.root]
@@ -53,16 +51,14 @@ class Purge(object):
         directories.sort()
 
         for f in files:
-            self._remove_file(os.path.join(repo.root, f))
+            self._remove_file(f)
 
         for f in directories[::-1]:
-            f = os.path.join(repo.root, f)
-            if not os.listdir(f):
+            if not os.listdir(os.path.join(repo.root, f)):
                 self._remove_dir(f)
 
         self._repo = None
         self._ui = None
-        self._hg_root = None
 
     def _error(self, msg):
         if self._abort_on_err:
@@ -71,72 +67,28 @@ class Purge(object):
             self._ui.warn(_('warning: %s\n') % msg)
 
     def _remove_file(self, name):
-        relative_name = self._relative_name(name)
         # dirstate.state() requires a path relative to the root
         # directory.
-        if self._repo.dirstate.state(relative_name) != '?':
+        if self._repo.dirstate.state(name) != '?':
             return
-        self._ui.note(_('Removing file %s\n') % relative_name)
+        self._ui.note(_('Removing file %s\n') % name)
         if self._act:
             try:
-                os.remove(name)
+                os.remove(os.path.join(self._repo.root, name))
             except OSError, e:
-                self._error(_('%s cannot be removed') % relative_name)
+                self._error(_('%s cannot be removed') % name)
         else:
-            self._ui.write('%s%s' % (relative_name, self._eol))
+            self._ui.write('%s%s' % (name, self._eol))
 
     def _remove_dir(self, name):
-        relative_name = self._relative_name(name)
-        self._ui.note(_('Removing directory %s\n') % relative_name)
+        self._ui.note(_('Removing directory %s\n') % name)
         if self._act:
             try:
-                os.rmdir(name)
+                os.rmdir(os.path.join(self._repo.root, name))
             except OSError, e:
-                self._error(_('%s cannot be removed') % relative_name)
+                self._error(_('%s cannot be removed') % name)
         else:
-            self._ui.write('%s%s' % (relative_name, self._eol))
-
-    def _relative_name(self, path):
-        '''
-        Returns "path" but relative to the root directory of the
-        repository and with '\\' replaced with '/'.
-        This is needed because this is the format required by
-        self._repo.dirstate.state().
-        '''
-        splitted_path = self._split_path(path)[len(self._hg_root):]
-        # Even on Windows self._repo.dirstate.state() wants '/'.
-        return self._join_path(splitted_path).replace('\\', '/')
-
-    def _split_path(self, path):
-        '''
-        Returns a list of the single files/directories in "path".
-        For instance:
-          '/home/user/test' -> ['/', 'home', 'user', 'test']
-          'C:\\Mercurial'   -> ['C:\\', 'Mercurial']
-        '''
-        ret = []
-        while True:
-            head, tail = os.path.split(path)
-            if tail:
-                ret.append(tail)
-            if head == path:
-                ret.append(head)
-                break
-            path = head
-        ret.reverse()
-        return ret
-
-    def _join_path(self, splitted_path):
-        '''
-        Joins a list returned by _split_path().
-        '''
-        ret = ''
-        for part in splitted_path:
-            if ret:
-                ret = os.path.join(ret, part)
-            else:
-                ret = part
-        return ret
+            self._ui.write('%s%s' % (name, self._eol))
 
 
 def purge(ui, repo, *dirs, **opts):



More information about the Mercurial-devel mailing list