1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
5
# charm-helpers is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3 as
7
# published by the Free Software Foundation.
9
# charm-helpers is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
18
from charmhelpers.fetch import (
22
from charmhelpers.core.host import mkdir
26
raise ImportError('GitPython does not support Python 3')
31
from charmhelpers.fetch import apt_install
32
apt_install("python-git")
35
from git.exc import GitCommandError # noqa E402
38
class GitUrlFetchHandler(BaseFetchHandler):
39
"""Handler for git branches via generic and github URLs"""
40
def can_handle(self, source):
41
url_parts = self.parse_url(source)
42
# TODO (mattyw) no support for ssh git@ yet
43
if url_parts.scheme not in ('http', 'https', 'git'):
48
def clone(self, source, dest, branch):
49
if not self.can_handle(source):
50
raise UnhandledSource("Cannot handle {}".format(source))
52
repo = Repo.clone_from(source, dest)
53
repo.git.checkout(branch)
55
def install(self, source, branch="master", dest=None):
56
url_parts = self.parse_url(source)
57
branch_name = url_parts.path.strip("/").split("/")[-1]
59
dest_dir = os.path.join(dest, branch_name)
61
dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
63
if not os.path.exists(dest_dir):
64
mkdir(dest_dir, perms=0o755)
66
self.clone(source, dest_dir, branch)
67
except GitCommandError as e:
68
raise UnhandledSource(e.message)
70
raise UnhandledSource(e.strerror)