Patchwork [2,of,4,V2] crecord: refactor hunk edit action to use ui.edit

login
register
mail settings
Submitter Jordi Gutiérrez Hermoso
Date March 21, 2016, 1:09 a.m.
Message ID <caefdf7d3b1a02476ed7.1458522579@Iris>
Download mbox | patch
Permalink /patch/14002/
State Accepted
Headers show

Comments

Jordi Gutiérrez Hermoso - March 21, 2016, 1:09 a.m.
# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh@octave.org>
# Date 1458521945 14400
#      Sun Mar 20 20:59:05 2016 -0400
# Node ID caefdf7d3b1a02476ed7c669857d9acb3b9c91fb
# Parent  c5bd96cb2fabf014db931430d266627633117dfd
crecord: refactor hunk edit action to use ui.edit

The previous version of this code did a lot of dancing around a
temporary edit file that ui.edit already handles.

Patch

diff --git a/mercurial/crecord.py b/mercurial/crecord.py
--- a/mercurial/crecord.py
+++ b/mercurial/crecord.py
@@ -1515,36 +1515,25 @@  are you sure you want to review/edit and
     all lines of the hunk are removed, then the edit is aborted and
     the hunk is left unchanged.
     """)
-            (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-",
-                    suffix=".diff", text=True)
-            ncpatchfp = None
+            # write the initial patch
+            patch = cStringIO.StringIO()
+            patch.write(''.join(['# %s\n' % i for i in phelp.splitlines()]))
+            chunk.header.write(patch)
+            chunk.write(patch)
+
+            # start the editor and wait for it to complete
             try:
-                # write the initial patch
-                f = os.fdopen(patchfd, "w")
-                chunk.header.write(f)
-                chunk.write(f)
-                f.write('\n'.join(['# ' + i for i in phelp.splitlines()]))
-                f.close()
-                # start the editor and wait for it to complete
-                editor = self.ui.geteditor()
-                ret = self.ui.system("%s \"%s\"" % (editor, patchfn),
-                          environ={'hguser': self.ui.username()})
-                if ret != 0:
-                    self.errorstr = "Editor exited with status %d" % ret
-                    return None
-                # remove comment lines
-                patchfp = open(patchfn)
-                ncpatchfp = cStringIO.StringIO()
-                for line in patchfp:
-                    if not line.startswith('#'):
-                        ncpatchfp.write(line)
-                patchfp.close()
-                ncpatchfp.seek(0)
-                newpatches = patchmod.parsepatch(ncpatchfp)
-            finally:
-                os.unlink(patchfn)
-                del ncpatchfp
-            return newpatches
+                patch = self.ui.edit(patch.getvalue(), "",
+                                     extra={"suffix": ".diff"})
+            except error.Abort as exc:
+                self.errorstr = str(exc)
+                return None
+
+            # remove comment lines
+            patch = [line + '\n' for line in patch.splitlines()
+                     if not line.startswith('#')]
+            return patchmod.parsepatch(patch)
+
         if item is None:
             item = self.currentselecteditem
         if isinstance(item, uiheader):