Commits

Yuya Nishihara committed 17f82f9

thgrepo: extract methods to start/stop filesystem monitoring (refs #1783)

They'll be utilized later in order to disable filesystem monitoring while
command is running.

Comments (0)

Files changed (2)

tests/nosehgenv.py

             # https://bitbucket.org/tortoisehg/thg/issue/1783/
             from tortoisehg.hgqt import thgrepo
             for e in thgrepo._repocache.itervalues():
-                w = e._pyqtobj._watcher._fswatcher
-                w.removePaths(w.directories())
-                w.removePaths(w.files())
+                w = e._pyqtobj._watcher
+                w.stopMonitoring()
 
             shutil.rmtree(self.tmpdir)

tortoisehg/hgqt/thgrepo.py

     def __init__(self, repo, parent=None):
         super(RepoWatcher, self).__init__(parent)
         self.repo = repo
+        self._fswatcher = None
         self.recordState()
         self._uimtime = time.time()
 
         elif monitorrepo == 'localonly' and paths.netdrive_status(repo.path):
             dbgoutput('not watching F/S events for network drive')
         else:
+            self.startMonitoring()
+
+    def startMonitoring(self):
+        """Start filesystem monitoring to notify changes automatically"""
+        if not self._fswatcher:
             self._fswatcher = QFileSystemWatcher(self)
-            self._fswatcher.addPath(hglib.tounicode(repo.path))
-            self._fswatcher.addPath(hglib.tounicode(repo.path + '/store'))
             self._fswatcher.directoryChanged.connect(self._pollChanges)
             self._fswatcher.fileChanged.connect(self._pollChanges)
-            self.addMissingPaths()
+        self._fswatcher.addPath(hglib.tounicode(self.repo.path))
+        self._fswatcher.addPath(hglib.tounicode(self.repo.path + '/store'))
+        self.addMissingPaths()
+
+    def stopMonitoring(self):
+        """Stop filesystem monitoring by removing all watched paths"""
+        if not self._fswatcher:
+            return
+        dirs = self._fswatcher.directories()
+        if dirs:
+            self._fswatcher.removePaths(dirs)
+        files = self._fswatcher.files()
+        if files:
+            self._fswatcher.removePaths(files)
 
     @pyqtSlot()
     def _pollChanges(self):
         if not os.path.exists(self.repo.path):
             dbgoutput('Repository destroyed', self.repo.root)
             self.repositoryDestroyed.emit()
-            # disable watcher by removing all watched paths
-            dirs = self._fswatcher.directories()
-            if dirs:
-                self._fswatcher.removePaths(dirs)
-            files = self._fswatcher.files()
-            if files:
-                self._fswatcher.removePaths(files)
+            self.stopMonitoring()
             if self.repo.root in _repocache:
                 del _repocache[self.repo.root]
             return