1"""
2BitBake 'Fetch' git annex implementation
3"""
4
5# Copyright (C) 2014 Otavio Salvador
6# Copyright (C) 2014 O.S. Systems Software LTDA.
7#
8# SPDX-License-Identifier: GPL-2.0-only
9#
10
11import bb
12from   bb.fetch2.git import Git
13from   bb.fetch2 import runfetchcmd
14
15class GitANNEX(Git):
16    def supports(self, ud, d):
17        """
18        Check to see if a given url can be fetched with git.
19        """
20        return ud.type in ['gitannex']
21
22    def urldata_init(self, ud, d):
23        super(GitANNEX, self).urldata_init(ud, d)
24        if ud.shallow:
25            ud.shallow_extra_refs += ['refs/heads/git-annex', 'refs/heads/synced/*']
26
27    def uses_annex(self, ud, d, wd):
28        for name in ud.names:
29            try:
30                runfetchcmd("%s rev-list git-annex" % (ud.basecmd), d, quiet=True, workdir=wd)
31                return True
32            except bb.fetch.FetchError:
33                pass
34
35        return False
36
37    def update_annex(self, ud, d, wd):
38        try:
39            runfetchcmd("%s annex get --all" % (ud.basecmd), d, quiet=True, workdir=wd)
40        except bb.fetch.FetchError:
41            return False
42        runfetchcmd("chmod u+w -R %s/annex" % (ud.clonedir), d, quiet=True, workdir=wd)
43
44        return True
45
46    def download(self, ud, d):
47        Git.download(self, ud, d)
48
49        if not ud.shallow or ud.localpath != ud.fullshallow:
50            if self.uses_annex(ud, d, ud.clonedir):
51                self.update_annex(ud, d, ud.clonedir)
52
53    def clone_shallow_local(self, ud, dest, d):
54        super(GitANNEX, self).clone_shallow_local(ud, dest, d)
55
56        try:
57            runfetchcmd("%s annex init" % ud.basecmd, d, workdir=dest)
58        except bb.fetch.FetchError:
59            pass
60
61        if self.uses_annex(ud, d, dest):
62            runfetchcmd("%s annex get" % ud.basecmd, d, workdir=dest)
63            runfetchcmd("chmod u+w -R %s/.git/annex" % (dest), d, quiet=True, workdir=dest)
64
65    def unpack(self, ud, destdir, d):
66        Git.unpack(self, ud, destdir, d)
67
68        try:
69            runfetchcmd("%s annex init" % (ud.basecmd), d, workdir=ud.destdir)
70        except bb.fetch.FetchError:
71            pass
72
73        annex = self.uses_annex(ud, d, ud.destdir)
74        if annex:
75            runfetchcmd("%s annex get" % (ud.basecmd), d, workdir=ud.destdir)
76            runfetchcmd("chmod u+w -R %s/.git/annex" % (ud.destdir), d, quiet=True, workdir=ud.destdir)
77
78