changeset 6888:1e836ad40272

obslog: use exception to convey the failure to find a diff pair I am not a fan of using exceptions for flow control, but it makes more sense here. The next patch will actually extend the functionality of patchavailable() to potentially return things other than just `cand`, and there the use of this exception will make even more sense.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Sun, 22 Sep 2024 12:20:03 +0200
parents 906b5af0b2a6
children a66cf9008781
files hgext3rd/evolve/obshistory.py
diffstat 1 files changed, 23 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/evolve/obshistory.py	Tue Sep 17 21:17:56 2024 +0400
+++ b/hgext3rd/evolve/obshistory.py	Sun Sep 22 12:20:03 2024 +0200
@@ -307,27 +307,39 @@
         '''
         pass
 
-def patchavailable(node, repo, candidates, successive=True):
+class _NoPatchAvailable(Exception):
+    """small internal exception
+
+    Carries the reason of why we cannot offer a patch to `displaymarkers`.
+
+    XXX: we could raise a more semantic reason and let `displaymarkers` create
+    the message.
+    """
+    def __init__(self, reason):
+        self.reason = reason
+        super(_NoPatchAvailable, self).__init__()
+
+def patchavailable(repo, node, candidates, successive=True):
     """ Check if it's possible to get a diff between node and candidates.
 
     `candidates` contains nodes, which can be either successors (`successive`
     is True) or predecessors (`successive` is False) of `node`.
     """
     if node not in repo:
-        return False, b"context is not local"
+        raise _NoPatchAvailable(b"context is not local")
 
     if len(candidates) == 0:
         if successive:
             msg = b"no successors"
         else:
             msg = b"no predecessors"
-        return False, msg
+        raise _NoPatchAvailable(msg)
     elif len(candidates) > 1:
         if successive:
             msg = b"too many successors (%d)"
         else:
             msg = b"too many predecessors (%d)"
-        return False, msg % len(candidates)
+        raise _NoPatchAvailable(msg % len(candidates))
 
     cand = candidates[0]
 
@@ -336,16 +348,16 @@
             msg = b"successor is unknown locally"
         else:
             msg = b"predecessor is unknown locally"
-        return False, msg
+        raise _NoPatchAvailable(msg)
 
     # Check that both node and cand have the same parents
     nodep1, nodep2 = repo[node].p1(), repo[node].p2()
     candp1, candp2 = repo[cand].p1(), repo[cand].p2()
 
     if nodep1 != candp1 or nodep2 != candp2:
-        return False, b"changesets rebased"
+        raise _NoPatchAvailable(b"changesets rebased")
 
-    return True, cand
+    return cand
 
 def getmarkerdescriptionpatch(repo, basedesc, succdesc):
     # description are stored without final new line,
@@ -678,14 +690,12 @@
 
     # Patch display
     if includediff is True:
-        _patchavailable = patchavailable(node, repo, nodes,
-                                         successive=successive)
 
-        if not _patchavailable[0]:
-            fm.data(nopatchreason=_patchavailable[1])
+        try:
+            diffnode = patchavailable(repo, node, nodes, successive=successive)
+        except _NoPatchAvailable as exc:
+            fm.data(nopatchreason=exc.reason)
         else:
-            diffnode = _patchavailable[1]
-
             if successive:
                 actx = repo[node]
                 bctx = repo[diffnode]