From d0e00c2879ddad6f7238b52f9720475d4068d759 Mon Sep 17 00:00:00 2001
From: Runxi Yu <me@runxiyu.org>
Date: Wed, 05 Mar 2025 10:34:24 +0800
Subject: [PATCH] repo/contrib/one: Diff against merge base

---
 http_handle_repo_contrib_one.go | 10 +++++++++-
 templates/repo_contrib_one.tmpl |  8 ++++++--

diff --git a/http_handle_repo_contrib_one.go b/http_handle_repo_contrib_one.go
index 9810d5dbbeda7ef138677df07a6736a17b401871..7ed0d6fd446c1d7cb6450e81dd931cdc61b0b483 100644
--- a/http_handle_repo_contrib_one.go
+++ b/http_handle_repo_contrib_one.go
@@ -68,7 +68,15 @@ 		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
diff --git a/templates/repo_contrib_one.tmpl b/templates/repo_contrib_one.tmpl
index dca8a8f2c0368d92fa22e70d74c40535888ba74f..1083e8c0a7c7fbb430754fdeb0d25b8d6800352d 100644
--- a/templates/repo_contrib_one.tmpl
+++ b/templates/repo_contrib_one.tmpl
@@ -39,11 +39,15 @@ 					<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">
@@ -53,7 +57,7 @@ 						<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 "" }}

-- 
2.48.1