]> Lady’s Gitweb - Gitweb/blobdiff - gitweb.perl
gitweb: Added parse_difftree_raw_line function for later use
[Gitweb] / gitweb.perl
index fdbadd7204362e027fae3808927535de096cf63d9c5877694b2010df43bf250b..9a0859bbf308deecca7d0c2eab1c5594d84e34fae21f989feecc471120803c7e 100755 (executable)
@@ -67,6 +67,68 @@ our $default_text_plain_charset  = undef;
 # (relative to the current git repository)
 our $mimetypes_file = undef;
 
+# You define site-wide feature defaults here; override them with
+# $GITWEB_CONFIG as necessary.
+our %feature =
+(
+
+# feature  => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
+
+'blame'         => {'sub' => \&feature_blame, 'override' => 0, 'default' => [0]},
+'snapshot'      => {'sub' => \&feature_snapshot, 'override' => 0, 'default' => ['x-gzip', 'gz', 'gzip']},
+
+);
+
+sub gitweb_check_feature {
+       my ($name) = @_;
+       return undef unless exists $feature{$name};
+       my ($sub, $override, @defaults) = ($feature{$name}{'sub'},
+                                               $feature{$name}{'override'},
+                                               @{$feature{$name}{'default'}});
+       if (!$override) { return @defaults; }
+       return $sub->(@defaults);
+}
+
+# To enable system wide have in $GITWEB_CONFIG
+# $feature{'blame'}{'default'} =  [1];
+# To have project specific config enable override in  $GITWEB_CONFIG
+# $feature{'blame'}{'override'} =  1;
+# and in project config gitweb.blame = 0|1;
+
+sub feature_blame {
+       my ($val) = git_get_project_config('blame', '--bool');
+
+       if ($val eq 'true') {
+               return 1;
+       } elsif ($val eq 'false') {
+               return 0;
+       }
+
+       return $_[0];
+}
+
+# To disable system wide have in $GITWEB_CONFIG
+# $feature{'snapshot'}{'default'} =  [undef];
+# To have project specific config enable override in  $GITWEB_CONFIG
+# $feature{'blame'}{'override'} =  1;
+# and in project config  gitweb.snapshot = none|gzip|bzip2
+
+sub feature_snapshot {
+       my ($ctype, $suffix, $command) = @_;
+
+       my ($val) = git_get_project_config('snapshot');
+
+       if ($val eq 'gzip') {
+               return ('x-gzip', 'gz', 'gzip');
+       } elsif ($val eq 'bzip2') {
+               return ('x-bzip2', 'bz2', 'bzip2');
+       } elsif ($val eq 'none') {
+               return ();
+       }
+
+       return ($ctype, $suffix, $command);
+}
+
 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
 
@@ -485,24 +547,21 @@ sub git_get_type {
 }
 
 sub git_get_project_config {
-       my $key = shift;
+       my ($key, $type) = @_;
 
        return unless ($key);
        $key =~ s/^gitweb\.//;
        return if ($key =~ m/\W/);
 
-       my $val = qx($GIT repo-config --get gitweb.$key);
+       my @x = ($GIT, 'repo-config');
+       if (defined $type) { push @x, $type; }
+       push @x, "--get";
+       push @x, "gitweb.$key";
+       my $val = qx(@x);
+       chomp $val;
        return ($val);
 }
 
-sub git_get_project_config_bool {
-       my $val = git_get_project_config (@_);
-       if ($val and $val =~ m/true|yes|on/) {
-               return (1);
-       }
-       return; # implicit false
-}
-
 # get hash of given path at given ref
 sub git_get_hash_by_path {
        my $base = shift;
@@ -859,6 +918,33 @@ sub parse_ref {
        return %ref_item;
 }
 
+sub parse_difftree_raw_line {
+       my $line = shift;
+       my %res;
+
+       # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
+       # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
+       if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
+               $res{'from_mode'} = $1;
+               $res{'to_mode'} = $2;
+               $res{'from_id'} = $3;
+               $res{'to_id'} = $4;
+               $res{'status'} = $5;
+               $res{'similarity'} = $6;
+               if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
+                       ($res{'from_file'}, $res{'to_file'}) = map(unquote, split("\t", $7));
+               } else {
+                       $res{'file'} = unquote($7);
+               }
+       }
+       # 'c512b523472485aef4fff9e57b229d9d243c967f'
+       #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
+       #       $res{'commit'} = $1;
+       #}
+
+       return wantarray ? %res : \%res;
+}
+
 ## ......................................................................
 ## parse to array of hashes functions
 
@@ -1397,7 +1483,10 @@ sub git_difftree_body {
 sub git_shortlog_body {
        # uses global variable $project
        my ($revlist, $from, $to, $refs, $extra) = @_;
-       my $have_snapshot = git_get_project_config_bool('snapshot');
+
+       my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
+       my $have_snapshot = (defined $ctype && defined $suffix);
+
        $from = 0 unless defined $from;
        $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
 
@@ -1858,7 +1947,10 @@ sub git_tag {
 sub git_blame2 {
        my $fd;
        my $ftype;
-       die_error(undef, "Permission denied") if (!git_get_project_config_bool ('blame'));
+
+       if (!gitweb_check_feature('blame')) {
+               die_error('403 Permission denied', "Permission denied");
+       }
        die_error('404 Not Found', "File name not defined") if (!$file_name);
        $hash_base ||= git_get_head_hash($project);
        die_error(undef, "Couldn't find base commit") unless ($hash_base);
@@ -1916,7 +2008,10 @@ sub git_blame2 {
 
 sub git_blame {
        my $fd;
-       die_error('403 Permission denied', "Permission denied") if (!git_get_project_config_bool ('blame'));
+
+       if (!gitweb_check_feature('blame')) {
+               die_error('403 Permission denied', "Permission denied");
+       }
        die_error('404 Not Found', "File name not defined") if (!$file_name);
        $hash_base ||= git_get_head_hash($project);
        die_error(undef, "Couldn't find base commit") unless ($hash_base);
@@ -2069,7 +2164,7 @@ sub git_blob {
                        die_error(undef, "No file name defined");
                }
        }
-       my $have_blame = git_get_project_config_bool ('blame');
+       my $have_blame = gitweb_check_feature('blame');
        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
                or die_error(undef, "Couldn't cat $file_name, $hash");
        my $mimetype = blob_mimetype($fd, $file_name);
@@ -2134,7 +2229,7 @@ sub git_tree {
        git_header_html();
        my %base_key = ();
        my $base = "";
-       my $have_blame = git_get_project_config_bool ('blame');
+       my $have_blame = gitweb_check_feature('blame');
        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
                $base_key{hash_base} = $hash_base;
                git_print_page_nav('tree','', $hash_base);
@@ -2195,25 +2290,31 @@ sub git_tree {
 
 sub git_snapshot {
 
+       my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
+       my $have_snapshot = (defined $ctype && defined $suffix);
+       if (!$have_snapshot) {
+               die_error('403 Permission denied', "Permission denied");
+       }
+
        if (!defined $hash) {
                $hash = git_get_head_hash($project);
        }
 
-       my $filename = basename($project) . "-$hash.tar.gz";
+       my $filename = basename($project) . "-$hash.tar.$suffix";
 
        print $cgi->header(-type => 'application/x-tar',
-                       -content-encoding => 'gzip',
-                       '-content-disposition' => "inline; filename=\"$filename\"",
-                       -status => '200 OK');
+                          -content-encoding => $ctype,
+                          '-content-disposition' =>
+                          "inline; filename=\"$filename\"",
+                          -status => '200 OK');
 
-       open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | gzip" or
-                               die_error(undef, "Execute git-tar-tree failed.");
+       open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
+               die_error(undef, "Execute git-tar-tree failed.");
        binmode STDOUT, ':raw';
        print <$fd>;
        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
        close $fd;
 
-
 }
 
 sub git_log {
@@ -2293,7 +2394,10 @@ sub git_commit {
        }
        my $refs = git_get_references();
        my $ref = format_ref_marker($refs, $co{'id'});
-       my $have_snapshot = git_get_project_config_bool('snapshot');
+
+       my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
+       my $have_snapshot = (defined $ctype && defined $suffix);
+
        my $formats_nav = '';
        if (defined $file_name && defined $co{'parent'}) {
                my $parent = $co{'parent'};
This page took 0.372911 seconds and 4 git commands to generate.