From 1bc898b5e1abd98d2932dce053dc82b3f3d6b3f8 Mon Sep 17 00:00:00 2001
From: Thilo Cestonaro <thilo.cestonaro@thalesgroup.com>
Date: Tue, 7 Feb 2023 13:44:02 +0100
Subject: [PATCH] bitbake: git fetcher: use urllib quote ...

to use the path url-compatible. This needs to happen before the shell quotation happens.

Without this commit, spaces in the clone URL will be used as " " and not as "%20" which will fail.
This commit changes the " " in the URL to "%20" when it is a http or https url.

Signed-off-by: Thilo Cestonaro <thilo.cestonaro@thalesgroup.com>
---
 lib/bb/fetch2/git.py  |  8 +++++++-
 lib/bb/tests/fetch.py | 13 +++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/lib/bb/fetch2/git.py b/lib/bb/fetch2/git.py
index 2a3c06fe..28729241 100644
--- a/lib/bb/fetch2/git.py
+++ b/lib/bb/fetch2/git.py
@@ -67,6 +67,7 @@ import re
 import shlex
 import subprocess
 import tempfile
+import urllib
 import bb
 import bb.progress
 from contextlib import contextmanager
@@ -698,7 +699,12 @@ class Git(FetchMethod):
             username = ud.user + '@'
         else:
             username = ""
-        return "%s://%s%s%s" % (ud.proto, username, ud.host, ud.path)
+
+        path = ud.path
+        if ud.proto in [ 'http', 'https' ]:
+            path = urllib.parse.quote(ud.path)
+
+        return "%s://%s%s%s" % (ud.proto, username, ud.host, path)
 
     def _revision_key(self, ud, d, name):
         """
diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
index 6ef0836f..db798f0d 100644
--- a/lib/bb/tests/fetch.py
+++ b/lib/bb/tests/fetch.py
@@ -1697,6 +1697,19 @@ class GitShallowTest(FetcherTest):
         ud = fetcher.ud[uri]
         return fetcher, ud
 
+    def assert_uri_with_spaces(self):
+        class FetchDataFake():
+            proto = "https"
+            user = ""
+            host = "example.org"
+            path = "/git/url with spaces/imaginary.git"
+
+        m = bb.fetch2.git.Git(self.d)
+        ud = FetchDataFake()
+        urlgenerated = m._get_repo_url(ud)
+
+        self.assertEqual("https://example.org/git/url%20with%20spaces/imaginary.git", urlgenerated)
+
     def fetch_and_unpack(self, uri=None):
         fetcher, ud = self.fetch(uri)
         fetcher.unpack(self.d.getVar('WORKDIR'))
-- 
2.39.2

