Yuya Nishihara avatar Yuya Nishihara committed 4e1c77f

manifestmodel: move factory of tree nodes to top-level function for readability

Comments (0)

Files changed (1)

tortoisehg/hgqt/manifestmodel.py

 
     def _newrootentry(self):
         """Create the tree of files and directories and return its root"""
-
-        def pathinstatus(path, status, uncleanpaths):
-            """Test path is included by the status filter"""
-            if util.any(c in self._statusfilter and path in e
-                        for c, e in status.iteritems()):
-                return True
-            if 'C' in self._statusfilter and path not in uncleanpaths:
-                return True
-            return False
-
-        def getctxtreeinfo(ctx):
-            """
-            Get the context information that is relevant to populating the tree
-            """
-            status = dict(zip(('M', 'A', 'R'),
-                      (set(a) for a in self._repo.status(ctx.parents()[0],
-                                                             ctx)[:3])))
-            uncleanpaths = status['M'] | status['A'] | status['R']
-            files = itertools.chain(ctx.manifest(), status['R'])
-            return status, uncleanpaths, files
-
-        def addfilestotree(treeroot, files, status, uncleanpaths):
-            """Add files to the tree according to their state"""
-            if self._namefilter:
-                files = fnmatch.filter(files, '*%s*' % self._namefilter)
-            for path in files:
-                if not pathinstatus(path, status, uncleanpaths):
-                    continue
-
-                origpath = path
-                path = self._repo.removeStandin(path)
-
-                e = treeroot
-                for p in hglib.tounicode(path).split('/'):
-                    if not p in e:
-                        e.addchild(p)
-                    e = e[p]
-
-                for st, filesofst in status.iteritems():
-                    if origpath in filesofst:
-                        e.status = st
-                        break
-                else:
-                    e.status = 'C'
-
-        # Add subrepos to the tree
-        def addrepocontentstotree(roote):
-            ctx = roote.ctx
-            for path, substate in ctx.substate.iteritems():
-                if not 'S' in self._statusfilter:
-                    break
-                e = roote
-                pathelements = hglib.tounicode(path).split('/')
-                for p in pathelements[:-1]:
-                    if not p in e:
-                        e.addchild(p)
-                    e = e[p]
-
-                p = pathelements[-1]
-                if not p in e:
-                    e.addchild(p)
-                e = e[p]
-                e.status = 'S'
-                e.subkind = substate[2]
-
-                # If the subrepo exists in the working directory
-                # and it is a mercurial subrepo,
-                # add the files that it contains to the tree as well, according
-                # to the status filter
-                abspath = os.path.join(ctx._repo.root, path)
-                if os.path.isdir(abspath):
-                    srev = substate[1]
-                    sub = ctx.sub(path)
-                    if srev and isinstance(sub, subrepo.hgsubrepo):
-                        srepo = sub._repo
-                        if srev in srepo:
-                            e.ctx = srepo[srev]
-                            # Add the subrepo contents to the tree
-                            e = addrepocontentstotree(e)
-
-            # Add regular files to the tree
-            status, uncleanpaths, files = getctxtreeinfo(ctx)
-
-            addfilestotree(roote, files, status, uncleanpaths)
-            return roote
-
         roote = _Entry()
         roote.ctx = self._repo[self._rev]
-        addrepocontentstotree(roote)
+        _populaterepo(roote, self._repo, self._statusfilter, self._namefilter)
         roote.sort()
         return roote
 
             key=lambda s: (not self[s].isdir, os.path.normcase(s)),
             reverse=reverse)
 
+
+def _populaterepo(roote, repo, statusfilter, namefilter):
+
+    def pathinstatus(path, status, uncleanpaths):
+        """Test path is included by the status filter"""
+        if util.any(c in statusfilter and path in e
+                    for c, e in status.iteritems()):
+            return True
+        if 'C' in statusfilter and path not in uncleanpaths:
+            return True
+        return False
+
+    def getctxtreeinfo(ctx):
+        """
+        Get the context information that is relevant to populating the tree
+        """
+        status = dict(zip(('M', 'A', 'R'),
+                          (set(a) for a in repo.status(ctx.parents()[0],
+                                                       ctx)[:3])))
+        uncleanpaths = status['M'] | status['A'] | status['R']
+        files = itertools.chain(ctx.manifest(), status['R'])
+        return status, uncleanpaths, files
+
+    def addfilestotree(treeroot, files, status, uncleanpaths):
+        """Add files to the tree according to their state"""
+        if namefilter:
+            files = fnmatch.filter(files, '*%s*' % namefilter)
+        for path in files:
+            if not pathinstatus(path, status, uncleanpaths):
+                continue
+
+            origpath = path
+            path = repo.removeStandin(path)
+
+            e = treeroot
+            for p in hglib.tounicode(path).split('/'):
+                if not p in e:
+                    e.addchild(p)
+                e = e[p]
+
+            for st, filesofst in status.iteritems():
+                if origpath in filesofst:
+                    e.status = st
+                    break
+            else:
+                e.status = 'C'
+
+    # Add subrepos to the tree
+    def addrepocontentstotree(roote):
+        ctx = roote.ctx
+        for path, substate in ctx.substate.iteritems():
+            if not 'S' in statusfilter:
+                break
+            e = roote
+            pathelements = hglib.tounicode(path).split('/')
+            for p in pathelements[:-1]:
+                if not p in e:
+                    e.addchild(p)
+                e = e[p]
+
+            p = pathelements[-1]
+            if not p in e:
+                e.addchild(p)
+            e = e[p]
+            e.status = 'S'
+            e.subkind = substate[2]
+
+            # If the subrepo exists in the working directory
+            # and it is a mercurial subrepo,
+            # add the files that it contains to the tree as well, according
+            # to the status filter
+            abspath = os.path.join(ctx._repo.root, path)
+            if os.path.isdir(abspath):
+                srev = substate[1]
+                sub = ctx.sub(path)
+                if srev and isinstance(sub, subrepo.hgsubrepo):
+                    srepo = sub._repo
+                    if srev in srepo:
+                        e.ctx = srepo[srev]
+                        # Add the subrepo contents to the tree
+                        e = addrepocontentstotree(e)
+
+        # Add regular files to the tree
+        status, uncleanpaths, files = getctxtreeinfo(ctx)
+
+        addfilestotree(roote, files, status, uncleanpaths)
+        return roote
+
+    addrepocontentstotree(roote)
+
+
 class ManifestCompleter(QCompleter):
     """QCompleter for ManifestModel"""
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.