git.py 34 KB

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