From 22f8de0f4f9b60ba3c6d3cc164cdf07ff988d2a4 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 31 Oct 2026 22:07:47 -1000 Subject: Show a renamed file's diff as the rename A diff limited to the new name cannot see the old one, so the diffstat's link for a renamed file, the commit page reached from that file's log, and the file's rawdiff and patch all showed every line as added. A path the old tree lacks is now diffed across the whole commit with rename detection aimed at it alone, the way git blame looks for a rename, so it is compared with the commit's deleted files and nothing else. Doing it in cgit_diff_tree keeps the log's counts for that commit in step with the page they link to. The rawdiff and patch pages leave the diff to git's own output, so for them that search finds the old name, and both names go into the limit with rename detection on. --- tests/t0107-diff.sh | 35 +++++++++++++++++++++++++++++++++++ tests/t0108-rawdiff.sh | 21 +++++++++++++++++++++ tests/t0109-patch.sh | 19 +++++++++++++++++++ 3 files changed, 75 insertions(+) (limited to 'tests') diff --git a/tests/t0107-diff.sh b/tests/t0107-diff.sh index e173d00..d03ecd2 100755 --- a/tests/t0107-diff.sh +++ b/tests/t0107-diff.sh @@ -104,4 +104,39 @@ test_expect_success 'stripping the highlight leaves every line intact' ' done old.txt && + git add old.txt && + git commit -m "add old.txt" && + git mv old.txt new.txt && + git commit -m "rename old.txt" + ) && + cgitrc_repo movedrc moved +' + +test_expect_success 'the diff of a renamed file shows the rename' ' + cgit_rc movedrc "url=moved/diff/new.txt" >tmp && + grep "diff --git a/old.txt b/new.txt" tmp && + grep " (renamed from old.txt)" tmp && + ! grep "new file mode" tmp +' + +test_expect_success 'so does the commit page limited to the new name' ' + cgit_rc movedrc "url=moved/commit/new.txt" >tmp && + grep "diff --git a/old.txt b/new.txt" tmp && + ! grep "new file mode" tmp +' + +test_expect_success 'a file added outright still shows as new' ' + added=$(git -C repos/moved rev-parse HEAD~1) && + cgit_rc movedrc "url=moved/diff/old.txt&id=$added" >tmp && + grep "new file mode 100644" tmp && + grep "
+10
" tmp +' + test_done diff --git a/tests/t0108-rawdiff.sh b/tests/t0108-rawdiff.sh index 9b4eb37..66f0cbe 100755 --- a/tests/t0108-rawdiff.sh +++ b/tests/t0108-rawdiff.sh @@ -52,4 +52,25 @@ test_expect_success 'a path limits the diff to that file' ' ! grep "file-5" actual ' +# The old name of a file renamed in the change joins the limit, so the diff +# shows the rename, the same as git diff given both names. +test_expect_success 'a renamed file is diffed as the rename' ' + mkrepo repos/moved 1 && + ( + cd repos/moved && + test_seq 1 10 >old.txt && + git add old.txt && + git commit -m "add old.txt" && + git mv old.txt new.txt && + echo eleven >>new.txt && + git commit -am "rename old.txt" + ) && + cgitrc_repo movedrc moved && + cgit_rc movedrc "url=moved/rawdiff/new.txt" >tmp && + git -C repos/moved diff HEAD^ HEAD -- old.txt new.txt >expect && + strip_headers actual && + test_cmp expect actual && + grep "^rename from old.txt$" actual +' + test_done diff --git a/tests/t0109-patch.sh b/tests/t0109-patch.sh index bc942fe..6aae556 100755 --- a/tests/t0109-patch.sh +++ b/tests/t0109-patch.sh @@ -70,4 +70,23 @@ test_expect_success 'the signature names the version when asked to' ' check_cgit_signature "$CGIT_VERSION" ' +# The old name of a file renamed in the commit joins the limit, so the patch +# carries the rename rather than a file added whole. +test_expect_success 'a renamed file is patched as the rename' ' + mkrepo repos/moved 1 && + ( + cd repos/moved && + test_seq 1 10 >old.txt && + git add old.txt && + git commit -m "add old.txt" && + git mv old.txt new.txt && + git commit -m "rename old.txt" + ) && + cgitrc_repo movedrc moved && + cgit_rc movedrc "url=moved/patch/new.txt" >tmp && + grep "^rename from old.txt$" tmp && + grep "^rename to new.txt$" tmp && + ! grep "^new file mode" tmp +' + test_done -- cgit 2.12.1