# HG changeset patch # User Bjoern Ricks # Date 1289236061 0 # Node ID 29f6d1f5cc53793039a72ab073fc55f4ffc22c53 # Parent c8268d40d35de3cbcf6e1984c24da6cfe4be7ce6 add a tag detector for git diff -r c8268d40d35d -r 29f6d1f5cc53 treepkg/git.py --- a/treepkg/git.py Mon Nov 08 16:40:19 2010 +0000 +++ b/treepkg/git.py Mon Nov 08 17:07:41 2010 +0000 @@ -123,3 +123,46 @@ def last_changed_revision(self): """Returns the last changed rev of the working copy""" return self.repository.last_changed_revision(self.localdir) + + def list_tags(self, pattern): + output = run.capture_output(cmdexpand("git tag -l $pattern", + pattern=pattern), cwd=self.localdir) + return output.splitlines() + + 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) + if output is None: + raise GitError("Cannot determine revision for %r" + % git_working_copy) + return output.strip() + +class TagDetector: + + """Class to detect tags from a git repository + + The tags are found using the parameters: + url -- The url of the git repository to use + pattern -- A regular expression matching the tags + """ + + def __init__(self, url, pattern, localdir): + self.url = url + self.pattern = pattern + repo = GitRepository(url) + self.workingcopy = GitWorkingCopy(repo, localdir) + + def newest_tag_revision(self): + self.workingcopy.update_or_checkout() + candidates = self.workingcopy.list_tags(self.pattern) + candidates = sorted(candidates) + urlrev = (None, None) + if candidates: + newest = candidates[-1] + try: + rev = self.workingcopy.get_revision(newest) + urlrev = (newest, rev) + except GitError: + pass + return urlrev