git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] gitweb: Add generic git_object subroutine to display object of any type

From
Jakub Narebski <jnareb@gmail.com>
Date
Dec 10, 2006, 12:25 UTC
Message-ID
<11657536121090-git-send-email-jnareb@gmail.com>
In-Reply-To
<7vk616ezu5.fsf@assigned-by-dhcp.cox.net>

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>
---
First version had checking if $hash and $hash_base are explicit SHA-1
if are defined.  This version doesn't have this check, which is
important for next patch.
 gitweb/gitweb.perl |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 0c2cfc7..a988f85 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -434,6 +434,7 @@ my %actions = (
 	"tags" => \&git_tags,
 	"tree" => \&git_tree,
 	"snapshot" => \&git_snapshot,
+	"object" => \&git_object,
 	# those below don't need $project
 	"opml" => \&git_opml,
 	"project_list" => \&git_project_list,
@@ -3620,6 +3621,53 @@ sub git_commit {
 	git_footer_html();
 }
 
+sub git_object {
+	# object is defined by:
+	# - hash or hash_base alone
+	# - hash_base and file_name
+	my $type;
+
+	# - hash or hash_base alone
+	if ($hash || ($hash_base && !defined $file_name)) {
+		my $object_id = $hash || $hash_base;
+
+		my $git_command = git_cmd_str();
+		open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
+			or die_error('404 Not Found', "Object does not exist");
+		$type = <$fd>;
+		chomp $type;
+		close $fd
+			or die_error('404 Not Found', "Object does not exist");
+
+	# - hash_base and file_name
+	} elsif ($hash_base && defined $file_name) {
+		$file_name =~ s,/+$,,;
+
+		system(git_cmd(), "cat-file", '-e', $hash_base) == 0
+			or die_error('404 Not Found', "Base object does not exist");
+
+		# here errors should not hapen
+		open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
+			or die_error(undef, "Open git-ls-tree failed");
+		my $line = <$fd>;
+		close $fd;
+
+		#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa	panic.c'
+		unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
+			die_error('404 Not Found', "File or directory for given base does not exist");
+		}
+		$type = $2;
+		$hash = $3;
+	} else {
+		die_error('404 Not Found', "Not enough information to find object");
+	}
+
+	print $cgi->redirect(-uri => href(action=>$type, -full=>1,
+	                                  hash=>$hash, hash_base=>$hash_base,
+	                                  file_name=>$file_name),
+	                     -status => '302 Found');
+}
+
 sub git_blobdiff {
 	my $format = shift || 'html';
 
-- 
1.4.4.1
Previous: Jakub NarebskiNext: Jakub Narebski
Message 9 of 12 in “gitweb: Better symbolic link support in "tree" view”
  1. gitweb: Better symbolic link support in "tree" viewJakub Narebsmi, Dec 4, 2006
  2. Junio C HamanoDec 5, 2006
  3. Jakub NarebskiDec 5, 2006
  4. Junio C HamanoDec 5, 2006
  5. Jakub NarebskiDec 5, 2006
  6. 0/3 gitweb: Better symbolic link support in "tree" viewJakub Narebski, Dec 10, 2006
  7. 0/3 gitweb: Better symbolic link support in "tree" viewJakub Narebski, Dec 10, 2006
  8. 1/3 gitweb: Show target of symbolic link in "tree" viewJakub Narebski, Dec 10, 2006
  9. 2/3 gitweb: Add generic git_object subroutine to display object of any typeJakub Narebski, Dec 10, 2006
  10. 3/3 gitweb: Hyperlink target of symbolic link in "tree" view (if possible)Jakub Narebski, Dec 10, 2006
  11. 4/3 gitweb: SHA-1 in commit log message links to "object" viewJakub Narebski, Dec 10, 2006
  12. Junio C HamanoDec 10, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.