Skip to content

Commit 0020818

Browse files
committed
Add support for kernel_shorthand pattern
Add a test Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent 7e24835 commit 0020818

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

src/packageurl/contrib/url2purl.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,15 @@ def build_cgit_purl(url):
756756
https://git.kernel.org/pub/scm/linux/kernel/git/deller/linux-fbdev.git/commit/?h=for-next&id=bd771cf5c4254511cc4abb88f3dab3bd58bdf8e8
757757
https://cgit.git.savannah.gnu.org/cgit/uddf.git/commit/?id=98c41e131dc952aee43d4ec392b80ca4c426be8d
758758
https://gitweb.gentoo.org/dev/darkside.git/commit/?id=8d4b0836f3b6ab7075212926d9aad0b50246d825
759+
https://git.kernel.org/stable/c/9a9a8fe26751334b7739193a94eba741073b8a55
759760
"""
760761

762+
kernel_shorthand = (
763+
r"^https?://(?P<domain>git\.kernel\.org)/"
764+
r"(?P<name>[^/]+)/c/"
765+
r"(?P<version>[0-9a-fA-F]{7,64})/?$"
766+
)
767+
761768
cgit_project_pattern = (
762769
r"^https?://"
763770
r"(?P<domain>[^/]+)/"
@@ -772,24 +779,30 @@ def build_cgit_purl(url):
772779
r"(?:&.*)?$"
773780
)
774781

775-
commit_match = re.search(cgit_project_pattern, url)
776-
if commit_match:
777-
domain = commit_match.group("domain")
778-
namespace = f"{domain}/{commit_match.group('namespace')}"
779-
return PackageURL(
780-
type="generic",
781-
namespace=namespace,
782-
name=commit_match.group("name"),
783-
version=commit_match.group("version"),
784-
qualifiers={},
785-
subpath="",
786-
)
782+
if match := re.search(kernel_shorthand, url):
783+
res = match.groupdict()
784+
namespace = res["domain"]
785+
elif match := re.search(cgit_project_pattern, url):
786+
res = match.groupdict()
787+
namespace = f"{res['domain']}/{res['namespace']}"
788+
else:
789+
return None
790+
791+
return PackageURL(
792+
type="generic",
793+
namespace=namespace,
794+
name=res["name"],
795+
version=res["version"],
796+
qualifiers={},
797+
subpath="",
798+
)
787799

788800

789801
GITILES_DOMAINS = [
790802
r"android\.googlesource\.com",
791803
r"aomedia\.googlesource\.com",
792804
r"chromium\.googlesource\.com",
805+
r"gerrit\.googlesource\.com",
793806
]
794807
GITILES_ROUTE_REGEX = build_route_regex(GITILES_DOMAINS)
795808

tests/contrib/data/url2purl.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
"https://cgit.git.savannah.gnu.org/cgit/uddf.git/commit/?id=98c41e131dc952aee43d4ec392b80ca4c426be8d": "pkg:generic/cgit.git.savannah.gnu.org/cgit/uddf@98c41e131dc952aee43d4ec392b80ca4c426be8d",
290290
"https://git.kernel.org/pub/scm/virt/kvm/mst/qemu.git/commit/?id=7457fe9541b5162f285454947448d553a5d5a531": "pkg:generic/git.kernel.org/pub/scm/virt/kvm/mst/qemu@7457fe9541b5162f285454947448d553a5d5a531",
291291
"https://git.kernel.org/pub/scm/linux/kernel/git/deller/linux-fbdev.git/commit/?h=for-next&id=bd771cf5c4254511cc4abb88f3dab3bd58bdf8e8": "pkg:generic/git.kernel.org/pub/scm/linux/kernel/git/deller/linux-fbdev@bd771cf5c4254511cc4abb88f3dab3bd58bdf8e8",
292+
"https://git.kernel.org/stable/c/9a9a8fe26751334b7739193a94eba741073b8a55": "pkg:generic/git.kernel.org/stable@9a9a8fe26751334b7739193a94eba741073b8a55",
292293

293294
"https://gitweb.gentoo.org/dev/darkside.git/commit/?id=8d4b0836f3b6ab7075212926d9aad0b50246d825": "pkg:generic/gitweb.gentoo.org/dev/darkside@8d4b0836f3b6ab7075212926d9aad0b50246d825",
294295
"https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=f73ae47c5e48010f504f3f55567152258f3013ae": "pkg:generic/gitweb.gentoo.org/repo/gentoo@f73ae47c5e48010f504f3f55567152258f3013ae",

0 commit comments

Comments
 (0)