2006-02-10 10:13:42 +00:00
|
|
|
# ex:ts=4:sw=4:sts=4:et
|
|
|
|
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
"""
|
|
|
|
BitBake 'Fetch' git implementation
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2007-01-08 23:53:01 +00:00
|
|
|
#Copyright (C) 2005 Richard Purdie
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2009-11-03 23:14:53 +00:00
|
|
|
import os
|
2006-02-10 10:13:42 +00:00
|
|
|
import bb
|
|
|
|
from bb import data
|
|
|
|
from bb.fetch import Fetch
|
2007-08-03 13:40:52 +00:00
|
|
|
from bb.fetch import runfetchcmd
|
2006-02-10 10:13:42 +00:00
|
|
|
|
|
|
|
class Git(Fetch):
|
|
|
|
"""Class to fetch a module or modules from git repositories"""
|
2010-01-20 18:46:02 +00:00
|
|
|
def init(self, d):
|
|
|
|
#
|
|
|
|
# Only enable _sortable revision if the key is set
|
|
|
|
#
|
|
|
|
if bb.data.getVar("BB_GIT_CLONE_FOR_SRCREV", d, True):
|
|
|
|
self._sortable_buildindex = self._sortable_buildindex_disabled
|
2006-11-16 15:02:15 +00:00
|
|
|
def supports(self, url, ud, d):
|
|
|
|
"""
|
2006-11-27 08:57:29 +00:00
|
|
|
Check to see if a given url can be fetched with git.
|
2006-02-10 10:13:42 +00:00
|
|
|
"""
|
2006-11-16 15:02:15 +00:00
|
|
|
return ud.type in ['git']
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2006-11-16 15:02:15 +00:00
|
|
|
def localpath(self, url, ud, d):
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2006-11-16 15:02:15 +00:00
|
|
|
if 'protocol' in ud.parm:
|
|
|
|
ud.proto = ud.parm['protocol']
|
2009-05-11 21:34:07 +00:00
|
|
|
elif not ud.host:
|
|
|
|
ud.proto = 'file'
|
|
|
|
else:
|
|
|
|
ud.proto = "rsync"
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2008-03-28 17:19:49 +00:00
|
|
|
ud.branch = ud.parm.get("branch", "master")
|
2008-01-20 12:09:31 +00:00
|
|
|
|
2009-12-11 17:09:26 +00:00
|
|
|
gitsrcname = '%s%s' % (ud.host, ud.path.replace('/', '.'))
|
|
|
|
ud.mirrortarball = 'git_%s.tar.gz' % (gitsrcname)
|
|
|
|
ud.clonedir = os.path.join(data.expand('${GITDIR}', d), gitsrcname)
|
|
|
|
|
2007-11-13 23:03:21 +00:00
|
|
|
tag = Fetch.srcrev_internal_helper(ud, d)
|
|
|
|
if tag is True:
|
|
|
|
ud.tag = self.latest_revision(url, ud, d)
|
|
|
|
elif tag:
|
|
|
|
ud.tag = tag
|
|
|
|
|
2009-05-11 21:34:07 +00:00
|
|
|
if not ud.tag or ud.tag == "master":
|
2007-11-13 23:03:21 +00:00
|
|
|
ud.tag = self.latest_revision(url, ud, d)
|
2007-11-07 23:49:55 +00:00
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
subdir = ud.parm.get("subpath", "")
|
|
|
|
if subdir != "":
|
|
|
|
if subdir.endswith("/"):
|
|
|
|
subdir = subdir[:-1]
|
|
|
|
subdirpath = os.path.join(ud.path, subdir);
|
|
|
|
else:
|
|
|
|
subdirpath = ud.path;
|
|
|
|
|
2009-12-11 17:11:32 +00:00
|
|
|
if 'fullclone' in ud.parm:
|
|
|
|
ud.localfile = ud.mirrortarball
|
|
|
|
else:
|
2010-01-20 18:46:02 +00:00
|
|
|
ud.localfile = data.expand('git_%s%s_%s.tar.gz' % (ud.host, subdirpath.replace('/', '.'), ud.tag), d)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2006-11-16 15:02:15 +00:00
|
|
|
return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2006-11-16 15:02:15 +00:00
|
|
|
def go(self, loc, ud, d):
|
|
|
|
"""Fetch url"""
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2007-08-03 13:40:52 +00:00
|
|
|
if Fetch.try_mirror(d, ud.localfile):
|
2006-11-16 15:02:15 +00:00
|
|
|
bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists (or was stashed). Skipping git checkout." % ud.localpath)
|
|
|
|
return
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2008-12-03 16:01:39 +00:00
|
|
|
if ud.user:
|
|
|
|
username = ud.user + '@'
|
|
|
|
else:
|
|
|
|
username = ""
|
|
|
|
|
2009-12-11 17:20:06 +00:00
|
|
|
repofile = os.path.join(data.getVar("DL_DIR", d, 1), ud.mirrortarball)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2006-11-16 15:02:15 +00:00
|
|
|
coname = '%s' % (ud.tag)
|
2009-12-11 17:09:26 +00:00
|
|
|
codir = os.path.join(ud.clonedir, coname)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2009-12-11 17:09:26 +00:00
|
|
|
if not os.path.exists(ud.clonedir):
|
2009-12-11 17:20:06 +00:00
|
|
|
if Fetch.try_mirror(d, ud.mirrortarball):
|
2009-12-11 17:09:26 +00:00
|
|
|
bb.mkdirhier(ud.clonedir)
|
|
|
|
os.chdir(ud.clonedir)
|
2007-08-03 13:40:52 +00:00
|
|
|
runfetchcmd("tar -xzf %s" % (repofile), d)
|
2006-11-16 15:02:15 +00:00
|
|
|
else:
|
2009-12-11 17:09:26 +00:00
|
|
|
runfetchcmd("git clone -n %s://%s%s%s %s" % (ud.proto, username, ud.host, ud.path, ud.clonedir), d)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2009-12-11 17:09:26 +00:00
|
|
|
os.chdir(ud.clonedir)
|
2006-11-16 15:02:15 +00:00
|
|
|
# Remove all but the .git directory
|
2009-05-11 21:34:07 +00:00
|
|
|
if not self._contains_ref(ud.tag, d):
|
|
|
|
runfetchcmd("rm * -Rf", d)
|
|
|
|
runfetchcmd("git fetch %s://%s%s%s %s" % (ud.proto, username, ud.host, ud.path, ud.branch), d)
|
|
|
|
runfetchcmd("git fetch --tags %s://%s%s%s" % (ud.proto, username, ud.host, ud.path), d)
|
|
|
|
runfetchcmd("git prune-packed", d)
|
|
|
|
runfetchcmd("git pack-redundant --all | xargs -r rm", d)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2009-12-11 17:09:26 +00:00
|
|
|
os.chdir(ud.clonedir)
|
2007-11-07 23:49:55 +00:00
|
|
|
mirror_tarballs = data.getVar("BB_GENERATE_MIRROR_TARBALLS", d, True)
|
2009-12-11 17:11:32 +00:00
|
|
|
if mirror_tarballs != "0" or 'fullclone' in ud.parm:
|
2007-11-07 23:49:55 +00:00
|
|
|
bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git repository")
|
|
|
|
runfetchcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ), d)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2009-12-11 17:11:32 +00:00
|
|
|
if 'fullclone' in ud.parm:
|
|
|
|
return
|
|
|
|
|
2006-11-16 15:02:15 +00:00
|
|
|
if os.path.exists(codir):
|
2008-08-18 07:56:04 +00:00
|
|
|
bb.utils.prunedir(codir)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
subdir = ud.parm.get("subpath", "")
|
|
|
|
if subdir != "":
|
|
|
|
if subdir.endswith("/"):
|
|
|
|
subdirbase = os.path.basename(subdir[:-1])
|
|
|
|
else:
|
|
|
|
subdirbase = os.path.basename(subdir)
|
|
|
|
else:
|
|
|
|
subdirbase = ""
|
|
|
|
|
|
|
|
if subdir != "":
|
|
|
|
readpathspec = ":%s" % (subdir)
|
|
|
|
codir = os.path.join(codir, "git")
|
|
|
|
coprefix = os.path.join(codir, subdirbase, "")
|
|
|
|
else:
|
|
|
|
readpathspec = ""
|
|
|
|
coprefix = os.path.join(codir, "git", "")
|
|
|
|
|
2006-11-16 15:02:15 +00:00
|
|
|
bb.mkdirhier(codir)
|
2009-12-11 17:09:26 +00:00
|
|
|
os.chdir(ud.clonedir)
|
2010-01-20 18:46:02 +00:00
|
|
|
runfetchcmd("git read-tree %s%s" % (ud.tag, readpathspec), d)
|
|
|
|
runfetchcmd("git checkout-index -q -f --prefix=%s -a" % (coprefix), d)
|
2006-02-10 10:13:42 +00:00
|
|
|
|
2006-11-16 15:02:15 +00:00
|
|
|
os.chdir(codir)
|
|
|
|
bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git checkout")
|
2007-08-03 13:40:52 +00:00
|
|
|
runfetchcmd("tar -czf %s %s" % (ud.localpath, os.path.join(".", "*") ), d)
|
|
|
|
|
2009-12-11 17:09:26 +00:00
|
|
|
os.chdir(ud.clonedir)
|
2008-08-18 07:56:04 +00:00
|
|
|
bb.utils.prunedir(codir)
|
2007-08-03 13:40:52 +00:00
|
|
|
|
|
|
|
def suppports_srcrev(self):
|
|
|
|
return True
|
|
|
|
|
2009-05-11 21:34:07 +00:00
|
|
|
def _contains_ref(self, tag, d):
|
|
|
|
output = runfetchcmd("git log --pretty=oneline -n 1 %s -- 2> /dev/null | wc -l" % tag, d, quiet=True)
|
|
|
|
return output.split()[0] != "0"
|
|
|
|
|
2007-08-03 13:40:52 +00:00
|
|
|
def _revision_key(self, url, ud, d):
|
|
|
|
"""
|
|
|
|
Return a unique key for the url
|
|
|
|
"""
|
|
|
|
return "git:" + ud.host + ud.path.replace('/', '.')
|
|
|
|
|
|
|
|
def _latest_revision(self, url, ud, d):
|
2008-01-20 14:58:00 +00:00
|
|
|
"""
|
|
|
|
Compute the HEAD revision for the url
|
|
|
|
"""
|
2008-12-17 12:07:41 +00:00
|
|
|
if ud.user:
|
|
|
|
username = ud.user + '@'
|
|
|
|
else:
|
|
|
|
username = ""
|
|
|
|
|
2009-12-11 12:48:14 +00:00
|
|
|
cmd = "git ls-remote %s://%s%s%s %s" % (ud.proto, username, ud.host, ud.path, ud.branch)
|
|
|
|
output = runfetchcmd(cmd, d, True)
|
|
|
|
if not output:
|
|
|
|
raise bb.fetch.FetchError("Fetch command %s gave empty output\n" % (cmd))
|
2007-08-03 13:40:52 +00:00
|
|
|
return output.split()[0]
|
|
|
|
|
2007-11-13 23:03:21 +00:00
|
|
|
def _build_revision(self, url, ud, d):
|
2007-11-17 22:20:55 +00:00
|
|
|
return ud.tag
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def _sortable_buildindex_disabled(self, url, ud, d, rev):
|
2009-06-10 10:21:43 +00:00
|
|
|
"""
|
2010-01-20 18:46:02 +00:00
|
|
|
Return a suitable buildindex for the revision specified. This is done by counting revisions
|
|
|
|
using "git rev-list" which may or may not work in different circumstances.
|
2009-06-10 10:21:43 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
cwd = os.getcwd()
|
|
|
|
|
|
|
|
# Check if we have the rev already
|
2010-01-20 18:46:02 +00:00
|
|
|
|
2009-12-11 17:09:26 +00:00
|
|
|
if not os.path.exists(ud.clonedir):
|
2009-06-10 10:21:43 +00:00
|
|
|
print "no repo"
|
|
|
|
self.go(None, ud, d)
|
2010-01-20 18:46:02 +00:00
|
|
|
if not os.path.exists(ud.clonedir):
|
|
|
|
bb.msg.error(bb.msg.domain.Fetcher, "GIT repository for %s doesn't exist in %s, cannot get sortable buildnumber, using old value" % (url, ud.clonedir))
|
|
|
|
return None
|
|
|
|
|
2009-06-10 10:21:43 +00:00
|
|
|
|
2009-12-11 17:09:26 +00:00
|
|
|
os.chdir(ud.clonedir)
|
2010-01-20 18:46:02 +00:00
|
|
|
if not self._contains_ref(rev, d):
|
2009-06-10 10:21:43 +00:00
|
|
|
self.go(None, ud, d)
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
output = runfetchcmd("git rev-list %s -- 2> /dev/null | wc -l" % rev, d, quiet=True)
|
2009-06-10 10:21:43 +00:00
|
|
|
os.chdir(cwd)
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
buildindex = "%s" % output.split()[0]
|
|
|
|
bb.msg.debug(1, bb.msg.domain.Fetcher, "GIT repository for %s in %s is returning %s revisions in rev-list before %s" % (url, repodir, buildindex, rev))
|
|
|
|
return buildindex
|
2009-06-10 10:21:43 +00:00
|
|
|
|