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

Re: [PATCH 2/3 v2] add new Perl API: Git::Repo, Git::Commit, Git::Tag, and Git::RepoRoot

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 19, 2008, 01:32 UTC
Message-ID
<7v8wutkcx4.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<1219088389-14828-2-git-send-email-LeWiemann@gmail.com>
Lea Wiemann <lewiemann@gmail.com> writes:
> +=head1 DESCRIPTION
> +...
> +The author, committer and message methods return Unicoded strings,
Unicoded?
> +decoded according to the "encoding" header, with UTF-8 and then
> +Latin-1 as fallbacks.  (These Unicode strings can contain code points
> +...
> +decoding for you, so you should not normally need this method.
Anyway, nicely written description.
Show 79 quoted lines
> +=back
> +
> +=cut
> +
> +
> +sub tree {
> +	my $self = shift;
> +	$self->_load;
> +	return $self->{tree};
> +}
> +
> +sub parents {
> +	my $self = shift;
> +	$self->_load;
> +	return map { ref($self)->new($self->repo, $_) } @{$self->{parents}};
> +}
> +
> +sub author {
> +	my $self = shift;
> +	$self->_load;
> +	return $self->_decode($self->{author});
> +}
> +
> +sub committer {
> +	my $self = shift;
> +	$self->_load;
> +	return $self->_decode($self->{committer});
> +}
> +
> +sub message {
> +	my $self = shift;
> +	$self->_load;
> +	return $self->_decode($self->{message});
> +}
> +
> +sub encoding {
> +	my $self = shift;
> +	$self->_load;
> +	return $self->{encoding};
> +}
> +
> +# Auxiliary method to load (and parse) the commit object from the
> +# repository if it hasn't already been loaded.  Optional parameter:
> +# The raw contents of the commit object; the commit object will be
> +# retrieved from the repository if that parameter is not given.
> +sub _load {
> +	my ($self, $raw_text) = shift;
> +	return if exists $self->{message};  # already loaded
> +
> +	my $sha1 = $self->sha1;
> +	if (!defined $raw_text) {
> +		# Retrieve from the repository.
> +		(my $type, $raw_text) = $self->repo->get_object($sha1);
> +		die "$sha1 is a $type object (expected a commit object)"
> +		    unless $type eq 'commit';
> +	}
> +
> +	(my $header, $self->{message}) = split "\n\n", $raw_text, 2;
> +	# Parse header.
> +	for my $line (split "\n", $header) {
> +		local $/ = "\n"; # for chomp
> +		chomp($line);
> +		my ($key, $value) = split ' ', $line, 2;
> +		if ($key eq 'tree') {
> +			$self->{tree} = $value;
> +		} elsif ($key eq 'parent') {
> +			push @{$self->{parents}}, $value;
> +		} elsif ($key eq 'author') {
> +			$self->{author} = $value;
> +		} elsif ($key eq 'committer') {
> +			$self->{committer} = $value;
> +		} elsif ($key eq 'encoding') {
> +			$self->{encoding} = $value;
> +		} else {
> +			# Ignore unrecognized header lines.
> +		}
> +	}
> +	undef;
> +}

Aside from seeming repetitive and quite similar to Git::Tag::_load(), I have to wonder how parent rewriting and grafts come into the picture. Git::Repo::get_object is just a cat-file which means you are ignoring any grafts. As a design decision it is fine, but it needs to be documented.

Also if you run "per path history" using "rev-list $head -- $path" to collect commits that touch the named $path, you might want to get rewritten parents for each commit and use it in the presentation, but it is something you cannot cache easily (i.e. you should not be reusing the parent list rewritten with respect to other paths).

The tests looked fine, too.
Previous: Lea WiemannNext: Lea Wiemann
Message 4 of 8 in “gitweb: add test suite with Test::WWW::Mechanize::CGI”
  1. 1/3 gitweb: add test suite with Test::WWW::Mechanize::CGILea Wiemann, Aug 18, 2008
  2. 2/3 add new Perl API: Git::Repo, Git::Commit, Git::Tag, and Git::RepoRootLea Wiemann, Aug 18, 2008
  3. 3/3 gitweb: use new Git::Repo API, and add optional cachingLea Wiemann, Aug 18, 2008
  4. Junio C HamanoAug 19, 2008
  5. Lea WiemannAug 19, 2008
  6. Lea WiemannAug 19, 2008
  7. Junio C HamanoAug 19, 2008
  8. Lea WiemannAug 19, 2008

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.