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