1 --- gitweb.cgi.orig 2020-10-06 10:13:45.621810025 +0100
2 +++ gitweb.cgi 2020-10-06 10:08:21.651823286 +0100
7 -use File::Basename qw(basename);
8 +use File::Basename qw(basename dirname);
9 use Time::HiRes qw(gettimeofday tv_interval);
10 use Digest::MD5 qw(md5_hex);
11 +use File::Spec; # hack
13 binmode STDOUT, ':utf8';
15 @@ -6644,6 +6645,20 @@
16 print "\n</div>\n"; # class="readme"
20 + if (!$prevent_xss) {
21 + $file_name = "README.md";
22 + my $proj_head_hash = git_get_head_hash($project);
23 + my $readme_blob_hash = git_get_hash_by_path($proj_head_hash, "README.md", "blob");
25 + if ($readme_blob_hash) { # if README.md exists
26 + print "<div class=\"header\">$file_name</div>\n";
27 + print "<div class=\"readme page_body\">"; # TODO find/create a better CSS class than page_body
28 + print get_markdown($file_name, $readme_blob_hash);
33 # we need to request one more than 16 (0..15) to check if
35 my @commitlist = $head ? parse_commits($head, 17) : ();
37 my $syntax = guess_file_syntax($highlight, $file_name);
38 $fd = run_highlighter($fd, $highlight, $syntax);
41 + my $ismarkdown = ($file_name =~ /md$/);
43 git_header_html(undef, $expires);
45 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
46 @@ -7160,6 +7178,10 @@
47 esc_attr(href(action=>"blob_plain", hash=>$hash,
48 hash_base=>$hash_base, file_name=>$file_name)) .
50 + } elsif ($ismarkdown) {
51 + print qq!<div class="readme page_body">\n!;
52 + print get_markdown($file_name, $hash);
53 + print qq!</div>\n!; # $cmd_markdownify
56 while (my $line = <$fd>) {
57 @@ -7177,6 +7199,79 @@
62 +sub get_norm_rel_path { # http://www.perlmonks.org/bare/?node_id=11907
63 + my $unnormpath = shift;
64 + while ($unnormpath =~ m!/\.!) {
65 + $unnormpath =~ s!/[^\/]+/\.\.!!;
66 + # print "Path is now -+$unnormpath+-\n";
71 + my $tfilename = shift;
73 + my $rethtmlstr = "";
74 + use open ":encoding(utf8)"; # needed to have utf8 survive through the shell pipe
75 + my $cmd_markdownify = $GIT . " " . git_cmd() . " cat-file blob " . $thash . " | perl -e 'my \$str = do { local \$/; <STDIN> }; \$str =~ s/<!--.*?--\s*>//gs; print \$str;' | markdown |";
76 + open (FOO, $cmd_markdownify) or die_error(500, "Open git-cat-file blob '$thash' failed");
78 + if ($_ =~ /(<img[^>]src=")(.*?)"/) {
79 + my $origcut = "".$2;
80 + my $testcut = "".$2;
81 + my $is_anchor = ($testcut =~ /^#/);
82 + my $is_absolute = ($testcut =~ /^http/);
83 + my $is_relative_up = ($testcut =~ /^\.\./);
84 + my $is_local_link = ((!$is_anchor) and (!$is_absolute));
85 + my $tdir = dirname($tfilename);
86 + my $is_tdir_proper = (($tdir ne "") and ($tdir ne "."));
87 + #print "XX: $origcut ($is_anchor, $is_absolute - $is_local_link) ($is_relative_up, $is_tdir_proper, $tdir, $tfilename)\n"; # dbg
88 + if ($is_local_link) {
89 + if ($is_relative_up) { # normalize
90 + if ($is_tdir_proper) {
91 + # cheat with absolute path here:
92 + my $resolved = get_norm_rel_path( File::Spec->rel2abs ("$origcut", "/$tdir" ) );
93 + $resolved = substr $resolved, 1;
94 + #print "YY: $resolved\n";
95 + $_ =~ s!(<img[^>]src=")(.*?)"!$1?p=$project;a=blob_plain;f=$resolved"!gi;
98 + $_ =~ s!(<img[^>]src=")(.*?)"!$1?p=$project;a=blob_plain;f=$2"!gi;
103 + if ($_ =~ /(<a[^>]href=")(.*?)"/) {
104 + my $origcut = "".$2;
105 + my $testcut = "".$2;
106 + my $is_anchor = ($testcut =~ /^#/);
107 + my $is_absolute = ($testcut =~ /^http/);
108 + my $is_relative_up = ($testcut =~ /^\.\./);
109 + my $is_local_link = ((!$is_anchor) and (!$is_absolute));
110 + my $tdir = dirname($tfilename);
111 + my $is_tdir_proper = (($tdir ne "") and ($tdir ne "."));
112 + #print "XX: $origcut ($is_anchor, $is_absolute - $is_local_link) ($is_relative_up, $is_tdir_proper, $tdir, $tfilename)\n"; # dbg
113 + if ($is_local_link) {
114 + if ($is_relative_up) { # normalize
115 + if ($is_tdir_proper) {
116 + # cheat with absolute path here:
117 + my $resolved = get_norm_rel_path( File::Spec->rel2abs ("$origcut", "/$tdir" ) );
118 + $resolved = substr $resolved, 1;
119 + #print "YY: $resolved\n";
120 + $_ =~ s!(<a[^>]href=")(.*?)"!$1?p=$project;a=blob;f=$resolved"!gi;
123 + $_ =~ s!(<a[^>]href=")(.*?)"!$1?p=$project;a=blob;f=$2"!gi;
131 + return $rethtmlstr;
135 if (!defined $hash_base) {