From 7c9e3a0f39c0dfe68535b993ede5e93baa3cec46 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 11 Feb 2025 22:20:30 +0800 Subject: repo_commit: Redirect faulty URLs --- handle_repo_commit.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/handle_repo_commit.go b/handle_repo_commit.go index 9e10a9f..22d58ec 100644 --- a/handle_repo_commit.go +++ b/handle_repo_commit.go @@ -17,22 +17,21 @@ type usable_file_patch struct { func handle_repo_commit(w http.ResponseWriter, r *http.Request) { data := make(map[string]any) // TODO: Sanitize path values - group_name, repo_name, commit_id_string := r.PathValue("group_name"), r.PathValue("repo_name"), r.PathValue("commit_id") + group_name, repo_name, commit_id_specified_string := r.PathValue("group_name"), r.PathValue("repo_name"), r.PathValue("commit_id") data["group_name"], data["repo_name"] = group_name, repo_name repo, err := open_git_repo(group_name, repo_name) if err != nil { _, _ = w.Write([]byte("Error opening repo: " + err.Error())) return } - commit_id_string_real := strings.TrimSuffix(commit_id_string, ".patch") - commit_id := plumbing.NewHash(commit_id_string_real) + commit_id_specified_string_without_suffix := strings.TrimSuffix(commit_id_specified_string, ".patch") + commit_id := plumbing.NewHash(commit_id_specified_string_without_suffix) commit_object, err := repo.CommitObject(commit_id) if err != nil { _, _ = w.Write([]byte("Error getting commit object: " + err.Error())) return } - - if commit_id_string_real != commit_id_string { + if commit_id_specified_string_without_suffix != commit_id_specified_string { patch, err := format_patch_from_commit(commit_object) if err != nil { _, _ = w.Write([]byte("Error formatting patch: " + err.Error())) @@ -41,9 +40,15 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(patch)) return } + commit_id_string := commit_object.Hash.String() + + if commit_id_string != commit_id_specified_string { + http.Redirect(w, r, commit_id_string, http.StatusSeeOther) + return + } data["commit_object"] = commit_object - data["commit_id"] = commit_object.Hash.String() + data["commit_id"] = commit_id_string parent_commit_object, err := commit_object.Parent(0) if err != nil { -- cgit v1.2.3