[meld: 1/2] VcView: use extension with temp files so syntax highlight correct (#459)



commit f3957027c597b59b9eff60afc734984605b81d25
Author: Alan Suran <alan1apds gmail com>
Date:   Wed Jun 3 02:55:32 2020 -0400

    VcView: use extension with temp files so syntax highlight correct (#459)
    
        When comparing a file with its previous version, sometimes
        the syntax highlighting is incorrect in the previous
        version. This is because the algorithm used by the
        "LanguageManager.get_language_from_file" (and ultimately
        gio) uses the file extension first to determine the file
        language. When the local file has an extension and the
        remote file does not, the language manager might pick a different
        language for the remote.
    
        Since the files being compared are the same one, just
        different versions, use the local extension for the
        remote temp file.

 meld/vc/_vc.py       | 8 ++++++--
 meld/vc/bzr.py       | 3 ++-
 meld/vc/cvs.py       | 3 ++-
 meld/vc/darcs.py     | 4 +++-
 meld/vc/git.py       | 7 +++++--
 meld/vc/mercurial.py | 3 ++-
 meld/vc/svn.py       | 3 ++-
 7 files changed, 22 insertions(+), 9 deletions(-)
---
diff --git a/meld/vc/_vc.py b/meld/vc/_vc.py
index 54a66b8d..e55449e8 100644
--- a/meld/vc/_vc.py
+++ b/meld/vc/_vc.py
@@ -413,7 +413,7 @@ def popen(cmd, cwd=None, use_locale_encoding=True):
     return process.stdout
 
 
-def call_temp_output(cmd, cwd, file_id=''):
+def call_temp_output(cmd, cwd, file_id='', suffix=None):
     """Call `cmd` in `cwd` and write the output to a temporary file
 
     This returns the name of the temporary file used. It is the
@@ -421,6 +421,9 @@ def call_temp_output(cmd, cwd, file_id=''):
 
     If `file_id` is provided, it is used as part of the
     temporary file's name, for ease of identification.
+
+    If `suffix` is provided, it is used as the extension
+    of the temporary file's name.
     """
     process = subprocess.Popen(
         cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -430,7 +433,8 @@ def call_temp_output(cmd, cwd, file_id=''):
     # sane response is to return an empty temp file.
 
     prefix = 'meld-tmp' + ('-' + file_id if file_id else '')
-    with tempfile.NamedTemporaryFile(prefix=prefix, delete=False) as f:
+    with tempfile.NamedTemporaryFile(prefix=prefix,
+                                     suffix=suffix, delete=False) as f:
         shutil.copyfileobj(vc_file, f)
     return f.name
 
diff --git a/meld/vc/bzr.py b/meld/vc/bzr.py
index 65467584..f30ad9ab 100644
--- a/meld/vc/bzr.py
+++ b/meld/vc/bzr.py
@@ -220,12 +220,13 @@ class Vc(_vc.Vc):
             raise _vc.InvalidVCPath(self, path, "Path not in repository")
 
         path = path[len(self.root) + 1:]
+        suffix = os.path.splitext(path)[1]
 
         args = [self.CMD, "cat", path]
         if commit:
             args.append("-r%s" % commit)
 
-        return _vc.call_temp_output(args, cwd=self.root)
+        return _vc.call_temp_output(args, cwd=self.root, suffix=suffix)
 
     def get_path_for_conflict(self, path, conflict):
         if path in self._reverse_rename_cache and not \
diff --git a/meld/vc/cvs.py b/meld/vc/cvs.py
index f24f6040..a1192af1 100644
--- a/meld/vc/cvs.py
+++ b/meld/vc/cvs.py
@@ -92,8 +92,9 @@ class Vc(_vc.Vc):
             raise _vc.InvalidVCPath(self, path, "Path not in repository")
         path = path[len(self.root) + 1:]
 
+        suffix = os.path.splitext(path)[1]
         args = [self.CMD, "-q", "update", "-p", path]
-        return _vc.call_temp_output(args, cwd=self.root)
+        return _vc.call_temp_output(args, cwd=self.root, suffix=suffix)
 
     def _find_files(self, path):
         relfiles = []
diff --git a/meld/vc/darcs.py b/meld/vc/darcs.py
index cf09eee6..dd11866d 100644
--- a/meld/vc/darcs.py
+++ b/meld/vc/darcs.py
@@ -99,11 +99,13 @@ class Vc(_vc.Vc):
             path = self._reverse_rename_cache[path]
 
         path = path[len(self.root) + 1:]
+        suffix = os.path.splitext(path)[1]
         process = subprocess.Popen(
             [self.CMD, "show", "contents", path], cwd=self.root,
             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
-        with tempfile.NamedTemporaryFile(prefix='meld-tmp', delete=False) as f:
+        with tempfile.NamedTemporaryFile(prefix='meld-tmp',
+                                         suffix=suffix, delete=False) as f:
             shutil.copyfileobj(process.stdout, f)
         return f.name
 
diff --git a/meld/vc/git.py b/meld/vc/git.py
index 2e505a19..5e45aaf2 100644
--- a/meld/vc/git.py
+++ b/meld/vc/git.py
@@ -226,9 +226,11 @@ class Vc(_vc.Vc):
         if os.name == "nt":
             path = path.replace("\\", "/")
 
+        suffix = os.path.splitext(path)[1]
         args = ["git", "show", ":%s:%s" % (self.conflict_map[conflict], path)]
         filename = _vc.call_temp_output(
-            args, cwd=self.location, file_id=_vc.conflicts[conflict])
+            args, cwd=self.location,
+            file_id=_vc.conflicts[conflict], suffix=suffix)
         return filename, True
 
     def get_path_for_repo_file(self, path, commit=None):
@@ -244,8 +246,9 @@ class Vc(_vc.Vc):
             path = path.replace("\\", "/")
 
         obj = commit + ":" + path
+        suffix = os.path.splitext(path)[1]
         args = [self.CMD, "cat-file", "blob", obj]
-        return _vc.call_temp_output(args, cwd=self.root)
+        return _vc.call_temp_output(args, cwd=self.root, suffix=suffix)
 
     @classmethod
     def valid_repo(cls, path):
diff --git a/meld/vc/mercurial.py b/meld/vc/mercurial.py
index 61b734d0..6a995eb4 100644
--- a/meld/vc/mercurial.py
+++ b/meld/vc/mercurial.py
@@ -76,8 +76,9 @@ class Vc(_vc.Vc):
             raise _vc.InvalidVCPath(self, path, "Path not in repository")
         path = path[len(self.root) + 1:]
 
+        suffix = os.path.splitext(path)[1]
         args = [self.CMD, "cat", path]
-        return _vc.call_temp_output(args, cwd=self.root)
+        return _vc.call_temp_output(args, cwd=self.root, suffix=suffix)
 
     def _update_tree_state_cache(self, path):
         """ Update the state of the file(s) at self._tree_cache['path'] """
diff --git a/meld/vc/svn.py b/meld/vc/svn.py
index 7af7f198..6e829180 100644
--- a/meld/vc/svn.py
+++ b/meld/vc/svn.py
@@ -81,8 +81,9 @@ class Vc(_vc.Vc):
             raise _vc.InvalidVCPath(self, path, "Path not in repository")
         path = path[len(self.root) + 1:]
 
+        suffix = os.path.splitext(path)[1]
         args = [self.CMD, "cat", "-r", commit, path]
-        return _vc.call_temp_output(args, cwd=self.root)
+        return _vc.call_temp_output(args, cwd=self.root, suffix=suffix)
 
     def get_path_for_conflict(self, path, conflict=None):
         """


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]