D7593: status: split morestatus data loading from display

rdamazio (Rodrigo Damazio Bovendorp) phabricator at mercurial-scm.org
Tue Dec 10 01:40:24 EST 2019


rdamazio created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7593

AFFECTED FILES
  mercurial/cmdutil.py
  mercurial/commands.py

CHANGE DETAILS

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -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()
 
 
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -24,6 +24,7 @@
     open,
     setattr,
 )
+from .thirdparty import attr
 
 from . import (
     bookmarks,
@@ -778,48 +779,66 @@
     return b'\n'.join(commentedlines) + b'\n'
 
 
-def _conflictsmsg(repo):
-    mergestate = mergemod.mergestate.read(repo)
-    if not mergestate.active():
-        return
-
-    m = scmutil.match(repo[None])
-    unresolvedlist = [f for f in mergestate.unresolved() if m(f)]
-    if unresolvedlist:
-        mergeliststr = b'\n'.join(
-            [
-                b'    %s' % util.pathto(repo.root, encoding.getcwd(), path)
-                for path in sorted(unresolvedlist)
-            ]
-        )
-        msg = (
-            _(
-                '''Unresolved merge conflicts:
+ at 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):



To: rdamazio, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list