changeset 509:c4288095887f

update git tag detector and add test
author Bjoern Ricks <bricks@intevation.de>
date Tue, 09 Nov 2010 14:54:55 +0000
parents 29f6d1f5cc53
children 1f04bd88fca3
files test/test_git.py treepkg/git.py
diffstat 2 files changed, 71 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test_git.py	Tue Nov 09 14:54:55 2010 +0000
@@ -0,0 +1,57 @@
+# Copyright (C) 2010 by Intevation GmbH
+# Authors:
+# Bjoern Ricks <bjoern.ricks@intevation.de>
+#
+# This program is free software under the GPL (>=v2)
+# Read the file COPYING coming with the software for details.
+
+"""Tests for the treepkg.git module"""
+
+
+import unittest
+import os
+
+import treepkg.run as run
+from treepkg.cmdexpand import cmdexpand
+from filesupport import FileTestMixin
+
+import treepkg.git as git
+
+class TestTagDetector(unittest.TestCase, FileTestMixin):
+
+    def test_tag(self):
+        dirname = self.create_test_specific_temp_dir()
+        repodir = self.create_temp_dir("repo")
+        clonedir = os.path.join(dirname, "clone")
+        run.call(cmdexpand("git init -q"), cwd=repodir)
+
+        file = open(os.path.join(repodir, "tmp"), "w")
+        file.write("test")
+        file.close()
+
+        run.call(cmdexpand("git add tmp"), cwd=repodir)
+        run.call(cmdexpand("git commit -m \"Add tmp file\""), cwd=repodir)
+        run.call(cmdexpand("git tag tag1"), cwd=repodir)
+        run.call(cmdexpand("git tag tag2"), cwd=repodir)
+        run.call(cmdexpand("git tag anothertag"), cwd=repodir)
+
+        tagdetector1 = git.TagDetector(repodir, "tag*", clonedir)
+        tags = tagdetector1.list_tags()
+        urlrev = tagdetector1.newest_tag_revision()
+
+        self.assertEquals(len(tags), 2)
+        self.assertEquals(urlrev[0], "tag2")
+
+        tagdetector2 = git.TagDetector(dirname, "*tag*", clonedir)
+        tags = tagdetector2.list_tags()
+        urlrev = tagdetector2.newest_tag_revision()
+        
+        self.assertEquals(len(tags), 3)
+        self.assertEquals(urlrev[0], "tag2")
+
+        tagdetector3 = git.TagDetector(dirname, "another*", clonedir)
+        tags = tagdetector3.list_tags()
+        urlrev = tagdetector3.newest_tag_revision()
+
+        self.assertEquals(len(tags), 1)
+        self.assertEquals(urlrev[0], "anothertag")
--- a/treepkg/git.py	Mon Nov 08 17:07:41 2010 +0000
+++ b/treepkg/git.py	Tue Nov 09 14:54:55 2010 +0000
@@ -21,15 +21,12 @@
 
     """Base class for Git specific errors raised by TreePKG"""
 
-def checkout(url, localdir, branch=None):
+def checkout(url, localdir, branch="master"):
     """Clones the repository at url into the localdir"""
-    run.call(cmdexpand("git clone $url $localdir", **locals()))
+    run.call(cmdexpand("git clone -q $url $localdir", **locals()))
     if branch:
-        run.call(cmdexpand("git checkout --track -b local $branch",
+        run.call(cmdexpand("git checkout -q --track -b local $branch",
                             **locals()), cwd=localdir)
-    else:
-        run.call(cmdexpand("git checkout --track -b local master"),
-                            cwd=localdir)
 
 def update(localdir, revision=None):
     """Runs git pull on the localdir."""
@@ -68,7 +65,6 @@
     def checkout(self, localdir):
         """Checks out the repository into localdir."""
         checkout(self.url , localdir, self.branch)
-        update(localdir)
 
     def export(self, localdir, destdir):
         """Exports the working copy in localdir to destdir"""
@@ -107,7 +103,8 @@
         """Updates the working copy or creates by checking out the repository.
            Revision number included for compatibility
         """
-        if os.path.exists(self.localdir):
+        gitdir = os.path.join(self.localdir, ".git")
+        if os.path.exists(gitdir):
             self.log_info("Updating the working copy in %r", self.localdir)
             update(self.localdir, self.repository.branch)
         else:
@@ -131,11 +128,11 @@
 
     def get_revision(self, refname="HEAD"):
         """Return the SHA1 sum of the latest commit"""
-        output = run.capture_output(cmdexpand("git rev-parse $id",
-            refname=refname), cwd=git_working_copy)
+        output = run.capture_output(cmdexpand("git rev-parse $refname",
+            refname=refname), cwd=self.localdir)
         if output is None:
             raise GitError("Cannot determine revision for %r"
-                           % git_working_copy)
+                           % self.localdir)
         return output.strip()
 
 class TagDetector:
@@ -153,10 +150,13 @@
         repo = GitRepository(url)
         self.workingcopy = GitWorkingCopy(repo, localdir)
 
-    def newest_tag_revision(self):
+    def list_tags(self):
         self.workingcopy.update_or_checkout()
-        candidates = self.workingcopy.list_tags(self.pattern)
-        candidates = sorted(candidates)
+        tags = self.workingcopy.list_tags(self.pattern)
+        return sorted(tags)
+
+    def newest_tag_revision(self):
+        candidates = self.list_tags()
         urlrev = (None, None)
         if candidates:
             newest = candidates[-1]
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)