Skip to content

Commit

Permalink
Merge branch 'ar/batch-cat'
Browse files Browse the repository at this point in the history
* ar/batch-cat:
  change quoting in test t1006-cat-file.sh
  builtin-cat-file.c: use parse_options()
  git-svn: Speed up fetch
  Git.pm: Add hash_and_insert_object and cat_blob
  Git.pm: Add command_bidi_pipe and command_close_bidi_pipe
  git-hash-object: Add --stdin-paths option
  Add more tests for git hash-object
  Move git-hash-object tests from t5303 to t1007
  git-cat-file: Add --batch option
  git-cat-file: Add --batch-check option
  git-cat-file: Make option parsing a little more flexible
  git-cat-file: Small refactor of cmd_cat_file
  Add tests for git cat-file
  • Loading branch information
Junio C Hamano committed May 25, 2008
2 parents 325566c + 6c41e21 commit 2931344
Show file tree
Hide file tree
Showing 9 changed files with 781 additions and 82 deletions.
43 changes: 38 additions & 5 deletions Documentation/git-cat-file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ git-cat-file - Provide content or type/size information for repository objects
SYNOPSIS
--------
'git-cat-file' [-t | -s | -e | -p | <type>] <object>
'git-cat-file' [--batch | --batch-check] < <list-of-objects>

DESCRIPTION
-----------
Provides content or type of objects in the repository. The type
is required unless '-t' or '-p' is used to find the object type,
or '-s' is used to find the object size.
In the first form, provides content or type of objects in the repository. The
type is required unless '-t' or '-p' is used to find the object type, or '-s'
is used to find the object size.

In the second form, a list of object (separated by LFs) is provided on stdin,
and the SHA1, type, and size of each object is printed on stdout.

OPTIONS
-------
Expand Down Expand Up @@ -46,6 +50,14 @@ OPTIONS
or to ask for a "blob" with <object> being a tag object that
points at it.

--batch::
Print the SHA1, type, size, and contents of each object provided on
stdin. May not be combined with any other options or arguments.

--batch-check::
Print the SHA1, type, and size of each object provided on stdin. May not be
combined with any other options or arguments.

OUTPUT
------
If '-t' is specified, one of the <type>.
Expand All @@ -56,9 +68,30 @@ If '-e' is specified, no output.

If '-p' is specified, the contents of <object> are pretty-printed.

Otherwise the raw (though uncompressed) contents of the <object> will
be returned.
If <type> is specified, the raw (though uncompressed) contents of the <object>
will be returned.

If '--batch' is specified, output of the following form is printed for each
object specified on stdin:

------------
<sha1> SP <type> SP <size> LF
<contents> LF
------------

If '--batch-check' is specified, output of the following form is printed for
each object specified fon stdin:

------------
<sha1> SP <type> SP <size> LF
------------

For both '--batch' and '--batch-check', output of the following form is printed
for each object specified on stdin that does not exist in the repository:

------------
<object> SP missing LF
------------

Author
------
Expand Down
5 changes: 4 additions & 1 deletion Documentation/git-hash-object.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ git-hash-object - Compute object ID and optionally creates a blob from a file

SYNOPSIS
--------
'git-hash-object' [-t <type>] [-w] [--stdin] [--] <file>...
'git-hash-object' [-t <type>] [-w] [--stdin | --stdin-paths] [--] <file>...

DESCRIPTION
-----------
Expand All @@ -32,6 +32,9 @@ OPTIONS
--stdin::
Read the object from standard input instead of from a file.

--stdin-paths::
Read file names from stdin instead of from the command-line.

Author
------
Written by Junio C Hamano <junkio@cox.net>
Expand Down
126 changes: 110 additions & 16 deletions builtin-cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "tag.h"
#include "tree.h"
#include "builtin.h"
#include "parse-options.h"

#define BATCH 1
#define BATCH_CHECK 2

static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
{
Expand Down Expand Up @@ -76,31 +80,16 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
write_or_die(1, cp, endp - cp);
}

int cmd_cat_file(int argc, const char **argv, const char *prefix)
static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
{
unsigned char sha1[20];
enum object_type type;
void *buf;
unsigned long size;
int opt;
const char *exp_type, *obj_name;

git_config(git_default_config);
if (argc != 3)
usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
exp_type = argv[1];
obj_name = argv[2];

if (get_sha1(obj_name, sha1))
die("Not a valid object name %s", obj_name);

opt = 0;
if ( exp_type[0] == '-' ) {
opt = exp_type[1];
if ( !opt || exp_type[2] )
opt = -1; /* Not a single character option */
}

buf = NULL;
switch (opt) {
case 't':
Expand Down Expand Up @@ -157,3 +146,108 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
write_or_die(1, buf, size);
return 0;
}

static int batch_one_object(const char *obj_name, int print_contents)
{
unsigned char sha1[20];
enum object_type type;
unsigned long size;
void *contents = contents;

if (!obj_name)
return 1;

if (get_sha1(obj_name, sha1)) {
printf("%s missing\n", obj_name);
return 0;
}

if (print_contents == BATCH)
contents = read_sha1_file(sha1, &type, &size);
else
type = sha1_object_info(sha1, &size);

if (type <= 0)
return 1;

printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
fflush(stdout);

if (print_contents == BATCH) {
write_or_die(1, contents, size);
printf("\n");
fflush(stdout);
}

return 0;
}

static int batch_objects(int print_contents)
{
struct strbuf buf;

strbuf_init(&buf, 0);
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
int error = batch_one_object(buf.buf, print_contents);
if (error)
return error;
}

return 0;
}

static const char * const cat_file_usage[] = {
"git-cat-file [-t|-s|-e|-p|<type>] <sha1>",
"git-cat-file [--batch|--batch-check] < <list_of_sha1s>",
NULL
};

int cmd_cat_file(int argc, const char **argv, const char *prefix)
{
int opt = 0, batch = 0;
const char *exp_type = NULL, *obj_name = NULL;

const struct option options[] = {
OPT_GROUP("<type> can be one of: blob, tree, commit, tag"),
OPT_SET_INT('t', NULL, &opt, "show object type", 't'),
OPT_SET_INT('s', NULL, &opt, "show object size", 's'),
OPT_SET_INT('e', NULL, &opt,
"exit with zero when there's no error", 'e'),
OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'),
OPT_SET_INT(0, "batch", &batch,
"show info and content of objects feeded on stdin", BATCH),
OPT_SET_INT(0, "batch-check", &batch,
"show info about objects feeded on stdin",
BATCH_CHECK),
OPT_END()
};

git_config(git_default_config);

if (argc != 3 && argc != 2)
usage_with_options(cat_file_usage, options);

argc = parse_options(argc, argv, options, cat_file_usage, 0);

if (opt) {
if (argc == 1)
obj_name = argv[0];
else
usage_with_options(cat_file_usage, options);
}
if (!opt && !batch) {
if (argc == 2) {
exp_type = argv[0];
obj_name = argv[1];
} else
usage_with_options(cat_file_usage, options);
}
if (batch && (opt || argc)) {
usage_with_options(cat_file_usage, options);
}

if (batch)
return batch_objects(batch);

return cat_one_file(opt, exp_type, obj_name);
}
42 changes: 20 additions & 22 deletions git-svn.perl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use warnings;
use strict;
use vars qw/ $AUTHOR $VERSION
$sha1 $sha1_short $_revision
$sha1 $sha1_short $_revision $_repository
$_q $_authors %users/;
$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
$VERSION = '@@GIT_VERSION@@';
Expand Down Expand Up @@ -222,6 +222,7 @@ BEGIN
}
$ENV{GIT_DIR} = $git_dir;
}
$_repository = Git->repository(Repository => $ENV{GIT_DIR});
}

my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
Expand Down Expand Up @@ -303,6 +304,7 @@ sub do_git_init_db {
}
}
command_noisy(@init_db);
$_repository = Git->repository(Repository => ".git");
}
my $set;
my $pfx = "svn-remote.$Git::SVN::default_repo_id";
Expand All @@ -319,6 +321,7 @@ sub init_subdir {
mkpath([$repo_path]) unless -d $repo_path;
chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
$ENV{GIT_DIR} = '.git';
$_repository = Git->repository(Repository => $ENV{GIT_DIR});
}

sub cmd_clone {
Expand Down Expand Up @@ -3030,6 +3033,7 @@ package SVN::Git::Fetcher;
use strict;
use warnings;
use Carp qw/croak/;
use File::Temp qw/tempfile/;
use IO::File qw//;

# file baton members: path, mode_a, mode_b, pool, fh, blob, base
Expand Down Expand Up @@ -3185,14 +3189,9 @@ sub apply_textdelta {
my $base = IO::File->new_tmpfile;
$base->autoflush(1);
if ($fb->{blob}) {
defined (my $pid = fork) or croak $!;
if (!$pid) {
open STDOUT, '>&', $base or croak $!;
print STDOUT 'link ' if ($fb->{mode_a} == 120000);
exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
}
waitpid $pid, 0;
croak $? if $?;
print $base 'link ' if ($fb->{mode_a} == 120000);
my $size = $::_repository->cat_blob($fb->{blob}, $base);
die "Failed to read object $fb->{blob}" unless $size;

if (defined $exp) {
seek $base, 0, 0 or croak $!;
Expand Down Expand Up @@ -3233,14 +3232,18 @@ sub close_file {
sysseek($fh, 0, 0) or croak $!;
}
}
defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
if (!$pid) {
open STDIN, '<&', $fh or croak $!;
exec qw/git-hash-object -w --stdin/ or croak $!;

my ($tmp_fh, $tmp_filename) = File::Temp::tempfile(UNLINK => 1);
my $result;
while ($result = sysread($fh, my $string, 1024)) {
syswrite($tmp_fh, $string, $result);
}
chomp($hash = do { local $/; <$out> });
close $out or croak $!;
defined $result or croak $!;
close $tmp_fh or croak $!;

close $fh or croak $!;

$hash = $::_repository->hash_and_insert_object($tmp_filename);
$hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
close $fb->{base} or croak $!;
} else {
Expand Down Expand Up @@ -3566,13 +3569,8 @@ sub chg_file {
} elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
$self->change_file_prop($fbat,'svn:special',undef);
}
defined(my $pid = fork) or croak $!;
if (!$pid) {
open STDOUT, '>&', $fh or croak $!;
exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
}
waitpid $pid, 0;
croak $? if $?;
my $size = $::_repository->cat_blob($m->{sha1_b}, $fh);
croak "Failed to read object $m->{sha1_b}" unless $size;
$fh->flush == 0 or croak $!;
seek $fh, 0, 0 or croak $!;

Expand Down
Loading

0 comments on commit 2931344

Please sign in to comment.