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 {
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";