gitweb: Faster return from git_get_preceding_references if possible
[gitweb.git] / gitweb / gitweb.perl
index fe9b9ee87a94913955dbb18ef5fba007c25dbcfa..01452d2c747bb29540d512ece6887c91b2ef040e 100755 (executable)
@@ -538,7 +538,6 @@ sub format_diff_line {
        } elsif ($char eq "@") {
                $diff_class = " chunk_header";
        } elsif ($char eq "\\") {
-               # skip errors (incomplete lines)
                $diff_class = " incomplete";
        }
        $line = untabify($line);
@@ -750,6 +749,57 @@ sub git_get_references {
        return \%refs;
 }
 
+sub git_get_following_references {
+       my $hash = shift || return undef;
+       my $type = shift;
+       my $base = shift || $hash_base || "HEAD";
+
+       my $refs = git_get_references($type);
+       open my $fd, "-|", $GIT, "rev-list", $base
+               or return undef;
+       my @commits = map { chomp; $_ } <$fd>;
+       close $fd
+               or return undef;
+
+       my @reflist;
+       my $lastref;
+
+       foreach my $commit (@commits) {
+               foreach my $ref (@{$refs->{$commit}}) {
+                       $lastref = $ref;
+                       push @reflist, $lastref;
+               }
+               if ($commit eq $hash) {
+                       last;
+               }
+       }
+
+       return wantarray ? @reflist : $lastref;
+}
+
+sub git_get_preceding_references {
+       my $hash = shift || return undef;
+       my $type = shift;
+
+       my $refs = git_get_references($type);
+       open my $fd, "-|", $GIT, "rev-list", $hash
+               or return undef;
+       my @commits = map { chomp; $_ } <$fd>;
+       close $fd
+               or return undef;
+
+       my @reflist;
+
+       foreach my $commit (@commits) {
+               foreach my $ref (@{$refs->{$commit}}) {
+                       return $ref unless wantarray;
+                       push @reflist, $ref;
+               }
+       }
+
+       return @reflist;
+}
+
 ## ----------------------------------------------------------------------
 ## parse to hash functions
 
@@ -1540,7 +1590,7 @@ sub git_difftree_body {
 }
 
 sub git_patchset_body {
-       my ($patchset, $difftree, $hash, $hash_parent) = @_;
+       my ($fd, $difftree, $hash, $hash_parent) = @_;
 
        my $patch_idx = 0;
        my $in_header = 0;
@@ -1549,7 +1599,9 @@ sub git_patchset_body {
 
        print "<div class=\"patchset\">\n";
 
-       LINE: foreach my $patch_line (@$patchset) {
+       LINE:
+       while (my $patch_line @$fd>) {
+               chomp $patch_line;
 
                if ($patch_line =~ m/^diff /) { # "git diff" header
                        # beginning of patch (in patchset)
@@ -2728,7 +2780,6 @@ sub git_commitdiff {
        # read commitdiff
        my $fd;
        my @difftree;
-       my @patchset;
        if ($format eq 'html') {
                open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
                        "--patch-with-raw", "--full-index", $hash_parent, $hash
@@ -2739,13 +2790,11 @@ sub git_commitdiff {
                        last unless $line;
                        push @difftree, $line;
                }
-               @patchset = map { chomp; $_ } <$fd>;
 
-               close $fd
-                       or die_error(undef, "Reading git-diff-tree failed");
        } elsif ($format eq 'plain') {
                open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
                        or die_error(undef, "Open git-diff-tree failed");
+
        } else {
                die_error(undef, "Unknown commitdiff format");
        }
@@ -2807,8 +2856,8 @@ sub git_commitdiff {
                #git_difftree_body(\@difftree, $hash, $hash_parent);
                #print "<br/>\n";
 
-               git_patchset_body(\@patchset, \@difftree, $hash, $hash_parent);
-
+               git_patchset_body($fd, \@difftree, $hash, $hash_parent);
+               close $fd;
                print "</div>\n"; # class="page_body"
                git_footer_html();