diff --git a/swh/web/browse/snapshot_context.py b/swh/web/browse/snapshot_context.py --- a/swh/web/browse/snapshot_context.py +++ b/swh/web/browse/snapshot_context.py @@ -1202,7 +1202,7 @@ snapshot_context["snapshot_id"], branches_from, PER_PAGE + 1, - target_types=["revision", "alias"], + target_types=["content", "directory", "revision", "alias"], branch_name_include_substring=branch_name_include, ) displayed_branches: List[Dict[str, Any]] = [] @@ -1211,23 +1211,33 @@ displayed_branches = [dict(branch) for branch in branches] for branch in displayed_branches: - rev_query_params = {} + query_params = {"snapshot": snapshot_id, "branch": branch["name"]} if origin_info: - rev_query_params["origin_url"] = origin_info["url"] + query_params["origin_url"] = origin_info["url"] - revision_url = reverse( - "browse-revision", - url_args={"sha1_git": branch["target"]}, - query_params=query_params, - ) - - query_params["branch"] = branch["name"] directory_url = reverse( browse_view_name, url_args=url_args, query_params=query_params ) - del query_params["branch"] - branch["revision_url"] = revision_url branch["directory_url"] = directory_url + del query_params["branch"] + + if branch["target_type"] in ("directory", "revision"): + target_url = reverse( + f"browse-{branch['target_type']}", + url_args={"sha1_git": branch["target"]}, + query_params=query_params, + ) + elif branch["target_type"] == "content": + target_url = reverse( + "browse-content", + url_args={"query_string": f"sha1_git:{branch['target']}"}, + query_params=query_params, + ) + branch["target_url"] = target_url + branch["tooltip"] = ( + f"The branch {branch['name']} targets " + f"{branch['target_type']} {branch['target']}" + ) if origin_info: browse_view_name = "browse-origin-branches" diff --git a/swh/web/browse/templates/browse-branches.html b/swh/web/browse/templates/browse-branches.html --- a/swh/web/browse/templates/browse-branches.html +++ b/swh/web/browse/templates/browse-branches.html @@ -19,7 +19,7 @@ Name - Revision + Target Message Date @@ -38,9 +38,10 @@ {{ branch.name }} - - - {{ branch.revision|slice:":7" }} + + + {{ branch.target|slice:":7" }} diff --git a/swh/web/tests/browse/views/test_snapshot.py b/swh/web/tests/browse/views/test_snapshot.py --- a/swh/web/tests/browse/views/test_snapshot.py +++ b/swh/web/tests/browse/views/test_snapshot.py @@ -125,25 +125,60 @@ ) -def test_snapshot_browse_branches(client, archive_data, origin): - snapshot = archive_data.snapshot_get_latest(origin["url"]) +def test_snapshot_browse_branches_targeting_revisions(client, archive_data, origin): + _origin_branches_test_helper(client, archive_data, origin["url"]) - snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) - snapshot_content = process_snapshot_branches(snapshot) - _origin_branches_test_helper( - client, origin, snapshot_content, snapshot_sizes, snapshot_id=snapshot["id"] +def test_snapshot_browse_branches_targeting_multiple_types( + client, archive_data, content_text, directory, revision +): + snapshot = Snapshot( + branches={ + b"content": SnapshotBranch( + target=hash_to_bytes(content_text["sha1_git"]), + target_type=TargetType.CONTENT, + ), + b"directory": SnapshotBranch( + target=hash_to_bytes(directory), + target_type=TargetType.DIRECTORY, + ), + b"revision": SnapshotBranch( + target=hash_to_bytes(revision), + target_type=TargetType.REVISION, + ), + }, + ) + archive_data.snapshot_add([snapshot]) + + origin_url = "https://git.example.org/user/project" + archive_data.origin_add([Origin(url=origin_url)]) + date = now() + visit = OriginVisit(origin=origin_url, date=date, type="git") + visit = archive_data.origin_visit_add([visit])[0] + visit_status = OriginVisitStatus( + origin=origin_url, + visit=visit.visit, + date=date, + status="full", + snapshot=snapshot.id, ) + archive_data.origin_visit_status_add([visit_status]) + _origin_branches_test_helper(client, archive_data, origin_url) -def _origin_branches_test_helper( - client, origin_info, origin_snapshot, snapshot_sizes, snapshot_id -): - query_params = {"origin_url": origin_info["url"], "snapshot": snapshot_id} + +def _origin_branches_test_helper(client, archive_data, origin_url): + + snapshot = archive_data.snapshot_get_latest(origin_url) + + snapshot_sizes = archive_data.snapshot_count_branches(snapshot["id"]) + snapshot_content = process_snapshot_branches(snapshot) + + query_params = {"origin_url": origin_url, "snapshot": snapshot["id"]} url = reverse( "browse-snapshot-branches", - url_args={"snapshot_id": snapshot_id}, + url_args={"snapshot_id": snapshot["id"]}, query_params=query_params, ) @@ -151,12 +186,12 @@ client, url, status_code=200, template_used="browse-branches.html" ) - origin_branches = origin_snapshot[0] - origin_releases = origin_snapshot[1] + origin_branches = snapshot_content[0] + origin_releases = snapshot_content[1] origin_branches_url = reverse("browse-origin-branches", query_params=query_params) assert_contains(resp, f'href="{escape(origin_branches_url)}"') - assert_contains(resp, f"Branches ({snapshot_sizes['revision']})") + assert_contains(resp, f"Branches ({snapshot_sizes['branch']})") origin_releases_url = reverse("browse-origin-releases", query_params=query_params) @@ -174,14 +209,29 @@ ) assert_contains(resp, '' % escape(browse_branch_url)) - browse_revision_url = reverse( - "browse-revision", - url_args={"sha1_git": branch["target"]}, - query_params=query_params, - ) - assert_contains(resp, '' % escape(browse_revision_url)) + if branch["target_type"] == "revision": + browse_revision_url = reverse( + "browse-revision", + url_args={"sha1_git": branch["target"]}, + query_params=query_params, + ) + assert_contains(resp, '' % escape(browse_revision_url)) + elif branch["target_type"] == "directory": + browse_directory_url = reverse( + "browse-directory", + url_args={"sha1_git": branch["target"]}, + query_params=query_params, + ) + assert_contains(resp, '' % escape(browse_directory_url)) + elif branch["target_type"] == "content": + browse_content_url = reverse( + "browse-content", + url_args={"query_string": f"sha1_git:{branch['target']}"}, + query_params=query_params, + ) + assert_contains(resp, '' % escape(browse_content_url)) - _check_origin_link(resp, origin_info["url"]) + _check_origin_link(resp, origin_url) def _check_origin_link(resp, origin_url):