diff --git a/swh/web/browse/views/content.py b/swh/web/browse/views/content.py --- a/swh/web/browse/views/content.py +++ b/swh/web/browse/views/content.py @@ -330,7 +330,7 @@ language=content_data["language"], licenses=content_data["licenses"], root_directory=root_dir, - path=f"/{path}" if path else "", + path=f"/{path}" if path else None, filename=filename or "", directory=directory_id, directory_url=directory_url, diff --git a/swh/web/browse/views/directory.py b/swh/web/browse/views/directory.py --- a/swh/web/browse/views/directory.py +++ b/swh/web/browse/views/directory.py @@ -140,7 +140,7 @@ nb_dirs=len(dirs), sum_file_sizes=sum_file_sizes, root_directory=root_sha1_git, - path=f"/{path}" if path else "/", + path=f"/{path}" if path else None, revision=None, revision_found=None, release=None, diff --git a/swh/web/browse/views/revision.py b/swh/web/browse/views/revision.py --- a/swh/web/browse/views/revision.py +++ b/swh/web/browse/views/revision.py @@ -465,7 +465,7 @@ error_description = "" extra_context = dict(revision_metadata) - extra_context["path"] = f"/{path}" if path else "/" + extra_context["path"] = f"/{path}" if path else None if content_data: breadcrumbs[-1]["url"] = None diff --git a/swh/web/common/identifiers.py b/swh/web/common/identifiers.py --- a/swh/web/common/identifiers.py +++ b/swh/web/common/identifiers.py @@ -64,7 +64,10 @@ """ try: obj_swhid = swhid( - object_type, object_id, scheme_version, cast(Dict[str, Any], metadata) + object_type, + object_id, + scheme_version, + cast(Dict[str, Any], {k: v for k, v in metadata.items() if v is not None}), ) except ValidationError as e: raise BadInputExc("Invalid object (%s) for SWHID. %s" % (object_id, e)) @@ -353,6 +356,8 @@ path = extra_context["path"] or "/" if "filename" in extra_context and object_type == CONTENT: path += extra_context["filename"] + if object_type == DIRECTORY and path == "/": + path = None if path: swhid_context["path"] = quote(path, safe="/?:@&") diff --git a/swh/web/tests/browse/views/test_content.py b/swh/web/tests/browse/views/test_content.py --- a/swh/web/tests/browse/views/test_content.py +++ b/swh/web/tests/browse/views/test_content.py @@ -458,7 +458,6 @@ "origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot), "anchor": gen_swhid(REVISION, branch_info["revision"]), - "path": "/", }, ) assert_contains(resp, dir_swhid) @@ -524,7 +523,6 @@ "origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot), "anchor": gen_swhid(RELEASE, release_info["id"]), - "path": "/", }, ) assert_contains(resp, dir_swhid) diff --git a/swh/web/tests/browse/views/test_directory.py b/swh/web/tests/browse/views/test_directory.py --- a/swh/web/tests/browse/views/test_directory.py +++ b/swh/web/tests/browse/views/test_directory.py @@ -305,7 +305,7 @@ if root_directory_sha1 != directory_entries[0]["dir_id"]: swhid_context["anchor"] = gen_swhid(DIRECTORY, root_directory_sha1) - swhid_context["path"] = f"/{path}/" if path else "/" + swhid_context["path"] = f"/{path}/" if path else None if root_directory_sha1 != directory_entries[0]["dir_id"]: swhid_context["anchor"] = gen_swhid(DIRECTORY, root_directory_sha1) diff --git a/swh/web/tests/browse/views/test_origin.py b/swh/web/tests/browse/views/test_origin.py --- a/swh/web/tests/browse/views/test_origin.py +++ b/swh/web/tests/browse/views/test_origin.py @@ -721,7 +721,6 @@ "origin": origin["url"], "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(RELEASE, release_data["id"]), - "path": "/", } swh_dir_id = gen_swhid( @@ -1083,7 +1082,7 @@ "origin": origin_info["url"], "visit": gen_swhid(SNAPSHOT, snapshot["id"]), "anchor": gen_swhid(REVISION, head_rev_id), - "path": f"/{path}" if path else "/", + "path": f"/{path}" if path else None, } swh_dir_id = gen_swhid( diff --git a/swh/web/tests/browse/views/test_revision.py b/swh/web/tests/browse/views/test_revision.py --- a/swh/web/tests/browse/views/test_revision.py +++ b/swh/web/tests/browse/views/test_revision.py @@ -287,7 +287,6 @@ assert_contains(resp, swh_rev_id_url) swhid_context["anchor"] = gen_swhid(REVISION, revision) - swhid_context["path"] = "/" swh_dir_id = gen_swhid(DIRECTORY, dir_id, metadata=swhid_context) swh_dir_id_url = reverse("browse-swhid", url_args={"swhid": swh_dir_id}) diff --git a/swh/web/tests/common/test_identifiers.py b/swh/web/tests/common/test_identifiers.py --- a/swh/web/tests/common/test_identifiers.py +++ b/swh/web/tests/common/test_identifiers.py @@ -52,6 +52,15 @@ assert gen_swhid(swh_object_type, sha1_git) == expected_swhid + assert ( + gen_swhid(swh_object_type, sha1_git, metadata={"foo": "bar"}) + == expected_swhid + ";foo=bar" + ) + + assert ( + gen_swhid(swh_object_type, sha1_git, metadata={"foo": None}) == expected_swhid + ) + with pytest.raises(BadInputExc) as e: gen_swhid("foo", sha1_git) assert e.match("Invalid object") @@ -137,15 +146,19 @@ @given(directory_with_subdirs()) def test_get_swhids_info_directory_context(archive_data, directory): - extra_context = {"path": "/"} swhid = get_swhids_info( [SWHObjectInfo(object_type=DIRECTORY, object_id=directory)], snapshot_context=None, - extra_context=extra_context, )[0] - swhid_dir_parsed = get_swhid(swhid["swhid_with_context"]) + assert swhid["swhid_with_context"] is None - assert swhid_dir_parsed.metadata == extra_context + # path qualifier should be discarded for a root directory + swhid = get_swhids_info( + [SWHObjectInfo(object_type=DIRECTORY, object_id=directory)], + snapshot_context=None, + extra_context={"path": "/"}, + )[0] + assert swhid["swhid_with_context"] is None dir_content = archive_data.directory_ls(directory) dir_subdirs = [e for e in dir_content if e["type"] == "dir"] @@ -227,7 +240,6 @@ assert swhid_dir_parsed.metadata == { "anchor": anchor, - "path": "/", } if dir_entry["type"] == "file": @@ -379,7 +391,6 @@ expected_dir_context = { "visit": snapshot_swhid, "anchor": anchor, - "path": "/", } expected_rev_context = {"visit": snapshot_swhid}