obshistory: factor out _markerseffects()
authorAnton Shestakov <av6@dwimlabs.net>
Sat, 11 Jan 2020 19:49:45 +0700
changeset 5066 8dc865544aa1
parent 5065 877d80a205d1
child 5067 e07f6af3cfec
obshistory: factor out _markerseffects()
hgext3rd/evolve/obshistory.py
--- a/hgext3rd/evolve/obshistory.py	Sat Jan 11 19:24:21 2020 +0700
+++ b/hgext3rd/evolve/obshistory.py	Sat Jan 11 19:49:45 2020 +0700
@@ -507,34 +507,10 @@
 
     fm.data(verb=verb)
 
-    effectflag = metadata.get(b'ef1')
-    if effectflag is not None:
-        try:
-            effectflag = int(effectflag)
-        except ValueError:
-            effectflag = None
-    if effectflag:
-        effects = []
-
-        # XXX should be a dict
-        if effectflag & DESCCHANGED:
-            effects.append(b'description')
-        if effectflag & METACHANGED:
-            effects.append(b'meta')
-        if effectflag & USERCHANGED:
-            effects.append(b'user')
-        if effectflag & DATECHANGED:
-            effects.append(b'date')
-        if effectflag & BRANCHCHANGED:
-            effects.append(b'branch')
-        if effectflag & PARENTCHANGED:
-            effects.append(b'parent')
-        if effectflag & DIFFCHANGED:
-            effects.append(b'content')
-
-        if effects:
-            fmteffect = fm.formatlist(effects, b'effect')
-            fm.write(b'effects', b'(%s)', fmteffect)
+    effects = _markerseffects([marker])
+    if effects:
+        fmteffect = fm.formatlist(effects, b'effect', sep=b', ')
+        fm.write(b'effects', b'(%s)', fmteffect)
 
     if len(succnodes) > 0:
         hexnodes = (nodemod.hex(succnode) for succnode in sorted(succnodes))
@@ -618,37 +594,10 @@
     fm.write(b'verb', b'%s', verb,
              label=b"evolve.verb")
 
-    # Effect flag
-    metadata = [dict(marker[3]) for marker in markers]
-    ef1 = [data.get(b'ef1') for data in metadata]
-
-    effectflag = 0
-    for ef in ef1:
-        if ef:
-            effectflag |= int(ef)
-
-    if effectflag:
-        effects = []
-
-        # XXX should be a dict
-        if effectflag & DESCCHANGED:
-            effects.append(b'description')
-        if effectflag & METACHANGED:
-            effects.append(b'meta')
-        if effectflag & USERCHANGED:
-            effects.append(b'user')
-        if effectflag & DATECHANGED:
-            effects.append(b'date')
-        if effectflag & BRANCHCHANGED:
-            effects.append(b'branch')
-        if effectflag & PARENTCHANGED:
-            effects.append(b'parent')
-        if effectflag & DIFFCHANGED:
-            effects.append(b'content')
-
-        if effects:
-            fmteffect = fm.formatlist(effects, b'effect', sep=b', ')
-            fm.write(b'effects', b'(%s)', fmteffect)
+    effects = _markerseffects(markers)
+    if effects:
+        fmteffect = fm.formatlist(effects, b'effect', sep=b', ')
+        fm.write(b'effects', b'(%s)', fmteffect)
 
     if len(succnodes) > 0:
         fm.plain(b' as ')
@@ -837,6 +786,40 @@
 
     return {b'users': sorted(users)}
 
+def _markerseffects(markers):
+    """ Return a list of effects as strings based on effect flags in markers
+
+    Return None if verb cannot be more precise than just "rewritten", i.e. when
+    markers collectively have more than one effect in the flags.
+    """
+    metadata = [dict(marker[3]) for marker in markers]
+    ef1 = [data.get(b'ef1') for data in metadata]
+    effects = []
+
+    combined = 0
+    for ef in ef1:
+        if ef:
+            combined |= int(ef)
+
+    if combined:
+        # XXX should be a dict
+        if combined & DESCCHANGED:
+            effects.append(b'description')
+        if combined & METACHANGED:
+            effects.append(b'meta')
+        if combined & USERCHANGED:
+            effects.append(b'user')
+        if combined & DATECHANGED:
+            effects.append(b'date')
+        if combined & BRANCHCHANGED:
+            effects.append(b'branch')
+        if combined & PARENTCHANGED:
+            effects.append(b'parent')
+        if combined & DIFFCHANGED:
+            effects.append(b'content')
+
+    return effects
+
 VERBMAPPING = {
     DESCCHANGED: b"reworded",
     METACHANGED: b"meta-changed",