Patchwork [1,of,3] histedit: extract method ruleeditor

login
register
mail settings
Submitter Mateusz Kwapich
Date Feb. 23, 2015, 7:38 p.m.
Message ID <5876af0d2972dbed4e14.1424720303@dev1429.prn1.facebook.com>
Download mbox | patch
Permalink /patch/7820/
State Accepted
Headers show

Comments

Mateusz Kwapich - Feb. 23, 2015, 7:38 p.m.
# HG changeset patch
# User Mateusz Kwapich <mitrandir@fb.com>
# Date 1424717847 28800
#      Mon Feb 23 10:57:27 2015 -0800
# Node ID 5876af0d2972dbed4e147d7bd1b38a24e6908b10
# Parent  ff5caa8dfd993680d9602ca6ebb14da9de10d5f4
histedit: extract method ruleeditor

Extract functionality of editing histedit rules to separate method so we can
reuse it in upcoming --edit-plan option.

Patch

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -638,16 +638,9 @@  def _histedit(ui, repo, state, *freeargs
 
         ctxs = [repo[r] for r in revs]
         if not rules:
-            rules = '\n'.join([makedesc(c) for c in ctxs])
-            rules += '\n\n'
-            rules += editcomment % (node.short(root), node.short(topmost))
-            rules = ui.edit(rules, ui.username())
-            # Save edit rules in .hg/histedit-last-edit.txt in case
-            # the user needs to ask for help after something
-            # surprising happens.
-            f = open(repo.join('histedit-last-edit.txt'), 'w')
-            f.write(rules)
-            f.close()
+            rules = ruleeditor(repo, ui, [['pick', c] for c in ctxs],
+                               editcomment=editcomment % (node.short(root),
+                                                          node.short(topmost)))
         else:
             if rules == '-':
                 f = sys.stdin
@@ -819,6 +812,25 @@  def makedesc(c):
     # trim to 80 columns so it's not stupidly wide in my editor
     return util.ellipsis(line, 80)
 
+def ruleeditor(repo, ui, rules, editcomment=""):
+    """open an editor to edit rules
+
+    rules are in the format [ [act, ctx], ...] like in state.rules
+    """
+    rules = '\n'.join([makedesc(repo[rev]) for [act, rev] in rules])
+    rules += '\n\n'
+    rules += editcomment
+    rules = ui.edit(rules, ui.username())
+
+    # Save edit rules in .hg/histedit-last-edit.txt in case
+    # the user needs to ask for help after something
+    # surprising happens.
+    f = open(repo.join('histedit-last-edit.txt'), 'w')
+    f.write(rules)
+    f.close()
+
+    return rules
+
 def verifyrules(rules, repo, ctxs):
     """Verify that there exists exactly one edit rule per given changeset.