summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/fetch2/git.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/fetch2/git.py')
-rw-r--r--bitbake/lib/bb/fetch2/git.py260
1 files changed, 260 insertions, 0 deletions
diff --git a/bitbake/lib/bb/fetch2/git.py b/bitbake/lib/bb/fetch2/git.py
new file mode 100644
index 0000000000..c62145770f
--- /dev/null
+++ b/bitbake/lib/bb/fetch2/git.py
@@ -0,0 +1,260 @@
1# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4BitBake 'Fetch' git implementation
5
6"""
7
8#Copyright (C) 2005 Richard Purdie
9#
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.
13#
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.
18#
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.
22
23import os
24import bb
25from bb import data
26from bb.fetch2 import Fetch
27from bb.fetch2 import runfetchcmd
28from bb.fetch2 import logger
29
30class Git(Fetch):
31 """Class to fetch a module or modules from git repositories"""
32 def init(self, d):
33 #
34 # Only enable _sortable revision if the key is set
35 #
36 if bb.data.getVar("BB_GIT_CLONE_FOR_SRCREV", d, True):
37 self._sortable_buildindex = self._sortable_buildindex_disabled
38 def supports(self, url, ud, d):
39 """
40 Check to see if a given url can be fetched with git.
41 """
42 return ud.type in ['git']
43
44 def urldata_init(self, ud, d):
45 """
46 init git specific variable within url data
47 so that the git method like latest_revision() can work
48 """
49 if 'protocol' in ud.parm:
50 ud.proto = ud.parm['protocol']
51 elif not ud.host:
52 ud.proto = 'file'
53 else:
54 ud.proto = "rsync"
55
56 ud.branch = ud.parm.get("branch", "master")
57
58 gitsrcname = '%s%s' % (ud.host, ud.path.replace('/', '.'))
59 ud.mirrortarball = 'git_%s.tar.gz' % (gitsrcname)
60 ud.clonedir = os.path.join(data.expand('${GITDIR}', d), gitsrcname)
61
62 ud.basecmd = data.getVar("FETCHCMD_git", d, True) or "git"
63
64 def localpath(self, url, ud, d):
65 ud.tag = ud.revision
66 if not ud.tag or ud.tag == "master":
67 ud.tag = self.latest_revision(url, ud, d)
68
69 subdir = ud.parm.get("subpath", "")
70 if subdir != "":
71 if subdir.endswith("/"):
72 subdir = subdir[:-1]
73 subdirpath = os.path.join(ud.path, subdir);
74 else:
75 subdirpath = ud.path;
76
77 if 'fullclone' in ud.parm:
78 ud.localfile = ud.mirrortarball
79 else:
80 ud.localfile = data.expand('git_%s%s_%s.tar.gz' % (ud.host, subdirpath.replace('/', '.'), ud.tag), d)
81
82 if 'noclone' in ud.parm:
83 ud.localfile = None
84 return None
85
86 return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
87
88 def forcefetch(self, url, ud, d):
89 if 'fullclone' in ud.parm:
90 return True
91 if 'noclone' in ud.parm:
92 return False
93 if os.path.exists(ud.localpath):
94 return False
95 if not self._contains_ref(ud.tag, d):
96 return True
97 return False
98
99 def try_premirror(self, u, ud, d):
100 if 'noclone' in ud.parm:
101 return False
102 if os.path.exists(ud.clonedir):
103 return False
104 if os.path.exists(ud.localpath):
105 return False
106
107 return True
108
109 def go(self, loc, ud, d):
110 """Fetch url"""
111
112 if ud.user:
113 username = ud.user + '@'
114 else:
115 username = ""
116
117 repofile = os.path.join(data.getVar("DL_DIR", d, 1), ud.mirrortarball)
118
119 coname = '%s' % (ud.tag)
120 codir = os.path.join(ud.clonedir, coname)
121
122 # If we have no existing clone and no mirror tarball, try and obtain one
123 if not os.path.exists(ud.clonedir) and not os.path.exists(repofile):
124 try:
125 Fetch.try_mirrors(ud.mirrortarball)
126 except:
127 pass
128
129 # If the checkout doesn't exist and the mirror tarball does, extract it
130 if not os.path.exists(ud.clonedir) and os.path.exists(repofile):
131 bb.mkdirhier(ud.clonedir)
132 os.chdir(ud.clonedir)
133 runfetchcmd("tar -xzf %s" % (repofile), d)
134
135 # If the repo still doesn't exist, fallback to cloning it
136 if not os.path.exists(ud.clonedir):
137 runfetchcmd("%s clone -n %s://%s%s%s %s" % (ud.basecmd, ud.proto, username, ud.host, ud.path, ud.clonedir), d)
138
139 os.chdir(ud.clonedir)
140 # Update the checkout if needed
141 if not self._contains_ref(ud.tag, d) or 'fullclone' in ud.parm:
142 # Remove all but the .git directory
143 runfetchcmd("rm * -Rf", d)
144 if 'fullclone' in ud.parm:
145 runfetchcmd("%s fetch --all" % (ud.basecmd), d)
146 else:
147 runfetchcmd("%s fetch %s://%s%s%s %s" % (ud.basecmd, ud.proto, username, ud.host, ud.path, ud.branch), d)
148 runfetchcmd("%s fetch --tags %s://%s%s%s" % (ud.basecmd, ud.proto, username, ud.host, ud.path), d)
149 runfetchcmd("%s prune-packed" % ud.basecmd, d)
150 runfetchcmd("%s pack-redundant --all | xargs -r rm" % ud.basecmd, d)
151
152 # Generate a mirror tarball if needed
153 os.chdir(ud.clonedir)
154 mirror_tarballs = data.getVar("BB_GENERATE_MIRROR_TARBALLS", d, True)
155 if mirror_tarballs != "0" or 'fullclone' in ud.parm:
156 logger.info("Creating tarball of git repository")
157 runfetchcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ), d)
158
159 if 'fullclone' in ud.parm:
160 return
161
162 if os.path.exists(codir):
163 bb.utils.prunedir(codir)
164
165 subdir = ud.parm.get("subpath", "")
166 if subdir != "":
167 if subdir.endswith("/"):
168 subdirbase = os.path.basename(subdir[:-1])
169 else:
170 subdirbase = os.path.basename(subdir)
171 else:
172 subdirbase = ""
173
174 if subdir != "":
175 readpathspec = ":%s" % (subdir)
176 codir = os.path.join(codir, "git")
177 coprefix = os.path.join(codir, subdirbase, "")
178 else:
179 readpathspec = ""
180 coprefix = os.path.join(codir, "git", "")
181
182 scmdata = ud.parm.get("scmdata", "")
183 if scmdata == "keep":
184 runfetchcmd("%s clone -n %s %s" % (ud.basecmd, ud.clonedir, coprefix), d)
185 os.chdir(coprefix)
186 runfetchcmd("%s checkout -q -f %s%s" % (ud.basecmd, ud.tag, readpathspec), d)
187 else:
188 bb.mkdirhier(codir)
189 os.chdir(ud.clonedir)
190 runfetchcmd("%s read-tree %s%s" % (ud.basecmd, ud.tag, readpathspec), d)
191 runfetchcmd("%s checkout-index -q -f --prefix=%s -a" % (ud.basecmd, coprefix), d)
192
193 os.chdir(codir)
194 logger.info("Creating tarball of git checkout")
195 runfetchcmd("tar -czf %s %s" % (ud.localpath, os.path.join(".", "*") ), d)
196
197 os.chdir(ud.clonedir)
198 bb.utils.prunedir(codir)
199
200 def supports_srcrev(self):
201 return True
202
203 def _contains_ref(self, tag, d):
204 basecmd = data.getVar("FETCHCMD_git", d, True) or "git"
205 output = runfetchcmd("%s log --pretty=oneline -n 1 %s -- 2> /dev/null | wc -l" % (basecmd, tag), d, quiet=True)
206 return output.split()[0] != "0"
207
208 def _revision_key(self, url, ud, d):
209 """
210 Return a unique key for the url
211 """
212 return "git:" + ud.host + ud.path.replace('/', '.') + ud.branch
213
214 def _latest_revision(self, url, ud, d):
215 """
216 Compute the HEAD revision for the url
217 """
218 if ud.user:
219 username = ud.user + '@'
220 else:
221 username = ""
222
223 basecmd = data.getVar("FETCHCMD_git", d, True) or "git"
224 cmd = "%s ls-remote %s://%s%s%s %s" % (basecmd, ud.proto, username, ud.host, ud.path, ud.branch)
225 output = runfetchcmd(cmd, d, True)
226 if not output:
227 raise bb.fetch2.FetchError("Fetch command %s gave empty output\n" % (cmd))
228 return output.split()[0]
229
230 def _build_revision(self, url, ud, d):
231 return ud.tag
232
233 def _sortable_buildindex_disabled(self, url, ud, d, rev):
234 """
235 Return a suitable buildindex for the revision specified. This is done by counting revisions
236 using "git rev-list" which may or may not work in different circumstances.
237 """
238
239 cwd = os.getcwd()
240
241 # Check if we have the rev already
242
243 if not os.path.exists(ud.clonedir):
244 print("no repo")
245 self.go(None, ud, d)
246 if not os.path.exists(ud.clonedir):
247 logger.error("GIT repository for %s doesn't exist in %s, cannot get sortable buildnumber, using old value", url, ud.clonedir)
248 return None
249
250
251 os.chdir(ud.clonedir)
252 if not self._contains_ref(rev, d):
253 self.go(None, ud, d)
254
255 output = runfetchcmd("%s rev-list %s -- 2> /dev/null | wc -l" % (ud.basecmd, rev), d, quiet=True)
256 os.chdir(cwd)
257
258 buildindex = "%s" % output.split()[0]
259 logger.debug(1, "GIT repository for %s in %s is returning %s revisions in rev-list before %s", url, ud.clonedir, buildindex, rev)
260 return buildindex