]> Lady’s Gitweb - Gitweb/commitdiff
gitweb: Add git_get_{following,preceding}_references functions
authorJakub Narebski <redacted>
Thu, 24 Aug 2006 17:37:04 +0000 (19:37 +0200)
committerLady <redacted>
Mon, 6 Apr 2026 04:07:10 +0000 (00:07 -0400)
Adds git_get_following_references function, based on code which was
used in git_commitdiff_plain to generate X-Git-Tag: header,
and companion git_get_preceding_references function.

Both functions return array of all references of given type (as
returned by git_get_references) following/preceding given commit in
array (list) context, and last following/first preceding ref in scalar
context.

Stripping ref (list of refs) of "$type/" (e.g. "tags/") is left to
caller.

Signed-off-by: Jakub Narebski <redacted>
Signed-off-by: Junio C Hamano <redacted>
gitweb.perl

index dbb25a9c7b93644b1a36bd9b30d1a7de3a514e0dbb86a50545027e63c8c4fb59..d227f81d3818a82b08bb2a80310261c8416f4f0e679217d265a1169c65a7e740 100755 (executable)
@@ -749,6 +749,58 @@ 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;
+       my $firstref;
+
+       foreach my $commit (@commits) {
+               foreach my $ref (@{$refs->{$commit}}) {
+                       $firstref = $ref unless $firstref;
+                       push @reflist, $ref;
+               }
+       }
+
+       return wantarray ? @reflist : $firstref;
+}
+
 ## ----------------------------------------------------------------------
 ## parse to hash functions
 
This page took 0.208965 seconds and 4 git commands to generate.