Patchwork D12192: filemerge: reduce some duplication in `_maketempfiles()`

login
register
mail settings
Submitter phabricator
Date Feb. 16, 2022, 4:09 a.m.
Message ID <differential-rev-PHID-DREV-ylnueyothzznhqp4tza6-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50529/
State New
Headers show

Comments

phabricator - Feb. 16, 2022, 4:09 a.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D12192

AFFECTED FILES
  mercurial/filemerge.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -19,7 +19,6 @@ 
 )
 from .pycompat import (
     getattr,
-    open,
 )
 
 from . import (
@@ -923,30 +922,24 @@ 
     """
     tmproot = pycompat.mkdtemp(prefix=b'hgmerge-')
 
-    def maketempfrompath(prefix, path):
+    def maketempfrompath(prefix, path, data):
         fullbase, ext = os.path.splitext(path)
         pre = b"%s~%s" % (os.path.basename(fullbase), prefix)
         name = os.path.join(tmproot, pre)
         if ext:
             name += ext
-        f = open(name, "wb")
-        return f, name
+        util.writefile(name, data)
+        return name
 
     def tempfromcontext(prefix, ctx):
-        f, name = maketempfrompath(prefix, ctx.path())
-        data = ctx.decodeddata()
-        f.write(data)
-        f.close()
-        return name
+        return maketempfrompath(prefix, ctx.path(), ctx.decodeddata())
 
     b = tempfromcontext(b"base", fca)
     c = tempfromcontext(b"other", fco)
     d = localpath
     if localpath is not None:
-        f, d = maketempfrompath(b"local", d)
         data = util.readfile(localpath)
-        f.write(data)
-        f.close()
+        d = maketempfrompath(b"local", localpath, data)
 
     try:
         yield b, c, d