All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fetch2/git: Check srcrev exists in checkstatus()
@ 2022-07-05 17:46 Maia Xiao
  2022-07-05 21:27 ` [bitbake-devel] " Richard Purdie
  2022-07-06 15:12 ` Bruce Ashfield
  0 siblings, 2 replies; 9+ messages in thread
From: Maia Xiao @ 2022-07-05 17:46 UTC (permalink / raw)
  To: bitbake-devel; +Cc: Paul Eggleton

Currently we only check that we can list the remote, but do not validate
if the srcrev actually exists. Now we ensure that the rev exists by
attempting to shallow clone it from the remote.

Fixes [YOCTO #11199].

Signed-off-by: Maia Xiao <t-maiaxiao@linux.microsoft.com>
---
 lib/bb/fetch2/git.py  | 28 +++++++++++++++++++++++-----
 lib/bb/tests/fetch.py | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/lib/bb/fetch2/git.py b/lib/bb/fetch2/git.py
index 07b3d9a7..99780de8 100644
--- a/lib/bb/fetch2/git.py
+++ b/lib/bb/fetch2/git.py
@@ -832,8 +832,26 @@ class Git(FetchMethod):
             return True, str(rev)
 
     def checkstatus(self, fetch, ud, d):
-        try:
-            self._lsremote(ud, d, "")
-            return True
-        except bb.fetch2.FetchError:
-            return False
+        repourl = self._get_repo_url(ud)
+        for name in ud.names:
+            tmpdir = tempfile.mkdtemp()
+            revision = ud.revisions[name]
+
+            try:
+                # Initialise an empty git repo for shallow fetching
+                runfetchcmd("%s init" % ud.basecmd, d, workdir=tmpdir)
+                runfetchcmd("%s remote add origin %s" %
+                            (ud.basecmd, shlex.quote(repourl)),
+                            d,
+                            workdir=tmpdir)
+
+                # Try to fetch only the specified srcrev
+                runfetchcmd("%s fetch -f --progress --depth 1 origin %s" %
+                            (ud.basecmd, revision),
+                            d,
+                            workdir=tmpdir)
+            except bb.fetch2.FetchError:
+                return False
+            finally:
+                bb.utils.remove(tmpdir, recurse=True)
+        return True
diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
index ee41bff4..00def1bd 100644
--- a/lib/bb/tests/fetch.py
+++ b/lib/bb/tests/fetch.py
@@ -1478,6 +1478,40 @@ class FetchCheckStatusTest(FetcherTest):
 
         connection_cache.close_connections()
 
+    @skipIfNoNetwork()
+    def test_git_checkstatus(self):
+        CHECKSTATUS_GOOD_REPO = "git://git.yoctoproject.org/yocto-docs;branch=master"
+        CHECKSTATUS_GOOD_HASH = "2bd14d3810a68082ce01b459cf2799d796a32e63"
+
+        def do_git_checkstatus(url, srcrev):
+            self.d.setVar("SRCREV", srcrev)
+            fetch = bb.fetch2.Fetch([url], self.d)
+            ud = fetch.ud[url]
+            m = ud.method
+            return m.checkstatus(fetch, ud, self.d)
+
+        # Valid URI + SRCREV returns true
+        ret = do_git_checkstatus(CHECKSTATUS_GOOD_REPO, CHECKSTATUS_GOOD_HASH)
+        self.assertTrue(ret,
+                        msg="URI %s, can't check status" %
+                        (CHECKSTATUS_GOOD_REPO))
+
+        # Unavailable server returns false
+        invalid_git_uri = 'git://git.test.invalid/yocto-docs;branch=master'
+        ret = do_git_checkstatus(invalid_git_uri, CHECKSTATUS_GOOD_HASH)
+        self.assertFalse(
+            ret,
+            msg="URI %s, checkstatus() returns true for invalid Git server" %
+            (invalid_git_uri))
+
+        # Invalid/disappeared SRCREV returns false
+        rev_bad = "f" * 40
+        ret = do_git_checkstatus(CHECKSTATUS_GOOD_REPO, rev_bad)
+        self.assertFalse(
+            ret,
+            msg="URI %s, REF %s, checkstatus() returns true for invalid ref" %
+            (CHECKSTATUS_GOOD_REPO, rev_bad))
+
 
 class GitMakeShallowTest(FetcherTest):
     def setUp(self):
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-07-07 20:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-05 17:46 [PATCH] fetch2/git: Check srcrev exists in checkstatus() Maia Xiao
2022-07-05 21:27 ` [bitbake-devel] " Richard Purdie
2022-07-05 22:02   ` Paul Eggleton
2022-07-06 15:12 ` Bruce Ashfield
2022-07-06 15:13   ` Bruce Ashfield
2022-07-06 16:19     ` Richard Purdie
2022-07-06 20:14       ` Maia Xiao
2022-07-07 16:55         ` Bruce Ashfield
2022-07-07 20:50           ` Richard Purdie

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.