Mercurial > treepkg > treepkg
diff treepkg/git.py @ 321:092925ff75d7
Added basic Git support, configuration options:
git_url
git_branch
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 12 Mar 2010 09:13:33 +0000 |
parents | |
children | ef983263b875 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/treepkg/git.py Fri Mar 12 09:13:33 2010 +0000 @@ -0,0 +1,124 @@ +# Copyright (C) 2010 by Intevation GmbHi +# Authors: +# Andre Heinecke <aheinecke@intevation.de> +# +# This program is free software under the GPL (>=v2) +# Read the file COPYING coming with the software for details. + +"""Collection of Git utility code""" + +import os +import shutil +import re +import StringIO + +import run +from cmdexpand import cmdexpand +from util import extract_value_for_key + + +class GitError(Exception): + + """Base class for Git specific errors raised by TreePKG""" + +def checkout(url, localdir, branch=None): + """Clones the repository at url into the localdir""" + run.call(cmdexpand("git clone $url $localdir", **locals())) + if branch: + run.call(cmdexpand("/bin/bash -c \"cd $localdir && git checkout --track -b local $branch\"", + **locals())) + else: + run.call(cmdexpand("/bin/bash -c \"cd $localdir && git checkout --track -b local master\"", + **locals())) + +def update(localdir, revision=None): + """Runs git pull on the localdir.""" + run.call(cmdexpand("/bin/bash -c \"cd $localdir && git pull -q\"", **locals())) + +def export(src, dest): + """Exports the local branch from src to dest""" + run.call(cmdexpand("/bin/bash -c \"cd $src && git checkout-index -a -f --prefix=$dest\"", **locals())) + +def last_changed_revision(git_working_copy): + """return the last changed revision of a Git branch as a incrementing Number""" + output = run.capture_output(cmdexpand("/bin/bash -c \"cd $git_working_copy && git rev-list local | nl | tail -n 1 | awk \'{print $$1}\'\"", **locals())) + if output is None: + raise GitError("Cannot determine last changed revision for %r" + % git_working_copy) + return int(output) + +class GitRepository(object): + + """Describes a git repository""" + + def __init__(self, url, branch=None): + """Initialize the git repository description + Parameters: + url -- The url of the repository + + branch -- The name of the remote Branch to track + defaults to master + """ + self.url = url + self.branch = branch + + 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""" + export(localdir, destdir) + + def last_changed_revision(self, localdir): + """Returns the last changed revision of the working copy in localdir""" + return last_changed_revision(localdir) + + def check_working_copy(self, localdir): + """ + FIXME STUB: Not implemented for git + """ + return None + +class GitWorkingCopy(object): + + """Represents a checkout of a git repository""" + + def __init__(self, repository, localdir, logger=None): + """ + Initialize the working copy. + Parameters: + repository -- The GitRepository instance describing the + repository + localdir -- The directory for the working copy + logger -- logging object to use for some info/debug messages + """ + self.repository = repository + self.localdir = localdir + self.logger = logger + + def log_info(self, *args): + if self.logger is not None: + self.logger.info(*args) + + def update_or_checkout(self, revision=0): + """Updates the working copy or creates by checking out the repository. + Revision number included for compatibility + """ + if os.path.exists(self.localdir): + self.log_info("Updating the working copy in %r", self.localdir) + update(self.localdir, self.repository.branch) + else: + self.log_info("The working copy in %r doesn't exist yet." + " Checking out from %r", + self.localdir, self.repository.url) + self.repository.checkout(self.localdir) + + def export(self, destdir): + """Exports the working copy to destdir""" + self.repository.export(self.localdir, destdir) + + def last_changed_revision(self): + """Returns the last changed rev of the working copy""" + return self.repository.last_changed_revision(self.localdir)