git.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. BitBake 'Fetch' git implementation
  5. git fetcher support the SRC_URI with format of:
  6. SRC_URI = "git://some.host/somepath;OptionA=xxx;OptionB=xxx;..."
  7. Supported SRC_URI options are:
  8. - branch
  9. The git branch to retrieve from. The default is "master"
  10. This option also supports multiple branch fetching, with branches
  11. separated by commas. In multiple branches case, the name option
  12. must have the same number of names to match the branches, which is
  13. used to specify the SRC_REV for the branch
  14. e.g:
  15. SRC_URI="git://some.host/somepath;branch=branchX,branchY;name=nameX,nameY"
  16. SRCREV_nameX = "xxxxxxxxxxxxxxxxxxxx"
  17. SRCREV_nameY = "YYYYYYYYYYYYYYYYYYYY"
  18. - tag
  19. The git tag to retrieve. The default is "master"
  20. - protocol
  21. The method to use to access the repository. Common options are "git",
  22. "http", "https", "file", "ssh" and "rsync". The default is "git".
  23. - rebaseable
  24. rebaseable indicates that the upstream git repo may rebase in the future,
  25. and current revision may disappear from upstream repo. This option will
  26. remind fetcher to preserve local cache carefully for future use.
  27. The default value is "0", set rebaseable=1 for rebaseable git repo.
  28. - nocheckout
  29. Don't checkout source code when unpacking. set this option for the recipe
  30. who has its own routine to checkout code.
  31. The default is "0", set nocheckout=1 if needed.
  32. - bareclone
  33. Create a bare clone of the source code and don't checkout the source code
  34. when unpacking. Set this option for the recipe who has its own routine to
  35. checkout code and tracking branch requirements.
  36. The default is "0", set bareclone=1 if needed.
  37. """
  38. #Copyright (C) 2005 Richard Purdie
  39. #
  40. # This program is free software; you can redistribute it and/or modify
  41. # it under the terms of the GNU General Public License version 2 as
  42. # published by the Free Software Foundation.
  43. #
  44. # This program is distributed in the hope that it will be useful,
  45. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  47. # GNU General Public License for more details.
  48. #
  49. # You should have received a copy of the GNU General Public License along
  50. # with this program; if not, write to the Free Software Foundation, Inc.,
  51. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  52. import os
  53. import bb
  54. from bb import data
  55. from bb.fetch2 import FetchMethod
  56. from bb.fetch2 import runfetchcmd
  57. from bb.fetch2 import logger
  58. class Git(FetchMethod):
  59. """Class to fetch a module or modules from git repositories"""
  60. def init(self, d):
  61. pass
  62. def supports(self, ud, d):
  63. """
  64. Check to see if a given url can be fetched with git.
  65. """
  66. return ud.type in ['git']
  67. def supports_checksum(self, urldata):
  68. return False
  69. def urldata_init(self, ud, d):
  70. """
  71. init git specific variable within url data
  72. so that the git method like latest_revision() can work
  73. """
  74. if 'protocol' in ud.parm:
  75. ud.proto = ud.parm['protocol']
  76. elif not ud.host:
  77. ud.proto = 'file'
  78. else:
  79. ud.proto = "git"
  80. if not ud.proto in ('git', 'file', 'ssh', 'http', 'https', 'rsync'):
  81. raise bb.fetch2.ParameterError("Invalid protocol type", ud.url)
  82. ud.nocheckout = ud.parm.get("nocheckout","0") == "1"
  83. ud.rebaseable = ud.parm.get("rebaseable","0") == "1"
  84. # bareclone implies nocheckout
  85. ud.bareclone = ud.parm.get("bareclone","0") == "1"
  86. if ud.bareclone:
  87. ud.nocheckout = 1
  88. branches = ud.parm.get("branch", "master").split(',')
  89. if len(branches) != len(ud.names):
  90. raise bb.fetch2.ParameterError("The number of name and branch parameters is not balanced", ud.url)
  91. ud.branches = {}
  92. for name in ud.names:
  93. branch = branches[ud.names.index(name)]
  94. ud.branches[name] = branch
  95. ud.basecmd = data.getVar("FETCHCMD_git", d, True) or "git"
  96. ud.write_tarballs = ((data.getVar("BB_GENERATE_MIRROR_TARBALLS", d, True) or "0") != "0") or ud.rebaseable
  97. ud.setup_revisons(d)
  98. for name in ud.names:
  99. # Ensure anything that doesn't look like a sha256 checksum/revision is translated into one
  100. if not ud.revisions[name] or len(ud.revisions[name]) != 40 or (False in [c in "abcdef0123456789" for c in ud.revisions[name]]):
  101. if ud.revisions[name]:
  102. ud.branches[name] = ud.revisions[name]
  103. ud.revisions[name] = self.latest_revision(ud.url, ud, d, name)
  104. gitsrcname = '%s%s' % (ud.host.replace(':','.'), ud.path.replace('/', '.').replace('*', '.'))
  105. # for rebaseable git repo, it is necessary to keep mirror tar ball
  106. # per revision, so that even the revision disappears from the
  107. # upstream repo in the future, the mirror will remain intact and still
  108. # contains the revision
  109. if ud.rebaseable:
  110. for name in ud.names:
  111. gitsrcname = gitsrcname + '_' + ud.revisions[name]
  112. ud.mirrortarball = 'git2_%s.tar.gz' % (gitsrcname)
  113. ud.fullmirror = os.path.join(d.getVar("DL_DIR", True), ud.mirrortarball)
  114. gitdir = d.getVar("GITDIR", True) or (d.getVar("DL_DIR", True) + "/git2/")
  115. ud.clonedir = os.path.join(gitdir, gitsrcname)
  116. ud.localfile = ud.clonedir
  117. def localpath(self, ud, d):
  118. return ud.clonedir
  119. def need_update(self, ud, d):
  120. if not os.path.exists(ud.clonedir):
  121. return True
  122. os.chdir(ud.clonedir)
  123. for name in ud.names:
  124. if not self._contains_ref(ud.revisions[name], ud.branches[name], d):
  125. return True
  126. if ud.write_tarballs and not os.path.exists(ud.fullmirror):
  127. return True
  128. return False
  129. def try_premirror(self, ud, d):
  130. # If we don't do this, updating an existing checkout with only premirrors
  131. # is not possible
  132. if d.getVar("BB_FETCH_PREMIRRORONLY", True) is not None:
  133. return True
  134. if os.path.exists(ud.clonedir):
  135. return False
  136. return True
  137. def download(self, ud, d):
  138. """Fetch url"""
  139. if ud.user:
  140. username = ud.user + '@'
  141. else:
  142. username = ""
  143. ud.repochanged = not os.path.exists(ud.fullmirror)
  144. # If the checkout doesn't exist and the mirror tarball does, extract it
  145. if not os.path.exists(ud.clonedir) and os.path.exists(ud.fullmirror):
  146. bb.utils.mkdirhier(ud.clonedir)
  147. os.chdir(ud.clonedir)
  148. runfetchcmd("tar -xzf %s" % (ud.fullmirror), d)
  149. repourl = "%s://%s%s%s" % (ud.proto, username, ud.host, ud.path)
  150. # If the repo still doesn't exist, fallback to cloning it
  151. if not os.path.exists(ud.clonedir):
  152. # We do this since git will use a "-l" option automatically for local urls where possible
  153. if repourl.startswith("file://"):
  154. repourl = repourl[7:]
  155. clone_cmd = "%s clone --bare --mirror %s %s" % (ud.basecmd, repourl, ud.clonedir)
  156. if ud.proto.lower() != 'file':
  157. bb.fetch2.check_network_access(d, clone_cmd)
  158. runfetchcmd(clone_cmd, d)
  159. os.chdir(ud.clonedir)
  160. # Update the checkout if needed
  161. needupdate = False
  162. for name in ud.names:
  163. if not self._contains_ref(ud.revisions[name], ud.branches[name], d):
  164. needupdate = True
  165. if needupdate:
  166. try:
  167. runfetchcmd("%s remote rm origin" % ud.basecmd, d)
  168. except bb.fetch2.FetchError:
  169. logger.debug(1, "No Origin")
  170. runfetchcmd("%s remote add --mirror=fetch origin %s" % (ud.basecmd, repourl), d)
  171. fetch_cmd = "%s fetch -f --prune %s refs/*:refs/*" % (ud.basecmd, repourl)
  172. if ud.proto.lower() != 'file':
  173. bb.fetch2.check_network_access(d, fetch_cmd, ud.url)
  174. runfetchcmd(fetch_cmd, d)
  175. runfetchcmd("%s prune-packed" % ud.basecmd, d)
  176. runfetchcmd("%s pack-redundant --all | xargs -r rm" % ud.basecmd, d)
  177. ud.repochanged = True
  178. def build_mirror_data(self, ud, d):
  179. # Generate a mirror tarball if needed
  180. if ud.write_tarballs and (ud.repochanged or not os.path.exists(ud.fullmirror)):
  181. # it's possible that this symlink points to read-only filesystem with PREMIRROR
  182. if os.path.islink(ud.fullmirror):
  183. os.unlink(ud.fullmirror)
  184. os.chdir(ud.clonedir)
  185. logger.info("Creating tarball of git repository")
  186. runfetchcmd("tar -czf %s %s" % (ud.fullmirror, os.path.join(".") ), d)
  187. runfetchcmd("touch %s.done" % (ud.fullmirror), d)
  188. def unpack(self, ud, destdir, d):
  189. """ unpack the downloaded src to destdir"""
  190. subdir = ud.parm.get("subpath", "")
  191. if subdir != "":
  192. readpathspec = ":%s" % (subdir)
  193. def_destsuffix = "%s/" % os.path.basename(subdir)
  194. else:
  195. readpathspec = ""
  196. def_destsuffix = "git/"
  197. destsuffix = ud.parm.get("destsuffix", def_destsuffix)
  198. destdir = ud.destdir = os.path.join(destdir, destsuffix)
  199. if os.path.exists(destdir):
  200. bb.utils.prunedir(destdir)
  201. cloneflags = "-s -n"
  202. if ud.bareclone:
  203. cloneflags += " --mirror"
  204. # Versions of git prior to 1.7.9.2 have issues where foo.git and foo get confused
  205. # and you end up with some horrible union of the two when you attempt to clone it
  206. # The least invasive workaround seems to be a symlink to the real directory to
  207. # fool git into ignoring any .git version that may also be present.
  208. #
  209. # The issue is fixed in more recent versions of git so we can drop this hack in future
  210. # when that version becomes common enough.
  211. clonedir = ud.clonedir
  212. if not ud.path.endswith(".git"):
  213. indirectiondir = destdir[:-1] + ".indirectionsymlink"
  214. if os.path.exists(indirectiondir):
  215. os.remove(indirectiondir)
  216. bb.utils.mkdirhier(os.path.dirname(indirectiondir))
  217. os.symlink(ud.clonedir, indirectiondir)
  218. clonedir = indirectiondir
  219. runfetchcmd("git clone %s %s/ %s" % (cloneflags, clonedir, destdir), d)
  220. if not ud.nocheckout:
  221. os.chdir(destdir)
  222. if subdir != "":
  223. runfetchcmd("%s read-tree %s%s" % (ud.basecmd, ud.revisions[ud.names[0]], readpathspec), d)
  224. runfetchcmd("%s checkout-index -q -f -a" % ud.basecmd, d)
  225. else:
  226. runfetchcmd("%s checkout %s" % (ud.basecmd, ud.revisions[ud.names[0]]), d)
  227. return True
  228. def clean(self, ud, d):
  229. """ clean the git directory """
  230. bb.utils.remove(ud.localpath, True)
  231. bb.utils.remove(ud.fullmirror)
  232. def supports_srcrev(self):
  233. return True
  234. def _contains_ref(self, tag, branch, d):
  235. basecmd = data.getVar("FETCHCMD_git", d, True) or "git"
  236. cmd = "%s branch --contains %s --list %s 2> /dev/null | wc -l" % (basecmd, tag, branch)
  237. try:
  238. output = runfetchcmd(cmd, d, quiet=True)
  239. except bb.fetch2.FetchError:
  240. return False
  241. if len(output.split()) > 1:
  242. raise bb.fetch2.FetchError("The command '%s' gave output with more then 1 line unexpectedly, output: '%s'" % (cmd, output))
  243. return output.split()[0] != "0"
  244. def _revision_key(self, ud, d, name):
  245. """
  246. Return a unique key for the url
  247. """
  248. return "git:" + ud.host + ud.path.replace('/', '.') + ud.branches[name]
  249. def _latest_revision(self, ud, d, name):
  250. """
  251. Compute the HEAD revision for the url
  252. """
  253. if ud.user:
  254. username = ud.user + '@'
  255. else:
  256. username = ""
  257. basecmd = data.getVar("FETCHCMD_git", d, True) or "git"
  258. cmd = "%s ls-remote %s://%s%s%s %s" % \
  259. (basecmd, ud.proto, username, ud.host, ud.path, ud.branches[name])
  260. if ud.proto.lower() != 'file':
  261. bb.fetch2.check_network_access(d, cmd)
  262. output = runfetchcmd(cmd, d, True)
  263. if not output:
  264. raise bb.fetch2.FetchError("The command %s gave empty output unexpectedly" % cmd, ud.url)
  265. return output.split()[0]
  266. def _build_revision(self, ud, d, name):
  267. return ud.revisions[name]
  268. def checkstatus(self, ud, d):
  269. fetchcmd = "%s ls-remote %s" % (ud.basecmd, ud.url)
  270. try:
  271. runfetchcmd(fetchcmd, d, quiet=True)
  272. return True
  273. except FetchError:
  274. return False