Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/revision.py
Show All 25 Lines | from swh.web.browse.utils import ( | ||||
gen_snapshot_link, | gen_snapshot_link, | ||||
get_directory_entries, | get_directory_entries, | ||||
get_readme_to_display, | get_readme_to_display, | ||||
get_revision_log_url, | get_revision_log_url, | ||||
prepare_content_for_display, | prepare_content_for_display, | ||||
request_content, | request_content, | ||||
) | ) | ||||
from swh.web.common import archive | from swh.web.common import archive | ||||
from swh.web.common.exc import NotFoundExc, handle_view_exception | from swh.web.common.exc import NotFoundExc | ||||
from swh.web.common.identifiers import get_swhids_info | from swh.web.common.identifiers import get_swhids_info | ||||
from swh.web.common.typing import RevisionMetadata, SWHObjectInfo | from swh.web.common.typing import RevisionMetadata, SWHObjectInfo | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
format_utc_iso_date, | format_utc_iso_date, | ||||
gen_path_info, | gen_path_info, | ||||
reverse, | reverse, | ||||
swh_object_icons, | swh_object_icons, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | @browse_route( | ||||
r"revision/(?P<sha1_git>[0-9a-f]+)/diff/", | r"revision/(?P<sha1_git>[0-9a-f]+)/diff/", | ||||
view_name="diff-revision", | view_name="diff-revision", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
) | ) | ||||
def _revision_diff(request, sha1_git): | def _revision_diff(request, sha1_git): | ||||
""" | """ | ||||
Browse internal endpoint to compute revision diff | Browse internal endpoint to compute revision diff | ||||
""" | """ | ||||
try: | |||||
revision = archive.lookup_revision(sha1_git) | revision = archive.lookup_revision(sha1_git) | ||||
snapshot_context = None | snapshot_context = None | ||||
origin_url = request.GET.get("origin_url", None) | origin_url = request.GET.get("origin_url", None) | ||||
if not origin_url: | if not origin_url: | ||||
origin_url = request.GET.get("origin", None) | origin_url = request.GET.get("origin", None) | ||||
timestamp = request.GET.get("timestamp", None) | timestamp = request.GET.get("timestamp", None) | ||||
visit_id = request.GET.get("visit_id", None) | visit_id = request.GET.get("visit_id", None) | ||||
if origin_url: | if origin_url: | ||||
snapshot_context = get_snapshot_context( | snapshot_context = get_snapshot_context( | ||||
origin_url=origin_url, timestamp=timestamp, visit_id=visit_id | origin_url=origin_url, timestamp=timestamp, visit_id=visit_id | ||||
) | ) | ||||
except Exception as exc: | |||||
return handle_view_exception(request, exc) | |||||
changes = archive.diff_revision(sha1_git) | changes = archive.diff_revision(sha1_git) | ||||
changes_msg = _gen_revision_changes_list(revision, changes, snapshot_context) | changes_msg = _gen_revision_changes_list(revision, changes, snapshot_context) | ||||
diff_data = { | diff_data = { | ||||
"total_nb_changes": len(changes), | "total_nb_changes": len(changes), | ||||
"changes": changes[:_max_displayed_file_diffs], | "changes": changes[:_max_displayed_file_diffs], | ||||
"changes_msg": changes_msg, | "changes_msg": changes_msg, | ||||
Show All 11 Lines | |||||
) | ) | ||||
def revision_log_browse(request, sha1_git): | def revision_log_browse(request, sha1_git): | ||||
""" | """ | ||||
Django view that produces an HTML display of the history | Django view that produces an HTML display of the history | ||||
log for a revision identified by its id. | log for a revision identified by its id. | ||||
The url that points to it is :http:get:`/browse/revision/(sha1_git)/log/` | The url that points to it is :http:get:`/browse/revision/(sha1_git)/log/` | ||||
""" | """ | ||||
try: | |||||
origin_url = request.GET.get("origin_url") | origin_url = request.GET.get("origin_url") | ||||
snapshot_id = request.GET.get("snapshot") | snapshot_id = request.GET.get("snapshot") | ||||
snapshot_context = None | snapshot_context = None | ||||
if origin_url or snapshot_id: | if origin_url or snapshot_id: | ||||
snapshot_context = get_snapshot_context( | snapshot_context = get_snapshot_context( | ||||
snapshot_id=snapshot_id, | snapshot_id=snapshot_id, | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
timestamp=request.GET.get("timestamp"), | timestamp=request.GET.get("timestamp"), | ||||
visit_id=request.GET.get("visit_id"), | visit_id=request.GET.get("visit_id"), | ||||
branch_name=request.GET.get("branch"), | branch_name=request.GET.get("branch"), | ||||
release_name=request.GET.get("release"), | release_name=request.GET.get("release"), | ||||
revision_id=sha1_git, | revision_id=sha1_git, | ||||
) | ) | ||||
per_page = int(request.GET.get("per_page", NB_LOG_ENTRIES)) | per_page = int(request.GET.get("per_page", NB_LOG_ENTRIES)) | ||||
offset = int(request.GET.get("offset", 0)) | offset = int(request.GET.get("offset", 0)) | ||||
revs_ordering = request.GET.get("revs_ordering", "committer_date") | revs_ordering = request.GET.get("revs_ordering", "committer_date") | ||||
session_key = "rev_%s_log_ordering_%s" % (sha1_git, revs_ordering) | session_key = "rev_%s_log_ordering_%s" % (sha1_git, revs_ordering) | ||||
rev_log_session = request.session.get(session_key, None) | rev_log_session = request.session.get(session_key, None) | ||||
rev_log = [] | rev_log = [] | ||||
revs_walker_state = None | revs_walker_state = None | ||||
if rev_log_session: | if rev_log_session: | ||||
rev_log = rev_log_session["rev_log"] | rev_log = rev_log_session["rev_log"] | ||||
revs_walker_state = rev_log_session["revs_walker_state"] | revs_walker_state = rev_log_session["revs_walker_state"] | ||||
if len(rev_log) < offset + per_page: | if len(rev_log) < offset + per_page: | ||||
revs_walker = archive.get_revisions_walker( | revs_walker = archive.get_revisions_walker( | ||||
revs_ordering, | revs_ordering, | ||||
sha1_git, | sha1_git, | ||||
max_revs=offset + per_page + 1, | max_revs=offset + per_page + 1, | ||||
state=revs_walker_state, | state=revs_walker_state, | ||||
) | ) | ||||
rev_log += [rev["id"] for rev in revs_walker] | rev_log += [rev["id"] for rev in revs_walker] | ||||
revs_walker_state = revs_walker.export_state() | revs_walker_state = revs_walker.export_state() | ||||
revs = rev_log[offset : offset + per_page] | revs = rev_log[offset : offset + per_page] | ||||
revision_log = archive.lookup_revision_multiple(revs) | revision_log = archive.lookup_revision_multiple(revs) | ||||
request.session[session_key] = { | request.session[session_key] = { | ||||
"rev_log": rev_log, | "rev_log": rev_log, | ||||
"revs_walker_state": revs_walker_state, | "revs_walker_state": revs_walker_state, | ||||
} | } | ||||
except Exception as exc: | |||||
return handle_view_exception(request, exc) | |||||
revs_ordering = request.GET.get("revs_ordering", "") | revs_ordering = request.GET.get("revs_ordering", "") | ||||
prev_log_url = None | prev_log_url = None | ||||
if len(rev_log) > offset + per_page: | if len(rev_log) > offset + per_page: | ||||
prev_log_url = reverse( | prev_log_url = reverse( | ||||
"browse-revision-log", | "browse-revision-log", | ||||
url_args={"sha1_git": sha1_git}, | url_args={"sha1_git": sha1_git}, | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | |||||
) | ) | ||||
def revision_browse(request, sha1_git): | def revision_browse(request, sha1_git): | ||||
""" | """ | ||||
Django view that produces an HTML display of a revision | Django view that produces an HTML display of a revision | ||||
identified by its id. | identified by its id. | ||||
The url that points to it is :http:get:`/browse/revision/(sha1_git)/`. | The url that points to it is :http:get:`/browse/revision/(sha1_git)/`. | ||||
""" | """ | ||||
try: | |||||
revision = archive.lookup_revision(sha1_git) | revision = archive.lookup_revision(sha1_git) | ||||
origin_info = None | origin_info = None | ||||
snapshot_context = None | snapshot_context = None | ||||
origin_url = request.GET.get("origin_url") | origin_url = request.GET.get("origin_url") | ||||
if not origin_url: | if not origin_url: | ||||
origin_url = request.GET.get("origin") | origin_url = request.GET.get("origin") | ||||
timestamp = request.GET.get("timestamp") | timestamp = request.GET.get("timestamp") | ||||
visit_id = request.GET.get("visit_id") | visit_id = request.GET.get("visit_id") | ||||
snapshot_id = request.GET.get("snapshot_id") | snapshot_id = request.GET.get("snapshot_id") | ||||
if not snapshot_id: | if not snapshot_id: | ||||
snapshot_id = request.GET.get("snapshot") | snapshot_id = request.GET.get("snapshot") | ||||
path = request.GET.get("path") | path = request.GET.get("path") | ||||
dir_id = None | dir_id = None | ||||
dirs, files = None, None | dirs, files = None, None | ||||
content_data = {} | content_data = {} | ||||
if origin_url: | if origin_url: | ||||
try: | try: | ||||
snapshot_context = get_snapshot_context( | snapshot_context = get_snapshot_context( | ||||
snapshot_id=snapshot_id, | snapshot_id=snapshot_id, | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
timestamp=timestamp, | timestamp=timestamp, | ||||
visit_id=visit_id, | visit_id=visit_id, | ||||
branch_name=request.GET.get("branch"), | branch_name=request.GET.get("branch"), | ||||
release_name=request.GET.get("release"), | release_name=request.GET.get("release"), | ||||
revision_id=sha1_git, | revision_id=sha1_git, | ||||
) | ) | ||||
except NotFoundExc as e: | except NotFoundExc as e: | ||||
raw_rev_url = reverse( | raw_rev_url = reverse("browse-revision", url_args={"sha1_git": sha1_git}) | ||||
"browse-revision", url_args={"sha1_git": sha1_git} | |||||
) | |||||
error_message = ( | error_message = ( | ||||
"The Software Heritage archive has a revision " | "The Software Heritage archive has a revision " | ||||
"with the hash you provided but the origin " | "with the hash you provided but the origin " | ||||
"mentioned in your request appears broken: %s. " | "mentioned in your request appears broken: %s. " | ||||
"Please check the URL and try again.\n\n" | "Please check the URL and try again.\n\n" | ||||
"Nevertheless, you can still browse the revision " | "Nevertheless, you can still browse the revision " | ||||
"without origin information: %s" | "without origin information: %s" | ||||
% (gen_link(origin_url), gen_link(raw_rev_url)) | % (gen_link(origin_url), gen_link(raw_rev_url)) | ||||
) | ) | ||||
if str(e).startswith("Origin"): | if str(e).startswith("Origin"): | ||||
raise NotFoundExc(error_message) | raise NotFoundExc(error_message) | ||||
else: | else: | ||||
raise e | raise e | ||||
origin_info = snapshot_context["origin_info"] | origin_info = snapshot_context["origin_info"] | ||||
snapshot_id = snapshot_context["snapshot_id"] | snapshot_id = snapshot_context["snapshot_id"] | ||||
elif snapshot_id: | elif snapshot_id: | ||||
snapshot_context = get_snapshot_context(snapshot_id) | snapshot_context = get_snapshot_context(snapshot_id) | ||||
if path: | if path: | ||||
file_info = archive.lookup_directory_with_path(revision["directory"], path) | file_info = archive.lookup_directory_with_path(revision["directory"], path) | ||||
if file_info["type"] == "dir": | if file_info["type"] == "dir": | ||||
dir_id = file_info["target"] | dir_id = file_info["target"] | ||||
else: | else: | ||||
query_string = "sha1_git:" + file_info["target"] | query_string = "sha1_git:" + file_info["target"] | ||||
content_data = request_content(query_string, raise_if_unavailable=False) | content_data = request_content(query_string, raise_if_unavailable=False) | ||||
else: | else: | ||||
dir_id = revision["directory"] | dir_id = revision["directory"] | ||||
if dir_id: | if dir_id: | ||||
path = "" if path is None else (path + "/") | path = "" if path is None else (path + "/") | ||||
dirs, files = get_directory_entries(dir_id) | dirs, files = get_directory_entries(dir_id) | ||||
except Exception as exc: | |||||
return handle_view_exception(request, exc) | |||||
revision_metadata = RevisionMetadata( | revision_metadata = RevisionMetadata( | ||||
object_type=REVISION, | object_type=REVISION, | ||||
object_id=sha1_git, | object_id=sha1_git, | ||||
revision=sha1_git, | revision=sha1_git, | ||||
revision_url=gen_revision_link(sha1_git), | revision_url=gen_revision_link(sha1_git), | ||||
author=revision["author"]["fullname"] if revision["author"] else "None", | author=revision["author"]["fullname"] if revision["author"] else "None", | ||||
author_url=gen_person_mail_link(revision["author"]) | author_url=gen_person_mail_link(revision["author"]) | ||||
▲ Show 20 Lines • Show All 216 Lines • Show Last 20 Lines |