Lindenii Project Forge
Commit info | |
---|---|
ID | d0e00c2879ddad6f7238b52f9720475d4068d759 |
Author | Runxi Yu<me@runxiyu.org> |
Author date | Wed, 05 Mar 2025 10:34:24 +0800 |
Committer | Runxi Yu<me@runxiyu.org> |
Committer date | Wed, 05 Mar 2025 10:34:24 +0800 |
Actions | Get patch |
repo/contrib/one: Diff against merge base
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileContributor: Runxi Yu <https://runxiyu.org> package main import ( "net/http" "strconv" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) func handle_repo_contrib_one(w http.ResponseWriter, r *http.Request, params map[string]any) { var mr_id_string string var mr_id int var err error var title, status, source_ref, destination_branch string var repo *git.Repository var source_ref_hash plumbing.Hash var source_commit *object.Commit mr_id_string = params["mr_id"].(string) mr_id_int64, 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 } mr_id = int(mr_id_int64) if err = database.QueryRow(r.Context(), "SELECT COALESCE(title, ''), status, source_ref, COALESCE(destination_branch, '') FROM merge_requests WHERE id = $1", mr_id, ).Scan(&title, &status, &source_ref, &destination_branch); err != nil { http.Error(w, "Error querying merge request: "+err.Error(), http.StatusInternalServerError) return } repo = params["repo"].(*git.Repository) if source_ref_hash, err = get_ref_hash_from_type_and_name(repo, "branch", source_ref); err != nil { http.Error(w, "Error getting source ref hash: "+err.Error(), http.StatusInternalServerError) return } if source_commit, err = repo.CommitObject(source_ref_hash); err != nil { http.Error(w, "Error getting source commit: "+err.Error(), http.StatusInternalServerError) return } params["source_commit"] = source_commit var destination_branch_hash plumbing.Hash if destination_branch == "" { destination_branch = "HEAD" destination_branch_hash, err = get_ref_hash_from_type_and_name(repo, "", "") } else { 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["destination_commit"] = destination_commit
patch, err := destination_commit.Patch(source_commit)
merge_bases, err := source_commit.MergeBase(destination_commit) if err != nil { http.Error(w, "Error getting merge base: "+err.Error(), http.StatusInternalServerError) return } merge_base := merge_bases[0] params["merge_base"] = merge_base patch, err := merge_base.Patch(source_commit)
if err != nil { http.Error(w, "Error getting patch: "+err.Error(), http.StatusInternalServerError) return } params["file_patches"] = make_usable_file_patches(patch) params["mr_title"], params["mr_status"], params["mr_source_ref"], params["mr_destination_branch"] = title, status, source_ref, destination_branch render_template(w, "repo_contrib_one", params) }
{{/* SPDX-License-Identifier: AGPL-3.0-only SPDX-FileContributor: Runxi Yu <https://runxiyu.org> */}} {{- define "repo_contrib_one" -}} <!DOCTYPE html> <html lang="en"> <head> {{ template "head_common" . }} <title>Merge requests – {{ .repo_name }} – {{ .group_name }} – {{ .global.forge_title }}</title> </head> <body class="repo-contrib-one"> {{ template "header" . }} <div class="padding-wrapper"> <table id="mr-info-table"> <thead> <tr class="title-row"> <th colspan="2">Merge request info</th> </tr> </thead> <tbody> <tr> <th scope="row">ID</th> <td>{{ .mr_id }}</td> </tr> <tr> <th scope="row">Status</th> <td>{{ .mr_status }}</td> </tr> <tr> <th scope="row">Title</th> <td>{{ .mr_title }}</td> </tr> <tr> <th scope="row">Source ref</th> <td>{{ .mr_source_ref }}</td> </tr> <tr> <th scope="row">Destination branch</th> <td>{{ .mr_destination_branch }}</td> </tr>
<tr> <th scope="row">Merge base</th> <td>{{ .merge_base.ID.String }}</td> </tr>
</tbody> </table> </div> <div class="padding-wrapper">
{{ $destination_commit := .destination_commit }}
{{ $merge_base := .merge_base }}
{{ $source_commit := .source_commit }} {{ range .file_patches }} <div class="file-patch toggle-on-wrapper"> <input type="checkbox" id="toggle-{{ .From.Hash }}{{ .To.Hash }}" class="file-toggle toggle-on-toggle"> <label for="toggle-{{ .From.Hash }}{{ .To.Hash }}" class="file-header toggle-on-header"> <div> {{ if eq .From.Path "" }} --- /dev/null {{ else }}
--- a/<a href="../../tree/{{ .From.Path }}?commit={{ $destination_commit.Hash }}">{{ .From.Path }}</a> {{ .From.Mode }}
--- a/<a href="../../tree/{{ .From.Path }}?commit={{ $merge_base.Hash }}">{{ .From.Path }}</a> {{ .From.Mode }}
{{ end }} <br /> {{ if eq .To.Path "" }} +++ /dev/null {{ else }} +++ b/<a href="../../tree/{{ .To.Path }}?commit={{ $source_commit.Hash }}">{{ .To.Path }}</a> {{ .To.Mode }} {{ end }} </div> </label> <div class="file-content toggle-on-content scroll"> {{ range .Chunks }} {{ if eq .Operation 0 }} <pre class="chunk chunk-unchanged">{{ .Content }}</pre> {{ else if eq .Operation 1 }} <pre class="chunk chunk-addition">{{ .Content }}</pre> {{ else if eq .Operation 2 }} <pre class="chunk chunk-deletion">{{ .Content }}</pre> {{ else }} <pre class="chunk chunk-unknown">{{ .Content }}</pre> {{ end }} {{ end }} </div> </div> {{ end }} </div> <footer> {{ template "footer" . }} </footer> </body> </html> {{- end -}}