1 # ex:ts=4:sw=4:sts=4:et
2 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4 BitBake 'Fetch' git implementation
8 #Copyright (C) 2005 Richard Purdie
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License version 2 as
12 # published by the Free Software Foundation.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 from bb.fetch import Fetch
27 from bb.fetch import FetchError
28 from bb.fetch import runfetchcmd
31 # Delete everything reachable from the directory named in 'topdir'.
32 # CAUTION: This is dangerous!
33 for root, dirs, files in os.walk(topdir, topdown=False):
35 os.remove(os.path.join(root, name))
37 os.rmdir(os.path.join(root, name))
40 """Class to fetch a module or modules from git repositories"""
41 def supports(self, url, ud, d):
43 Check to see if a given url can be fetched with git.
45 return ud.type in ['git']
47 def localpath(self, url, ud, d):
50 if 'protocol' in ud.parm:
51 ud.proto = ud.parm['protocol']
53 tag = Fetch.srcrev_internal_helper(ud, d)
55 ud.tag = self.latest_revision(url, ud, d)
60 ud.tag = self.latest_revision(url, ud, d)
62 if ud.tag == "master":
63 ud.tag = self.latest_revision(url, ud, d)
65 ud.localfile = data.expand('git_%s%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.tag), d)
67 return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
69 def go(self, loc, ud, d):
72 if Fetch.try_mirror(d, ud.localfile):
73 bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists (or was stashed). Skipping git checkout." % ud.localpath)
76 gitsrcname = '%s%s' % (ud.host, ud.path.replace('/', '.'))
78 repofilename = 'git_%s.tar.gz' % (gitsrcname)
79 repofile = os.path.join(data.getVar("DL_DIR", d, 1), repofilename)
80 repodir = os.path.join(data.expand('${GITDIR}', d), gitsrcname)
82 coname = '%s' % (ud.tag)
83 codir = os.path.join(repodir, coname)
85 if not os.path.exists(repodir):
86 if Fetch.try_mirror(d, repofilename):
89 runfetchcmd("tar -xzf %s" % (repofile), d)
91 runfetchcmd("git clone -n %s://%s%s %s" % (ud.proto, ud.host, ud.path, repodir), d)
94 # Remove all but the .git directory
95 runfetchcmd("rm * -Rf", d)
96 runfetchcmd("git fetch %s://%s%s" % (ud.proto, ud.host, ud.path), d)
97 runfetchcmd("git pull --tags %s://%s%s" % (ud.proto, ud.host, ud.path), d)
98 runfetchcmd("git prune-packed", d)
99 runfetchcmd("git pack-redundant --all | xargs -r rm", d)
102 mirror_tarballs = data.getVar("BB_GENERATE_MIRROR_TARBALLS", d, True)
103 if mirror_tarballs != "0":
104 bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git repository")
105 runfetchcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ), d)
107 if os.path.exists(codir):
112 runfetchcmd("git read-tree %s" % (ud.tag), d)
113 runfetchcmd("git checkout-index -q -f --prefix=%s -a" % (os.path.join(codir, "git", "")), d)
116 bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git checkout")
117 runfetchcmd("tar -czf %s %s" % (ud.localpath, os.path.join(".", "*") ), d)
122 def suppports_srcrev(self):
125 def _revision_key(self, url, ud, d):
127 Return a unique key for the url
129 return "git:" + ud.host + ud.path.replace('/', '.')
131 def _latest_revision(self, url, ud, d):
133 output = runfetchcmd("git ls-remote %s://%s%s" % (ud.proto, ud.host, ud.path), d, True)
134 return output.split()[0]
136 def _build_revision(self, url, ud, d):