git.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. BitBake 'Fetch' git implementation
  5. git fetcher support the SRC_URI with format of:
  6. SRC_URI = "git://some.host/somepath;OptionA=xxx;OptionB=xxx;..."
  7. Supported SRC_URI options are:
  8. - branch
  9. The git branch to retrieve from. The default is "master"
  10. This option also supports multiple branch fetching, with branches
  11. separated by commas. In multiple branches case, the name option
  12. must have the same number of names to match the branches, which is
  13. used to specify the SRC_REV for the branch
  14. e.g:
  15. SRC_URI="git://some.host/somepath;branch=branchX,branchY;name=nameX,nameY"
  16. SRCREV_nameX = "xxxxxxxxxxxxxxxxxxxx"
  17. SRCREV_nameY = "YYYYYYYYYYYYYYYYYYYY"
  18. - tag
  19. The git tag to retrieve. The default is "master"
  20. - protocol
  21. The method to use to access the repository. Common options are "git",
  22. "http", "https", "file", "ssh" and "rsync". The default is "git".
  23. - rebaseable
  24. rebaseable indicates that the upstream git repo may rebase in the future,
  25. and current revision may disappear from upstream repo. This option will
  26. remind fetcher to preserve local cache carefully for future use.
  27. The default value is "0", set rebaseable=1 for rebaseable git repo.
  28. - nocheckout
  29. Don't checkout source code when unpacking. set this option for the recipe
  30. who has its own routine to checkout code.
  31. The default is "0", set nocheckout=1 if needed.
  32. - bareclone
  33. Create a bare clone of the source code and don't checkout the source code
  34. when unpacking. Set this option for the recipe who has its own routine to
  35. checkout code and tracking branch requirements.
  36. The default is "0", set bareclone=1 if needed.
  37. - nobranch
  38. Don't check the SHA validation for branch. set this option for the recipe
  39. referring to commit which is valid in tag instead of branch.
  40. The default is "0", set nobranch=1 if needed.
  41. - usehead
  42. For local git:// urls to use the current branch HEAD as the revision for use with
  43. AUTOREV. Implies nobranch.
  44. """
  45. #Copyright (C) 2005 Richard Purdie
  46. #
  47. # This program is free software; you can redistribute it and/or modify
  48. # it under the terms of the GNU General Public License version 2 as
  49. # published by the Free Software Foundation.
  50. #
  51. # This program is distributed in the hope that it will be useful,
  52. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  53. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  54. # GNU General Public License for more details.
  55. #
  56. # You should have received a copy of the GNU General Public License along
  57. # with this program; if not, write to the Free Software Foundation, Inc.,
  58. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  59. import errno
  60. import os
  61. import re
  62. import bb
  63. import errno
  64. import bb.progress
  65. from bb.fetch2 import FetchMethod
  66. from bb.fetch2 import runfetchcmd
  67. from bb.fetch2 import logger
  68. class GitProgressHandler(bb.progress.LineFilterProgressHandler):
  69. """Extract progress information from git output"""
  70. def __init__(self, d):
  71. self._buffer = ''
  72. self._count = 0
  73. super(GitProgressHandler, self).__init__(d)
  74. # Send an initial progress event so the bar gets shown
  75. self._fire_progress(-1)
  76. def write(self, string):
  77. self._buffer += string
  78. stages = ['Counting objects', 'Compressing objects', 'Receiving objects', 'Resolving deltas']
  79. stage_weights = [0.2, 0.05, 0.5, 0.25]
  80. stagenum = 0
  81. for i, stage in reversed(list(enumerate(stages))):
  82. if stage in self._buffer:
  83. stagenum = i
  84. self._buffer = ''
  85. break
  86. self._status = stages[stagenum]
  87. percs = re.findall(r'(\d+)%', string)
  88. if percs:
  89. progress = int(round((int(percs[-1]) * stage_weights[stagenum]) + (sum(stage_weights[:stagenum]) * 100)))
  90. rates = re.findall(r'([\d.]+ [a-zA-Z]*/s+)', string)
  91. if rates:
  92. rate = rates[-1]
  93. else:
  94. rate = None
  95. self.update(progress, rate)
  96. else:
  97. if stagenum == 0:
  98. percs = re.findall(r': (\d+)', string)
  99. if percs:
  100. count = int(percs[-1])
  101. if count > self._count:
  102. self._count = count
  103. self._fire_progress(-count)
  104. super(GitProgressHandler, self).write(string)
  105. class Git(FetchMethod):
  106. """Class to fetch a module or modules from git repositories"""
  107. def init(self, d):
  108. pass
  109. def supports(self, ud, d):
  110. """
  111. Check to see if a given url can be fetched with git.
  112. """
  113. return ud.type in ['git']
  114. def supports_checksum(self, urldata):
  115. return False
  116. def urldata_init(self, ud, d):
  117. """
  118. init git specific variable within url data
  119. so that the git method like latest_revision() can work
  120. """
  121. if 'protocol' in ud.parm:
  122. ud.proto = ud.parm['protocol']
  123. elif not ud.host:
  124. ud.proto = 'file'
  125. else:
  126. ud.proto = "git"
  127. if not ud.proto in ('git', 'file', 'ssh', 'http', 'https', 'rsync'):
  128. raise bb.fetch2.ParameterError("Invalid protocol type", ud.url)
  129. ud.nocheckout = ud.parm.get("nocheckout","0") == "1"
  130. ud.rebaseable = ud.parm.get("rebaseable","0") == "1"
  131. ud.nobranch = ud.parm.get("nobranch","0") == "1"
  132. # usehead implies nobranch
  133. ud.usehead = ud.parm.get("usehead","0") == "1"
  134. if ud.usehead:
  135. if ud.proto != "file":
  136. raise bb.fetch2.ParameterError("The usehead option is only for use with local ('protocol=file') git repositories", ud.url)
  137. ud.nobranch = 1
  138. # bareclone implies nocheckout
  139. ud.bareclone = ud.parm.get("bareclone","0") == "1"
  140. if ud.bareclone:
  141. ud.nocheckout = 1
  142. ud.unresolvedrev = {}
  143. branches = ud.parm.get("branch", "master").split(',')
  144. if len(branches) != len(ud.names):
  145. raise bb.fetch2.ParameterError("The number of name and branch parameters is not balanced", ud.url)
  146. ud.branches = {}
  147. for pos, name in enumerate(ud.names):
  148. branch = branches[pos]
  149. ud.branches[name] = branch
  150. ud.unresolvedrev[name] = branch
  151. if ud.usehead:
  152. ud.unresolvedrev['default'] = 'HEAD'
  153. ud.basecmd = d.getVar("FETCHCMD_git") or "git -c core.fsyncobjectfiles=0"
  154. ud.write_tarballs = ((d.getVar("BB_GENERATE_MIRROR_TARBALLS") or "0") != "0") or ud.rebaseable
  155. ud.setup_revisions(d)
  156. for name in ud.names:
  157. # Ensure anything that doesn't look like a sha256 checksum/revision is translated into one
  158. if not ud.revisions[name] or len(ud.revisions[name]) != 40 or (False in [c in "abcdef0123456789" for c in ud.revisions[name]]):
  159. if ud.revisions[name]:
  160. ud.unresolvedrev[name] = ud.revisions[name]
  161. ud.revisions[name] = self.latest_revision(ud, d, name)
  162. gitsrcname = '%s%s' % (ud.host.replace(':', '.'), ud.path.replace('/', '.').replace('*', '.'))
  163. if gitsrcname.startswith('.'):
  164. gitsrcname = gitsrcname[1:]
  165. # for rebaseable git repo, it is necessary to keep mirror tar ball
  166. # per revision, so that even the revision disappears from the
  167. # upstream repo in the future, the mirror will remain intact and still
  168. # contains the revision
  169. if ud.rebaseable:
  170. for name in ud.names:
  171. gitsrcname = gitsrcname + '_' + ud.revisions[name]
  172. mirrortarball = 'git2_%s.tar.gz' % gitsrcname
  173. ud.fullmirror = os.path.join(d.getVar("DL_DIR"), mirrortarball)
  174. ud.mirrortarballs = [mirrortarball]
  175. gitdir = d.getVar("GITDIR") or (d.getVar("DL_DIR") + "/git2/")
  176. ud.clonedir = os.path.join(gitdir, gitsrcname)
  177. ud.localfile = ud.clonedir
  178. def localpath(self, ud, d):
  179. return ud.clonedir
  180. def need_update(self, ud, d):
  181. if not os.path.exists(ud.clonedir):
  182. return True
  183. for name in ud.names:
  184. if not self._contains_ref(ud, d, name, ud.clonedir):
  185. return True
  186. if ud.write_tarballs and not os.path.exists(ud.fullmirror):
  187. return True
  188. return False
  189. def try_premirror(self, ud, d):
  190. # If we don't do this, updating an existing checkout with only premirrors
  191. # is not possible
  192. if d.getVar("BB_FETCH_PREMIRRORONLY") is not None:
  193. return True
  194. if os.path.exists(ud.clonedir):
  195. return False
  196. return True
  197. def download(self, ud, d):
  198. """Fetch url"""
  199. # If the checkout doesn't exist and the mirror tarball does, extract it
  200. if not os.path.exists(ud.clonedir) and os.path.exists(ud.fullmirror):
  201. bb.utils.mkdirhier(ud.clonedir)
  202. runfetchcmd("tar -xzf %s" % ud.fullmirror, d, workdir=ud.clonedir)
  203. repourl = self._get_repo_url(ud)
  204. # If the repo still doesn't exist, fallback to cloning it
  205. if not os.path.exists(ud.clonedir):
  206. # We do this since git will use a "-l" option automatically for local urls where possible
  207. if repourl.startswith("file://"):
  208. repourl = repourl[7:]
  209. clone_cmd = "LANG=C %s clone --bare --mirror %s %s --progress" % (ud.basecmd, repourl, ud.clonedir)
  210. if ud.proto.lower() != 'file':
  211. bb.fetch2.check_network_access(d, clone_cmd, ud.url)
  212. progresshandler = GitProgressHandler(d)
  213. runfetchcmd(clone_cmd, d, log=progresshandler)
  214. # Update the checkout if needed
  215. needupdate = False
  216. for name in ud.names:
  217. if not self._contains_ref(ud, d, name, ud.clonedir):
  218. needupdate = True
  219. if needupdate:
  220. try:
  221. runfetchcmd("%s remote rm origin" % ud.basecmd, d, workdir=ud.clonedir)
  222. except bb.fetch2.FetchError:
  223. logger.debug(1, "No Origin")
  224. runfetchcmd("%s remote add --mirror=fetch origin %s" % (ud.basecmd, repourl), d, workdir=ud.clonedir)
  225. fetch_cmd = "LANG=C %s fetch -f --prune --progress %s refs/*:refs/*" % (ud.basecmd, repourl)
  226. if ud.proto.lower() != 'file':
  227. bb.fetch2.check_network_access(d, fetch_cmd, ud.url)
  228. progresshandler = GitProgressHandler(d)
  229. runfetchcmd(fetch_cmd, d, log=progresshandler, workdir=ud.clonedir)
  230. runfetchcmd("%s prune-packed" % ud.basecmd, d, workdir=ud.clonedir)
  231. runfetchcmd("%s pack-redundant --all | xargs -r rm" % ud.basecmd, d, workdir=ud.clonedir)
  232. try:
  233. os.unlink(ud.fullmirror)
  234. except OSError as exc:
  235. if exc.errno != errno.ENOENT:
  236. raise
  237. for name in ud.names:
  238. if not self._contains_ref(ud, d, name, ud.clonedir):
  239. raise bb.fetch2.FetchError("Unable to find revision %s in branch %s even from upstream" % (ud.revisions[name], ud.branches[name]))
  240. def build_mirror_data(self, ud, d):
  241. # Generate a mirror tarball if needed
  242. if ud.write_tarballs and not os.path.exists(ud.fullmirror):
  243. # it's possible that this symlink points to read-only filesystem with PREMIRROR
  244. if os.path.islink(ud.fullmirror):
  245. os.unlink(ud.fullmirror)
  246. logger.info("Creating tarball of git repository")
  247. runfetchcmd("tar -czf %s ." % ud.fullmirror, d, workdir=ud.clonedir)
  248. runfetchcmd("touch %s.done" % ud.fullmirror, d)
  249. def unpack(self, ud, destdir, d):
  250. """ unpack the downloaded src to destdir"""
  251. subdir = ud.parm.get("subpath", "")
  252. if subdir != "":
  253. readpathspec = ":%s" % subdir
  254. def_destsuffix = "%s/" % os.path.basename(subdir.rstrip('/'))
  255. else:
  256. readpathspec = ""
  257. def_destsuffix = "git/"
  258. destsuffix = ud.parm.get("destsuffix", def_destsuffix)
  259. destdir = ud.destdir = os.path.join(destdir, destsuffix)
  260. if os.path.exists(destdir):
  261. bb.utils.prunedir(destdir)
  262. cloneflags = "-s -n"
  263. if ud.bareclone:
  264. cloneflags += " --mirror"
  265. runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, cloneflags, ud.clonedir, destdir), d)
  266. repourl = self._get_repo_url(ud)
  267. runfetchcmd("%s remote set-url origin %s" % (ud.basecmd, repourl), d, workdir=destdir)
  268. if not ud.nocheckout:
  269. if subdir != "":
  270. runfetchcmd("%s read-tree %s%s" % (ud.basecmd, ud.revisions[ud.names[0]], readpathspec), d,
  271. workdir=destdir)
  272. runfetchcmd("%s checkout-index -q -f -a" % ud.basecmd, d, workdir=destdir)
  273. elif not ud.nobranch:
  274. branchname = ud.branches[ud.names[0]]
  275. runfetchcmd("%s checkout -B %s %s" % (ud.basecmd, branchname, \
  276. ud.revisions[ud.names[0]]), d, workdir=destdir)
  277. runfetchcmd("%s branch --set-upstream %s origin/%s" % (ud.basecmd, branchname, \
  278. branchname), d, workdir=destdir)
  279. else:
  280. runfetchcmd("%s checkout %s" % (ud.basecmd, ud.revisions[ud.names[0]]), d, workdir=destdir)
  281. return True
  282. def clean(self, ud, d):
  283. """ clean the git directory """
  284. bb.utils.remove(ud.localpath, True)
  285. bb.utils.remove(ud.fullmirror)
  286. bb.utils.remove(ud.fullmirror + ".done")
  287. def supports_srcrev(self):
  288. return True
  289. def _contains_ref(self, ud, d, name, wd):
  290. cmd = ""
  291. if ud.nobranch:
  292. cmd = "%s log --pretty=oneline -n 1 %s -- 2> /dev/null | wc -l" % (
  293. ud.basecmd, ud.revisions[name])
  294. else:
  295. cmd = "%s branch --contains %s --list %s 2> /dev/null | wc -l" % (
  296. ud.basecmd, ud.revisions[name], ud.branches[name])
  297. try:
  298. output = runfetchcmd(cmd, d, quiet=True, workdir=wd)
  299. except bb.fetch2.FetchError:
  300. return False
  301. if len(output.split()) > 1:
  302. raise bb.fetch2.FetchError("The command '%s' gave output with more then 1 line unexpectedly, output: '%s'" % (cmd, output))
  303. return output.split()[0] != "0"
  304. def _get_repo_url(self, ud):
  305. """
  306. Return the repository URL
  307. """
  308. if ud.user:
  309. username = ud.user + '@'
  310. else:
  311. username = ""
  312. return "%s://%s%s%s" % (ud.proto, username, ud.host, ud.path)
  313. def _revision_key(self, ud, d, name):
  314. """
  315. Return a unique key for the url
  316. """
  317. return "git:" + ud.host + ud.path.replace('/', '.') + ud.unresolvedrev[name]
  318. def _lsremote(self, ud, d, search):
  319. """
  320. Run git ls-remote with the specified search string
  321. """
  322. # Prevent recursion e.g. in OE if SRCPV is in PV, PV is in WORKDIR,
  323. # and WORKDIR is in PATH (as a result of RSS), our call to
  324. # runfetchcmd() exports PATH so this function will get called again (!)
  325. # In this scenario the return call of the function isn't actually
  326. # important - WORKDIR isn't needed in PATH to call git ls-remote
  327. # anyway.
  328. if d.getVar('_BB_GIT_IN_LSREMOTE', False):
  329. return ''
  330. d.setVar('_BB_GIT_IN_LSREMOTE', '1')
  331. try:
  332. repourl = self._get_repo_url(ud)
  333. cmd = "%s ls-remote %s %s" % \
  334. (ud.basecmd, repourl, search)
  335. if ud.proto.lower() != 'file':
  336. bb.fetch2.check_network_access(d, cmd, repourl)
  337. output = runfetchcmd(cmd, d, True)
  338. if not output:
  339. raise bb.fetch2.FetchError("The command %s gave empty output unexpectedly" % cmd, ud.url)
  340. finally:
  341. d.delVar('_BB_GIT_IN_LSREMOTE')
  342. return output
  343. def _latest_revision(self, ud, d, name):
  344. """
  345. Compute the HEAD revision for the url
  346. """
  347. output = self._lsremote(ud, d, "")
  348. # Tags of the form ^{} may not work, need to fallback to other form
  349. if ud.unresolvedrev[name][:5] == "refs/" or ud.usehead:
  350. head = ud.unresolvedrev[name]
  351. tag = ud.unresolvedrev[name]
  352. else:
  353. head = "refs/heads/%s" % ud.unresolvedrev[name]
  354. tag = "refs/tags/%s" % ud.unresolvedrev[name]
  355. for s in [head, tag + "^{}", tag]:
  356. for l in output.strip().split('\n'):
  357. sha1, ref = l.split()
  358. if s == ref:
  359. return sha1
  360. raise bb.fetch2.FetchError("Unable to resolve '%s' in upstream git repository in git ls-remote output for %s" % \
  361. (ud.unresolvedrev[name], ud.host+ud.path))
  362. def latest_versionstring(self, ud, d):
  363. """
  364. Compute the latest release name like "x.y.x" in "x.y.x+gitHASH"
  365. by searching through the tags output of ls-remote, comparing
  366. versions and returning the highest match.
  367. """
  368. pupver = ('', '')
  369. tagregex = re.compile(d.getVar('UPSTREAM_CHECK_GITTAGREGEX') or "(?P<pver>([0-9][\.|_]?)+)")
  370. try:
  371. output = self._lsremote(ud, d, "refs/tags/*")
  372. except bb.fetch2.FetchError or bb.fetch2.NetworkAccess:
  373. return pupver
  374. verstring = ""
  375. revision = ""
  376. for line in output.split("\n"):
  377. if not line:
  378. break
  379. tag_head = line.split("/")[-1]
  380. # Ignore non-released branches
  381. m = re.search("(alpha|beta|rc|final)+", tag_head)
  382. if m:
  383. continue
  384. # search for version in the line
  385. tag = tagregex.search(tag_head)
  386. if tag == None:
  387. continue
  388. tag = tag.group('pver')
  389. tag = tag.replace("_", ".")
  390. if verstring and bb.utils.vercmp(("0", tag, ""), ("0", verstring, "")) < 0:
  391. continue
  392. verstring = tag
  393. revision = line.split()[0]
  394. pupver = (verstring, revision)
  395. return pupver
  396. def _build_revision(self, ud, d, name):
  397. return ud.revisions[name]
  398. def gitpkgv_revision(self, ud, d, name):
  399. """
  400. Return a sortable revision number by counting commits in the history
  401. Based on gitpkgv.bblass in meta-openembedded
  402. """
  403. rev = self._build_revision(ud, d, name)
  404. localpath = ud.localpath
  405. rev_file = os.path.join(localpath, "oe-gitpkgv_" + rev)
  406. if not os.path.exists(localpath):
  407. commits = None
  408. else:
  409. if not os.path.exists(rev_file) or not os.path.getsize(rev_file):
  410. from pipes import quote
  411. commits = bb.fetch2.runfetchcmd(
  412. "git rev-list %s -- | wc -l" % quote(rev),
  413. d, quiet=True).strip().lstrip('0')
  414. if commits:
  415. open(rev_file, "w").write("%d\n" % int(commits))
  416. else:
  417. commits = open(rev_file, "r").readline(128).strip()
  418. if commits:
  419. return False, "%s+%s" % (commits, rev[:7])
  420. else:
  421. return True, str(rev)
  422. def checkstatus(self, fetch, ud, d):
  423. try:
  424. self._lsremote(ud, d, "")
  425. return True
  426. except bb.fetch2.FetchError:
  427. return False