From fda513d6fea267f2b59a1c5a212d98e4b6ae4187 Mon Sep 17 00:00:00 2001 From: Travor Liu Date: Fri, 17 Jul 2026 21:52:45 +0800 Subject: [PATCH] gitweb: shorten index hashes with trailing file modes Diff index lines have included a trailing file mode since ec1fcc16af (Show original and resulting blob object info in diff output, 2005-10-07) when the old and new file modes match: index .. 100644 gitweb recognizes that trailing mode before it tries to shorten and link the object IDs. This appends the file-type annotation first, but the object-ID matcher requires the ID range to end the line. As a result, this common form keeps both full object IDs as plain text. That is inconsistent with other hash displays and makes commitdiff output wider than necessary. Recent gitweb changes have fixed mobile overflow in log, commit, blob and diff views; leaving two full object IDs in this header preserves an avoidable long line in the diff header. * gitweb/gitweb.perl: Remove the trailing mode before matching the index IDs, then append it again after the IDs have been shortened and linked. This preserves the mode display while letting ordinary and combined index lines use the existing object-ID formatting paths. * t/t9502-gitweb-standalone-parse-output.sh: Add coverage for that common form by rendering a commitdiff for a regular file modification. Check that the visible index line contains linked short blob IDs followed by the mode and file-type annotation, and that the full unlinked form is not emitted. Signed-off-by: Travor Liu Signed-off-by: Junio C Hamano --- gitweb/gitweb.perl | 18 +++++++++++++----- t/t9502-gitweb-standalone-parse-output.sh | 13 +++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index fde804593b..8c2d9b8eef 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -2339,12 +2339,14 @@ sub format_extended_diff_header_line { $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"}, esc_path($to->{'file'})); } - # match single - if ($line =~ m/\s(\d{6})$/) { - $line .= ' (' . - file_type_long($1) . - ')'; + + # Temporarily remove a trailing so an index line ends with its + # object IDs and can be shortened below. + my $mode; + if ($line =~ s/\s(\d{6})$//) { + $mode = $1; } + # match if ($line =~ oid_nlen_prefix_infix_regex($sha1_len, "index ", ",") | $line =~ oid_nlen_prefix_infix_regex($sha256_len, "index ", ",")) { @@ -2388,6 +2390,12 @@ sub format_extended_diff_header_line { my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'}); $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!; } + if (defined $mode) { + $line .= " $mode" . + ' (' . + file_type_long($mode) . + ')'; + } return $line . "
\n"; } diff --git a/t/t9502-gitweb-standalone-parse-output.sh b/t/t9502-gitweb-standalone-parse-output.sh index 81d5625557..85f771650d 100755 --- a/t/t9502-gitweb-standalone-parse-output.sh +++ b/t/t9502-gitweb-standalone-parse-output.sh @@ -115,6 +115,19 @@ test_expect_success 'snapshot: hierarchical branch name (xx/test)' ' ' test_debug 'cat gitweb.headers' +test_expect_success 'commitdiff: index line shortens hashes with mode' ' + old_blob=$(git rev-parse HEAD:foo) && + old_short=$(git rev-parse --short=7 HEAD:foo) && + echo changed >foo && + git commit -am "change foo" && + new_blob=$(git rev-parse HEAD:foo) && + new_short=$(git rev-parse --short=7 HEAD:foo) && + gitweb_run "p=.git;a=commitdiff;h=HEAD" && + test_grep ">${old_short}\\.\\.]*>${new_short} 100644 (file)" \ + gitweb.body && + test_grep ! "index ${old_blob}\\.\\.${new_blob} 100644" gitweb.body +' + # ---------------------------------------------------------------------- # forks of projects