fetch2/git: Check fullshallow tarball and clonedir before access
v2 cover fetch2/git: Check fullshallow tarball and clonedir before access
In contrast to the first version of the patches, we don't use bb.fatal to signal an error but raise an bb.fetch2.UnpackError exception.
LogRecord is not included in the patches since it is not used anymore.
Urs Fässler (3):
fetch2/git: prevent access to non-existing clonedir fetch2/git: explicitly show the decision logic to select the source in
code
fetch2/git: provide information about missing sources
lib/bb/fetch2/git.py | 26 +++++++++++++++++++++----- lib/bb/tests/fetch.py | 13 +++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-)
Signature · 1 line
2.19.0
PATCH v2 1/3 fetch2/git: prevent access to non-existing clonedir
A user friendly error is throw when neither the clonedir nor fullshallow exist. Without the check, a difficult to interpret error is throw from within the fetch command.
--- lib/bb/fetch2/git.py | 4 +++- lib/bb/tests/fetch.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-)
lib/bb/fetch2/git.py +3 -1
@@ -479,8 +479,10 @@ class Git(FetchMethod): if ud.shallow and os.path.exists(ud.fullshallow) and self.clonedir_need_update(ud, d): bb.utils.mkdirhier(destdir) runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) - else: + elif not self.clonedir_need_update(ud, d): runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) + else: + raise bb.fetch2.UnpackError("No up to date source found", ud.url) repourl = self._get_repo_url(ud) runfetchcmd("%s remote set-url origin %s" % (ud.basecmd, repourl), d, workdir=destdir)
lib/bb/tests/fetch.py +11 -0
@@ -1581,6 +1581,17 @@ class GitShallowTest(FetcherTest): self.assertRefs(['master', 'origin/master']) self.assertRevCount(orig_revs - 1758) + def test_that_unpack_throws_an_error_when_the_git_clone_nor_shallow_tarball_exist(self): + self.add_empty_file('a') + fetcher, ud = self.fetch() + bb.utils.remove(self.gitdir, recurse=True) + bb.utils.remove(self.dldir, recurse=True) + + with self.assertRaises(bb.fetch2.UnpackError) as context: + fetcher.unpack(self.d.getVar('WORKDIR')) + + self.assertTrue("No up to date source found" in context.exception.msg) + @skipIfNoNetwork() def test_that_unpack_does_work_when_using_git_shallow_tarball_but_tarball_is_not_available(self): self.d.setVar('SRCREV', 'e5939ff608b95cdd4d0ab0e1935781ab9a276ac0')
Signature · 1 line
2.19.0
PATCH v2 2/3 fetch2/git: explicitly show the decision logic to select the source in code
In reply to Urs Fässler--- lib/bb/fetch2/git.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/bb/fetch2/git.py +4 -3
@@ -476,11 +476,12 @@ class Git(FetchMethod): if os.path.exists(destdir): bb.utils.prunedir(destdir) - if ud.shallow and os.path.exists(ud.fullshallow) and self.clonedir_need_update(ud, d): + clonedir_is_up_to_date = not self.clonedir_need_update(ud, d) + if clonedir_is_up_to_date: + runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) + elif ud.shallow and os.path.exists(ud.fullshallow): bb.utils.mkdirhier(destdir) runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) - elif not self.clonedir_need_update(ud, d): - runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) else: raise bb.fetch2.UnpackError("No up to date source found", ud.url)
Signature · 1 line
2.19.0
PATCH v2 3/3 fetch2/git: provide information about missing sources
In reply to Urs FässlerProvide more information in the case the sources are not found in the unpack step.
--- lib/bb/fetch2/git.py | 29 +++++++++++++++++++++-------- lib/bb/tests/fetch.py | 2 ++ 2 files changed, 23 insertions(+), 8 deletions(-)
lib/bb/fetch2/git.py +21 -8
@@ -476,14 +476,27 @@ class Git(FetchMethod): if os.path.exists(destdir): bb.utils.prunedir(destdir) - clonedir_is_up_to_date = not self.clonedir_need_update(ud, d) - if clonedir_is_up_to_date: - runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) - elif ud.shallow and os.path.exists(ud.fullshallow): - bb.utils.mkdirhier(destdir) - runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) - else: - raise bb.fetch2.UnpackError("No up to date source found", ud.url) + source_found = False + source_error = [] + + if not source_found: + clonedir_is_up_to_date = not self.clonedir_need_update(ud, d) + if clonedir_is_up_to_date: + runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) + source_found = True + else: + source_error.append("clone directory not available or not up to date: " + ud.clonedir) + + if not source_found: + if ud.shallow and os.path.exists(ud.fullshallow): + bb.utils.mkdirhier(destdir) + runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) + source_found = True + else: + source_error.append("shallow clone not enabled or not available: " + ud.fullshallow) + + if not source_found: + raise bb.fetch2.UnpackError("No up to date source found: " + "; ".join(source_error), ud.url) repourl = self._get_repo_url(ud) runfetchcmd("%s remote set-url origin %s" % (ud.basecmd, repourl), d, workdir=destdir)
lib/bb/tests/fetch.py +2 -0
@@ -1591,6 +1591,8 @@ class GitShallowTest(FetcherTest): fetcher.unpack(self.d.getVar('WORKDIR')) self.assertTrue("No up to date source found" in context.exception.msg) + self.assertTrue("clone directory not available or not up to date" in context.exception.msg) + self.assertTrue("shallow clone not enabled or not available" in context.exception.msg) @skipIfNoNetwork() def test_that_unpack_does_work_when_using_git_shallow_tarball_but_tarball_is_not_available(self):
Signature · 1 line
2.19.0
Re: [PATCH v2 3/3] fetch2/git: provide information about missing sources
Quoted 50 lines · Urs Fässler
Provide more information in the case the sources are not found in the unpack step.
Signed-off-by Urs Fässler <[email protected]>Signed-off-by Pascal Bach <[email protected]>--- lib/bb/fetch2/git.py | 29 +++++++++++++++++++++-------- lib/bb/tests/fetch.py | 2 ++ 2 files changed, 23 insertions(+), 8 deletions(-)lib/bb/fetch2/git.py +7
-8@@ -476,14 +476,27 @@ class Git(FetchMethod): if os.path.exists(destdir): bb.utils.prunedir(destdir) - clonedir_is_up_to_date = not self.clonedir_need_update(ud, d) - if clonedir_is_up_to_date: - runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) - elif ud.shallow and os.path.exists(ud.fullshallow): - bb.utils.mkdirhier(destdir) - runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) - else: - raise bb.fetch2.UnpackError("No up to date source found", ud.url) + source_found = False + source_error = [] + + if not source_found: + clonedir_is_up_to_date = not self.clonedir_need_update(ud, d) + if clonedir_is_up_to_date: + runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd,ud.cloneflags, ud.clonedir, destdir), d)
lib/bb/fetch2/git.py +3
-0+ source_found = True + else: + source_error.append("clone directory not available or notup to date: " + ud.clonedir)
lib/bb/fetch2/git.py +5
-0+ + if not source_found: + if ud.shallow and os.path.exists(ud.fullshallow): + bb.utils.mkdirhier(destdir) + runfetchcmd("tar -xzf %s" % ud.fullshallow, d,workdir=destdir)
lib/bb/fetch2/git.py +3
-0+ source_found = True + else: + source_error.append("shallow clone not enabled or not
Quoted 2 lines
available: " + ud.fullshallow)
This logic is flawed. In the case where the source hasn’t been found and ud.shallow is false, as shallow is disabled, the else clause tries to access ud.fullshallow, which isn’t an existing attribute:
Exception: AttributeError: 'FetchData' object has no attribute 'fullshallow'
Signature · 4 lines
Christopher Larson kergoth at gmail dot com Founder - BitBake, OpenEmbedded, OpenZaurus Senior Software Engineer, Mentor Graphics