Project: pygitweb
import { escapeHtml } from "./dom.js";
import { renderUnifiedDiff } from "./d2h-render.js";
const root = document.getElementById("mr-diff-root");
const listEl = document.getElementById("mr-diff-output");
const emptyEl = document.getElementById("mr-diff-empty");
const statusEl = document.getElementById("mr-diff-status");
const diffTab = document.getElementById("mr-tab-diff-link");
/** @type {"idle" | "loading" | "ready" | "error"} */
let summaryState = "idle";
function setStatus(text, isError) {
if (!statusEl) return;
if (!text) {
statusEl.classList.add("d-none");
statusEl.textContent = "";
statusEl.classList.remove("text-danger");
return;
}
statusEl.classList.remove("d-none");
statusEl.textContent = text;
statusEl.classList.toggle("text-danger", !!isError);
}
function mrQueryParams() {
if (!root) return null;
const project = root.dataset.project || "";
const mr = root.dataset.mrRef || "";
const h = root.dataset.tagOid || "";
if (!project || (!mr && !h)) return null;
const params = new URLSearchParams({ project });
if (mr) params.set("mr", mr);
else params.set("h", h);
return params;
}
function summaryUrl() {
const params = mrQueryParams();
return params ? "/mr/diff/summary?" + params.toString() : "";
}
function patchUrl(path, oldPath) {
const params = mrQueryParams();
if (!params) return "";
params.set("path", path);
if (oldPath) params.set("old_path", oldPath);
return "/mr/diff/patch?" + params.toString();
}
function pathLabel(file) {
const oldPath = file.old_path || "";
const newPath = file.new_path || file.path || "";
if (file.status === "renamed" && oldPath && newPath && oldPath !== newPath) {
return oldPath + " → " + newPath;
}
return newPath || oldPath || file.path || "";
}
function statsLabel(file) {
if (file.binary) return "binary";
const add = Number(file.additions) || 0;
const del = Number(file.deletions) || 0;
return "+" + add + " −" + del;
}
function renderFileRow(file) {
const details = document.createElement("details");
details.className = "mr-diff-file";
details.dataset.path = file.path || "";
if (file.old_path) details.dataset.oldPath = file.old_path;
details.dataset.status = file.status || "";
details.dataset.binary = file.binary ? "true" : "false";
const summary = document.createElement("summary");
summary.className = "mr-diff-file-summary";
const badge = document.createElement("span");
badge.className = "mr-diff-file-status";
badge.dataset.status = file.status || "";
badge.textContent = file.status_char || "?";
const pathEl = document.createElement("span");
pathEl.className = "mr-diff-file-path";
pathEl.textContent = pathLabel(file);
const stats = document.createElement("span");
stats.className = "mr-diff-file-stats text-muted";
stats.textContent = statsLabel(file);
summary.appendChild(badge);
summary.appendChild(pathEl);
summary.appendChild(stats);
const body = document.createElement("div");
body.className = "mr-diff-file-body";
const placeholder = document.createElement("p");
placeholder.className = "text-muted small mb-0 mr-diff-file-placeholder";
placeholder.textContent = file.binary
? "Binary file — no textual diff."
: "Expand to load this file’s diff.";
body.appendChild(placeholder);
details.appendChild(summary);
details.appendChild(body);
return details;
}
function renderSummary(body) {
if (!listEl || !emptyEl) return;
const files = body && Array.isArray(body.files) ? body.files : [];
listEl.innerHTML = "";
if (!files.length || body.empty) {
listEl.classList.add("d-none");
emptyEl.textContent = "No changes between ours and the merge request head.";
emptyEl.classList.remove("d-none");
return;
}
emptyEl.classList.add("d-none");
listEl.classList.remove("d-none");
const frag = document.createDocumentFragment();
files.forEach(function (file) {
frag.appendChild(renderFileRow(file));
});
listEl.appendChild(frag);
}
function showPatchError(bodyEl, message) {
bodyEl.innerHTML =
'<p class="text-danger small mb-0">Failed to load patch: ' +
escapeHtml(message.slice(0, 240)) +
"</p>";
}
function loadFilePatch(details) {
const bodyEl = details.querySelector(".mr-diff-file-body");
if (!bodyEl) return;
if (details.dataset.loaded === "1" || details.dataset.loading === "1") return;
if (details.dataset.binary === "true") {
bodyEl.innerHTML =
'<p class="text-muted small mb-0">Binary file — no textual diff.</p>';
details.dataset.loaded = "1";
return;
}
const path = details.dataset.path || "";
const oldPath = details.dataset.oldPath || "";
const url = patchUrl(path, oldPath || undefined);
if (!url || !path) {
showPatchError(bodyEl, "Missing path.");
return;
}
details.dataset.loading = "1";
bodyEl.innerHTML = '<p class="text-muted small mb-0">Loading diff…</p>';
fetch(url, { credentials: "same-origin" })
.then(function (res) {
if (!res.ok) {
return res.text().then(function (text) {
throw new Error(text || "HTTP " + res.status);
});
}
return res.json();
})
.then(function (payload) {
details.dataset.loading = "0";
if (payload && payload.binary) {
bodyEl.innerHTML =
'<p class="text-muted small mb-0">Binary file — no textual diff.</p>';
details.dataset.loaded = "1";
return;
}
const diff = payload && typeof payload.diff === "string" ? payload.diff : "";
if (!diff.trim()) {
bodyEl.innerHTML =
'<p class="text-muted small mb-0">No textual changes for this file.</p>';
details.dataset.loaded = "1";
return;
}
bodyEl.innerHTML = "";
const mount = document.createElement("div");
mount.className = "mr-diff-file-d2h";
bodyEl.appendChild(mount);
const ok = renderUnifiedDiff(mount, diff, { drawFileList: false });
if (!ok && !mount.querySelector(".text-danger")) {
bodyEl.innerHTML =
'<p class="text-muted small mb-0">No textual changes for this file.</p>';
}
details.dataset.loaded = "1";
})
.catch(function (err) {
details.dataset.loading = "0";
const message =
err && err.message ? String(err.message) : "Could not load patch.";
showPatchError(bodyEl, message);
});
}
function onFileToggle(event) {
const details = event.target;
if (!(details instanceof HTMLDetailsElement)) return;
if (!details.classList.contains("mr-diff-file")) return;
if (!details.open) return;
loadFilePatch(details);
}
function loadSummary() {
if (!root || !listEl || !emptyEl) return;
if (summaryState === "loading" || summaryState === "ready") return;
const url = summaryUrl();
if (!url) {
setStatus("Missing project or merge request for diff.", true);
summaryState = "error";
return;
}
summaryState = "loading";
setStatus("Loading file list…", false);
listEl.innerHTML = "";
listEl.classList.add("d-none");
emptyEl.classList.add("d-none");
fetch(url, { credentials: "same-origin" })
.then(function (res) {
if (!res.ok) {
return res.text().then(function (text) {
throw new Error(text || "HTTP " + res.status);
});
}
return res.json();
})
.then(function (body) {
setStatus("", false);
renderSummary(body);
summaryState = "ready";
})
.catch(function (err) {
summaryState = "error";
const message =
err && err.message ? String(err.message) : "Could not load diff.";
setStatus("", false);
listEl.classList.remove("d-none");
listEl.innerHTML =
'<p class="text-danger mb-0">Failed to load diff: ' +
escapeHtml(message.slice(0, 240)) +
"</p>";
});
}
export function initMrDiff() {
if (!root || !diffTab) return;
if (listEl) listEl.addEventListener("toggle", onFileToggle, true);
diffTab.addEventListener("shown.bs.tab", loadSummary);
if (diffTab.classList.contains("active")) {
loadSummary();
}
}
initMrDiff();