blob: 0392e48d1aa0219499dc4195f726014cbe75f5bd [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)
113 runfetchcmd(ud.basecmd + " submodule init", d)
114 runfetchcmd(ud.basecmd + " submodule update", d)
115 self._set_relative_paths(tmpclonedir)
116 runfetchcmd("sed " + gitdir + "/config -i -e 's/bare.*=.*false/bare = true/'", d)
117 os.rename(gitdir, ud.clonedir,)
118 bb.utils.remove(tmpclonedir, True)
119
120 def download(self, ud, d):
121 Git.download(self, ud, d)
122
123 os.chdir(ud.clonedir)
124 submodules = self.uses_submodules(ud, d)
125 if submodules:
126 self.update_submodules(ud, d)
127
128 def unpack(self, ud, destdir, d):
129 Git.unpack(self, ud, destdir, d)
130
131 os.chdir(ud.destdir)
132 submodules = self.uses_submodules(ud, d)
133 if submodules:
134 runfetchcmd("cp -r " + ud.clonedir + "/modules " + ud.destdir + "/.git/", d)
135 runfetchcmd(ud.basecmd + " submodule init", d)
136 runfetchcmd(ud.basecmd + " submodule update", d)
137