From 60a59f650da76dd95f6c6221287b2745b7e44291 Mon Sep 17 00:00:00 2001 From: Saya Andy Date: Tue, 8 Sep 2026 10:07:45 +0700 Subject: diff: show the default from ref in the refs panel By default a diff against a ref is shown as that ref against its parent, but the refs panel left the from picker empty, so the controls described a different diff than the page showed. When no from ref is given, preselect the to ref with one extra commit back, which is the parent of the (stepped) to rev, and reuse the same values in the flip link. --- tests/t0106-diff.sh | 29 ++++++++++++++++++-- ui-diff.c | 78 ++++++++++++++++++++++++++--------------------------- 2 files changed, 65 insertions(+), 42 deletions(-) diff --git a/tests/t0106-diff.sh b/tests/t0106-diff.sh index 0e45087..4490ecc 100755 --- a/tests/t0106-diff.sh +++ b/tests/t0106-diff.sh @@ -71,9 +71,17 @@ test_expect_success 'ref picker: to: shows master when id=master' ' grep "master" tmp ' -test_expect_success 'ref picker: from: empty when no id2' ' +test_expect_success 'ref picker: from: preselects the to ref when no id2' ' cgit_query "url=foo/diff" >tmp && - grep ""); html(" | "); - { - const char *flip_new_ref; - int flip_idoff; - int idoff = ctx.qry.idoff; - int id2off = ctx.qry.id2off; - - if (old_ref && *old_ref) { - /* explicit from ref */ - flip_new_ref = old_ref; - flip_idoff = id2off; - } else if (!is_null_oid(old_rev_oid)) { - /* - * The from rev is the parent of the (stepped) - * to rev, i.e. new_ref~(idoff+1); keep the - * ref name in the url instead of a materialized - * parent sha. - */ - flip_new_ref = new_ref; - flip_idoff = idoff + 1; - } else { - /* root: no from rev to flip to */ - flip_new_ref = NULL; - flip_idoff = 0; - } - - /* - * cgit_diff_link() emits the offsets from ctx.qry; - * swap them around the call so they follow the refs. - */ - ctx.qry.idoff = flip_idoff; - ctx.qry.id2off = idoff; - cgit_diff_link("flip", "swap from and to", "button", - ctx.qry.head, - flip_new_ref, new_ref, - ctx.qry.path); - ctx.qry.idoff = idoff; - ctx.qry.id2off = id2off; - } + /* + * The flip link swaps the displayed refs, so the displayed + * from ref becomes the to ref. cgit_diff_link() emits the + * offsets from ctx.qry; swap them around the call so they + * follow the refs. + */ + ctx.qry.idoff = disp_id2off; + ctx.qry.id2off = idoff; + cgit_diff_link("flip", "swap from and to", "button", + ctx.qry.head, + disp_old_ref, new_ref, + ctx.qry.path); + ctx.qry.idoff = idoff; + ctx.qry.id2off = id2off; html(""); } html(""); -- cgit v1.3.1+17