@matheusbsbs / matheus-bagliano / commits / 25691fbe6d
Fork 0

gitweb: Use --git-dir parameter instead of setting $ENV{'GIT_DIR'}

This makes it possible to run gitweb under mod_perl's Apache::Registry. It needs a fairly new git version, with --git-dir=<path> parameter to git wrapper, i.e. post v1.4.2-rc2-g6acbcb9 version. Signed-off-by: Dennis Stosberg <dennis@stosberg.net> Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Dennis Stosberg committed Aug 28, 2006 at 17:49 UTC 25691fbe6d02135d55dfc3a5180e29890dce1521
1 file changed +54 -36
gitweb/gitweb.perl
+54 -36
@@ -157,6 +157,9 @@ require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
157 # version of the core git binary
158 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
159
160 +# path to the current git repository
161 +our $git_dir;
162 +
163 $projects_list ||= $projectroot;
164
165 # ======================================================================
@@ -184,7 +187,7 @@ if (defined $project) {
187 if (!(-e "$projectroot/$project/HEAD")) {
188 die_error(undef, "No such project");
189 }
187 - $ENV{'GIT_DIR'} = "$projectroot/$project";
190 + $git_dir = "$projectroot/$project";
191 }
192
193 our $file_name = $cgi->param('f');
@@ -572,21 +575,31 @@ sub format_diff_line {
575 ## ----------------------------------------------------------------------
576 ## git utility subroutines, invoking git commands
577
578 +# returns path to the core git executable and the --git-dir parameter as list
579 +sub git_cmd {
580 + return $GIT, '--git-dir='.$git_dir;
581 +}
582 +
583 +# returns path to the core git executable and the --git-dir parameter as string
584 +sub git_cmd_str {
585 + return join(' ', git_cmd());
586 +}
587 +
588 # get HEAD ref of given project as hash
589 sub git_get_head_hash {
590 my $project = shift;
578 - my $oENV = $ENV{'GIT_DIR'};
591 + my $o_git_dir = $git_dir;
592 my $retval = undef;
580 - $ENV{'GIT_DIR'} = "$projectroot/$project";
581 - if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
593 + $git_dir = "$projectroot/$project";
594 + if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
595 my $head = <$fd>;
596 close $fd;
597 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
598 $retval = $1;
599 }
600 }
588 - if (defined $oENV) {
589 - $ENV{'GIT_DIR'} = $oENV;
601 + if (defined $o_git_dir) {
602 + $git_dir = $o_git_dir;
603 }
604 return $retval;
605 }
@@ -595,7 +608,7 @@ sub git_get_head_hash {
608 sub git_get_type {
609 my $hash = shift;
610
598 - open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
611 + open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
612 my $type = <$fd>;
613 close $fd or return;
614 chomp $type;
@@ -609,7 +622,7 @@ sub git_get_project_config {
622 $key =~ s/^gitweb\.//;
623 return if ($key =~ m/\W/);
624
612 - my @x = ($GIT, 'repo-config');
625 + my @x = (git_cmd(), 'repo-config');
626 if (defined $type) { push @x, $type; }
627 push @x, "--get";
628 push @x, "gitweb.$key";
@@ -625,7 +638,7 @@ sub git_get_hash_by_path {
638
639 my $tree = $base;
640
628 - open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
641 + open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
642 or die_error(undef, "Open git-ls-tree failed");
643 my $line = <$fd>;
644 close $fd or return undef;
@@ -756,7 +769,7 @@ sub git_get_references {
769 open $fd, "$projectroot/$project/info/refs"
770 or return;
771 } else {
759 - open $fd, "-|", $GIT, "ls-remote", "."
772 + open $fd, "-|", git_cmd(), "ls-remote", "."
773 or return;
774 }
775
@@ -777,7 +790,7 @@ sub git_get_references {
790 sub git_get_rev_name_tags {
791 my $hash = shift || return undef;
792
780 - open my $fd, "-|", $GIT, "name-rev", "--tags", $hash
793 + open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
794 or return;
795 my $name_rev = <$fd>;
796 close $fd;
@@ -825,7 +838,7 @@ sub parse_tag {
838 my %tag;
839 my @comment;
840
828 - open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
841 + open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
842 $tag{'id'} = $tag_id;
843 while (my $line = <$fd>) {
844 chomp $line;
@@ -866,7 +879,7 @@ sub parse_commit {
879 @commit_lines = @$commit_text;
880 } else {
881 $/ = "\0";
869 - open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
882 + open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
883 or return;
884 @commit_lines = split '\n', <$fd>;
885 close $fd or return;
@@ -1935,7 +1948,7 @@ sub git_project_list {
1948 if (!defined $head) {
1949 next;
1950 }
1938 - $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1951 + $git_dir = "$projectroot/$pr->{'path'}";
1952 my %co = parse_commit($head);
1953 if (!%co) {
1954 next;
@@ -2054,7 +2067,8 @@ sub git_summary {
2067 }
2068 print "</table>\n";
2069
2057 - open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2070 + open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2071 + git_get_head_hash($project)
2072 or die_error(undef, "Open git-rev-list failed");
2073 my @revlist = map { chomp; $_ } <$fd>;
2074 close $fd;
@@ -2132,7 +2146,7 @@ sub git_blame2 {
2146 if ($ftype !~ "blob") {
2147 die_error("400 Bad Request", "Object is not a blob");
2148 }
2135 - open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2149 + open ($fd, "-|", git_cmd(), "blame", '-l', $file_name, $hash_base)
2150 or die_error(undef, "Open git-blame failed");
2151 git_header_html();
2152 my $formats_nav =
@@ -2197,7 +2211,7 @@ sub git_blame {
2211 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2212 or die_error(undef, "Error lookup file");
2213 }
2200 - open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2214 + open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2215 or die_error(undef, "Open git-annotate failed");
2216 git_header_html();
2217 my $formats_nav =
@@ -2318,7 +2332,7 @@ sub git_blob_plain {
2332 }
2333 }
2334 my $type = shift;
2321 - open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2335 + open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2336 or die_error(undef, "Couldn't cat $file_name, $hash");
2337
2338 $type ||= blob_mimetype($fd, $file_name);
@@ -2360,7 +2374,7 @@ sub git_blob {
2374 }
2375 }
2376 my $have_blame = gitweb_check_feature('blame');
2363 - open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2377 + open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2378 or die_error(undef, "Couldn't cat $file_name, $hash");
2379 my $mimetype = blob_mimetype($fd, $file_name);
2380 if ($mimetype !~ m/^text\//) {
@@ -2425,7 +2439,7 @@ sub git_tree {
2439 }
2440 }
2441 $/ = "\0";
2428 - open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2442 + open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2443 or die_error(undef, "Open git-ls-tree failed");
2444 my @entries = map { chomp; $_ } <$fd>;
2445 close $fd or die_error(undef, "Reading tree failed");
@@ -2528,7 +2542,8 @@ sub git_snapshot {
2542 -content_disposition => "inline; filename=\"$filename\"",
2543 -status => '200 OK');
2544
2531 - open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2545 + my $git_command = git_cmd_str();
2546 + open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2547 die_error(undef, "Execute git-tar-tree failed.");
2548 binmode STDOUT, ':raw';
2549 print <$fd>;
@@ -2548,7 +2563,7 @@ sub git_log {
2563 my $refs = git_get_references();
2564
2565 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2551 - open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2566 + open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2567 or die_error(undef, "Open git-rev-list failed");
2568 my @revlist = map { chomp; $_ } <$fd>;
2569 close $fd;
@@ -2603,7 +2618,7 @@ sub git_commit {
2618 if (!defined $parent) {
2619 $parent = "--root";
2620 }
2606 - open my $fd, "-|", $GIT, "diff-tree", '-r', @diff_opts, $parent, $hash
2621 + open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2622 or die_error(undef, "Open git-diff-tree failed");
2623 my @difftree = map { chomp; $_ } <$fd>;
2624 close $fd or die_error(undef, "Reading git-diff-tree failed");
@@ -2710,7 +2725,7 @@ sub git_blobdiff {
2725 if (defined $hash_base && defined $hash_parent_base) {
2726 if (defined $file_name) {
2727 # read raw output
2713 - open $fd, "-|", $GIT, "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
2728 + open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
2729 "--", $file_name
2730 or die_error(undef, "Open git-diff-tree failed");
2731 @difftree = map { chomp; $_ } <$fd>;
@@ -2728,7 +2743,7 @@ sub git_blobdiff {
2743 # try to find filename from $hash
2744
2745 # read filtered raw output
2731 - open $fd, "-|", $GIT, "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
2746 + open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
2747 or die_error(undef, "Open git-diff-tree failed");
2748 @difftree =
2749 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
@@ -2762,7 +2777,7 @@ sub git_blobdiff {
2777 }
2778
2779 # open patch output
2765 - open $fd, "-|", $GIT, "diff-tree", '-r', @diff_opts,
2780 + open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2781 '-p', $hash_parent_base, $hash_base,
2782 "--", $file_name
2783 or die_error(undef, "Open git-diff-tree failed");
@@ -2798,7 +2813,7 @@ sub git_blobdiff {
2813 }
2814
2815 # open patch output
2801 - open $fd, "-|", $GIT, "diff", '-p', @diff_opts, $hash_parent, $hash
2816 + open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
2817 or die_error(undef, "Open git-diff failed");
2818 } else {
2819 die_error('404 Not Found', "Missing one of the blob diff parameters")
@@ -2883,7 +2898,7 @@ sub git_commitdiff {
2898 my $fd;
2899 my @difftree;
2900 if ($format eq 'html') {
2886 - open $fd, "-|", $GIT, "diff-tree", '-r', @diff_opts,
2901 + open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2902 "--patch-with-raw", "--full-index", $hash_parent, $hash
2903 or die_error(undef, "Open git-diff-tree failed");
2904
@@ -2894,7 +2909,7 @@ sub git_commitdiff {
2909 }
2910
2911 } elsif ($format eq 'plain') {
2897 - open $fd, "-|", $GIT, "diff-tree", '-r', @diff_opts,
2912 + open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2913 '-p', $hash_parent, $hash
2914 or die_error(undef, "Open git-diff-tree failed");
2915
@@ -2994,7 +3009,8 @@ sub git_history {
3009 git_print_page_path($file_name, $ftype, $hash_base);
3010
3011 open my $fd, "-|",
2997 - $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
3012 + git_cmd(), "rev-list", "--full-history", $hash_base, "--", $file_name;
3013 +
3014 git_history_body($fd, $refs, $hash_base, $ftype);
3015
3016 close $fd;
@@ -3034,7 +3050,7 @@ sub git_search {
3050 my $alternate = 0;
3051 if ($commit_search) {
3052 $/ = "\0";
3037 - open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
3053 + open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3054 while (my $commit_text = <$fd>) {
3055 if (!grep m/$searchtext/i, $commit_text) {
3056 next;
@@ -3086,7 +3102,9 @@ sub git_search {
3102
3103 if ($pickaxe_search) {
3104 $/ = "\n";
3089 - open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
3105 + my $git_command = git_cmd_str();
3106 + open my $fd, "-|", "$git_command rev-list $hash | " .
3107 + "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3108 undef %co;
3109 my @files;
3110 while (my $line = <$fd>) {
@@ -3153,7 +3171,7 @@ sub git_shortlog {
3171 my $refs = git_get_references();
3172
3173 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3156 - open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3174 + open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3175 or die_error(undef, "Open git-rev-list failed");
3176 my @revlist = map { chomp; $_ } <$fd>;
3177 close $fd;
@@ -3181,7 +3199,7 @@ sub git_shortlog {
3199
3200 sub git_rss {
3201 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3184 - open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3202 + open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3203 or die_error(undef, "Open git-rev-list failed");
3204 my @revlist = map { chomp; $_ } <$fd>;
3205 close $fd or die_error(undef, "Reading git-rev-list failed");
@@ -3204,7 +3222,7 @@ XML
3222 last;
3223 }
3224 my %cd = parse_date($co{'committer_epoch'});
3207 - open $fd, "-|", $GIT, "diff-tree", '-r', @diff_opts,
3225 + open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3226 $co{'parent'}, $co{'id'}
3227 or next;
3228 my @difftree = map { chomp; $_ } <$fd>;
@@ -3262,7 +3280,7 @@ XML
3280 if (!defined $head) {
3281 next;
3282 }
3265 - $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3283 + $git_dir = "$projectroot/$proj{'path'}";
3284 my %co = parse_commit($head);
3285 if (!%co) {
3286 next;