Commits

Anonymous committed 1923043

Initial implementation of refresh all

  • Participants
  • Parent commits 005c6f8

Comments (0)

Files changed (1)

File editcommitmsgs.py

 This extension do only have an effect if Mercurial Queues (mq) is enables.
 Then it will allow you to edit commit messages of all patches in a single file.
 '''
+import os
+
 from mercurial import extensions
+from mercurial import patch
+from mercurial import util
+from mercurial import cmdutil
 from mercurial.i18n import _
 
+_patchlinestarter = "HG: Patch: "
+
 def editcommitmsgs(ui, repo):
-    ui.write('Not implemented yet')
+    """"Edit commit message of all applied patches"""
+    try:
+        mq = extensions.find('mq')
+    except KeyError:
+        return
+
+    if not repo.mq.applied:
+        ui.warn(_('No patches applied'))
+        return
+
+    allmessages = introductiontext();
+    patchmap = {}
+    appliedbefore = repo.mq.applied[:]
+
+    #temporarily disable writing
+    oldwrite = ui.write
+    def nowrite(self, *args, **opts):
+        pass
+    ui.write = nowrite
+
+    for patch in repo.mq.applied:
+        patchheader = mq.patchheader(os.path.join(repo.mq.path, patch.name))
+        patchmap[patch.name] = patchheader
+        allmessages += _patchlinestarter + patch.name + '\n'
+        allmessages += ('\n').join(patchheader.message) + '\n'
+        allmessages += "HG: --\n"
+
+    editedmessages = ui.edit(allmessages, ui.username)
+    commitmessages = parsemessage(editedmessages)
+    savecommitcomments(ui, repo, mq, appliedbefore, commitmessages)
+    ui.write = oldwrite
+
+def introductiontext():
+    """"Returns an introduction text for the edit file"""
+    return _("HG: Enter commit messages for the different patches.\n"
+       "HG: Lines beginning with 'HG:' is used to apply the\n"
+       "HG: messages to the right patch, and gives some status on the patch\n"
+       "HG: Which can help you write the commit messages, so please do not\n"
+       "HG: edit them\n"
+       "HG: --\n"
+       "HG: Applied patches:\n"
+       "HG: --\n");
+
+def parsemessage(text):
+    """Parses the edited text.
+
+    It parses it to a map from patch name to lines of the commit messages
+    """
+    currentpatch = None
+    commitcomments = {}
+    for line in text.splitlines():
+        if line.startswith(_patchlinestarter):
+            currentpatch = line.replace(_patchlinestarter, "")
+        elif line.startswith("HG: "):
+            continue
+        else:
+            if currentpatch != None:
+                if not currentpatch in commitcomments:
+                    commitcomments[currentpatch] = []
+                commitcomments[currentpatch].append(line)
+    return commitcomments
+
+def savecommitcomments(ui, repo, mq, appliedbefore, commitmessages):
+    """Saves commit messages.
+
+    It does so by first popping all applied patches, then pushing them back
+    one by one, while updating the commit messages.
+    """
+    mq.pop(ui, repo, all=True)
+    for patch in appliedbefore:
+        mq.push(ui, repo, patch.name)
+        message = '\n'.join(commitmessages[patch.name])
+        mq.refresh(ui, repo, message=message)
 
 def uisetup(ui):
     try: