Patchwork [1,of,3] crecord: implement uihunk.reversehunk

login
register
mail settings
Submitter Jun Wu
Date June 21, 2017, 3:45 a.m.
Message ID <9b1db75aec5d7c6b6900.1498016732@x1c>
Download mbox | patch
Permalink /patch/21572/
State Changes Requested
Headers show

Comments

Jun Wu - June 21, 2017, 3:45 a.m.
# HG changeset patch
# User Jun Wu <quark@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

Patch

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)