summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/fetch2/gitsm.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/fetch2/gitsm.py')
-rw-r--r--bitbake/lib/bb/fetch2/gitsm.py126
1 files changed, 126 insertions, 0 deletions
diff --git a/bitbake/lib/bb/fetch2/gitsm.py b/bitbake/lib/bb/fetch2/gitsm.py
new file mode 100644
index 0000000..1a76215
--- /dev/null
+++ b/bitbake/lib/bb/fetch2/gitsm.py
@@ -0,0 +1,126 @@
1# 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"""
6
7# Copyright (C) 2013 Richard Purdie
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22import os
23import bb
24from bb import data
25from bb.fetch2.git import Git
26from bb.fetch2 import runfetchcmd
27from bb.fetch2 import logger
28
29class GitSM(Git):
30 def supports(self, ud, d):
31 """
32 Check to see if a given url can be fetched with git.
33 """
34 return ud.type in ['gitsm']
35
36 def uses_submodules(self, ud, d):
37 for name in ud.names:
38 try:
39 runfetchcmd("%s show %s:.gitmodules" % (ud.basecmd, ud.revisions[name]), d, quiet=True)
40 return True
41 except bb.fetch.FetchError:
42 pass
43 return False
44
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
57 for module in submodules:
58 repo_conf = os.path.join(repopath, module, '.git')
59 if os.path.exists(repo_conf):
60 with open(repo_conf, 'r') as f:
61 lines = f.readlines()
62 newpath = ''
63 for i, line in enumerate(lines):
64 if line.startswith('gitdir:'):
65 oldpath = line.split(': ')[-1].rstrip()
66 if oldpath.startswith('/'):
67 newpath = '../' * (module.count('/') + 1) + '.git/modules/' + module
68 lines[i] = 'gitdir: %s\n' % newpath
69 break
70 if newpath:
71 with open(repo_conf, 'w') as f:
72 for line in lines:
73 f.write(line)
74
75 repo_conf2 = os.path.join(repopath, '.git', 'modules', module, 'config')
76 if os.path.exists(repo_conf2):
77 with open(repo_conf2, 'r') as f:
78 lines = f.readlines()
79 newpath = ''
80 for i, line in enumerate(lines):
81 if line.lstrip().startswith('worktree = '):
82 oldpath = line.split(' = ')[-1].rstrip()
83 if oldpath.startswith('/'):
84 newpath = '../' * (module.count('/') + 3) + module
85 lines[i] = '\tworktree = %s\n' % newpath
86 break
87 if newpath:
88 with open(repo_conf2, 'w') as f:
89 for line in lines:
90 f.write(line)
91
92 def update_submodules(self, ud, d):
93 # We have to convert bare -> full repo, do the submodule bit, then convert back
94 tmpclonedir = ud.clonedir + ".tmp"
95 gitdir = tmpclonedir + os.sep + ".git"
96 bb.utils.remove(tmpclonedir, True)
97 os.mkdir(tmpclonedir)
98 os.rename(ud.clonedir, gitdir)
99 runfetchcmd("sed " + gitdir + "/config -i -e 's/bare.*=.*true/bare = false/'", d)
100 os.chdir(tmpclonedir)
101 runfetchcmd(ud.basecmd + " reset --hard", d)
102 runfetchcmd(ud.basecmd + " submodule init", d)
103 runfetchcmd(ud.basecmd + " submodule update", d)
104 self._set_relative_paths(tmpclonedir)
105 runfetchcmd("sed " + gitdir + "/config -i -e 's/bare.*=.*false/bare = true/'", d)
106 os.rename(gitdir, ud.clonedir,)
107 bb.utils.remove(tmpclonedir, True)
108
109 def download(self, ud, d):
110 Git.download(self, ud, d)
111
112 os.chdir(ud.clonedir)
113 submodules = self.uses_submodules(ud, d)
114 if submodules:
115 self.update_submodules(ud, d)
116
117 def unpack(self, ud, destdir, d):
118 Git.unpack(self, ud, destdir, d)
119
120 os.chdir(ud.destdir)
121 submodules = self.uses_submodules(ud, d)
122 if submodules:
123 runfetchcmd("cp -r " + ud.clonedir + "/modules " + ud.destdir + "/.git/", d)
124 runfetchcmd(ud.basecmd + " submodule init", d)
125 runfetchcmd(ud.basecmd + " submodule update", d)
126