xref: /openbmc/openbmc/poky/bitbake/lib/bb/fetch2/gitsm.py (revision eb8dc403)
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
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.fetch2.git import Git
35from   bb.fetch2 import runfetchcmd
36from   bb.fetch2 import logger
37
38class GitSM(Git):
39    def supports(self, ud, d):
40        """
41        Check to see if a given url can be fetched with git.
42        """
43        return ud.type in ['gitsm']
44
45    def uses_submodules(self, ud, d, wd):
46        for name in ud.names:
47            try:
48                runfetchcmd("%s show %s:.gitmodules" % (ud.basecmd, ud.revisions[name]), d, quiet=True, workdir=wd)
49                return True
50            except bb.fetch.FetchError:
51                pass
52        return False
53
54    def _set_relative_paths(self, repopath):
55        """
56        Fix submodule paths to be relative instead of absolute,
57        so that when we move the repo it doesn't break
58        (In Git 1.7.10+ this is done automatically)
59        """
60        submodules = []
61        with open(os.path.join(repopath, '.gitmodules'), 'r') as f:
62            for line in f.readlines():
63                if line.startswith('[submodule'):
64                    submodules.append(line.split('"')[1])
65
66        for module in submodules:
67            repo_conf = os.path.join(repopath, module, '.git')
68            if os.path.exists(repo_conf):
69                with open(repo_conf, 'r') as f:
70                    lines = f.readlines()
71                newpath = ''
72                for i, line in enumerate(lines):
73                    if line.startswith('gitdir:'):
74                        oldpath = line.split(': ')[-1].rstrip()
75                        if oldpath.startswith('/'):
76                            newpath = '../' * (module.count('/') + 1) + '.git/modules/' + module
77                            lines[i] = 'gitdir: %s\n' % newpath
78                            break
79                if newpath:
80                    with open(repo_conf, 'w') as f:
81                        for line in lines:
82                            f.write(line)
83
84            repo_conf2 = os.path.join(repopath, '.git', 'modules', module, 'config')
85            if os.path.exists(repo_conf2):
86                with open(repo_conf2, 'r') as f:
87                    lines = f.readlines()
88                newpath = ''
89                for i, line in enumerate(lines):
90                    if line.lstrip().startswith('worktree = '):
91                        oldpath = line.split(' = ')[-1].rstrip()
92                        if oldpath.startswith('/'):
93                            newpath = '../' * (module.count('/') + 3) + module
94                            lines[i] = '\tworktree = %s\n' % newpath
95                            break
96                if newpath:
97                    with open(repo_conf2, 'w') as f:
98                        for line in lines:
99                            f.write(line)
100
101    def update_submodules(self, ud, d):
102        # We have to convert bare -> full repo, do the submodule bit, then convert back
103        tmpclonedir = ud.clonedir + ".tmp"
104        gitdir = tmpclonedir + os.sep + ".git"
105        bb.utils.remove(tmpclonedir, True)
106        os.mkdir(tmpclonedir)
107        os.rename(ud.clonedir, gitdir)
108        runfetchcmd("sed " + gitdir + "/config -i -e 's/bare.*=.*true/bare = false/'", d)
109        runfetchcmd(ud.basecmd + " reset --hard", d, workdir=tmpclonedir)
110        runfetchcmd(ud.basecmd + " checkout -f " + ud.revisions[ud.names[0]], d, workdir=tmpclonedir)
111        runfetchcmd(ud.basecmd + " submodule update --init --recursive", d, workdir=tmpclonedir)
112        self._set_relative_paths(tmpclonedir)
113        runfetchcmd("sed " + gitdir + "/config -i -e 's/bare.*=.*false/bare = true/'", d, workdir=tmpclonedir)
114        os.rename(gitdir, ud.clonedir,)
115        bb.utils.remove(tmpclonedir, True)
116
117    def download(self, ud, d):
118        Git.download(self, ud, d)
119
120        if not ud.shallow or ud.localpath != ud.fullshallow:
121            submodules = self.uses_submodules(ud, d, ud.clonedir)
122            if submodules:
123                self.update_submodules(ud, d)
124
125    def clone_shallow_local(self, ud, dest, d):
126        super(GitSM, self).clone_shallow_local(ud, dest, d)
127
128        runfetchcmd('cp -fpPRH "%s/modules" "%s/"' % (ud.clonedir, os.path.join(dest, '.git')), d)
129
130    def unpack(self, ud, destdir, d):
131        Git.unpack(self, ud, destdir, d)
132
133        if self.uses_submodules(ud, d, ud.destdir):
134            runfetchcmd(ud.basecmd + " checkout " + ud.revisions[ud.names[0]], d, workdir=ud.destdir)
135            runfetchcmd(ud.basecmd + " submodule update --init --recursive", d, workdir=ud.destdir)
136