gitsm.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 submodules implementation
  5. Inherits from and extends the Git fetcher to retrieve submodules of a git repository
  6. after cloning.
  7. SRC_URI = "gitsm://<see Git fetcher for syntax>"
  8. See the Git fetcher, git://, for usage documentation.
  9. NOTE: Switching a SRC_URI from "git://" to "gitsm://" requires a clean of your recipe.
  10. """
  11. # Copyright (C) 2013 Richard Purdie
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License version 2 as
  15. # published by the Free Software Foundation.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License along
  23. # with this program; if not, write to the Free Software Foundation, Inc.,
  24. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25. import os
  26. import bb
  27. from bb import data
  28. from bb.fetch2.git import Git
  29. from bb.fetch2 import runfetchcmd
  30. from bb.fetch2 import logger
  31. class GitSM(Git):
  32. def supports(self, ud, d):
  33. """
  34. Check to see if a given url can be fetched with git.
  35. """
  36. return ud.type in ['gitsm']
  37. def uses_submodules(self, ud, d):
  38. for name in ud.names:
  39. try:
  40. runfetchcmd("%s show %s:.gitmodules" % (ud.basecmd, ud.revisions[name]), d, quiet=True)
  41. return True
  42. except bb.fetch.FetchError:
  43. pass
  44. return False
  45. def _set_relative_paths(self, repopath):
  46. """
  47. Fix submodule paths to be relative instead of absolute,
  48. so that when we move the repo it doesn't break
  49. (In Git 1.7.10+ this is done automatically)
  50. """
  51. submodules = []
  52. with open(os.path.join(repopath, '.gitmodules'), 'r') as f:
  53. for line in f.readlines():
  54. if line.startswith('[submodule'):
  55. submodules.append(line.split('"')[1])
  56. for module in submodules:
  57. repo_conf = os.path.join(repopath, module, '.git')
  58. if os.path.exists(repo_conf):
  59. with open(repo_conf, 'r') as f:
  60. lines = f.readlines()
  61. newpath = ''
  62. for i, line in enumerate(lines):
  63. if line.startswith('gitdir:'):
  64. oldpath = line.split(': ')[-1].rstrip()
  65. if oldpath.startswith('/'):
  66. newpath = '../' * (module.count('/') + 1) + '.git/modules/' + module
  67. lines[i] = 'gitdir: %s\n' % newpath
  68. break
  69. if newpath:
  70. with open(repo_conf, 'w') as f:
  71. for line in lines:
  72. f.write(line)
  73. repo_conf2 = os.path.join(repopath, '.git', 'modules', module, 'config')
  74. if os.path.exists(repo_conf2):
  75. with open(repo_conf2, 'r') as f:
  76. lines = f.readlines()
  77. newpath = ''
  78. for i, line in enumerate(lines):
  79. if line.lstrip().startswith('worktree = '):
  80. oldpath = line.split(' = ')[-1].rstrip()
  81. if oldpath.startswith('/'):
  82. newpath = '../' * (module.count('/') + 3) + module
  83. lines[i] = '\tworktree = %s\n' % newpath
  84. break
  85. if newpath:
  86. with open(repo_conf2, 'w') as f:
  87. for line in lines:
  88. f.write(line)
  89. def update_submodules(self, ud, d):
  90. # We have to convert bare -> full repo, do the submodule bit, then convert back
  91. tmpclonedir = ud.clonedir + ".tmp"
  92. gitdir = tmpclonedir + os.sep + ".git"
  93. bb.utils.remove(tmpclonedir, True)
  94. os.mkdir(tmpclonedir)
  95. os.rename(ud.clonedir, gitdir)
  96. runfetchcmd("sed " + gitdir + "/config -i -e 's/bare.*=.*true/bare = false/'", d)
  97. os.chdir(tmpclonedir)
  98. runfetchcmd(ud.basecmd + " reset --hard", d)
  99. runfetchcmd(ud.basecmd + " checkout " + ud.revisions[ud.names[0]], d)
  100. runfetchcmd(ud.basecmd + " submodule init", d)
  101. runfetchcmd(ud.basecmd + " submodule update", d)
  102. self._set_relative_paths(tmpclonedir)
  103. runfetchcmd("sed " + gitdir + "/config -i -e 's/bare.*=.*false/bare = true/'", d)
  104. os.rename(gitdir, ud.clonedir,)
  105. bb.utils.remove(tmpclonedir, True)
  106. def download(self, ud, d):
  107. Git.download(self, ud, d)
  108. os.chdir(ud.clonedir)
  109. submodules = self.uses_submodules(ud, d)
  110. if submodules:
  111. self.update_submodules(ud, d)
  112. def unpack(self, ud, destdir, d):
  113. Git.unpack(self, ud, destdir, d)
  114. os.chdir(ud.destdir)
  115. submodules = self.uses_submodules(ud, d)
  116. if submodules:
  117. runfetchcmd(ud.basecmd + " checkout " + ud.revisions[ud.names[0]], d)
  118. runfetchcmd(ud.basecmd + " submodule init", d)
  119. runfetchcmd(ud.basecmd + " submodule update", d)