]> gitweb.ps.run Git - flakes/blob - patches/gitweb.patch
8372f32c1d2ba01ad402d6a19c7dc6329faa89b9
[flakes] / patches / gitweb.patch
1 --- gitweb.cgi.orig     2020-10-06 10:13:45.621810025 +0100
2 +++ gitweb.cgi  2020-10-06 10:08:21.651823286 +0100
3 @@ -18,9 +18,10 @@
4  use Encode;
5  use Fcntl ':mode';
6  use File::Find qw();
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
12  
13  binmode STDOUT, ':utf8';
14  
15 @@ -6644,6 +6645,20 @@
16                 print "\n</div>\n"; # class="readme"
17         }
18  
19 +       # hack
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");
24
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);
29 +                       print "</div>";
30 +               }
31 +       }
32 +
33         # we need to request one more than 16 (0..15) to check if
34         # those 16 are all
35         my @commitlist = $head ? parse_commits($head, 17) : ();
36 @@ -7117,6 +7132,9 @@
37         my $syntax = guess_file_syntax($highlight, $file_name);
38         $fd = run_highlighter($fd, $highlight, $syntax);
39  
40 +       # hack
41 +       my $ismarkdown = ($file_name =~ /md$/);
42 +
43         git_header_html(undef, $expires);
44         my $formats_nav = '';
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)) .
49                       qq!" />\n!;
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
54         } else {
55                 my $nr;
56                 while (my $line = <$fd>) {
57 @@ -7177,6 +7199,79 @@
58         git_footer_html();
59  }
60  
61 +# hack
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";
67 +   }
68 +   return $unnormpath;
69 +}
70 +sub get_markdown {
71 +   my $tfilename = shift;
72 +   my $thash = 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");
77 +   while (<FOO>) {
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;
96 +                   }
97 +               } else {
98 +                   $_ =~ s!(<img[^>]src=")(.*?)"!$1?p=$project;a=blob_plain;f=$2"!gi;
99 +                   #print "ZZ: $_\n";
100 +               }
101 +           }
102 +       }
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;
121 +                   }
122 +               } else {
123 +                   $_ =~ s!(<a[^>]href=")(.*?)"!$1?p=$project;a=blob;f=$2"!gi;
124 +                   #print "ZZ: $_\n";
125 +               }
126 +           }
127 +       }
128 +       $rethtmlstr .= $_;
129 +   }
130 +   close(FOO);
131 +   return $rethtmlstr;
132 +}
133 +
134  sub git_tree {
135         if (!defined $hash_base) {
136                 $hash_base = "HEAD";
137