git.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. """
  6. #Copyright (C) 2005 Richard Purdie
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License version 2 as
  10. # published by the Free Software Foundation.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. import os, re
  21. import bb
  22. from bb import data
  23. from bb.fetch import Fetch
  24. from bb.fetch import FetchError
  25. def prunedir(topdir):
  26. # Delete everything reachable from the directory named in 'topdir'.
  27. # CAUTION: This is dangerous!
  28. for root, dirs, files in os.walk(topdir, topdown=False):
  29. for name in files:
  30. os.remove(os.path.join(root, name))
  31. for name in dirs:
  32. os.rmdir(os.path.join(root, name))
  33. def rungitcmd(cmd,d):
  34. bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % cmd)
  35. # Need to export PATH as git is likely to be in metadata paths
  36. # rather than host provided
  37. pathcmd = 'export PATH=%s; %s' % (data.expand('${PATH}', d), cmd)
  38. myret = os.system(pathcmd)
  39. if myret != 0:
  40. raise FetchError("Git: %s failed" % pathcmd)
  41. class Git(Fetch):
  42. """Class to fetch a module or modules from git repositories"""
  43. def supports(self, url, ud, d):
  44. """
  45. Check to see if a given url can be fetched with git.
  46. """
  47. return ud.type in ['git']
  48. def localpath(self, url, ud, d):
  49. ud.proto = "rsync"
  50. if 'protocol' in ud.parm:
  51. ud.proto = ud.parm['protocol']
  52. ud.tag = "master"
  53. if 'tag' in ud.parm:
  54. ud.tag = ud.parm['tag']
  55. ud.localfile = data.expand('git_%s%s_%s.tar.gz' % (ud.host, ud.path.replace('/', '.'), ud.tag), d)
  56. return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
  57. def forcefetch(self, url, ud, d):
  58. # tag=="master" must always update
  59. if (ud.tag == "master"):
  60. return True
  61. return False
  62. def go(self, loc, ud, d):
  63. """Fetch url"""
  64. if not self.forcefetch(loc, ud, d) and Fetch.try_mirror(d, ud.localfile):
  65. bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists (or was stashed). Skipping git checkout." % ud.localpath)
  66. return
  67. gitsrcname = '%s%s' % (ud.host, ud.path.replace('/', '.'))
  68. repofilename = 'git_%s.tar.gz' % (gitsrcname)
  69. repofile = os.path.join(data.getVar("DL_DIR", d, 1), repofilename)
  70. repodir = os.path.join(data.expand('${GITDIR}', d), gitsrcname)
  71. coname = '%s' % (ud.tag)
  72. codir = os.path.join(repodir, coname)
  73. if not os.path.exists(repodir):
  74. if Fetch.try_mirror(d, repofilename):
  75. bb.mkdirhier(repodir)
  76. os.chdir(repodir)
  77. rungitcmd("tar -xzf %s" % (repofile),d)
  78. else:
  79. rungitcmd("git clone -n %s://%s%s %s" % (ud.proto, ud.host, ud.path, repodir),d)
  80. os.chdir(repodir)
  81. rungitcmd("git pull %s://%s%s" % (ud.proto, ud.host, ud.path),d)
  82. rungitcmd("git pull --tags %s://%s%s" % (ud.proto, ud.host, ud.path),d)
  83. rungitcmd("git prune-packed", d)
  84. rungitcmd("git pack-redundant --all | xargs -r rm", d)
  85. # Remove all but the .git directory
  86. rungitcmd("rm * -Rf", d)
  87. # old method of downloading tags
  88. #rungitcmd("rsync -a --verbose --stats --progress rsync://%s%s/ %s" % (ud.host, ud.path, os.path.join(repodir, ".git", "")),d)
  89. os.chdir(repodir)
  90. bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git repository")
  91. rungitcmd("tar -czf %s %s" % (repofile, os.path.join(".", ".git", "*") ),d)
  92. if os.path.exists(codir):
  93. prunedir(codir)
  94. bb.mkdirhier(codir)
  95. os.chdir(repodir)
  96. rungitcmd("git read-tree %s" % (ud.tag),d)
  97. rungitcmd("git checkout-index -q -f --prefix=%s -a" % (os.path.join(codir, "git", "")),d)
  98. os.chdir(codir)
  99. bb.msg.note(1, bb.msg.domain.Fetcher, "Creating tarball of git checkout")
  100. rungitcmd("tar -czf %s %s" % (ud.localpath, os.path.join(".", "*") ),d)