# HG changeset patch # User Pierre-Yves David # Date 1727000403 -7200 # Node ID 1e836ad40272728974bd136e5c3001e5d318ba68 # Parent 906b5af0b2a68a785546ac74d09bb351585d6edf 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. diff -r 906b5af0b2a6 -r 1e836ad40272 hgext3rd/evolve/obshistory.py --- 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]