comparison mercurial/filemerge.py @ 48787:69000dc0dced

filemerge: reduce some duplication in `_maketempfiles()` The two callers of the local `maketempfrompath()` function used the returned file object in the same way. We can reduce duplication by moving that code into the function. Differential Revision: https://phab.mercurial-scm.org/D12192
author Martin von Zweigbergk <martinvonz@google.com>
date Mon, 14 Feb 2022 22:16:29 -0800
parents f20feb496d3c
children f90337706ce7
comparison
equal deleted inserted replaced
48786:f20feb496d3c 48787:69000dc0dced
17 hex, 17 hex,
18 short, 18 short,
19 ) 19 )
20 from .pycompat import ( 20 from .pycompat import (
21 getattr, 21 getattr,
22 open,
23 ) 22 )
24 23
25 from . import ( 24 from . import (
26 encoding, 25 encoding,
27 error, 26 error,
921 None) copies `localpath` to another temporary file, so an external merge 920 None) copies `localpath` to another temporary file, so an external merge
922 tool may use them. 921 tool may use them.
923 """ 922 """
924 tmproot = pycompat.mkdtemp(prefix=b'hgmerge-') 923 tmproot = pycompat.mkdtemp(prefix=b'hgmerge-')
925 924
926 def maketempfrompath(prefix, path): 925 def maketempfrompath(prefix, path, data):
927 fullbase, ext = os.path.splitext(path) 926 fullbase, ext = os.path.splitext(path)
928 pre = b"%s~%s" % (os.path.basename(fullbase), prefix) 927 pre = b"%s~%s" % (os.path.basename(fullbase), prefix)
929 name = os.path.join(tmproot, pre) 928 name = os.path.join(tmproot, pre)
930 if ext: 929 if ext:
931 name += ext 930 name += ext
932 f = open(name, "wb") 931 util.writefile(name, data)
933 return f, name 932 return name
934 933
935 def tempfromcontext(prefix, ctx): 934 def tempfromcontext(prefix, ctx):
936 f, name = maketempfrompath(prefix, ctx.path()) 935 return maketempfrompath(prefix, ctx.path(), ctx.decodeddata())
937 data = ctx.decodeddata()
938 f.write(data)
939 f.close()
940 return name
941 936
942 b = tempfromcontext(b"base", fca) 937 b = tempfromcontext(b"base", fca)
943 c = tempfromcontext(b"other", fco) 938 c = tempfromcontext(b"other", fco)
944 d = localpath 939 d = localpath
945 if localpath is not None: 940 if localpath is not None:
946 f, d = maketempfrompath(b"local", d)
947 data = util.readfile(localpath) 941 data = util.readfile(localpath)
948 f.write(data) 942 d = maketempfrompath(b"local", localpath, data)
949 f.close()
950 943
951 try: 944 try:
952 yield b, c, d 945 yield b, c, d
953 finally: 946 finally:
954 shutil.rmtree(tmproot) 947 shutil.rmtree(tmproot)