[PATCH] histedit: refactored out diff/patch logic (issue 3527)

Leah Xue leahxue at fb.com
Mon Aug 27 17:41:04 CDT 2012


# HG changeset patch
# User Leah Xue <leahxue at fb.com>
# Date 1345590394 25200
# Node ID 4347210a4b909d4d608688a598fdbed064238605
# Parent  c6f88e7f95b764e23b7e0b4353c5a6458bbc3cc4
histedit: refactored out diff/patch logic (issue 3527)

This patch is the first step towards a refactoring of the histedit extension to
use underlying graft machinery instead of diff/patch. Replacing diff/patch with
graft is necessary to fix, for example, issue 3582.

diff -r c6f88e7f95b7 -r 4347210a4b90 hgext/histedit.py
--- a/hgext/histedit.py	Sat Aug 11 12:45:53 2012 -0500
+++ b/hgext/histedit.py	Tue Aug 21 16:06:34 2012 -0700
@@ -175,6 +175,27 @@
 #
 """)
 
+def patchhistories(ui, repo, node1, node2, opts):
+    """ calculate diff between node1 and node2, then patch to squash.
+    """
+    fd, patchfile = tempfile.mkstemp(prefix='hg-histedit-')
+    fp = os.fdopen(fd, 'w')
+    diffopts = patch.diffopts(ui, opts)
+    diffopts.git = True
+    diffopts.ignorews = False
+    diffopts.ignorewsamount = False
+    diffopts.ignoreblanklines = False
+    gen = patch.diff(repo, node1, node2, opts=diffopts)
+    for chunk in gen:
+        fp.write(chunk)
+    fp.close()
+    files = set()
+    try:
+        patch.patch(ui, repo, patchfile, files=files, eolmode=None)
+    finally:
+        os.unlink(patchfile)
+    return files
+
 def between(repo, old, new, keep):
     revs = [old]
     current = old
@@ -200,27 +221,12 @@
         ui.debug('node %s unchanged\n' % ha)
         return oldctx, [], [], []
     hg.update(repo, ctx.node())
-    fd, patchfile = tempfile.mkstemp(prefix='hg-histedit-')
-    fp = os.fdopen(fd, 'w')
-    diffopts = patch.diffopts(ui, opts)
-    diffopts.git = True
-    diffopts.ignorews = False
-    diffopts.ignorewsamount = False
-    diffopts.ignoreblanklines = False
-    gen = patch.diff(repo, oldctx.parents()[0].node(), ha, opts=diffopts)
-    for chunk in gen:
-        fp.write(chunk)
-    fp.close()
     try:
-        files = set()
-        try:
-            patch.patch(ui, repo, patchfile, files=files, eolmode=None)
-            if not files:
-                ui.warn(_('%s: empty changeset')
-                             % node.hex(ha))
-                return ctx, [], [], []
-        finally:
-            os.unlink(patchfile)
+        files = patchhistories(ui, repo, oldctx.parents()[0].node(), ha, opts)
+        if not files:
+            ui.warn(_('%s: empty changeset')
+                         % node.hex(ha))
+            return ctx, [], [], []
     except Exception:
         raise util.Abort(_('Fix up the change and run '
                            'hg histedit --continue'))
@@ -232,23 +238,8 @@
 def edit(ui, repo, ctx, ha, opts):
     oldctx = repo[ha]
     hg.update(repo, ctx.node())
-    fd, patchfile = tempfile.mkstemp(prefix='hg-histedit-')
-    fp = os.fdopen(fd, 'w')
-    diffopts = patch.diffopts(ui, opts)
-    diffopts.git = True
-    diffopts.ignorews = False
-    diffopts.ignorewsamount = False
-    diffopts.ignoreblanklines = False
-    gen = patch.diff(repo, oldctx.parents()[0].node(), ha, opts=diffopts)
-    for chunk in gen:
-        fp.write(chunk)
-    fp.close()
     try:
-        files = set()
-        try:
-            patch.patch(ui, repo, patchfile, files=files, eolmode=None)
-        finally:
-            os.unlink(patchfile)
+        files = patchhistories(ui, repo, oldctx.parents()[0].node(), ha, opts)
     except Exception:
         pass
     raise util.Abort(_('Make changes as needed, you may commit or record as '
@@ -258,27 +249,12 @@
 def fold(ui, repo, ctx, ha, opts):
     oldctx = repo[ha]
     hg.update(repo, ctx.node())
-    fd, patchfile = tempfile.mkstemp(prefix='hg-histedit-')
-    fp = os.fdopen(fd, 'w')
-    diffopts = patch.diffopts(ui, opts)
-    diffopts.git = True
-    diffopts.ignorews = False
-    diffopts.ignorewsamount = False
-    diffopts.ignoreblanklines = False
-    gen = patch.diff(repo, oldctx.parents()[0].node(), ha, opts=diffopts)
-    for chunk in gen:
-        fp.write(chunk)
-    fp.close()
     try:
-        files = set()
-        try:
-            patch.patch(ui, repo, patchfile, files=files, eolmode=None)
-            if not files:
-                ui.warn(_('%s: empty changeset')
-                             % node.hex(ha))
-                return ctx, [], [], []
-        finally:
-            os.unlink(patchfile)
+        files = patchhistories(ui, repo, oldctx.parents()[0].node(), ha, opts)
+        if not files:
+            ui.warn(_('%s: empty changeset')
+                         % node.hex(ha))
+            return ctx, [], [], []
     except Exception:
         raise util.Abort(_('Fix up the change and run '
                            'hg histedit --continue'))
@@ -289,22 +265,7 @@
 def finishfold(ui, repo, ctx, oldctx, newnode, opts, internalchanges):
     parent = ctx.parents()[0].node()
     hg.update(repo, parent)
-    fd, patchfile = tempfile.mkstemp(prefix='hg-histedit-')
-    fp = os.fdopen(fd, 'w')
-    diffopts = patch.diffopts(ui, opts)
-    diffopts.git = True
-    diffopts.ignorews = False
-    diffopts.ignorewsamount = False
-    diffopts.ignoreblanklines = False
-    gen = patch.diff(repo, parent, newnode, opts=diffopts)
-    for chunk in gen:
-        fp.write(chunk)
-    fp.close()
-    files = set()
-    try:
-        patch.patch(ui, repo, patchfile, files=files, eolmode=None)
-    finally:
-        os.unlink(patchfile)
+    files = patchhistories(ui, repo, parent, newnode, opts)
     newmessage = '\n***\n'.join(
         [ctx.description()] +
         [repo[r].description() for r in internalchanges] +
@@ -326,23 +287,8 @@
 def message(ui, repo, ctx, ha, opts):
     oldctx = repo[ha]
     hg.update(repo, ctx.node())
-    fd, patchfile = tempfile.mkstemp(prefix='hg-histedit-')
-    fp = os.fdopen(fd, 'w')
-    diffopts = patch.diffopts(ui, opts)
-    diffopts.git = True
-    diffopts.ignorews = False
-    diffopts.ignorewsamount = False
-    diffopts.ignoreblanklines = False
-    gen = patch.diff(repo, oldctx.parents()[0].node(), ha, opts=diffopts)
-    for chunk in gen:
-        fp.write(chunk)
-    fp.close()
     try:
-        files = set()
-        try:
-            patch.patch(ui, repo, patchfile, files=files, eolmode=None)
-        finally:
-            os.unlink(patchfile)
+        files = patchhistories(ui, repo, oldctx.parents()[0].node(), ha, opts)
     except Exception:
         raise util.Abort(_('Fix up the change and run '
                            'hg histedit --continue'))


More information about the Mercurial-devel mailing list