1
0
mirror of https://git.yoctoproject.org/poky synced 2026-07-16 03:47:03 +00:00

bitbake: fetch2: Check for git-lfs existence before using it

So far, existence of `git-lfs` was only checked during unpacking. As the
binary is also used in earlier steps also check for its existence there.

Additionally, factor out the LFS existence check into a dedicated
function and call it wherever git-lfs is used for the first time.

(Bitbake rev: 5818367db9b261b7e07c347d38044e6cba8f9727)

Signed-off-by: Philip Lorenz <philip.lorenz@bmw.de>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Philip Lorenz
2025-04-29 10:11:19 +02:00
committed by Richard Purdie
parent 6d0cb00c68
commit 7642477dc8
2 changed files with 68 additions and 31 deletions
+50 -23
View File
@@ -9,6 +9,7 @@
import contextlib
import shutil
import unittest
import unittest.mock
import urllib.parse
import hashlib
import tempfile
@@ -2292,12 +2293,18 @@ class GitLfsTest(FetcherTest):
self.git_init(cwd=self.srcdir)
self.commit_file('.gitattributes', '*.mp3 filter=lfs -text')
def commit_file(self, filename, content):
with open(os.path.join(self.srcdir, filename), "w") as f:
def commit(self, *, cwd=None):
cwd = cwd or self.srcdir
self.git(["commit", "-m", "Change"], cwd=cwd)
return self.git(["rev-parse", "HEAD"], cwd=cwd).strip()
def commit_file(self, filename, content, *, cwd=None):
cwd = cwd or self.srcdir
with open(os.path.join(cwd, filename), "w") as f:
f.write(content)
self.git(["add", filename], cwd=self.srcdir)
self.git(["commit", "-m", "Change"], cwd=self.srcdir)
return self.git(["rev-parse", "HEAD"], cwd=self.srcdir).strip()
self.git(["add", filename], cwd=cwd)
return self.commit(cwd=cwd)
def fetch(self, uri=None, download=True):
uris = self.d.getVar('SRC_URI').split()
@@ -2406,6 +2413,21 @@ class GitLfsTest(FetcherTest):
fetcher, ud = self.fetch()
fetcher.unpack(self.d.getVar('WORKDIR'))
@skipIfNoGitLFS()
def test_lfs_enabled_not_installed_during_unpack(self):
uri = 'git://%s;protocol=file;lfs=1;branch=master' % self.srcdir
self.d.setVar('SRC_URI', uri)
# Careful: suppress initial attempt at downloading
fetcher, ud = self.fetch(uri=None, download=False)
fetcher.download()
# If git-lfs cannot be found, the unpack should throw an error
with self.assertRaises(bb.fetch2.FetchError):
with unittest.mock.patch("shutil.which", return_value=None):
shutil.rmtree(self.gitdir, ignore_errors=True)
fetcher.unpack(self.d.getVar('WORKDIR'))
def test_lfs_enabled_not_installed(self):
uri = 'git://%s;protocol=file;lfs=1;branch=master' % self.srcdir
self.d.setVar('SRC_URI', uri)
@@ -2413,18 +2435,10 @@ class GitLfsTest(FetcherTest):
# Careful: suppress initial attempt at downloading
fetcher, ud = self.fetch(uri=None, download=False)
# Artificially assert that git-lfs is not installed, so
# we can verify a failure to unpack in it's absence.
old_find_git_lfs = ud.method._find_git_lfs
try:
# If git-lfs cannot be found, the unpack should throw an error
# If git-lfs cannot be found, the download should throw an error
with unittest.mock.patch("shutil.which", return_value=None):
with self.assertRaises(bb.fetch2.FetchError):
fetcher.download()
ud.method._find_git_lfs = lambda d: False
shutil.rmtree(self.gitdir, ignore_errors=True)
fetcher.unpack(self.d.getVar('WORKDIR'))
finally:
ud.method._find_git_lfs = old_find_git_lfs
def test_lfs_disabled_not_installed(self):
uri = 'git://%s;protocol=file;lfs=0;branch=master' % self.srcdir
@@ -2433,17 +2447,30 @@ class GitLfsTest(FetcherTest):
# Careful: suppress initial attempt at downloading
fetcher, ud = self.fetch(uri=None, download=False)
# Artificially assert that git-lfs is not installed, so
# we can verify a failure to unpack in it's absence.
old_find_git_lfs = ud.method._find_git_lfs
try:
# Even if git-lfs cannot be found, the unpack should be successful
# Even if git-lfs cannot be found, the download / unpack should be successful
with unittest.mock.patch("shutil.which", return_value=None):
fetcher.download()
shutil.rmtree(self.gitdir, ignore_errors=True)
fetcher.unpack(self.d.getVar('WORKDIR'))
def test_lfs_enabled_not_installed_but_not_needed(self):
srcdir = os.path.join(self.tempdir, "emptygit")
bb.utils.mkdirhier(srcdir)
self.git_init(srcdir)
self.commit_file("test", "test content", cwd=srcdir)
uri = 'git://%s;protocol=file;lfs=1;branch=master' % srcdir
self.d.setVar('SRC_URI', uri)
# Careful: suppress initial attempt at downloading
fetcher, ud = self.fetch(uri=None, download=False)
# It shouldnt't matter that git-lfs cannot be found as the repository configuration does not
# specify any LFS filters.
with unittest.mock.patch("shutil.which", return_value=None):
fetcher.download()
ud.method._find_git_lfs = lambda d: False
shutil.rmtree(self.gitdir, ignore_errors=True)
fetcher.unpack(self.d.getVar('WORKDIR'))
finally:
ud.method._find_git_lfs = old_find_git_lfs
class GitURLWithSpacesTest(FetcherTest):
test_git_urls = {