commit: move commit editor to cmdutil, pass as function
authorMatt Mackall <mpm@selenic.com>
Thu, 14 May 2009 13:20:40 -0500
changeset 8407 223000a687b0
parent 8406 6ad1f72bdf34
child 8408 72538f1909ec
commit: move commit editor to cmdutil, pass as function
hgext/fetch.py
hgext/keyword.py
mercurial/cmdutil.py
mercurial/commands.py
mercurial/localrepo.py
--- a/hgext/fetch.py	Thu May 14 13:20:40 2009 -0500
+++ b/hgext/fetch.py	Thu May 14 13:20:40 2009 -0500
@@ -124,10 +124,11 @@
             message = (cmdutil.logmessage(opts) or
                        (_('Automated merge with %s') %
                         url.removeauth(other.url())))
-            force_editor = opts.get('force_editor') or opts.get('edit')
-            n = repo.commit(mod + add + rem, message,
-                            opts['user'], opts['date'], force=True,
-                            force_editor=force_editor)
+            editor = cmdutil.commiteditor
+            if opts.get('force_editor') or opts.get('edit'):
+                editor = cmdutil.commitforceeditor
+            n = repo.commit(mod + add + rem, message, opts['user'],
+                            opts['date'], force=True, editor=editor)
             ui.status(_('new changeset %d:%s merges remote changes '
                         'with local\n') % (repo.changelog.rev(n),
                                            short(n)))
--- a/hgext/keyword.py	Thu May 14 13:20:40 2009 -0500
+++ b/hgext/keyword.py	Thu May 14 13:20:40 2009 -0500
@@ -450,8 +450,7 @@
             return kwt.wread(filename, data)
 
         def commit(self, files=None, text='', user=None, date=None,
-                   match=None, force=False, force_editor=False,
-                   extra={}, empty_ok=False):
+                   match=None, force=False, editor=None, extra={}):
             wlock = lock = None
             _p1 = _p2 = None
             try:
@@ -473,8 +472,7 @@
                         _p2 = hex(_p2)
 
                 n = super(kwrepo, self).commit(files, text, user, date, match,
-                                               force, force_editor,
-                                               extra, empty_ok)
+                                               force, editor, extra)
 
                 # restore commit hooks
                 for name, cmd in commithooks.iteritems():
--- a/mercurial/cmdutil.py	Thu May 14 13:20:40 2009 -0500
+++ b/mercurial/cmdutil.py	Thu May 14 13:20:40 2009 -0500
@@ -1221,3 +1221,40 @@
         return commitfunc(ui, repo, message, m, opts)
     except ValueError, inst:
         raise util.Abort(str(inst))
+
+def commiteditor(repo, ctx, added, updated, removed):
+    if ctx.description():
+        return ctx.description()
+    return commitforceeditor(repo, ctx, added, updated, removed)
+
+def commitforceeditor(repo, ctx, added, updated, removed):
+    edittext = []
+    if ctx.description():
+        edittext.append(ctx.description())
+    edittext.append("")
+    edittext.append("") # Empty line between message and comments.
+    edittext.append(_("HG: Enter commit message."
+                      "  Lines beginning with 'HG:' are removed."))
+    edittext.append("HG: --")
+    edittext.append(_("HG: user: %s") % ctx.user())
+    if ctx.p2():
+        edittext.append(_("HG: branch merge"))
+    if ctx.branch():
+        edittext.append(_("HG: branch '%s'")
+                        % encoding.tolocal(ctx.branch()))
+    edittext.extend([_("HG: added %s") % f for f in added])
+    edittext.extend([_("HG: changed %s") % f for f in updated])
+    edittext.extend([_("HG: removed %s") % f for f in removed])
+    if not added and not updated and not removed:
+        edittext.append(_("HG: no files changed"))
+    edittext.append("")
+    # run editor in the repository root
+    olddir = os.getcwd()
+    os.chdir(repo.root)
+    text = repo.ui.edit("\n".join(edittext), ctx.user())
+    os.chdir(olddir)
+
+    if not text.strip():
+        raise util.Abort(_("empty commit message"))
+
+    return text
--- a/mercurial/commands.py	Thu May 14 13:20:40 2009 -0500
+++ b/mercurial/commands.py	Thu May 14 13:20:40 2009 -0500
@@ -642,10 +642,13 @@
     extra = {}
     if opts.get('close_branch'):
         extra['close'] = 1
+    e = cmdutil.commiteditor
+    if opts.get('force_editor'):
+        e = cmdutil.commitforceeditor
+
     def commitfunc(ui, repo, message, match, opts):
         return repo.commit(match.files(), message, opts.get('user'),
-            opts.get('date'), match, force_editor=opts.get('force_editor'),
-            extra=extra)
+            opts.get('date'), match, editor=e, extra=extra)
 
     node = cmdutil.commit(ui, repo, commitfunc, pats, opts)
     if not node:
@@ -1741,7 +1744,8 @@
                     files = patch.updatedir(ui, repo, files, similarity=sim/100.)
                 if not opts.get('no_commit'):
                     n = repo.commit(files, message, opts.get('user') or user,
-                                    opts.get('date') or date)
+                                    opts.get('date') or date,
+                                    editor=cmdutil.commiteditor)
                     if opts.get('exact'):
                         if hex(n) != nodeid:
                             repo.rollback()
--- a/mercurial/localrepo.py	Thu May 14 13:20:40 2009 -0500
+++ b/mercurial/localrepo.py	Thu May 14 13:20:40 2009 -0500
@@ -768,7 +768,7 @@
         return fparent1
 
     def commit(self, files=None, text="", user=None, date=None, match=None,
-               force=False, force_editor=False, extra={}, empty_ok=False):
+               force=False, editor=False, extra={}):
         wlock = lock = None
         if extra.get("close"):
             force = True
@@ -811,7 +811,7 @@
                                                     "(see hg resolve)"))
             wctx = context.workingctx(self, (p1, p2), text, user, date,
                                       extra, changes)
-            r = self._commitctx(wctx, force, force_editor, empty_ok, True)
+            r = self._commitctx(wctx, force, editor, True)
             ms.reset()
             return r
 
@@ -824,11 +824,9 @@
         Revision information is passed in the context.memctx argument.
         commitctx() does not touch the working directory.
         """
-        return self._commitctx(ctx, force=True, force_editor=False,
-                               empty_ok=True, working=False)
+        return self._commitctx(ctx, force=True, editor=None, working=False)
 
-    def _commitctx(self, ctx, force=False, force_editor=False, empty_ok=False,
-                   working=True):
+    def _commitctx(self, ctx, force=False, editor=None, working=True):
         lock = self.lock()
         tr = None
         valid = 0 # don't save the dirstate if this isn't set
@@ -895,39 +893,12 @@
             mn = self.manifest.add(m1, trp, linkrev, c1[0], c2[0],
                                    (new, removed1))
 
-            # add changeset
-            if (not empty_ok and not text) or force_editor:
-                edittext = []
-                if text:
-                    edittext.append(text)
-                edittext.append("")
-                edittext.append("") # Empty line between message and comments.
-                edittext.append(_("HG: Enter commit message."
-                                  "  Lines beginning with 'HG:' are removed."))
-                edittext.append("HG: --")
-                edittext.append(_("HG: user: %s") % user)
-                if p2 != nullid:
-                    edittext.append(_("HG: branch merge"))
-                if branchname:
-                    edittext.append(_("HG: branch '%s'")
-                                    % encoding.tolocal(branchname))
-                edittext.extend([_("HG: added %s") % f for f in added])
-                edittext.extend([_("HG: changed %s") % f for f in updated])
-                edittext.extend([_("HG: removed %s") % f for f in removed])
-                if not added and not updated and not removed:
-                    edittext.append(_("HG: no files changed"))
-                edittext.append("")
-                # run editor in the repository root
-                olddir = os.getcwd()
-                os.chdir(self.root)
-                text = self.ui.edit("\n".join(edittext), user)
-                os.chdir(olddir)
+            if editor:
+                text = editor(self, ctx, added, updated, removed)
 
             lines = [line.rstrip() for line in text.rstrip().splitlines()]
             while lines and not lines[0]:
                 del lines[0]
-            if not lines and working:
-                raise util.Abort(_("empty commit message"))
             text = '\n'.join(lines)
 
             self.changelog.delayupdate()