status: split morestatus data loading from display
authorRodrigo Damazio Bovendorp <rdamazio@google.com>
Mon, 09 Dec 2019 18:15:38 -0800
changeset 43841 fb4a6d584756
parent 43840 79c0121220e3
child 43842 7315464f0613
status: split morestatus data loading from display This is a small refactoring in preparation for adding more morestatus functionality (notably for templated/JSON output) - the goal is to use the data inside the status display loop, as well as output the overall state in a templatable/structured way. Differential Revision: https://phab.mercurial-scm.org/D7593
mercurial/cmdutil.py
mercurial/commands.py
--- a/mercurial/cmdutil.py	Thu Nov 21 16:54:00 2019 +0100
+++ b/mercurial/cmdutil.py	Mon Dec 09 18:15:38 2019 -0800
@@ -24,6 +24,7 @@
     open,
     setattr,
 )
+from .thirdparty import attr
 
 from . import (
     bookmarks,
@@ -778,47 +779,66 @@
     return b'\n'.join(commentedlines) + b'\n'
 
 
-def _conflictsmsg(repo):
-    mergestate = mergemod.mergestate.read(repo)
-    if not mergestate.active():
-        return
-
-    unresolvedlist = sorted(mergestate.unresolved())
-    if unresolvedlist:
-        mergeliststr = b'\n'.join(
-            [
-                b'    %s' % util.pathto(repo.root, encoding.getcwd(), path)
-                for path in unresolvedlist
-            ]
-        )
-        msg = (
-            _(
-                '''Unresolved merge conflicts:
+@attr.s(frozen=True)
+class morestatus(object):
+    reporoot = attr.ib()
+    unfinishedop = attr.ib()
+    unfinishedmsg = attr.ib()
+    inmergestate = attr.ib()
+    unresolvedpaths = attr.ib()
+    _label = b'status.morestatus'
+
+    def formatfooter(self, fm):
+        statemsg = _(b'The repository is in an unfinished *%s* state.'
+                     ) % self.unfinishedop
+        fm.plain(b'%s\n' % _commentlines(statemsg), label=self._label)
+
+        self._formatconflicts(fm)
+        if self.unfinishedmsg:
+            fm.plain(b'%s\n' % _commentlines(self.unfinishedmsg),
+                     label=self._label)
+
+    def _formatconflicts(self, fm):
+        if not self.inmergestate:
+            return
+
+        if self.unresolvedpaths:
+            mergeliststr = b'\n'.join(
+                [
+                    b'    %s' % util.pathto(self.reporoot, encoding.getcwd(),
+                                            path)
+                    for path in self.unresolvedpaths
+                ]
+            )
+            msg = (
+                _(
+                    '''Unresolved merge conflicts:
 
 %s
 
 To mark files as resolved:  hg resolve --mark FILE'''
+                )
+                % mergeliststr
             )
-            % mergeliststr
-        )
-    else:
-        msg = _(b'No unresolved merge conflicts.')
-
-    return _commentlines(msg)
-
-
-def morestatus(repo, fm):
+        else:
+            msg = _(b'No unresolved merge conflicts.')
+
+        fm.plain(b'%s\n' % _commentlines(msg), label=self._label)
+
+
+def readmorestatus(repo):
+    """Returns a morestatus object if the repo has unfinished state."""
     statetuple = statemod.getrepostate(repo)
-    label = b'status.morestatus'
-    if statetuple:
-        state, helpfulmsg = statetuple
-        statemsg = _(b'The repository is in an unfinished *%s* state.') % state
-        fm.plain(b'%s\n' % _commentlines(statemsg), label=label)
-        conmsg = _conflictsmsg(repo)
-        if conmsg:
-            fm.plain(b'%s\n' % conmsg, label=label)
-        if helpfulmsg:
-            fm.plain(b'%s\n' % _commentlines(helpfulmsg), label=label)
+    if not statetuple:
+        return None
+
+    unfinishedop, unfinishedmsg = statetuple
+    mergestate = mergemod.mergestate.read(repo)
+    unresolved = None
+    if mergestate.active():
+        unresolved = sorted(mergestate.unresolved())
+    return morestatus(repo.root, unfinishedop, unfinishedmsg,
+                      unresolved is not None, unresolved)
 
 
 def findpossible(cmd, table, strict=False):
--- a/mercurial/commands.py	Thu Nov 21 16:54:00 2019 +0100
+++ b/mercurial/commands.py	Mon Dec 09 18:15:38 2019 -0800
@@ -6867,6 +6867,12 @@
     ) and not opts.get(b'no_status'):
         copy = copies.pathcopies(ctx1, ctx2, m)
 
+    morestatus = None
+    if (
+        ui.verbose or ui.configbool(b'commands', b'status.verbose')
+    ) and not ui.plain():
+        morestatus = cmdutil.readmorestatus(repo)
+
     ui.pager(b'status')
     fm = ui.formatter(b'status', opts)
     fmt = b'%s' + end
@@ -6888,10 +6894,8 @@
                         label=b'status.copied',
                     )
 
-    if (
-        ui.verbose or ui.configbool(b'commands', b'status.verbose')
-    ) and not ui.plain():
-        cmdutil.morestatus(repo, fm)
+    if morestatus:
+        morestatus.formatfooter(fm)
     fm.end()