[meld] Add git support for new file request interface



commit 05a37fd12efcfe6643e5ebe4510011611e9d7c13
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Mon Sep 26 07:59:47 2011 +1000

    Add git support for new file request interface

 meld/vc/_vc.py |   27 +++++++++++++++++++++++++++
 meld/vc/git.py |   24 ++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 0 deletions(-)
---
diff --git a/meld/vc/_vc.py b/meld/vc/_vc.py
index 61a583e..b9b8a0c 100644
--- a/meld/vc/_vc.py
+++ b/meld/vc/_vc.py
@@ -234,6 +234,33 @@ class CachedVc(Vc):
             self.cache_inventory(directory)
         return self._tree_cache
 
+
+class InvalidVCPath(ValueError):
+    """Raised when a VC module is passed an invalid (or not present) path."""
+
+    def __init__(self, vc, path, err):
+        self.vc = vc
+        self.path = path
+        self.error = err
+
+    def __str__(self):
+        return "%s: Path %s is invalid or not present\nError: %s\n" % \
+              (self.vc.NAME, self.path, self.error)
+
+
+class InvalidVCRevision(ValueError):
+    """Raised when a VC module is passed a revision spec it can't handle."""
+
+    def __init__(self, vc, rev, err):
+        self.vc = vc
+        self.revision = rev
+        self.error = err
+
+    def __str__(self):
+        return "%s: Doesn't understand or have revision %s\nError: %s\n" % \
+              (self.vc.NAME, self.revision, self.error)
+
+
 # Return the stdout output of a given command
 def popen(cmd, cwd=None):
     return subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE).stdout
diff --git a/meld/vc/git.py b/meld/vc/git.py
index 7ea55df..a352d2d 100644
--- a/meld/vc/git.py
+++ b/meld/vc/git.py
@@ -31,6 +31,8 @@
 import errno
 import os
 import re
+import shutil
+import tempfile
 
 from . import _vc
 
@@ -76,6 +78,28 @@ class Vc(_vc.CachedVc):
         return [self.CMD,"rm"]
     def revert_command(self):
         return [self.CMD,"checkout"]
+
+    def get_repo_file_path(self, path, commit=None):
+        if commit is None:
+            commit = "HEAD"
+        else:
+            raise NotImplementedError()
+
+        if not path.startswith(self.root + os.path.sep):
+            raise _vc.InvalidVCPath(self, path, "Path not in repository")
+        path = path[len(self.root) + 1:]
+
+        vc_file = _vc.popen(["git", "cat-file", "blob", commit + ":" + path],
+                            cwd=self.location)
+
+        # TODO: In Python 2.6+, this could be done with NamedTemporaryFile
+        tmp_handle, tmp_path = tempfile.mkstemp(prefix='meld-tmp', text=True)
+        tmp_file = os.fdopen(tmp_handle, 'w')
+        shutil.copyfileobj(vc_file, tmp_file)
+        tmp_file.close()
+
+        return tmp_path
+
     def valid_repo(self):
         # TODO: On Windows, this exit code is wrong under the normal shell; it
         # appears to be correct under the default git bash shell however.



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