changeset 28550:e2b9145e35d8

histedit: do not close stdin Closing stdin is unexpected by chgserver and is not a good idea generally. This patch refactors related code a bit and make sure stdin is not closed. It will make chg much happier on test-histedit*.t.
author Jun Wu <quark@fb.com>
date Tue, 15 Mar 2016 00:42:33 +0000
parents e01bd7385f4f
children 8e5312f8df30
files hgext/histedit.py
diffstat 1 files changed, 9 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/histedit.py	Tue Mar 15 09:51:54 2016 +0000
+++ b/hgext/histedit.py	Tue Mar 15 00:42:33 2016 +0000
@@ -998,6 +998,13 @@
         return goaleditplan
     return goalnew
 
+def _readfile(path):
+    if path == '-':
+        return sys.stdin.read()
+    else:
+        with open(path, 'rb') as f:
+            return f.read()
+
 def _validateargs(ui, repo, state, freeargs, opts, goal, rules, revs):
     # TODO only abort if we try to histedit mq patches, not just
     # blanket if mq patches are applied somewhere
@@ -1190,12 +1197,7 @@
                                  node.short(state.topmost))
         rules = ruleeditor(repo, ui, state.actions, comment)
     else:
-        if rules == '-':
-            f = sys.stdin
-        else:
-            f = open(rules)
-        rules = f.read()
-        f.close()
+        rules = _readfile(rules)
     actions = parserules(rules, state)
     ctxs = [repo[act.nodetoverify()] \
             for act in state.actions if act.nodetoverify()]
@@ -1236,12 +1238,7 @@
         actions = [pick(state, r) for r in revs]
         rules = ruleeditor(repo, ui, actions, comment)
     else:
-        if rules == '-':
-            f = sys.stdin
-        else:
-            f = open(rules)
-        rules = f.read()
-        f.close()
+        rules = _readfile(rules)
     actions = parserules(rules, state)
     warnverifyactions(ui, repo, actions, state, ctxs)