@matheusbsbs / matheus-bagliano / commits / 756bbf548d
Fork 0

gitweb: Add parse_commits, used to bulk load commit objects.

Add a new method parse_commits which is able to parse multiple commit objects at once. Reworked parse_commit to share the commit object parsing logic. Signed-off-by: Robert Fitzsimons <robfitz@273k.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Robert Fitzsimons committed Dec 24, 2006 at 14:31 UTC 756bbf548dbef5b738c50ce6517f5c63ab929373
1 file changed +74 -17
gitweb/gitweb.perl
+74 -17
@@ -1270,25 +1270,13 @@ sub parse_tag {
1270 return %tag
1271 }
1272
1273 -sub parse_commit {
1274 - my $commit_id = shift;
1275 - my $commit_text = shift;
1276 -
1277 - my @commit_lines;
1273 +sub parse_commit_text {
1274 + my ($commit_text) = @_;
1275 + my @commit_lines = split '\n', $commit_text;
1276 my %co;
1277
1280 - if (defined $commit_text) {
1281 - @commit_lines = @$commit_text;
1282 - } else {
1283 - local $/ = "\0";
1284 - open my $fd, "-|", git_cmd(), "rev-list",
1285 - "--header", "--parents", "--max-count=1",
1286 - $commit_id, "--"
1287 - or return;
1288 - @commit_lines = split '\n', <$fd>;
1289 - close $fd or return;
1290 - pop @commit_lines;
1291 - }
1278 + pop @commit_lines; # Remove '\0'
1279 +
1280 my $header = shift @commit_lines;
1281 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1282 return;
@@ -1375,6 +1363,75 @@ sub parse_commit {
1363 return %co;
1364 }
1365
1366 +sub parse_commit {
1367 + my ($commit_id) = @_;
1368 + my %co;
1369 +
1370 + local $/ = "\0";
1371 +
1372 + open my $fd, "-|", git_cmd(), "rev-list",
1373 + "--header",
1374 + "--parents",
1375 + "--max-count=1",
1376 + $commit_id,
1377 + "--",
1378 + or die_error(undef, "Open git-rev-list failed");
1379 + %co = parse_commit_text(<$fd>);
1380 + close $fd;
1381 +
1382 + return %co;
1383 +}
1384 +
1385 +sub parse_commits {
1386 + my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1387 + my @cos;
1388 +
1389 + $maxcount ||= 1;
1390 + $skip ||= 0;
1391 +
1392 + # Delete once rev-list supports the --skip option
1393 + if ($skip > 0) {
1394 + open my $fd, "-|", git_cmd(), "rev-list",
1395 + ($arg ? ($arg) : ()),
1396 + ("--max-count=" . ($maxcount + $skip)),
1397 + $commit_id,
1398 + "--",
1399 + ($filename ? ($filename) : ())
1400 + or die_error(undef, "Open git-rev-list failed");
1401 + while (my $line = <$fd>) {
1402 + if ($skip-- <= 0) {
1403 + chomp $line;
1404 + my %co = parse_commit($line);
1405 + push @cos, \%co;
1406 + }
1407 + }
1408 + close $fd;
1409 +
1410 + return wantarray ? @cos : \@cos;
1411 + }
1412 +
1413 + local $/ = "\0";
1414 +
1415 + open my $fd, "-|", git_cmd(), "rev-list",
1416 + "--header",
1417 + "--parents",
1418 + ($arg ? ($arg) : ()),
1419 + ("--max-count=" . $maxcount),
1420 + # Add once rev-list supports the --skip option
1421 + # ("--skip=" . $skip),
1422 + $commit_id,
1423 + "--",
1424 + ($filename ? ($filename) : ())
1425 + or die_error(undef, "Open git-rev-list failed");
1426 + while (my $line = <$fd>) {
1427 + my %co = parse_commit_text($line);
1428 + push @cos, \%co;
1429 + }
1430 + close $fd;
1431 +
1432 + return wantarray ? @cos : \@cos;
1433 +}
1434 +
1435 # parse ref from ref_file, given by ref_id, with given type
1436 sub parse_ref {
1437 my $ref_file = shift;