Implement diff.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 12 Nov 2010 08:56:41 +0000 (09:56 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 12 Nov 2010 08:56:41 +0000 (09:56 +0100)
buildfarm/history.py
buildfarm/tests/test_history.py

index 408ceb3c8b832ca8f86cf1e425e05be3225e8c6f..537ce48c4851f5715bf76843364924e4c6b28293 100644 (file)
 #   along with this program; if not, write to the Free Software
 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
+from cStringIO import StringIO
 
+from dulwich.objects import Tree
+from dulwich.patch import write_blob_diff
 from dulwich.repo import Repo
-import subprocess
-
-BASEDIR = "/home/build/master"
-HISTORYDIR = "/home/build/master/cache"
-TIMEZONE = "PST"
-TIMEOFFSET = 0
-UNPACKED_DIR = "/home/ftp/pub/unpacked"
 
 
 class Branch(object):
@@ -61,11 +57,28 @@ class GitBranch(object):
 
     def __init__(self, path, branch="master"):
         self.repo = Repo(path)
+        self.store = self.repo.object_store
         self.branch = branch
 
+    def _changes_for(self, commit):
+        if len(commit.parents) == 0:
+            parent_tree = Tree().id
+        else:
+            parent_tree = self.store[commit.parents[0]].tree
+        return self.store.tree_changes(parent_tree, commit.tree)
+
     def _revision_from_commit(self, commit):
-        # FIXME: modified/added/removed
-        return Revision(commit.id, commit.commit_time, commit.author, commit.message)
+        added = set()
+        modified = set()
+        removed = set()
+        for ((oldpath, newpath), (oldmode, newmode), (oldsha, newsha)) in self._changes_for(commit):
+            if oldpath is None:
+                added.add(newpath)
+            elif newpath is None:
+                removed.add(oldpath)
+            else:
+                modified.add(newpath)
+        return Revision(commit.id, commit.commit_time, commit.author, commit.message, modified=modified, removed=removed, added=added)
 
     def log(self, from_rev=None, exclude_revs=None):
         if from_rev is None:
@@ -91,5 +104,7 @@ class GitBranch(object):
 
     def diff(self, revision):
         commit = self.repo[revision]
-        x = subprocess.Popen(["git", "show", revision], cwd=self.repo.path, stdout=subprocess.PIPE)
-        return (self._revision_from_commit(commit), x.communicate()[0])
+        f = StringIO()
+        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in self._changes_for(commit):
+            write_blob_diff((oldpath, oldmode, self.store[oldsha]), (newpath, newmode, self.store[newsha]))
+        return (self._revision_from_commit(commit), f.getvalue())
index 0d072417987d9f6cd38e83603f0dd26912ea4372..29d7c1aa991f3fa9704f4b478dcfb700c267df4f 100644 (file)
@@ -21,7 +21,6 @@ from dulwich.repo import Repo
 
 import tempfile
 from testtools import TestCase
-from testtools.testcase import TestSkipped
 
 
 class GitBranchTests(TestCase):
@@ -46,5 +45,4 @@ class GitBranchTests(TestCase):
         revid = self.repo.do_commit("message", committer="Jelmer Vernooij")
         entry, diff = list(branch.diff(revid))
         self.assertEquals("message", entry.message)
-        raise TestSkipped("Must use alternative to git show")
         self.assertEquals("", diff)