]> Lady’s Gitweb - Gitweb/blobdiff - gitweb.perl
gitweb: protect blob and diff output lines from controls.
[Gitweb] / gitweb.perl
index 39a19585d1974765364364e86ede74f33be54349de3b709b3ad3981cb851270f..ab5fefa2008ba407f729e08b9bfde1da38d8c37bfb8a47b7e18cab21d538ef52 100755 (executable)
@@ -577,11 +577,10 @@ sub esc_html ($;%) {
 
        $str = to_utf8($str);
        $str = escapeHTML($str);
-       $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
-       $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
        if ($opts{'-nbsp'}) {
                $str =~ s/ / /g;
        }
+       $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
        return $str;
 }
 
@@ -786,6 +785,32 @@ sub file_type {
        }
 }
 
+# convert file mode in octal to file type description string
+sub file_type_long {
+       my $mode = shift;
+
+       if ($mode !~ m/^[0-7]+$/) {
+               return $mode;
+       } else {
+               $mode = oct $mode;
+       }
+
+       if (S_ISDIR($mode & S_IFMT)) {
+               return "directory";
+       } elsif (S_ISLNK($mode)) {
+               return "symlink";
+       } elsif (S_ISREG($mode)) {
+               if ($mode & S_IXUSR) {
+                       return "executable";
+               } else {
+                       return "file";
+               };
+       } else {
+               return "unknown";
+       }
+}
+
+
 ## ----------------------------------------------------------------------
 ## functions returning short HTML fragments, or transforming HTML fragments
 ## which don't beling to other sections
@@ -1854,17 +1879,17 @@ sub git_print_page_path {
                        $fullname .= ($fullname ? '/' : '') . $dir;
                        print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
                                                     hash_base=>$hb),
-                                     -title => $fullname}, esc_path($dir));
+                                     -title => esc_html($fullname)}, esc_path($dir));
                        print " / ";
                }
                if (defined $type && $type eq 'blob') {
                        print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
                                                     hash_base=>$hb),
-                                     -title => $name}, esc_path($basename));
+                                     -title => esc_html($name)}, esc_path($basename));
                } elsif (defined $type && $type eq 'tree') {
                        print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
                                                     hash_base=>$hb),
-                                     -title => $name}, esc_path($basename));
+                                     -title => esc_html($name)}, esc_path($basename));
                        print " / ";
                } else {
                        print esc_path($basename);
@@ -1987,7 +2012,7 @@ sub git_print_tree_entry {
 
 sub git_difftree_body {
        my ($difftree, $hash, $parent) = @_;
-
+       my ($have_blame) = gitweb_check_feature('blame');
        print "<div class=\"list_head\">\n";
        if ($#{$difftree} > 10) {
                print(($#{$difftree} + 1) . " files changed:\n");
@@ -2060,9 +2085,13 @@ sub git_difftree_body {
                        print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
                                                     hash_base=>$parent, file_name=>$diff{'file'})},
                                      "blob") . " | ";
-                       print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
-                                                    file_name=>$diff{'file'})},
-                                     "blame") . " | ";
+                       if ($have_blame) {
+                               print $cgi->a({-href =>
+                                                  href(action=>"blame",
+                                                       hash_base=>$parent,
+                                                       file_name=>$diff{'file'})},
+                                             "blame") . " | ";
+                       }
                        print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
                                                     file_name=>$diff{'file'})},
                                      "history");
@@ -2108,9 +2137,12 @@ sub git_difftree_body {
                        print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
                                                     hash_base=>$hash, file_name=>$diff{'file'})},
                                      "blob") . " | ";
-                       print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
-                                                    file_name=>$diff{'file'})},
-                                     "blame") . " | ";
+                       if ($have_blame) {
+                               print $cgi->a({-href => href(action=>"blame",
+                                                            hash_base=>$hash,
+                                                            file_name=>$diff{'file'})},
+                                             "blame") . " | ";
+                       }
                        print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
                                                     file_name=>$diff{'file'})},
                                      "history");
@@ -2151,9 +2183,12 @@ sub git_difftree_body {
                        print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
                                                     hash_base=>$parent, file_name=>$diff{'from_file'})},
                                      "blob") . " | ";
-                       print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
-                                                    file_name=>$diff{'from_file'})},
-                                     "blame") . " | ";
+                       if ($have_blame) {
+                               print $cgi->a({-href => href(action=>"blame",
+                                                            hash_base=>$hash,
+                                                            file_name=>$diff{'to_file'})},
+                                             "blame") . " | ";
+                       }
                        print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
                                                    file_name=>$diff{'from_file'})},
                                      "history");
@@ -2172,6 +2207,7 @@ sub git_patchset_body {
        my $in_header = 0;
        my $patch_found = 0;
        my $diffinfo;
+       my (%from, %to);
 
        print "<div class=\"patchset\">\n";
 
@@ -2182,6 +2218,10 @@ sub git_patchset_body {
                if ($patch_line =~ m/^diff /) { # "git diff" header
                        # beginning of patch (in patchset)
                        if ($patch_found) {
+                               # close extended header for previous empty patch
+                               if ($in_header) {
+                                       print "</div>\n" # class="diff extended_header"
+                               }
                                # close previous patch
                                print "</div>\n"; # class="patch"
                        } else {
@@ -2190,89 +2230,113 @@ sub git_patchset_body {
                        }
                        print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
 
+                       # read and prepare patch information
                        if (ref($difftree->[$patch_idx]) eq "HASH") {
+                               # pre-parsed (or generated by hand)
                                $diffinfo = $difftree->[$patch_idx];
                        } else {
                                $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
                        }
+                       $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
+                       $to{'file'}   = $diffinfo->{'to_file'}   || $diffinfo->{'file'};
+                       if ($diffinfo->{'status'} ne "A") { # not new (added) file
+                               $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
+                                                    hash=>$diffinfo->{'from_id'},
+                                                    file_name=>$from{'file'});
+                       }
+                       if ($diffinfo->{'status'} ne "D") { # not deleted file
+                               $to{'href'} = href(action=>"blob", hash_base=>$hash,
+                                                  hash=>$diffinfo->{'to_id'},
+                                                  file_name=>$to{'file'});
+                       }
                        $patch_idx++;
 
-                       if ($diffinfo->{'status'} eq "A") { # added
-                               print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
-                                     $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
-                                                            hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
-                                             $diffinfo->{'to_id'}) . " (new)" .
-                                     "</div>\n"; # class="diff_info"
-
-                       } elsif ($diffinfo->{'status'} eq "D") { # deleted
-                               print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
-                                     $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
-                                                            hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
-                                             $diffinfo->{'from_id'}) . " (deleted)" .
-                                     "</div>\n"; # class="diff_info"
-
-                       } elsif ($diffinfo->{'status'} eq "R" || # renamed
-                                $diffinfo->{'status'} eq "C" || # copied
-                                $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
-                               print "<div class=\"diff_info\">" .
-                                     file_type($diffinfo->{'from_mode'}) . ":" .
-                                     $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
-                                                            hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
-                                             $diffinfo->{'from_id'}) .
-                                     " -> " .
-                                     file_type($diffinfo->{'to_mode'}) . ":" .
-                                     $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
-                                                            hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
-                                             $diffinfo->{'to_id'});
-                               print "</div>\n"; # class="diff_info"
-
-                       } else { # modified, mode changed, ...
-                               print "<div class=\"diff_info\">" .
-                                     file_type($diffinfo->{'from_mode'}) . ":" .
-                                     $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
-                                                            hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
-                                             $diffinfo->{'from_id'}) .
-                                     " -> " .
-                                     file_type($diffinfo->{'to_mode'}) . ":" .
-                                     $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
-                                                            hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
-                                             $diffinfo->{'to_id'});
-                               print "</div>\n"; # class="diff_info"
+                       # print "git diff" header
+                       $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
+                       if ($from{'href'}) {
+                               $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
+                                                      'a/' . esc_path($from{'file'}));
+                       } else { # file was added
+                               $patch_line .= 'a/' . esc_path($from{'file'});
+                       }
+                       $patch_line .= ' ';
+                       if ($to{'href'}) {
+                               $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
+                                                      'b/' . esc_path($to{'file'}));
+                       } else { # file was deleted
+                               $patch_line .= 'b/' . esc_path($to{'file'});
                        }
 
-                       #print "<div class=\"diff extended_header\">\n";
+                       print "<div class=\"diff header\">$patch_line</div>\n";
+                       print "<div class=\"diff extended_header\">\n";
                        $in_header = 1;
                        next LINE;
-               } # start of patch in patchset
+               }
 
+               if ($in_header) {
+                       if ($patch_line !~ m/^---/) {
+                               # match <path>
+                               if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
+                                       $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
+                                                               esc_path($from{'file'}));
+                               }
+                               if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
+                                       $patch_line = $cgi->a({-href=>$to{'href'}, -class=>"path"},
+                                                             esc_path($to{'file'}));
+                               }
+                               # match <mode>
+                               if ($patch_line =~ m/\s(\d{6})$/) {
+                                       $patch_line .= '<span class="info"> (' .
+                                                      file_type_long($1) .
+                                                      ')</span>';
+                               }
+                               # match <hash>
+                               if ($patch_line =~ m/^index/) {
+                                       my ($from_link, $to_link);
+                                       if ($from{'href'}) {
+                                               $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
+                                                                    substr($diffinfo->{'from_id'},0,7));
+                                       } else {
+                                               $from_link = '0' x 7;
+                                       }
+                                       if ($to{'href'}) {
+                                               $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
+                                                                  substr($diffinfo->{'to_id'},0,7));
+                                       } else {
+                                               $to_link = '0' x 7;
+                                       }
+                                       my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
+                                       $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
+                               }
+                               print $patch_line . "<br/>\n";
 
-               if ($in_header && $patch_line =~ m/^---/) {
-                       #print "</div>\n"; # class="diff extended_header"
-                       $in_header = 0;
+                       } else {
+                               #$in_header && $patch_line =~ m/^---/;
+                               print "</div>\n"; # class="diff extended_header"
+                               $in_header = 0;
+
+                               if ($from{'href'}) {
+                                       $patch_line = '--- a/' .
+                                                     $cgi->a({-href=>$from{'href'}, -class=>"path"},
+                                                             esc_path($from{'file'}));
+                               }
+                               print "<div class=\"diff from_file\">$patch_line</div>\n";
 
-                       my $file = $diffinfo->{'from_file'};
-                       $file  ||= $diffinfo->{'file'};
-                       $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
-                                                      hash=>$diffinfo->{'from_id'}, file_name=>$file),
-                                       -class => "list"}, esc_path($file));
-                       $patch_line =~ s|a/.*$|a/$file|g;
-                       print "<div class=\"diff from_file\">$patch_line</div>\n";
+                               $patch_line = <$fd>;
+                               chomp $patch_line;
 
-                       $patch_line = <$fd>;
-                       chomp $patch_line;
+                               #$patch_line =~ m/^+++/;
+                               if ($to{'href'}) {
+                                       $patch_line = '+++ b/' .
+                                                     $cgi->a({-href=>$to{'href'}, -class=>"path"},
+                                                             esc_path($to{'file'}));
+                               }
+                               print "<div class=\"diff to_file\">$patch_line</div>\n";
 
-                       #$patch_line =~ m/^+++/;
-                       $file    = $diffinfo->{'to_file'};
-                       $file  ||= $diffinfo->{'file'};
-                       $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
-                                                      hash=>$diffinfo->{'to_id'}, file_name=>$file),
-                                       -class => "list"}, esc_path($file));
-                       $patch_line =~ s|b/.*|b/$file|g;
-                       print "<div class=\"diff to_file\">$patch_line</div>\n";
+                       }
 
                        next LINE;
                }
-               next LINE if $in_header;
 
                print format_diff_line($patch_line);
        }
@@ -2787,6 +2851,7 @@ sub git_tag {
        print "<div class=\"page_body\">";
        my $comment = $tag{'comment'};
        foreach my $line (@$comment) {
+               chomp($line);
                print esc_html($line) . "<br/>\n";
        }
        print "</div>\n";
@@ -2856,6 +2921,7 @@ HTML
                        }
                }
                my $data = $_;
+               chomp($data);
                my $rev = substr($full_rev, 0, 8);
                my $author = $meta->{'author'};
                my %date = parse_date($meta->{'author-time'},
This page took 0.328008 seconds and 4 git commands to generate.