Subject: Re: [PATCH 1/4] gitweb: notes feature

From: Jakub Narebski <[email-masked]>
Date: Sun, 7 Feb 2010 02:38:52 +0100

On Sun, 7 Feb 2010, Jakub Narebski wrote:

> Right.  This method would be contrary to the goals of fan-out schemes...
> well, we could use 'git ls-tree' without '-r' option, or simply 
> 'git cat-file --batch' to read trees (note that we would get raw, 
> unformatted tree, which is parseable with Perl, but it is not that easy),
> and go down level-by-level.

FYI, here is how you can parse raw tree output from 'git cat-file --batch',
assuming that you have plain-ASCII filenames ('use bytes;' would probably
be needed):

-- 8< --
sub decode_tree {
	my $contents = shift;

	# ...

	while (my @entry = decode_tree_entry($contents)) {

		# ...

		my $len = tree_entry_len(@entry);
		contents = substr($contents, $len);
		last unless $contents;
	}

	# ...
}

sub tree_entry_len {
	my ($mode_str, $filename) = @_;

	# length of mode string + separator + 20 bytes of SHA-1
	# + length of filename (in bytes) + terminating NUL ('\0')
	length($mode_str)+1 + length($filename)+1 + 20;
}

sub decode_tree_entry {
	my $buf = shift;

	$buf =~ s/^([0-7]+) //;
	my ($mode_str) = $1;
	my ($filename, $sha1_str) = unpack('Z*H[40]', $buf);

	return ($mode_str, $filename, $sha1_str);
}
-- >8 --

-- 
Jakub Narebski
Poland