D464: editor: use an unambiguous path suffix for editor files

mbolin (Michael Bolin) phabricator at mercurial-scm.org
Wed Aug 30 15:38:23 EDT 2017


mbolin updated this revision to Diff 1448.
mbolin added a comment.


  Address "don't export and assign at once" issue.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D464?vs=1447&id=1448

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D464

AFFECTED FILES
  hgext/histedit.py
  hgext/patchbomb.py
  mercurial/cmdutil.py
  mercurial/crecord.py
  mercurial/ui.py
  tests/test-editor-filename.t

CHANGE DETAILS

diff --git a/tests/test-editor-filename.t b/tests/test-editor-filename.t
new file mode 100644
--- /dev/null
+++ b/tests/test-editor-filename.t
@@ -0,0 +1,36 @@
+Test temp file used with an editor has the expected suffix.
+
+  $ hg init t
+  $ cd t
+
+Create an editor that writes its arguments to stdout and set it to $HGEDITOR.
+
+  $ cat > editor.sh << EOF
+  > #!/bin/bash
+  > echo "\$@"
+  > exit 1
+  > EOF
+  $ chmod +x editor.sh
+  $ hg add editor.sh
+  $ HGEDITOR=`realpath ./editor.sh`
+  $ export HGEDITOR
+
+Verify that the path for a commit editor has the expected suffix.
+
+  $ hg commit
+  *.commit.hg.txt (glob)
+  abort: edit failed: editor.sh exited with status 1
+  [255]
+
+Verify that the path for a histedit editor has the expected suffix.
+
+  $ cat >> $HGRCPATH <<EOF
+  > [extensions]
+  > rebase=
+  > histedit=
+  > EOF
+  $ hg commit --message 'At least one commit for histedit.'
+  $ hg histedit
+  *.histedit.hg.txt (glob)
+  abort: edit failed: editor.sh exited with status 1
+  [255]
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1345,20 +1345,31 @@
             self.write(*msg, **opts)
 
     def edit(self, text, user, extra=None, editform=None, pending=None,
-             repopath=None):
+             repopath=None, action=None):
+        if action is None:
+            self.develwarn('action is None but will soon be a required '
+                           'parameter to ui.edit()')
         extra_defaults = {
             'prefix': 'editor',
             'suffix': '.txt',
         }
         if extra is not None:
+            if extra.get('suffix') is not None:
+                self.develwarn('extra.suffix is not None but will soon be '
+                               'ignored by ui.edit()')
             extra_defaults.update(extra)
         extra = extra_defaults
 
+        if action:
+            suffix = '.%s.hg.txt' % action
+        else:
+            suffix = extra['suffix']
+
         rdir = None
         if self.configbool('experimental', 'editortmpinhg'):
             rdir = repopath
         (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-',
-                                      suffix=extra['suffix'],
+                                      suffix=suffix,
                                       dir=rdir)
         try:
             f = os.fdopen(fd, r'wb')
diff --git a/mercurial/crecord.py b/mercurial/crecord.py
--- a/mercurial/crecord.py
+++ b/mercurial/crecord.py
@@ -1563,8 +1563,7 @@
 
             # start the editor and wait for it to complete
             try:
-                patch = self.ui.edit(patch.getvalue(), "",
-                                     extra={"suffix": ".diff"})
+                patch = self.ui.edit(patch.getvalue(), "", action="diff")
             except error.Abort as exc:
                 self.errorstr = str(exc)
                 return None
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -341,7 +341,7 @@
                              + crecordmod.patchhelptext
                              + fp.read())
                 reviewedpatch = ui.edit(patchtext, "",
-                                        extra={"suffix": ".diff"},
+                                        action="diff",
                                         repopath=repo.path)
                 fp.truncate(0)
                 fp.write(reviewedpatch)
@@ -3217,7 +3217,7 @@
 
     editortext = repo.ui.edit(committext, ctx.user(), ctx.extra(),
                               editform=editform, pending=pending,
-                              repopath=repo.path)
+                              repopath=repo.path, action='commit')
     text = editortext
 
     # strip away anything below this special string (used for editors that want
diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py
--- a/hgext/patchbomb.py
+++ b/hgext/patchbomb.py
@@ -308,7 +308,8 @@
     else:
         ui.write(_('\nWrite the introductory message for the '
                    'patch series.\n\n'))
-        body = ui.edit(defaultbody, sender, repopath=repo.path)
+        body = ui.edit(defaultbody, sender, repopath=repo.path,
+                       action='patchbombbody')
         # Save series description in case sendmail fails
         msgfile = repo.vfs('last-email.txt', 'wb')
         msgfile.write(body)
diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -1370,7 +1370,7 @@
     rules += '\n\n'
     rules += editcomment
     rules = ui.edit(rules, ui.username(), {'prefix': 'histedit'},
-                    repopath=repo.path)
+                    repopath=repo.path, action='histedit')
 
     # Save edit rules in .hg/histedit-last-edit.txt in case
     # the user needs to ask for help after something



To: mbolin, quark, durin42, #hg-reviewers, ryanmce
Cc: smf, ryanmce, mercurial-devel


More information about the Mercurial-devel mailing list