git.py 29 KB

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