@matheusbsbs / matheus-bagliano / commits / ca94601c8f
Fork 0

gitweb: Add generic git_object subroutine to display object of any type

Add generic "object" view implemented in git_object subroutine, which is used to display object of any type; to be more exact it redirects to the view of correct type: "blob", "tree", "commit" or "tag". To identify object you have to provide either hash (identifier of an object), or (in the case of tree and blob objects) hash of commit object (hash_base) and path (file_name). Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jakub Narebski committed Dec 10, 2006 at 13:25 UTC ca94601c8fcc4ddda4b48f05c748b3d52c54809c
1 file changed +48
gitweb/gitweb.perl
+48
@@ -434,6 +434,7 @@ my %actions = (
434 "tags" => \&git_tags,
435 "tree" => \&git_tree,
436 "snapshot" => \&git_snapshot,
437 + "object" => \&git_object,
438 # those below don't need $project
439 "opml" => \&git_opml,
440 "project_list" => \&git_project_list,
@@ -3619,6 +3620,53 @@ sub git_commit {
3620 git_footer_html();
3621 }
3622
3623 +sub git_object {
3624 + # object is defined by:
3625 + # - hash or hash_base alone
3626 + # - hash_base and file_name
3627 + my $type;
3628 +
3629 + # - hash or hash_base alone
3630 + if ($hash || ($hash_base && !defined $file_name)) {
3631 + my $object_id = $hash || $hash_base;
3632 +
3633 + my $git_command = git_cmd_str();
3634 + open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
3635 + or die_error('404 Not Found', "Object does not exist");
3636 + $type = <$fd>;
3637 + chomp $type;
3638 + close $fd
3639 + or die_error('404 Not Found', "Object does not exist");
3640 +
3641 + # - hash_base and file_name
3642 + } elsif ($hash_base && defined $file_name) {
3643 + $file_name =~ s,/+$,,;
3644 +
3645 + system(git_cmd(), "cat-file", '-e', $hash_base) == 0
3646 + or die_error('404 Not Found', "Base object does not exist");
3647 +
3648 + # here errors should not hapen
3649 + open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
3650 + or die_error(undef, "Open git-ls-tree failed");
3651 + my $line = <$fd>;
3652 + close $fd;
3653 +
3654 + #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3655 + unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
3656 + die_error('404 Not Found', "File or directory for given base does not exist");
3657 + }
3658 + $type = $2;
3659 + $hash = $3;
3660 + } else {
3661 + die_error('404 Not Found', "Not enough information to find object");
3662 + }
3663 +
3664 + print $cgi->redirect(-uri => href(action=>$type, -full=>1,
3665 + hash=>$hash, hash_base=>$hash_base,
3666 + file_name=>$file_name),
3667 + -status => '302 Found');
3668 +}
3669 +
3670 sub git_blobdiff {
3671 my $format = shift || 'html';
3672