git.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. """
  2. BitBake 'Fetch' git implementation
  3. git fetcher support the SRC_URI with format of:
  4. SRC_URI = "git://some.host/somepath;OptionA=xxx;OptionB=xxx;..."
  5. Supported SRC_URI options are:
  6. - branch
  7. The git branch to retrieve from. The default is "master"
  8. This option also supports multiple branch fetching, with branches
  9. separated by commas. In multiple branches case, the name option
  10. must have the same number of names to match the branches, which is
  11. used to specify the SRC_REV for the branch
  12. e.g:
  13. SRC_URI="git://some.host/somepath;branch=branchX,branchY;name=nameX,nameY"
  14. SRCREV_nameX = "xxxxxxxxxxxxxxxxxxxx"
  15. SRCREV_nameY = "YYYYYYYYYYYYYYYYYYYY"
  16. - tag
  17. The git tag to retrieve. The default is "master"
  18. - protocol
  19. The method to use to access the repository. Common options are "git",
  20. "http", "https", "file", "ssh" and "rsync". The default is "git".
  21. - rebaseable
  22. rebaseable indicates that the upstream git repo may rebase in the future,
  23. and current revision may disappear from upstream repo. This option will
  24. remind fetcher to preserve local cache carefully for future use.
  25. The default value is "0", set rebaseable=1 for rebaseable git repo.
  26. - nocheckout
  27. Don't checkout source code when unpacking. set this option for the recipe
  28. who has its own routine to checkout code.
  29. The default is "0", set nocheckout=1 if needed.
  30. - bareclone
  31. Create a bare clone of the source code and don't checkout the source code
  32. when unpacking. Set this option for the recipe who has its own routine to
  33. checkout code and tracking branch requirements.
  34. The default is "0", set bareclone=1 if needed.
  35. - nobranch
  36. Don't check the SHA validation for branch. set this option for the recipe
  37. referring to commit which is valid in any namespace (branch, tag, ...)
  38. instead of branch.
  39. The default is "0", set nobranch=1 if needed.
  40. - subpath
  41. Limit the checkout to a specific subpath of the tree.
  42. By default, checkout the whole tree, set subpath=<path> if needed
  43. - destsuffix
  44. The name of the path in which to place the checkout.
  45. By default, the path is git/, set destsuffix=<suffix> if needed
  46. - usehead
  47. For local git:// urls to use the current branch HEAD as the revision for use with
  48. AUTOREV. Implies nobranch.
  49. - lfs
  50. Enable the checkout to use LFS for large files. This will download all LFS files
  51. in the download step, as the unpack step does not have network access.
  52. The default is "1", set lfs=0 to skip.
  53. """
  54. # Copyright (C) 2005 Richard Purdie
  55. #
  56. # SPDX-License-Identifier: GPL-2.0-only
  57. #
  58. import collections
  59. import errno
  60. import fnmatch
  61. import os
  62. import re
  63. import shlex
  64. import shutil
  65. import subprocess
  66. import tempfile
  67. import bb
  68. import bb.progress
  69. from contextlib import contextmanager
  70. from bb.fetch2 import FetchMethod
  71. from bb.fetch2 import runfetchcmd
  72. from bb.fetch2 import logger
  73. from bb.fetch2 import trusted_network
  74. sha1_re = re.compile(r'^[0-9a-f]{40}$')
  75. slash_re = re.compile(r"/+")
  76. class GitProgressHandler(bb.progress.LineFilterProgressHandler):
  77. """Extract progress information from git output"""
  78. def __init__(self, d):
  79. self._buffer = ''
  80. self._count = 0
  81. super(GitProgressHandler, self).__init__(d)
  82. # Send an initial progress event so the bar gets shown
  83. self._fire_progress(-1)
  84. def write(self, string):
  85. self._buffer += string
  86. stages = ['Counting objects', 'Compressing objects', 'Receiving objects', 'Resolving deltas']
  87. stage_weights = [0.2, 0.05, 0.5, 0.25]
  88. stagenum = 0
  89. for i, stage in reversed(list(enumerate(stages))):
  90. if stage in self._buffer:
  91. stagenum = i
  92. self._buffer = ''
  93. break
  94. self._status = stages[stagenum]
  95. percs = re.findall(r'(\d+)%', string)
  96. if percs:
  97. progress = int(round((int(percs[-1]) * stage_weights[stagenum]) + (sum(stage_weights[:stagenum]) * 100)))
  98. rates = re.findall(r'([\d.]+ [a-zA-Z]*/s+)', string)
  99. if rates:
  100. rate = rates[-1]
  101. else:
  102. rate = None
  103. self.update(progress, rate)
  104. else:
  105. if stagenum == 0:
  106. percs = re.findall(r': (\d+)', string)
  107. if percs:
  108. count = int(percs[-1])
  109. if count > self._count:
  110. self._count = count
  111. self._fire_progress(-count)
  112. super(GitProgressHandler, self).write(string)
  113. class Git(FetchMethod):
  114. bitbake_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.join(os.path.abspath(__file__))), '..', '..', '..'))
  115. make_shallow_path = os.path.join(bitbake_dir, 'bin', 'git-make-shallow')
  116. """Class to fetch a module or modules from git repositories"""
  117. def init(self, d):
  118. pass
  119. def supports(self, ud, d):
  120. """
  121. Check to see if a given url can be fetched with git.
  122. """
  123. return ud.type in ['git']
  124. def supports_checksum(self, urldata):
  125. return False
  126. def cleanup_upon_failure(self):
  127. return False
  128. def urldata_init(self, ud, d):
  129. """
  130. init git specific variable within url data
  131. so that the git method like latest_revision() can work
  132. """
  133. if 'protocol' in ud.parm:
  134. ud.proto = ud.parm['protocol']
  135. elif not ud.host:
  136. ud.proto = 'file'
  137. else:
  138. ud.proto = "git"
  139. if ud.host == "github.com" and ud.proto == "git":
  140. # github stopped supporting git protocol
  141. # https://github.blog/2021-09-01-improving-git-protocol-security-github/#no-more-unauthenticated-git
  142. ud.proto = "https"
  143. bb.warn("URL: %s uses git protocol which is no longer supported by github. Please change to ;protocol=https in the url." % ud.url)
  144. if not ud.proto in ('git', 'file', 'ssh', 'http', 'https', 'rsync'):
  145. raise bb.fetch2.ParameterError("Invalid protocol type", ud.url)
  146. ud.nocheckout = ud.parm.get("nocheckout","0") == "1"
  147. ud.rebaseable = ud.parm.get("rebaseable","0") == "1"
  148. ud.nobranch = ud.parm.get("nobranch","0") == "1"
  149. # usehead implies nobranch
  150. ud.usehead = ud.parm.get("usehead","0") == "1"
  151. if ud.usehead:
  152. if ud.proto != "file":
  153. raise bb.fetch2.ParameterError("The usehead option is only for use with local ('protocol=file') git repositories", ud.url)
  154. ud.nobranch = 1
  155. # bareclone implies nocheckout
  156. ud.bareclone = ud.parm.get("bareclone","0") == "1"
  157. if ud.bareclone:
  158. ud.nocheckout = 1
  159. ud.unresolvedrev = {}
  160. branches = ud.parm.get("branch", "").split(',')
  161. if branches == [""] and not ud.nobranch:
  162. bb.warn("URL: %s does not set any branch parameter. The future default branch used by tools and repositories is uncertain and we will therefore soon require this is set in all git urls." % ud.url)
  163. branches = ["master"]
  164. if len(branches) != len(ud.names):
  165. raise bb.fetch2.ParameterError("The number of name and branch parameters is not balanced", ud.url)
  166. ud.noshared = d.getVar("BB_GIT_NOSHARED") == "1"
  167. ud.cloneflags = "-n"
  168. if not ud.noshared:
  169. ud.cloneflags += " -s"
  170. if ud.bareclone:
  171. ud.cloneflags += " --mirror"
  172. ud.shallow = d.getVar("BB_GIT_SHALLOW") == "1"
  173. ud.shallow_extra_refs = (d.getVar("BB_GIT_SHALLOW_EXTRA_REFS") or "").split()
  174. depth_default = d.getVar("BB_GIT_SHALLOW_DEPTH")
  175. if depth_default is not None:
  176. try:
  177. depth_default = int(depth_default or 0)
  178. except ValueError:
  179. raise bb.fetch2.FetchError("Invalid depth for BB_GIT_SHALLOW_DEPTH: %s" % depth_default)
  180. else:
  181. if depth_default < 0:
  182. raise bb.fetch2.FetchError("Invalid depth for BB_GIT_SHALLOW_DEPTH: %s" % depth_default)
  183. else:
  184. depth_default = 1
  185. ud.shallow_depths = collections.defaultdict(lambda: depth_default)
  186. revs_default = d.getVar("BB_GIT_SHALLOW_REVS")
  187. ud.shallow_revs = []
  188. ud.branches = {}
  189. for pos, name in enumerate(ud.names):
  190. branch = branches[pos]
  191. ud.branches[name] = branch
  192. ud.unresolvedrev[name] = branch
  193. shallow_depth = d.getVar("BB_GIT_SHALLOW_DEPTH_%s" % name)
  194. if shallow_depth is not None:
  195. try:
  196. shallow_depth = int(shallow_depth or 0)
  197. except ValueError:
  198. raise bb.fetch2.FetchError("Invalid depth for BB_GIT_SHALLOW_DEPTH_%s: %s" % (name, shallow_depth))
  199. else:
  200. if shallow_depth < 0:
  201. raise bb.fetch2.FetchError("Invalid depth for BB_GIT_SHALLOW_DEPTH_%s: %s" % (name, shallow_depth))
  202. ud.shallow_depths[name] = shallow_depth
  203. revs = d.getVar("BB_GIT_SHALLOW_REVS_%s" % name)
  204. if revs is not None:
  205. ud.shallow_revs.extend(revs.split())
  206. elif revs_default is not None:
  207. ud.shallow_revs.extend(revs_default.split())
  208. if (ud.shallow and
  209. not ud.shallow_revs and
  210. all(ud.shallow_depths[n] == 0 for n in ud.names)):
  211. # Shallow disabled for this URL
  212. ud.shallow = False
  213. if ud.usehead:
  214. # When usehead is set let's associate 'HEAD' with the unresolved
  215. # rev of this repository. This will get resolved into a revision
  216. # later. If an actual revision happens to have also been provided
  217. # then this setting will be overridden.
  218. for name in ud.names:
  219. ud.unresolvedrev[name] = 'HEAD'
  220. ud.basecmd = d.getVar("FETCHCMD_git") or "git -c gc.autoDetach=false -c core.pager=cat -c safe.bareRepository=all -c clone.defaultRemoteName=origin"
  221. write_tarballs = d.getVar("BB_GENERATE_MIRROR_TARBALLS") or "0"
  222. ud.write_tarballs = write_tarballs != "0" or ud.rebaseable
  223. ud.write_shallow_tarballs = (d.getVar("BB_GENERATE_SHALLOW_TARBALLS") or write_tarballs) != "0"
  224. ud.setup_revisions(d)
  225. for name in ud.names:
  226. # Ensure any revision that doesn't look like a SHA-1 is translated into one
  227. if not sha1_re.match(ud.revisions[name] or ''):
  228. if ud.revisions[name]:
  229. ud.unresolvedrev[name] = ud.revisions[name]
  230. ud.revisions[name] = self.latest_revision(ud, d, name)
  231. gitsrcname = '%s%s' % (ud.host.replace(':', '.'), ud.path.replace('/', '.').replace('*', '.').replace(' ','_').replace('(', '_').replace(')', '_'))
  232. if gitsrcname.startswith('.'):
  233. gitsrcname = gitsrcname[1:]
  234. # For a rebaseable git repo, it is necessary to keep a mirror tar ball
  235. # per revision, so that even if the revision disappears from the
  236. # upstream repo in the future, the mirror will remain intact and still
  237. # contain the revision
  238. if ud.rebaseable:
  239. for name in ud.names:
  240. gitsrcname = gitsrcname + '_' + ud.revisions[name]
  241. dl_dir = d.getVar("DL_DIR")
  242. gitdir = d.getVar("GITDIR") or (dl_dir + "/git2")
  243. ud.clonedir = os.path.join(gitdir, gitsrcname)
  244. ud.localfile = ud.clonedir
  245. mirrortarball = 'git2_%s.tar.gz' % gitsrcname
  246. ud.fullmirror = os.path.join(dl_dir, mirrortarball)
  247. ud.mirrortarballs = [mirrortarball]
  248. if ud.shallow:
  249. tarballname = gitsrcname
  250. if ud.bareclone:
  251. tarballname = "%s_bare" % tarballname
  252. if ud.shallow_revs:
  253. tarballname = "%s_%s" % (tarballname, "_".join(sorted(ud.shallow_revs)))
  254. for name, revision in sorted(ud.revisions.items()):
  255. tarballname = "%s_%s" % (tarballname, ud.revisions[name][:7])
  256. depth = ud.shallow_depths[name]
  257. if depth:
  258. tarballname = "%s-%s" % (tarballname, depth)
  259. shallow_refs = []
  260. if not ud.nobranch:
  261. shallow_refs.extend(ud.branches.values())
  262. if ud.shallow_extra_refs:
  263. shallow_refs.extend(r.replace('refs/heads/', '').replace('*', 'ALL') for r in ud.shallow_extra_refs)
  264. if shallow_refs:
  265. tarballname = "%s_%s" % (tarballname, "_".join(sorted(shallow_refs)).replace('/', '.'))
  266. fetcher = self.__class__.__name__.lower()
  267. ud.shallowtarball = '%sshallow_%s.tar.gz' % (fetcher, tarballname)
  268. ud.fullshallow = os.path.join(dl_dir, ud.shallowtarball)
  269. ud.mirrortarballs.insert(0, ud.shallowtarball)
  270. def localpath(self, ud, d):
  271. return ud.clonedir
  272. def need_update(self, ud, d):
  273. return self.clonedir_need_update(ud, d) \
  274. or self.shallow_tarball_need_update(ud) \
  275. or self.tarball_need_update(ud) \
  276. or self.lfs_need_update(ud, d)
  277. def clonedir_need_update(self, ud, d):
  278. if not os.path.exists(ud.clonedir):
  279. return True
  280. if ud.shallow and ud.write_shallow_tarballs and self.clonedir_need_shallow_revs(ud, d):
  281. return True
  282. for name in ud.names:
  283. if not self._contains_ref(ud, d, name, ud.clonedir):
  284. return True
  285. return False
  286. def lfs_need_update(self, ud, d):
  287. if self.clonedir_need_update(ud, d):
  288. return True
  289. for name in ud.names:
  290. if not self._lfs_objects_downloaded(ud, d, name, ud.clonedir):
  291. return True
  292. return False
  293. def clonedir_need_shallow_revs(self, ud, d):
  294. for rev in ud.shallow_revs:
  295. try:
  296. runfetchcmd('%s rev-parse -q --verify %s' % (ud.basecmd, rev), d, quiet=True, workdir=ud.clonedir)
  297. except bb.fetch2.FetchError:
  298. return rev
  299. return None
  300. def shallow_tarball_need_update(self, ud):
  301. return ud.shallow and ud.write_shallow_tarballs and not os.path.exists(ud.fullshallow)
  302. def tarball_need_update(self, ud):
  303. return ud.write_tarballs and not os.path.exists(ud.fullmirror)
  304. def try_premirror(self, ud, d):
  305. # If we don't do this, updating an existing checkout with only premirrors
  306. # is not possible
  307. if bb.utils.to_boolean(d.getVar("BB_FETCH_PREMIRRORONLY")):
  308. return True
  309. # If the url is not in trusted network, that is, BB_NO_NETWORK is set to 0
  310. # and BB_ALLOWED_NETWORKS does not contain the host that ud.url uses, then
  311. # we need to try premirrors first as using upstream is destined to fail.
  312. if not trusted_network(d, ud.url):
  313. return True
  314. # the following check is to ensure incremental fetch in downloads, this is
  315. # because the premirror might be old and does not contain the new rev required,
  316. # and this will cause a total removal and new clone. So if we can reach to
  317. # network, we prefer upstream over premirror, though the premirror might contain
  318. # the new rev.
  319. if os.path.exists(ud.clonedir):
  320. return False
  321. return True
  322. def download(self, ud, d):
  323. """Fetch url"""
  324. # A current clone is preferred to either tarball, a shallow tarball is
  325. # preferred to an out of date clone, and a missing clone will use
  326. # either tarball.
  327. if ud.shallow and os.path.exists(ud.fullshallow) and self.need_update(ud, d):
  328. ud.localpath = ud.fullshallow
  329. return
  330. elif os.path.exists(ud.fullmirror) and self.need_update(ud, d):
  331. if not os.path.exists(ud.clonedir):
  332. bb.utils.mkdirhier(ud.clonedir)
  333. runfetchcmd("tar -xzf %s" % ud.fullmirror, d, workdir=ud.clonedir)
  334. else:
  335. tmpdir = tempfile.mkdtemp(dir=d.getVar('DL_DIR'))
  336. runfetchcmd("tar -xzf %s" % ud.fullmirror, d, workdir=tmpdir)
  337. output = runfetchcmd("%s remote" % ud.basecmd, d, quiet=True, workdir=ud.clonedir)
  338. if 'mirror' in output:
  339. runfetchcmd("%s remote rm mirror" % ud.basecmd, d, workdir=ud.clonedir)
  340. runfetchcmd("%s remote add --mirror=fetch mirror %s" % (ud.basecmd, tmpdir), d, workdir=ud.clonedir)
  341. fetch_cmd = "LANG=C %s fetch -f --update-head-ok --progress mirror " % (ud.basecmd)
  342. runfetchcmd(fetch_cmd, d, workdir=ud.clonedir)
  343. repourl = self._get_repo_url(ud)
  344. needs_clone = False
  345. if os.path.exists(ud.clonedir):
  346. # The directory may exist, but not be the top level of a bare git
  347. # repository in which case it needs to be deleted and re-cloned.
  348. try:
  349. # Since clones can be bare, use --absolute-git-dir instead of --show-toplevel
  350. output = runfetchcmd("LANG=C %s rev-parse --absolute-git-dir" % ud.basecmd, d, workdir=ud.clonedir)
  351. toplevel = output.rstrip()
  352. if not bb.utils.path_is_descendant(toplevel, ud.clonedir):
  353. logger.warning("Top level directory '%s' is not a descendant of '%s'. Re-cloning", toplevel, ud.clonedir)
  354. needs_clone = True
  355. except bb.fetch2.FetchError as e:
  356. logger.warning("Unable to get top level for %s (not a git directory?): %s", ud.clonedir, e)
  357. needs_clone = True
  358. except FileNotFoundError as e:
  359. logger.warning("%s", e)
  360. needs_clone = True
  361. if needs_clone:
  362. shutil.rmtree(ud.clonedir)
  363. else:
  364. needs_clone = True
  365. # If the repo still doesn't exist, fallback to cloning it
  366. if needs_clone:
  367. # We do this since git will use a "-l" option automatically for local urls where possible,
  368. # but it doesn't work when git/objects is a symlink, only works when it is a directory.
  369. if repourl.startswith("file://"):
  370. repourl_path = repourl[7:]
  371. objects = os.path.join(repourl_path, 'objects')
  372. if os.path.isdir(objects) and not os.path.islink(objects):
  373. repourl = repourl_path
  374. clone_cmd = "LANG=C %s clone --bare --mirror %s %s --progress" % (ud.basecmd, shlex.quote(repourl), ud.clonedir)
  375. if ud.proto.lower() != 'file':
  376. bb.fetch2.check_network_access(d, clone_cmd, ud.url)
  377. progresshandler = GitProgressHandler(d)
  378. runfetchcmd(clone_cmd, d, log=progresshandler)
  379. # Update the checkout if needed
  380. if self.clonedir_need_update(ud, d):
  381. output = runfetchcmd("%s remote" % ud.basecmd, d, quiet=True, workdir=ud.clonedir)
  382. if "origin" in output:
  383. runfetchcmd("%s remote rm origin" % ud.basecmd, d, workdir=ud.clonedir)
  384. runfetchcmd("%s remote add --mirror=fetch origin %s" % (ud.basecmd, shlex.quote(repourl)), d, workdir=ud.clonedir)
  385. if ud.nobranch:
  386. fetch_cmd = "LANG=C %s fetch -f --progress %s refs/*:refs/*" % (ud.basecmd, shlex.quote(repourl))
  387. else:
  388. fetch_cmd = "LANG=C %s fetch -f --progress %s refs/heads/*:refs/heads/* refs/tags/*:refs/tags/*" % (ud.basecmd, shlex.quote(repourl))
  389. if ud.proto.lower() != 'file':
  390. bb.fetch2.check_network_access(d, fetch_cmd, ud.url)
  391. progresshandler = GitProgressHandler(d)
  392. runfetchcmd(fetch_cmd, d, log=progresshandler, workdir=ud.clonedir)
  393. runfetchcmd("%s prune-packed" % ud.basecmd, d, workdir=ud.clonedir)
  394. runfetchcmd("%s pack-refs --all" % ud.basecmd, d, workdir=ud.clonedir)
  395. runfetchcmd("%s pack-redundant --all | xargs -r rm" % ud.basecmd, d, workdir=ud.clonedir)
  396. try:
  397. os.unlink(ud.fullmirror)
  398. except OSError as exc:
  399. if exc.errno != errno.ENOENT:
  400. raise
  401. for name in ud.names:
  402. if not self._contains_ref(ud, d, name, ud.clonedir):
  403. raise bb.fetch2.FetchError("Unable to find revision %s in branch %s even from upstream" % (ud.revisions[name], ud.branches[name]))
  404. if ud.shallow and ud.write_shallow_tarballs:
  405. missing_rev = self.clonedir_need_shallow_revs(ud, d)
  406. if missing_rev:
  407. raise bb.fetch2.FetchError("Unable to find revision %s even from upstream" % missing_rev)
  408. if self.lfs_need_update(ud, d):
  409. # Unpack temporary working copy, use it to run 'git checkout' to force pre-fetching
  410. # of all LFS blobs needed at the srcrev.
  411. #
  412. # It would be nice to just do this inline here by running 'git-lfs fetch'
  413. # on the bare clonedir, but that operation requires a working copy on some
  414. # releases of Git LFS.
  415. with tempfile.TemporaryDirectory(dir=d.getVar('DL_DIR')) as tmpdir:
  416. # Do the checkout. This implicitly involves a Git LFS fetch.
  417. Git.unpack(self, ud, tmpdir, d)
  418. # Scoop up a copy of any stuff that Git LFS downloaded. Merge them into
  419. # the bare clonedir.
  420. #
  421. # As this procedure is invoked repeatedly on incremental fetches as
  422. # a recipe's SRCREV is bumped throughout its lifetime, this will
  423. # result in a gradual accumulation of LFS blobs in <ud.clonedir>/lfs
  424. # corresponding to all the blobs reachable from the different revs
  425. # fetched across time.
  426. #
  427. # Only do this if the unpack resulted in a .git/lfs directory being
  428. # created; this only happens if at least one blob needed to be
  429. # downloaded.
  430. if os.path.exists(os.path.join(ud.destdir, ".git", "lfs")):
  431. runfetchcmd("tar -cf - lfs | tar -xf - -C %s" % ud.clonedir, d, workdir="%s/.git" % ud.destdir)
  432. def build_mirror_data(self, ud, d):
  433. # Create as a temp file and move atomically into position to avoid races
  434. @contextmanager
  435. def create_atomic(filename):
  436. fd, tfile = tempfile.mkstemp(dir=os.path.dirname(filename))
  437. try:
  438. yield tfile
  439. umask = os.umask(0o666)
  440. os.umask(umask)
  441. os.chmod(tfile, (0o666 & ~umask))
  442. os.rename(tfile, filename)
  443. finally:
  444. os.close(fd)
  445. if ud.shallow and ud.write_shallow_tarballs:
  446. if not os.path.exists(ud.fullshallow):
  447. if os.path.islink(ud.fullshallow):
  448. os.unlink(ud.fullshallow)
  449. tempdir = tempfile.mkdtemp(dir=d.getVar('DL_DIR'))
  450. shallowclone = os.path.join(tempdir, 'git')
  451. try:
  452. self.clone_shallow_local(ud, shallowclone, d)
  453. logger.info("Creating tarball of git repository")
  454. with create_atomic(ud.fullshallow) as tfile:
  455. runfetchcmd("tar -czf %s ." % tfile, d, workdir=shallowclone)
  456. runfetchcmd("touch %s.done" % ud.fullshallow, d)
  457. finally:
  458. bb.utils.remove(tempdir, recurse=True)
  459. elif ud.write_tarballs and not os.path.exists(ud.fullmirror):
  460. if os.path.islink(ud.fullmirror):
  461. os.unlink(ud.fullmirror)
  462. logger.info("Creating tarball of git repository")
  463. with create_atomic(ud.fullmirror) as tfile:
  464. mtime = runfetchcmd("{} log --all -1 --format=%cD".format(ud.basecmd), d,
  465. quiet=True, workdir=ud.clonedir)
  466. runfetchcmd("tar -czf %s --owner oe:0 --group oe:0 --mtime \"%s\" ."
  467. % (tfile, mtime), d, workdir=ud.clonedir)
  468. runfetchcmd("touch %s.done" % ud.fullmirror, d)
  469. def clone_shallow_local(self, ud, dest, d):
  470. """
  471. Shallow fetch from ud.clonedir (${DL_DIR}/git2/<gitrepo> by default):
  472. - For BB_GIT_SHALLOW_DEPTH: git fetch --depth <depth> rev
  473. - For BB_GIT_SHALLOW_REVS: git fetch --shallow-exclude=<revs> rev
  474. """
  475. bb.utils.mkdirhier(dest)
  476. init_cmd = "%s init -q" % ud.basecmd
  477. if ud.bareclone:
  478. init_cmd += " --bare"
  479. runfetchcmd(init_cmd, d, workdir=dest)
  480. runfetchcmd("%s remote add origin %s" % (ud.basecmd, ud.clonedir), d, workdir=dest)
  481. # Check the histories which should be excluded
  482. shallow_exclude = ''
  483. for revision in ud.shallow_revs:
  484. shallow_exclude += " --shallow-exclude=%s" % revision
  485. for name in ud.names:
  486. revision = ud.revisions[name]
  487. depth = ud.shallow_depths[name]
  488. # The --depth and --shallow-exclude can't be used together
  489. if depth and shallow_exclude:
  490. raise bb.fetch2.FetchError("BB_GIT_SHALLOW_REVS is set, but BB_GIT_SHALLOW_DEPTH is not 0.")
  491. # For nobranch, we need a ref, otherwise the commits will be
  492. # removed, and for non-nobranch, we truncate the branch to our
  493. # srcrev, to avoid keeping unnecessary history beyond that.
  494. branch = ud.branches[name]
  495. if ud.nobranch:
  496. ref = "refs/shallow/%s" % name
  497. elif ud.bareclone:
  498. ref = "refs/heads/%s" % branch
  499. else:
  500. ref = "refs/remotes/origin/%s" % branch
  501. fetch_cmd = "%s fetch origin %s" % (ud.basecmd, revision)
  502. if depth:
  503. fetch_cmd += " --depth %s" % depth
  504. if shallow_exclude:
  505. fetch_cmd += shallow_exclude
  506. # Advertise the revision for lower version git such as 2.25.1:
  507. # error: Server does not allow request for unadvertised object.
  508. # The ud.clonedir is a local temporary dir, will be removed when
  509. # fetch is done, so we can do anything on it.
  510. adv_cmd = 'git branch -f advertise-%s %s' % (revision, revision)
  511. runfetchcmd(adv_cmd, d, workdir=ud.clonedir)
  512. runfetchcmd(fetch_cmd, d, workdir=dest)
  513. runfetchcmd("%s update-ref %s %s" % (ud.basecmd, ref, revision), d, workdir=dest)
  514. # Apply extra ref wildcards
  515. all_refs_remote = runfetchcmd("%s ls-remote origin 'refs/*'" % ud.basecmd, \
  516. d, workdir=dest).splitlines()
  517. all_refs = []
  518. for line in all_refs_remote:
  519. all_refs.append(line.split()[-1])
  520. extra_refs = []
  521. for r in ud.shallow_extra_refs:
  522. if not ud.bareclone:
  523. r = r.replace('refs/heads/', 'refs/remotes/origin/')
  524. if '*' in r:
  525. matches = filter(lambda a: fnmatch.fnmatchcase(a, r), all_refs)
  526. extra_refs.extend(matches)
  527. else:
  528. extra_refs.append(r)
  529. for ref in extra_refs:
  530. ref_fetch = os.path.basename(ref)
  531. runfetchcmd("%s fetch origin --depth 1 %s" % (ud.basecmd, ref_fetch), d, workdir=dest)
  532. revision = runfetchcmd("%s rev-parse FETCH_HEAD" % ud.basecmd, d, workdir=dest)
  533. runfetchcmd("%s update-ref %s %s" % (ud.basecmd, ref, revision), d, workdir=dest)
  534. # The url is local ud.clonedir, set it to upstream one
  535. repourl = self._get_repo_url(ud)
  536. runfetchcmd("%s remote set-url origin %s" % (ud.basecmd, shlex.quote(repourl)), d, workdir=dest)
  537. def unpack(self, ud, destdir, d):
  538. """ unpack the downloaded src to destdir"""
  539. subdir = ud.parm.get("subdir")
  540. subpath = ud.parm.get("subpath")
  541. readpathspec = ""
  542. def_destsuffix = "git/"
  543. if subpath:
  544. readpathspec = ":%s" % subpath
  545. def_destsuffix = "%s/" % os.path.basename(subpath.rstrip('/'))
  546. if subdir:
  547. # If 'subdir' param exists, create a dir and use it as destination for unpack cmd
  548. if os.path.isabs(subdir):
  549. if not os.path.realpath(subdir).startswith(os.path.realpath(destdir)):
  550. raise bb.fetch2.UnpackError("subdir argument isn't a subdirectory of unpack root %s" % destdir, ud.url)
  551. destdir = subdir
  552. else:
  553. destdir = os.path.join(destdir, subdir)
  554. def_destsuffix = ""
  555. destsuffix = ud.parm.get("destsuffix", def_destsuffix)
  556. destdir = ud.destdir = os.path.join(destdir, destsuffix)
  557. if os.path.exists(destdir):
  558. bb.utils.prunedir(destdir)
  559. if not ud.bareclone:
  560. ud.unpack_tracer.unpack("git", destdir)
  561. need_lfs = self._need_lfs(ud)
  562. if not need_lfs:
  563. ud.basecmd = "GIT_LFS_SKIP_SMUDGE=1 " + ud.basecmd
  564. source_found = False
  565. source_error = []
  566. clonedir_is_up_to_date = not self.clonedir_need_update(ud, d)
  567. if clonedir_is_up_to_date:
  568. runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d)
  569. source_found = True
  570. else:
  571. source_error.append("clone directory not available or not up to date: " + ud.clonedir)
  572. if not source_found:
  573. if ud.shallow:
  574. if os.path.exists(ud.fullshallow):
  575. bb.utils.mkdirhier(destdir)
  576. runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir)
  577. source_found = True
  578. else:
  579. source_error.append("shallow clone not available: " + ud.fullshallow)
  580. else:
  581. source_error.append("shallow clone not enabled")
  582. if not source_found:
  583. raise bb.fetch2.UnpackError("No up to date source found: " + "; ".join(source_error), ud.url)
  584. repourl = self._get_repo_url(ud)
  585. runfetchcmd("%s remote set-url origin %s" % (ud.basecmd, shlex.quote(repourl)), d, workdir=destdir)
  586. if self._contains_lfs(ud, d, destdir):
  587. if need_lfs and not self._find_git_lfs(d):
  588. raise bb.fetch2.FetchError("Repository %s has LFS content, install git-lfs on host to download (or set lfs=0 to ignore it)" % (repourl))
  589. elif not need_lfs:
  590. bb.note("Repository %s has LFS content but it is not being fetched" % (repourl))
  591. else:
  592. runfetchcmd("%s lfs install --local" % ud.basecmd, d, workdir=destdir)
  593. if not ud.nocheckout:
  594. if subpath:
  595. runfetchcmd("%s read-tree %s%s" % (ud.basecmd, ud.revisions[ud.names[0]], readpathspec), d,
  596. workdir=destdir)
  597. runfetchcmd("%s checkout-index -q -f -a" % ud.basecmd, d, workdir=destdir)
  598. elif not ud.nobranch:
  599. branchname = ud.branches[ud.names[0]]
  600. runfetchcmd("%s checkout -B %s %s" % (ud.basecmd, branchname, \
  601. ud.revisions[ud.names[0]]), d, workdir=destdir)
  602. runfetchcmd("%s branch %s --set-upstream-to origin/%s" % (ud.basecmd, branchname, \
  603. branchname), d, workdir=destdir)
  604. else:
  605. runfetchcmd("%s checkout %s" % (ud.basecmd, ud.revisions[ud.names[0]]), d, workdir=destdir)
  606. return True
  607. def clean(self, ud, d):
  608. """ clean the git directory """
  609. to_remove = [ud.localpath, ud.fullmirror, ud.fullmirror + ".done"]
  610. # The localpath is a symlink to clonedir when it is cloned from a
  611. # mirror, so remove both of them.
  612. if os.path.islink(ud.localpath):
  613. clonedir = os.path.realpath(ud.localpath)
  614. to_remove.append(clonedir)
  615. # Remove shallow mirror tarball
  616. if ud.shallow:
  617. to_remove.append(ud.fullshallow)
  618. to_remove.append(ud.fullshallow + ".done")
  619. for r in to_remove:
  620. if os.path.exists(r) or os.path.islink(r):
  621. bb.note('Removing %s' % r)
  622. bb.utils.remove(r, True)
  623. def supports_srcrev(self):
  624. return True
  625. def _contains_ref(self, ud, d, name, wd):
  626. cmd = ""
  627. if ud.nobranch:
  628. cmd = "%s log --pretty=oneline -n 1 %s -- 2> /dev/null | wc -l" % (
  629. ud.basecmd, ud.revisions[name])
  630. else:
  631. cmd = "%s branch --contains %s --list %s 2> /dev/null | wc -l" % (
  632. ud.basecmd, ud.revisions[name], ud.branches[name])
  633. try:
  634. output = runfetchcmd(cmd, d, quiet=True, workdir=wd)
  635. except bb.fetch2.FetchError:
  636. return False
  637. if len(output.split()) > 1:
  638. raise bb.fetch2.FetchError("The command '%s' gave output with more then 1 line unexpectedly, output: '%s'" % (cmd, output))
  639. return output.split()[0] != "0"
  640. def _lfs_objects_downloaded(self, ud, d, name, wd):
  641. """
  642. Verifies whether the LFS objects for requested revisions have already been downloaded
  643. """
  644. # Bail out early if this repository doesn't use LFS
  645. if not self._need_lfs(ud) or not self._contains_lfs(ud, d, wd):
  646. return True
  647. # The Git LFS specification specifies ([1]) the LFS folder layout so it should be safe to check for file
  648. # existence.
  649. # [1] https://github.com/git-lfs/git-lfs/blob/main/docs/spec.md#intercepting-git
  650. cmd = "%s lfs ls-files -l %s" \
  651. % (ud.basecmd, ud.revisions[name])
  652. output = runfetchcmd(cmd, d, quiet=True, workdir=wd).rstrip()
  653. # Do not do any further matching if no objects are managed by LFS
  654. if not output:
  655. return True
  656. # Match all lines beginning with the hexadecimal OID
  657. oid_regex = re.compile("^(([a-fA-F0-9]{2})([a-fA-F0-9]{2})[A-Fa-f0-9]+)")
  658. for line in output.split("\n"):
  659. oid = re.search(oid_regex, line)
  660. if not oid:
  661. bb.warn("git lfs ls-files output '%s' did not match expected format." % line)
  662. if not os.path.exists(os.path.join(wd, "lfs", "objects", oid.group(2), oid.group(3), oid.group(1))):
  663. return False
  664. return True
  665. def _need_lfs(self, ud):
  666. return ud.parm.get("lfs", "1") == "1"
  667. def _contains_lfs(self, ud, d, wd):
  668. """
  669. Check if the repository has 'lfs' (large file) content
  670. """
  671. if ud.nobranch:
  672. # If no branch is specified, use the current git commit
  673. refname = self._build_revision(ud, d, ud.names[0])
  674. elif wd == ud.clonedir:
  675. # The bare clonedir doesn't use the remote names; it has the branch immediately.
  676. refname = ud.branches[ud.names[0]]
  677. else:
  678. refname = "origin/%s" % ud.branches[ud.names[0]]
  679. cmd = "%s grep lfs %s:.gitattributes | wc -l" % (
  680. ud.basecmd, refname)
  681. try:
  682. output = runfetchcmd(cmd, d, quiet=True, workdir=wd)
  683. if int(output) > 0:
  684. return True
  685. except (bb.fetch2.FetchError,ValueError):
  686. pass
  687. return False
  688. def _find_git_lfs(self, d):
  689. """
  690. Return True if git-lfs can be found, False otherwise.
  691. """
  692. return shutil.which("git-lfs", path=d.getVar('PATH')) is not None
  693. def _get_repo_url(self, ud):
  694. """
  695. Return the repository URL
  696. """
  697. # Note that we do not support passwords directly in the git urls. There are several
  698. # reasons. SRC_URI can be written out to things like buildhistory and people don't
  699. # want to leak passwords like that. Its also all too easy to share metadata without
  700. # removing the password. ssh keys, ~/.netrc and ~/.ssh/config files can be used as
  701. # alternatives so we will not take patches adding password support here.
  702. if ud.user:
  703. username = ud.user + '@'
  704. else:
  705. username = ""
  706. return "%s://%s%s%s" % (ud.proto, username, ud.host, ud.path)
  707. def _revision_key(self, ud, d, name):
  708. """
  709. Return a unique key for the url
  710. """
  711. # Collapse adjacent slashes
  712. return "git:" + ud.host + slash_re.sub(".", ud.path) + ud.unresolvedrev[name]
  713. def _lsremote(self, ud, d, search):
  714. """
  715. Run git ls-remote with the specified search string
  716. """
  717. # Prevent recursion e.g. in OE if SRCPV is in PV, PV is in WORKDIR,
  718. # and WORKDIR is in PATH (as a result of RSS), our call to
  719. # runfetchcmd() exports PATH so this function will get called again (!)
  720. # In this scenario the return call of the function isn't actually
  721. # important - WORKDIR isn't needed in PATH to call git ls-remote
  722. # anyway.
  723. if d.getVar('_BB_GIT_IN_LSREMOTE', False):
  724. return ''
  725. d.setVar('_BB_GIT_IN_LSREMOTE', '1')
  726. try:
  727. repourl = self._get_repo_url(ud)
  728. cmd = "%s ls-remote %s %s" % \
  729. (ud.basecmd, shlex.quote(repourl), search)
  730. if ud.proto.lower() != 'file':
  731. bb.fetch2.check_network_access(d, cmd, repourl)
  732. output = runfetchcmd(cmd, d, True)
  733. if not output:
  734. raise bb.fetch2.FetchError("The command %s gave empty output unexpectedly" % cmd, ud.url)
  735. finally:
  736. d.delVar('_BB_GIT_IN_LSREMOTE')
  737. return output
  738. def _latest_revision(self, ud, d, name):
  739. """
  740. Compute the HEAD revision for the url
  741. """
  742. if not d.getVar("__BBSRCREV_SEEN"):
  743. raise bb.fetch2.FetchError("Recipe uses a floating tag/branch '%s' for repo '%s' without a fixed SRCREV yet doesn't call bb.fetch2.get_srcrev() (use SRCPV in PV for OE)." % (ud.unresolvedrev[name], ud.host+ud.path))
  744. # Ensure we mark as not cached
  745. bb.fetch2.mark_recipe_nocache(d)
  746. output = self._lsremote(ud, d, "")
  747. # Tags of the form ^{} may not work, need to fallback to other form
  748. if ud.unresolvedrev[name][:5] == "refs/" or ud.usehead:
  749. head = ud.unresolvedrev[name]
  750. tag = ud.unresolvedrev[name]
  751. else:
  752. head = "refs/heads/%s" % ud.unresolvedrev[name]
  753. tag = "refs/tags/%s" % ud.unresolvedrev[name]
  754. for s in [head, tag + "^{}", tag]:
  755. for l in output.strip().split('\n'):
  756. sha1, ref = l.split()
  757. if s == ref:
  758. return sha1
  759. raise bb.fetch2.FetchError("Unable to resolve '%s' in upstream git repository in git ls-remote output for %s" % \
  760. (ud.unresolvedrev[name], ud.host+ud.path))
  761. def latest_versionstring(self, ud, d):
  762. """
  763. Compute the latest release name like "x.y.x" in "x.y.x+gitHASH"
  764. by searching through the tags output of ls-remote, comparing
  765. versions and returning the highest match.
  766. """
  767. pupver = ('', '')
  768. try:
  769. output = self._lsremote(ud, d, "refs/tags/*")
  770. except (bb.fetch2.FetchError, bb.fetch2.NetworkAccess) as e:
  771. bb.note("Could not list remote: %s" % str(e))
  772. return pupver
  773. rev_tag_re = re.compile(r"([0-9a-f]{40})\s+refs/tags/(.*)")
  774. pver_re = re.compile(d.getVar('UPSTREAM_CHECK_GITTAGREGEX') or r"(?P<pver>([0-9][\.|_]?)+)")
  775. nonrel_re = re.compile(r"(alpha|beta|rc|final)+")
  776. verstring = ""
  777. for line in output.split("\n"):
  778. if not line:
  779. break
  780. m = rev_tag_re.match(line)
  781. if not m:
  782. continue
  783. (revision, tag) = m.groups()
  784. # Ignore non-released branches
  785. if nonrel_re.search(tag):
  786. continue
  787. # search for version in the line
  788. m = pver_re.search(tag)
  789. if not m:
  790. continue
  791. pver = m.group('pver').replace("_", ".")
  792. if verstring and bb.utils.vercmp(("0", pver, ""), ("0", verstring, "")) < 0:
  793. continue
  794. verstring = pver
  795. pupver = (verstring, revision)
  796. return pupver
  797. def _build_revision(self, ud, d, name):
  798. return ud.revisions[name]
  799. def gitpkgv_revision(self, ud, d, name):
  800. """
  801. Return a sortable revision number by counting commits in the history
  802. Based on gitpkgv.bblass in meta-openembedded
  803. """
  804. rev = self._build_revision(ud, d, name)
  805. localpath = ud.localpath
  806. rev_file = os.path.join(localpath, "oe-gitpkgv_" + rev)
  807. if not os.path.exists(localpath):
  808. commits = None
  809. else:
  810. if not os.path.exists(rev_file) or not os.path.getsize(rev_file):
  811. commits = bb.fetch2.runfetchcmd(
  812. "git rev-list %s -- | wc -l" % shlex.quote(rev),
  813. d, quiet=True).strip().lstrip('0')
  814. if commits:
  815. open(rev_file, "w").write("%d\n" % int(commits))
  816. else:
  817. commits = open(rev_file, "r").readline(128).strip()
  818. if commits:
  819. return False, "%s+%s" % (commits, rev[:7])
  820. else:
  821. return True, str(rev)
  822. def checkstatus(self, fetch, ud, d):
  823. try:
  824. self._lsremote(ud, d, "")
  825. return True
  826. except bb.fetch2.FetchError:
  827. return False