From: Jakub Narebski Date: Thu, 24 Aug 2006 17:37:04 GMT Subject: [PATCH 6] gitweb: Add git_get_{following,preceding}_references functions Message-ID: <200608241937.04849.jnareb@gmail.com> In-Reply-To: <200608240015.15071.jnareb@gmail.com> 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 --- Junio C Hamano wrote: >>  * "commitdiff_plain" now only generates X-Git-Tag: line only if >> there is tag pointing to the current commit. > > Hmph... > >>    ...; besides we are >>    interested rather in tags _preceding_ the commit, and _heads_ >>    following the commit. > > Interesting observation.  When somebody says "feature X was > introduced in such and such commit", people would want to know (1) > the point release they are using has the feature -- which means you > would want to know the earliest tag that comes after the commit, or > (2) if the branch they are working on already has that > feature -- which again means if the head follows the commit.  So > I am not sure when preceding tag is interesting... Actually, the two following patches adresses this, not this patch... gitweb/gitweb.perl | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 52 insertions(+), 0 deletions(-) diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index d72b19a..2d3776a 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -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 -- 1.4.1.1