comparison mercurial/crecord.py @ 28636:de64020bb4ec

crecord: refactor hunk edit action to use ui.edit The previous version of this code did a lot of dancing around a temporary edit file that ui.edit already handles.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sun, 20 Mar 2016 20:59:05 -0400
parents 3c8f0a605504
children 4874b8efe7d2
comparison
equal deleted inserted replaced
28635:87f92d6f0bc3 28636:de64020bb4ec
15 import os 15 import os
16 import re 16 import re
17 import signal 17 import signal
18 import struct 18 import struct
19 import sys 19 import sys
20 import tempfile
21 20
22 from .i18n import _ 21 from .i18n import _
23 from . import ( 22 from . import (
24 encoding, 23 encoding,
25 error, 24 error,
1496 added to the record list. if it does not apply cleanly, a rejects 1495 added to the record list. if it does not apply cleanly, a rejects
1497 file will be generated: you can use that when you try again. if 1496 file will be generated: you can use that when you try again. if
1498 all lines of the hunk are removed, then the edit is aborted and 1497 all lines of the hunk are removed, then the edit is aborted and
1499 the hunk is left unchanged. 1498 the hunk is left unchanged.
1500 """) 1499 """)
1501 (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-", 1500 # write the initial patch
1502 suffix=".diff", text=True) 1501 patch = cStringIO.StringIO()
1503 ncpatchfp = None 1502 patch.write(''.join(['# %s\n' % i for i in phelp.splitlines()]))
1503 chunk.header.write(patch)
1504 chunk.write(patch)
1505
1506 # start the editor and wait for it to complete
1504 try: 1507 try:
1505 # write the initial patch 1508 patch = self.ui.edit(patch.getvalue(), "",
1506 f = os.fdopen(patchfd, "w") 1509 extra={"suffix": ".diff"})
1507 chunk.header.write(f) 1510 except error.Abort as exc:
1508 chunk.write(f) 1511 self.errorstr = str(exc)
1509 f.write('\n'.join(['# ' + i for i in phelp.splitlines()])) 1512 return None
1510 f.close() 1513
1511 # start the editor and wait for it to complete 1514 # remove comment lines
1512 editor = self.ui.geteditor() 1515 patch = [line + '\n' for line in patch.splitlines()
1513 ret = self.ui.system("%s \"%s\"" % (editor, patchfn), 1516 if not line.startswith('#')]
1514 environ={'hguser': self.ui.username()}) 1517 return patchmod.parsepatch(patch)
1515 if ret != 0: 1518
1516 self.errorstr = "Editor exited with status %d" % ret
1517 return None
1518 # remove comment lines
1519 patchfp = open(patchfn)
1520 ncpatchfp = cStringIO.StringIO()
1521 for line in patchfp:
1522 if not line.startswith('#'):
1523 ncpatchfp.write(line)
1524 patchfp.close()
1525 ncpatchfp.seek(0)
1526 newpatches = patchmod.parsepatch(ncpatchfp)
1527 finally:
1528 os.unlink(patchfn)
1529 del ncpatchfp
1530 return newpatches
1531 if item is None: 1519 if item is None:
1532 item = self.currentselecteditem 1520 item = self.currentselecteditem
1533 if isinstance(item, uiheader): 1521 if isinstance(item, uiheader):
1534 return 1522 return
1535 if isinstance(item, uihunkline): 1523 if isinstance(item, uihunkline):