@matheusbsbs / matheus-bagliano / commits / e130ddaaf3
Fork 0

gitweb: Make command invocations go through the git wrapper

This patch makes invocations of core git commands go through the 'git' binary itself, which improves readability and might help system administrators lock down their CGI environment for security. Signed-off-by: Alp Toker <alp@atoker.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Alp Toker committed Jul 12, 2006 at 23:55 UTC e130ddaaf3451d0bd60860cb0019e47c9f421442
1 file changed +29 -33
gitweb/gitweb.cgi
+29 -33
@@ -22,20 +22,16 @@ our $my_url = $cgi->url();
22 our $my_uri = $cgi->url(-absolute => 1);
23 our $rss_link = "";
24
25 -# location of the git-core binaries
26 -our $gitbin = "/usr/bin";
25 +# core git executable to use
26 +# this can just be "git" if your webserver has a sensible PATH
27 +our $GIT = "/usr/bin/git";
28
29 # absolute fs-path which will be prepended to the project path
30 #our $projectroot = "/pub/scm";
31 our $projectroot = "/home/kay/public_html/pub/scm";
32
32 -# version of the git-core binaries
33 -our $git_version = qx($gitbin/git --version);
34 -if ($git_version =~ m/git version (.*)$/) {
35 - $git_version = $1;
36 -} else {
37 - $git_version = "unknown";
38 -}
33 +# version of the core git binary
34 +our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
35
36 # location for temporary files needed for diffs
37 our $git_temp = "/tmp/gitweb";
@@ -392,7 +388,7 @@ sub die_error {
388 sub git_get_type {
389 my $hash = shift;
390
395 - open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
391 + open my $fd, "-|", "$GIT cat-file -t $hash" or return;
392 my $type = <$fd>;
393 close $fd or return;
394 chomp $type;
@@ -404,7 +400,7 @@ sub git_read_head {
400 my $oENV = $ENV{'GIT_DIR'};
401 my $retval = undef;
402 $ENV{'GIT_DIR'} = "$projectroot/$project";
407 - if (open my $fd, "-|", "$gitbin/git-rev-parse", "--verify", "HEAD") {
403 + if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
404 my $head = <$fd>;
405 close $fd;
406 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
@@ -444,7 +440,7 @@ sub git_read_tag {
440 my %tag;
441 my @comment;
442
447 - open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
443 + open my $fd, "-|", "$GIT cat-file tag $tag_id" or return;
444 $tag{'id'} = $tag_id;
445 while (my $line = <$fd>) {
446 chomp $line;
@@ -516,7 +512,7 @@ sub git_read_commit {
512 @commit_lines = @$commit_text;
513 } else {
514 $/ = "\0";
519 - open my $fd, "-|", "$gitbin/git-rev-list --header --parents --max-count=1 $commit_id" or return;
515 + open my $fd, "-|", "$GIT rev-list --header --parents --max-count=1 $commit_id" or return;
516 @commit_lines = split '\n', <$fd>;
517 close $fd or return;
518 $/ = "\n";
@@ -614,7 +610,7 @@ sub git_diff_print {
610 if (defined $from) {
611 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
612 open my $fd2, "> $from_tmp";
617 - open my $fd, "-|", "$gitbin/git-cat-file blob $from";
613 + open my $fd, "-|", "$GIT cat-file blob $from";
614 my @file = <$fd>;
615 print $fd2 @file;
616 close $fd2;
@@ -625,7 +621,7 @@ sub git_diff_print {
621 if (defined $to) {
622 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
623 open my $fd2, "> $to_tmp";
628 - open my $fd, "-|", "$gitbin/git-cat-file blob $to";
624 + open my $fd, "-|", "$GIT cat-file blob $to";
625 my @file = <$fd>;
626 print $fd2 @file;
627 close $fd2;
@@ -844,7 +840,7 @@ sub git_get_project_config {
840 $key =~ s/^gitweb\.//;
841 return if ($key =~ m/\W/);
842
847 - my $val = qx($gitbin/git-repo-config --get gitweb.$key);
843 + my $val = qx($GIT repo-config --get gitweb.$key);
844 return ($val);
845 }
846
@@ -1066,7 +1062,7 @@ sub git_summary {
1062 "<tr><td>owner</td><td>$owner</td></tr>\n" .
1063 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1064 "</table>\n";
1069 - open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_head($project) or die_error(undef, "Open failed.");
1065 + open my $fd, "-|", "$GIT rev-list --max-count=17 " . git_read_head($project) or die_error(undef, "Open failed.");
1066 my (@revlist) = map { chomp; $_ } <$fd>;
1067 close $fd;
1068 print "<div>\n" .
@@ -1254,7 +1250,7 @@ sub git_blame {
1250 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1251 or die_error(undef, "Error lookup file.");
1252 }
1257 - open ($fd, "-|", "$gitbin/git-annotate", '-l', '-t', '-r', $file_name, $hash_base)
1253 + open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
1254 or die_error(undef, "Open failed.");
1255 git_header_html();
1256 print "<div class=\"page_nav\">\n" .
@@ -1449,7 +1445,7 @@ sub git_get_hash_by_path {
1445 my $tree = $base;
1446 my @parts = split '/', $path;
1447 while (my $part = shift @parts) {
1452 - open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
1448 + open my $fd, "-|", "$GIT ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
1449 my (@entries) = map { chomp; $_ } <$fd>;
1450 close $fd or return undef;
1451 foreach my $line (@entries) {
@@ -1536,7 +1532,7 @@ sub git_blob_plain_mimetype {
1532
1533 sub git_blob_plain {
1534 my $type = shift;
1539 - open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error("Couldn't cat $file_name, $hash");
1535 + open my $fd, "-|", "$GIT cat-file blob $hash" or die_error("Couldn't cat $file_name, $hash");
1536
1537 $type ||= git_blob_plain_mimetype($fd, $file_name);
1538
@@ -1563,7 +1559,7 @@ sub git_blob {
1559 $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
1560 }
1561 my $have_blame = git_get_project_config_bool ('blame');
1566 - open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1562 + open my $fd, "-|", "$GIT cat-file blob $hash" or die_error(undef, "Open failed.");
1563 my $mimetype = git_blob_plain_mimetype($fd, $file_name);
1564 if ($mimetype !~ m/^text\//) {
1565 close $fd;
@@ -1629,7 +1625,7 @@ sub git_tree {
1625 }
1626 }
1627 $/ = "\0";
1632 - open my $fd, "-|", "$gitbin/git-ls-tree -z $hash" or die_error(undef, "Open git-ls-tree failed.");
1628 + open my $fd, "-|", "$GIT ls-tree -z $hash" or die_error(undef, "Open git-ls-tree failed.");
1629 chomp (my (@entries) = <$fd>);
1630 close $fd or die_error(undef, "Reading tree failed.");
1631 $/ = "\n";
@@ -1712,7 +1708,7 @@ sub git_tree {
1708
1709 sub git_rss {
1710 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1715 - open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_head($project) or die_error(undef, "Open failed.");
1711 + open my $fd, "-|", "$GIT rev-list --max-count=150 " . git_read_head($project) or die_error(undef, "Open failed.");
1712 my (@revlist) = map { chomp; $_ } <$fd>;
1713 close $fd or die_error(undef, "Reading rev-list failed.");
1714 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
@@ -1732,7 +1728,7 @@ sub git_rss {
1728 last;
1729 }
1730 my %cd = date_str($co{'committer_epoch'});
1735 - open $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $co{'id'}" or next;
1731 + open $fd, "-|", "$GIT diff-tree -r $co{'parent'} $co{'id'}" or next;
1732 my @difftree = map { chomp; $_ } <$fd>;
1733 close $fd or next;
1734 print "<item>\n" .
@@ -1820,7 +1816,7 @@ sub git_log {
1816 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
1817
1818 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1823 - open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
1819 + open my $fd, "-|", "$GIT rev-list $limit $hash" or die_error(undef, "Open failed.");
1820 my (@revlist) = map { chomp; $_ } <$fd>;
1821 close $fd;
1822
@@ -1911,7 +1907,7 @@ sub git_commit {
1907 $root = " --root";
1908 $parent = "";
1909 }
1914 - open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
1910 + open my $fd, "-|", "$GIT diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
1911 @difftree = map { chomp; $_ } <$fd>;
1912 close $fd or die_error(undef, "Reading diff-tree failed.");
1913
@@ -2153,7 +2149,7 @@ sub git_commitdiff {
2149 if (!defined $hash_parent) {
2150 $hash_parent = $co{'parent'};
2151 }
2156 - open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2152 + open my $fd, "-|", "$GIT diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2153 my (@difftree) = map { chomp; $_ } <$fd>;
2154 close $fd or die_error(undef, "Reading diff-tree failed.");
2155
@@ -2243,14 +2239,14 @@ sub git_commitdiff {
2239
2240 sub git_commitdiff_plain {
2241 mkdir($git_temp, 0700);
2246 - open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2242 + open my $fd, "-|", "$GIT diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2243 my (@difftree) = map { chomp; $_ } <$fd>;
2244 close $fd or die_error(undef, "Reading diff-tree failed.");
2245
2246 # try to figure out the next tag after this commit
2247 my $tagname;
2248 my $refs = read_info_ref("tags");
2253 - open $fd, "-|", "$gitbin/git-rev-list HEAD";
2249 + open $fd, "-|", "$GIT rev-list HEAD";
2250 chomp (my (@commits) = <$fd>);
2251 close $fd;
2252 foreach my $commit (@commits) {
@@ -2321,7 +2317,7 @@ sub git_history {
2317 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b><br/></div>\n";
2318
2319 open my $fd, "-|",
2324 - "$gitbin/git-rev-list --full-history $hash -- \'$file_name\'";
2320 + "$GIT rev-list --full-history $hash -- \'$file_name\'";
2321 print "<table cellspacing=\"0\">\n";
2322 my $alternate = 0;
2323 while (my $line = <$fd>) {
@@ -2408,7 +2404,7 @@ sub git_search {
2404 my $alternate = 0;
2405 if ($commit_search) {
2406 $/ = "\0";
2411 - open my $fd, "-|", "$gitbin/git-rev-list --header --parents $hash" or next;
2407 + open my $fd, "-|", "$GIT rev-list --header --parents $hash" or next;
2408 while (my $commit_text = <$fd>) {
2409 if (!grep m/$searchtext/i, $commit_text) {
2410 next;
@@ -2458,7 +2454,7 @@ sub git_search {
2454
2455 if ($pickaxe_search) {
2456 $/ = "\n";
2461 - open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S\'$searchtext\'";
2457 + open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2458 undef %co;
2459 my @files;
2460 while (my $line = <$fd>) {
@@ -2529,7 +2525,7 @@ sub git_shortlog {
2525 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
2526
2527 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2532 - open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
2528 + open my $fd, "-|", "$GIT rev-list $limit $hash" or die_error(undef, "Open failed.");
2529 my (@revlist) = map { chomp; $_ } <$fd>;
2530 close $fd;
2531