From ded9d435b081ab552d8c5d4e1f655e7b26a8be0a Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 19 Feb 2025 08:45:09 +0800 Subject: [PATCH] repo/contrib: Display merge request diffs --- http_handle_repo_commit.go | 59 +++++++++++++++++++++++++++++------------------------ http_handle_repo_contrib_num.go | 9 --------- http_handle_repo_contrib_one.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ http_server.go | 5 ++++- templates/repo_contrib_num.tmpl | 18 ------------------ templates/repo_contrib_one.tmpl | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index 11f16e402798170c03bdfd9c22944eba147ef464..25802edc4d1370a9a04e55fc0ddd5919f8ee367d 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -62,9 +62,39 @@ } params["parent_commit_hash"] = parent_commit_hash.String() params["patch"] = patch + params["file_patches"] = make_usable_file_patches(patch) + + render_template(w, "repo_commit", params) +} + +type fake_diff_file struct { + hash plumbing.Hash + mode filemode.FileMode + path string +} + +func (f fake_diff_file) Hash() plumbing.Hash { + return f.hash +} + +func (f fake_diff_file) Mode() filemode.FileMode { + return f.mode +} + +func (f fake_diff_file) Path() string { + return f.path +} + +var fake_diff_file_null = fake_diff_file{ + hash: plumbing.NewHash("0000000000000000000000000000000000000000"), + mode: misc.First_or_panic(filemode.New("100644")), + path: "", +} + +func make_usable_file_patches(patch diff.Patch) (usable_file_patches []usable_file_patch) { // TODO: Remove unnecessary context // TODO: Prepend "+"/"-"/" " instead of solely distinguishing based on color - usable_file_patches := make([]usable_file_patch, 0) + usable_file_patches = make([]usable_file_patch, 0) for _, file_patch := range patch.FilePatches() { from, to := file_patch.Files() if from == nil { @@ -91,31 +121,6 @@ To: to, } usable_file_patches = append(usable_file_patches, usable_file_patch) } - params["file_patches"] = usable_file_patches - - render_template(w, "repo_commit", params) + return } -type fake_diff_file struct { - hash plumbing.Hash - mode filemode.FileMode - path string -} - -func (f fake_diff_file) Hash() plumbing.Hash { - return f.hash -} - -func (f fake_diff_file) Mode() filemode.FileMode { - return f.mode -} - -func (f fake_diff_file) Path() string { - return f.path -} - -var fake_diff_file_null = fake_diff_file{ - hash: plumbing.NewHash("0000000000000000000000000000000000000000"), - mode: misc.First_or_panic(filemode.New("100644")), - path: "", -} diff --git a/http_handle_repo_contrib_num.go b/http_handle_repo_contrib_num.go deleted file mode 100644 index c4ed5d15f098bfb3fe6c07bff7d83a984d77bede..0000000000000000000000000000000000000000 --- a/http_handle_repo_contrib_num.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import ( - "net/http" -) - -func handle_repo_contrib_num(w http.ResponseWriter, r *http.Request, params map[string]any) { - render_template(w, "repo_contrib_num", params) -} diff --git a/http_handle_repo_contrib_one.go b/http_handle_repo_contrib_one.go new file mode 100644 index 0000000000000000000000000000000000000000..a09b6e805a0c72768953f21396f062370040fa3b --- /dev/null +++ b/http_handle_repo_contrib_one.go @@ -0,0 +1,56 @@ +package main + +import ( + "net/http" + "strconv" + + "github.com/go-git/go-git/v5" +) + +func handle_repo_contrib_one(w http.ResponseWriter, r *http.Request, params map[string]any) { + mr_id_string := params["mr_id"].(string) + mr_id, err := strconv.ParseInt(mr_id_string, 10, strconv.IntSize) + if err != nil { + http.Error(w, "Merge request ID not an integer: "+err.Error(), http.StatusBadRequest) + return + } + + var title, status, source_ref, destination_branch string + err = database.QueryRow(r.Context(), "SELECT title, status, source_ref, destination_branch FROM merge_requests WHERE id = $1", mr_id).Scan(&title, &status, &source_ref, &destination_branch) + if err != nil { + http.Error(w, "Error querying merge request: "+err.Error(), http.StatusInternalServerError) + return + } + params["mr_title"], params["mr_status"], params["mr_source_ref"], params["mr_destination_branch"] = title, status, source_ref, destination_branch + + repo := params["repo"].(*git.Repository) + + source_ref_hash, err := get_ref_hash_from_type_and_name(repo, "branch", source_ref) + if err != nil { + http.Error(w, "Error getting source ref hash: "+err.Error(), http.StatusInternalServerError) + return + } + source_commit, err := repo.CommitObject(source_ref_hash) + if err != nil { + http.Error(w, "Error getting source commit: "+err.Error(), http.StatusInternalServerError) + return + } + params["source_commit"] = source_commit + + destination_branch_hash, err := get_ref_hash_from_type_and_name(repo, "branch", destination_branch) + if err != nil { + http.Error(w, "Error getting destination branch hash: "+err.Error(), http.StatusInternalServerError) + return + } + destination_commit, err := repo.CommitObject(destination_branch_hash) + if err != nil { + http.Error(w, "Error getting destination commit: "+err.Error(), http.StatusInternalServerError) + return + } + params["source_commit"] = source_commit + + patch, err := destination_commit.Patch(source_commit) + params["file_patches"] = make_usable_file_patches(patch) + + render_template(w, "repo_contrib_one", params) +} diff --git a/http_server.go b/http_server.go index b44add335aed5ef73f74e452df93a73f63111b9c..077c63e5f76d39b051e7e1d7f2878dfa8f63ca76 100644 --- a/http_server.go +++ b/http_server.go @@ -84,6 +84,8 @@ break } } + params["separator_index"] = separator_index + // TODO if separator_index > 1 { http.Error(w, "Subgroups haven't been implemented yet", http.StatusNotImplemented) @@ -198,7 +200,8 @@ switch non_empty_last_segments_len { case separator_index+4: handle_repo_contrib_index(w, r, params) case separator_index+5: - handle_repo_contrib_num(w, r, params) + params["mr_id"] = segments[separator_index+4] + handle_repo_contrib_one(w, r, params) default: http.Error(w, "Too many parameters", http.StatusBadRequest) } diff --git a/templates/repo_contrib_num.tmpl b/templates/repo_contrib_num.tmpl deleted file mode 100644 index dc34c536279078e2e796e2cfc999529b696f82ad..0000000000000000000000000000000000000000 --- a/templates/repo_contrib_num.tmpl +++ /dev/null @@ -1,18 +0,0 @@ -{{- define "repo_contrib_num" -}} - - - - {{ template "head_common" . }} - Merge requests – {{ .repo_name }} – {{ .group_name }} – {{ .global.forge_title }} - - - {{ template "header" . }} -
- Test -
- - - -{{- end -}} diff --git a/templates/repo_contrib_one.tmpl b/templates/repo_contrib_one.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..85cac10efee2ff0efd1d93bbaec2f24973cb97e6 --- /dev/null +++ b/templates/repo_contrib_one.tmpl @@ -0,0 +1,83 @@ +{{- define "repo_contrib_one" -}} + + + + {{ template "head_common" . }} + Merge requests – {{ .repo_name }} – {{ .group_name }} – {{ .global.forge_title }} + + + {{ template "header" . }} +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Merge request info
ID{{ .mr_id }}
Status{{ .mr_status }}
Title{{ .mr_title }}
Source ref{{ .mr_source_ref }}
Destination branch{{ .mr_destination_branch }}
+
+
+ {{ $destination_commit := .destination_commit }} + {{ $source_commit := .source_commit }} + {{ range .file_patches }} +
+ + +
+ {{ range .Chunks }} + {{ if eq .Operation 0 }} +
{{ .Content }}
+ {{ else if eq .Operation 1 }} +
{{ .Content }}
+ {{ else if eq .Operation 2 }} +
{{ .Content }}
+ {{ else }} +
{{ .Content }}
+ {{ end }} + {{ end }} +
+
+ {{ end }} +
+ + + +{{- end -}} -- 2.48.1