@matheusbsbs / matheus-bagliano / commits / 470b96d483
Fork 0

gitweb: Add git_get_{following,preceding}_references functions

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 <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Aug 24, 2006 at 19:37 UTC 470b96d4837c5b019f15c8a5b013501724236de4
1 file changed +52
gitweb/gitweb.perl
+52
@@ -749,6 +749,58 @@ sub git_get_references {
749 return \%refs;
750 }
751
752 +sub git_get_following_references {
753 + my $hash = shift || return undef;
754 + my $type = shift;
755 + my $base = shift || $hash_base || "HEAD";
756 +
757 + my $refs = git_get_references($type);
758 + open my $fd, "-|", $GIT, "rev-list", $base
759 + or return undef;
760 + my @commits = map { chomp; $_ } <$fd>;
761 + close $fd
762 + or return undef;
763 +
764 + my @reflist;
765 + my $lastref;
766 +
767 + foreach my $commit (@commits) {
768 + foreach my $ref (@{$refs->{$commit}}) {
769 + $lastref = $ref;
770 + push @reflist, $lastref;
771 + }
772 + if ($commit eq $hash) {
773 + last;
774 + }
775 + }
776 +
777 + return wantarray ? @reflist : $lastref;
778 +}
779 +
780 +sub git_get_preceding_references {
781 + my $hash = shift || return undef;
782 + my $type = shift;
783 +
784 + my $refs = git_get_references($type);
785 + open my $fd, "-|", $GIT, "rev-list", $hash
786 + or return undef;
787 + my @commits = map { chomp; $_ } <$fd>;
788 + close $fd
789 + or return undef;
790 +
791 + my @reflist;
792 + my $firstref;
793 +
794 + foreach my $commit (@commits) {
795 + foreach my $ref (@{$refs->{$commit}}) {
796 + $firstref = $ref unless $firstref;
797 + push @reflist, $ref;
798 + }
799 + }
800 +
801 + return wantarray ? @reflist : $firstref;
802 +}
803 +
804 ## ----------------------------------------------------------------------
805 ## parse to hash functions
806