@matheusbsbs / matheus-bagliano / commits / 3bf9d57051
Fork 0

gitweb: Hyperlink target of symbolic link in "tree" view (if possible)

Make symbolic link target in "tree" view into hyperlink to generic "object" view (as we don't know if the link target is file (blob) or directory (tree), and if it exist at all). Target of link is made into hyperlink when: * hash_base is provided (otherwise we cannot find hash of link target) * link is relative * in no place link goes out of root tree (top dir) Full path of symlink target from the root dir is provided in the title attribute of hyperlink. Currently symbolic link name uses ordinary file style (hidden hyperlink), while the hyperlink to symlink target uses default hyperlink style, so it is underlined while link target which is not made into hyperlink is not underlined. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Dec 10, 2006 at 13:25 UTC 3bf9d57051845cee996f73b5402a07cbeda84c88
1 file changed +51 -1
gitweb/gitweb.perl
+51 -1
@@ -2008,6 +2008,48 @@ sub git_get_link_target {
2008 return $link_target;
2009 }
2010
2011 +# given link target, and the directory (basedir) the link is in,
2012 +# return target of link relative to top directory (top tree);
2013 +# return undef if it is not possible (including absolute links).
2014 +sub normalize_link_target {
2015 + my ($link_target, $basedir, $hash_base) = @_;
2016 +
2017 + # we can normalize symlink target only if $hash_base is provided
2018 + return unless $hash_base;
2019 +
2020 + # absolute symlinks (beginning with '/') cannot be normalized
2021 + return if (substr($link_target, 0, 1) eq '/');
2022 +
2023 + # normalize link target to path from top (root) tree (dir)
2024 + my $path;
2025 + if ($basedir) {
2026 + $path = $basedir . '/' . $link_target;
2027 + } else {
2028 + # we are in top (root) tree (dir)
2029 + $path = $link_target;
2030 + }
2031 +
2032 + # remove //, /./, and /../
2033 + my @path_parts;
2034 + foreach my $part (split('/', $path)) {
2035 + # discard '.' and ''
2036 + next if (!$part || $part eq '.');
2037 + # handle '..'
2038 + if ($part eq '..') {
2039 + if (@path_parts) {
2040 + pop @path_parts;
2041 + } else {
2042 + # link leads outside repository (outside top dir)
2043 + return;
2044 + }
2045 + } else {
2046 + push @path_parts, $part;
2047 + }
2048 + }
2049 + $path = join('/', @path_parts);
2050 +
2051 + return $path;
2052 +}
2053
2054 # print tree entry (row of git_tree), but without encompassing <tr> element
2055 sub git_print_tree_entry {
@@ -2029,7 +2071,15 @@ sub git_print_tree_entry {
2071 if (S_ISLNK(oct $t->{'mode'})) {
2072 my $link_target = git_get_link_target($t->{'hash'});
2073 if ($link_target) {
2032 - print " -> " . esc_path($link_target);
2074 + my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2075 + if (defined $norm_target) {
2076 + print " -> " .
2077 + $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2078 + file_name=>$norm_target),
2079 + -title => $norm_target}, esc_path($link_target));
2080 + } else {
2081 + print " -> " . esc_path($link_target);
2082 + }
2083 }
2084 }
2085 print "</td>\n";