Index: p4trac/api.py
===================================================================
--- p4trac/api.py	(revision 1262)
+++ p4trac/api.py	(working copy)
@@ -36,6 +36,24 @@
     else:
         return rev
 
+# FIXME: 
+# This is a hack around the fact that a Connection object with empty client
+# will default to the user name client and you can not add new attributes to a
+# Connection object. We use a dummy client name to indicate that we don't want
+# to use the client
+_NO_CLIENT = '_P4TRAC_DUMMY_CLIENT'
+def rootPath(connection):
+    """Root path for quereis.
+
+    If we have a client set, then we limit our queries to the client view.
+    Otherwise we start from the very top.
+    """
+    if connection.client != _NO_CLIENT:
+        return '//%s/...' % connection.client
+    else:
+        return '//...'
+
+
 class PerforceConnector(Component):
     """Component that registers the Perforce repository file system with
     the Trac repository manager.
@@ -90,11 +108,6 @@
                 )
         p4.user = options['user']
 
-        if 'password' in options:
-            p4.password = options['password']
-        else:
-            p4.password = ''
-
         if 'unicode' in options:
             if options['unicode'] == '1':
                 p4.charset = 'utf8'
@@ -109,13 +122,10 @@
         else:
             p4.charset = 'none'
 
-        if 'language' in options:
-            p4.language = options['language']
-        else:
-            p4.language = ''
+        p4.password = options.get('password', '')
+        p4.language = options.get('language', '')
+        p4.client = options.get('client', _NO_CLIENT)
 
-        p4.client = ''
-
         repos = PerforceRepository(p4, self.log)
 
         from trac.versioncontrol.cache import CachedRepository
@@ -166,8 +176,6 @@
 
     def updateCache(self, fromChange):
 
-        from perforce import ConnectionDropped
-
         # Update the database in batches of 1000 changes so that we don't
         # overload the virtual memory system by trying to store information
         # about every change in the repository at once during the initial
@@ -180,38 +188,32 @@
         self.log.debug("Updating cache with changes [%i,%i]" % (lowerBound,
                                                                 upperBound))
         
-        try:
-            while lowerBound < upperBound:
-                batchUpperBound = min(lowerBound + batchSize, upperBound)
+        while lowerBound < upperBound:
+            batchUpperBound = min(lowerBound + batchSize, upperBound)
 
-                # Get the next batch of changes to cache
-                from p4trac.repos import _P4ChangesOutputConsumer
-                output = _P4ChangesOutputConsumer(self.repos._repos)
-                self.repos._connection.run('changes', '-l', '-s', 'submitted',
-                                           '@>=%i,@<%i' % (lowerBound,
-                                                           batchUpperBound),
-                                           output=output)
+            # Get the next batch of changes to cache
+            from p4trac.repos import _P4ChangesOutputConsumer
+            output = _P4ChangesOutputConsumer(self.repos._repos)
+            root = rootPath(self.repos._connection)
+            depot_path = '%s@>=%i,@<%i' % (root, lowerBound, batchUpperBound)
+            self.repos._connection.run('changes', '-l', '-s', 'submitted',
+                                       depot_path, output=output)
             
-                if output.errors:
-                    from p4trac.repos import PerforceError
-                    raise PerforceError(output.errors)
+            if output.errors:
+                from p4trac.repos import PerforceError
+                raise PerforceError(output.errors)
 
-                changes = output.changes
-                changes.reverse()
+            changes = output.changes
+            changes.reverse()
 
-                # Pre-cache all information about these changes in memory
-                # before caching in the database. Clear the in-memory cache
-                # afterwards to save on memory usage.
-                self.repos._repos.precacheFileInformationForChanges(changes)
-                self.storeChangesInDB(changes)
-                self.repos._repos.clearFileInformationCache()
+            # Pre-cache all information about these changes in memory
+            # before caching in the database. Clear the in-memory cache
+            # afterwards to save on memory usage.
+            self.repos._repos.precacheFileInformationForChanges(changes)
+            self.storeChangesInDB(changes)
+            self.repos._repos.clearFileInformationCache()
 
-                lowerBound += batchSize
-                
-        except ConnectionDropped, e:
-            self.log.debug('Rolling back uncommitted cache updates')
-            self.db.rollback()
-            raise TracError('Connection to Perforce server lost')
+            lowerBound += batchSize
         
     def sync(self):
 
@@ -331,9 +333,10 @@
 
         from p4trac.repos import _P4ChangesOutputConsumer
         output = _P4ChangesOutputConsumer(self._repos)
+        depot_path = '%s@>=%s,@<=%s' % \
+                (rootPath(self._connection), startDate, stopDate)
         self._connection.run('changes', '-l', '-s', 'submitted',
-                             '@>=%s,@<=%s' % (startDate, stopDate),
-                             output=output)
+                             depot_path, output=output)
 
         if output.errors:
             from p4trac.repos import PerforceError
@@ -374,10 +377,10 @@
 
         from p4trac.repos import _P4ChangesOutputConsumer
         output = _P4ChangesOutputConsumer(self._repos)
+        depot_path = '%s@<%i' % (rootPath(self._connection), rev)
         self._connection.run('changes', '-l', '-s', 'submitted',
                              '-m', '1',
-                             '@<%i' % rev,
-                             output=output)
+                             depot_path, output=output)
 
         if output.errors:
             from p4trac.repos import PerforcError
@@ -444,12 +447,13 @@
                     
             from p4trac.repos import _P4ChangesOutputConsumer
             output = _P4ChangesOutputConsumer(self._repos)
+            depot_path = '%s%s@>=%i,@<=%i' % (self.rootPath(self._connection), 
+                                              queryPath, lowerBound, 
+                                              batchUpperBound)
+
             self._connection.run('changes', '-l', '-s', 'submitted',
                                  '-m', str(batchSize),
-                                 '%s@>=%i,@<=%i' % (queryPath,
-                                                    lowerBound,
-                                                    batchUpperBound),
-                                 output=output)
+                                 depot_path, output=output)
 
             if output.errors:
                 from p4trac.repos import PerforcError
@@ -601,13 +605,15 @@
         if newNode.isDirectory or oldNode.isDirectory:
 
             if oldNodePath.isRoot:
-                oldQueryPath = u'//...%s' % oldNodePath.rev
+                oldQueryPath = u'%s%s' % \
+                        (rootPath(self._connection), oldNodePath.rev)
             else:
                 oldQueryPath = u'%s/...%s' % (oldNodePath.path,
                                              oldNodePath.rev)
 
             if newNodePath.isRoot:
-                newQueryPath = u'//...%s' % newNodePath.rev
+                newQueryPath = u'%s%s' % \
+                        (rootPath(self._connection), newNodePath.rev)
             else:
                 newQueryPath = u'%s/...%s' % (newNodePath.path,
                                              newNodePath.rev)
@@ -773,9 +779,8 @@
 
                         if currentNode.fileRevision > 1:
                             # Get the previous revision
-                            nodePath = NodePath(
-                                currentNode.nodePath.path,
-                                '#%i' % (currentNode.fileRevision - 1))
+                            nodePath = NodePath(currentNode.nodePath.path,
+                                                '#%i' % (currentNode.rev - 1))
                             currentNode = self._repos.getNode(nodePath)
                         else:
                             currentNode = None
@@ -832,7 +837,8 @@
             output = _P4ChangesOutputConsumer(self._repos)
 
             if self._nodePath.isRoot:
-                queryPath = '//...%s' % self._nodePath.rev
+                queryPath = '%s%s' % (rootPath(self._repos._connection), 
+                                      self._nodePath.rev)
             else:
                 queryPath = '%s/...%s' % (self._nodePath.path,
                                           self._nodePath.rev)
@@ -852,6 +858,7 @@
                     output=output)
 
             if output.errors:
+                from p4trac.repos import PerforceError
                 raise PerforceError(output.errors)
             
             changes = output.changes
