diff options
Diffstat (limited to 'bitbake/lib/bb/fetch2/gitsm.py')
-rw-r--r-- | bitbake/lib/bb/fetch2/gitsm.py | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/bitbake/lib/bb/fetch2/gitsm.py b/bitbake/lib/bb/fetch2/gitsm.py new file mode 100644 index 0000000000..c125cff54b --- /dev/null +++ b/bitbake/lib/bb/fetch2/gitsm.py | |||
@@ -0,0 +1,136 @@ | |||
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 | |||
6 | Inherits from and extends the Git fetcher to retrieve submodules of a git repository | ||
7 | after cloning. | ||
8 | |||
9 | SRC_URI = "gitsm://<see Git fetcher for syntax>" | ||
10 | |||
11 | See the Git fetcher, git://, for usage documentation. | ||
12 | |||
13 | NOTE: 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 | |||
32 | import os | ||
33 | import bb | ||
34 | from bb import data | ||
35 | from bb.fetch2.git import Git | ||
36 | from bb.fetch2 import runfetchcmd | ||
37 | from bb.fetch2 import logger | ||
38 | |||
39 | class 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 + " submodule init", d) | ||
113 | runfetchcmd(ud.basecmd + " submodule update", d) | ||
114 | 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: | ||
133 | runfetchcmd("cp -r " + ud.clonedir + "/modules " + ud.destdir + "/.git/", d) | ||
134 | runfetchcmd(ud.basecmd + " submodule init", d) | ||
135 | runfetchcmd(ud.basecmd + " submodule update", d) | ||
136 | |||