[PATCH 1 of 3] crecord: implement uihunk.reversehunk

Jun Wu quark at fb.com
Wed Jun 21 03:45:32 UTC 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1498014667 25200
#      Tue Jun 20 20:11:07 2017 -0700
# Node ID 9b1db75aec5d7c6b6900c1afadddd9fa3e0cbe91
# Parent  0ce2cbebd74964ffe61e79de8941461bccc9371b
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 9b1db75aec5d
crecord: implement uihunk.reversehunk

diff --git a/mercurial/crecord.py b/mercurial/crecord.py
--- a/mercurial/crecord.py
+++ b/mercurial/crecord.py
@@ -428,4 +428,44 @@ class uihunk(patchnode):
         return x.getvalue()
 
+    def reversehunk(self):
+        """return a recordhunk which is the reverse of the hunk
+
+        For example, given the following selection:
+
+                 0
+            [x] -1
+            [ ] -2
+            [x] +3
+            [ ] +4
+            [x] +5
+                 6
+
+        This function generates something like:
+
+                 0
+                -3
+                -4
+                -5
+                +1
+                +4
+                 6
+        """
+        dels = []
+        adds = []
+        for line in self.changedlines:
+            text = line.linetext
+            if line.applied:
+                if text[0] == '+':
+                    dels.append(text[1:])
+                elif text[0] == '-':
+                    adds.append(text[1:])
+            elif text[0] == '+':
+                dels.append(text[1:])
+                adds.append(text[1:])
+        hunk = ['-%s' % l for l in dels] + ['+%s' % l for l in adds]
+        h = self._hunk
+        return patchmod.recordhunk(h.header, h.toline, h.fromline, h.proc,
+                                   h.before, hunk, h.after)
+
     def __getattr__(self, name):
         return getattr(self._hunk, name)


More information about the Mercurial-devel mailing list