From: Adam Roben <aroben@apple.com>
To: git@vger.kernel.org
Cc: Adam Roben <aroben@apple.com>, Junio C Hamano <gitster@pobox.com>,
Brian Downing <bdowning@lavos.net>
Subject: [PATCH 04/11] git-cat-file: Add --batch-check option
Date: Wed, 23 Apr 2008 15:17:46 -0400 [thread overview]
Message-ID: <1208978273-98146-5-git-send-email-aroben@apple.com> (raw)
In-Reply-To: <1208978273-98146-4-git-send-email-aroben@apple.com>
This new option allows multiple objects to be specified on stdin. For each
object specified, a line of the following form is printed:
<sha1> SP <type> SP <size> LF
If the object does not exist in the repository, a line of the following form is
printed:
<object> SP missing LF
Signed-off-by: Adam Roben <aroben@apple.com>
---
Junio C Hamano <gitster@pobox.com> wrote:
> Here is an alternative suggestion.
>
> Two new options, --batch and --batch-check, are introduced.
> These options are incompatible with -[tsep] or an object type
> given as the first parameter to git-cat-file.
>
> * git-cat-file --batch-check <list-of-sha1
>
> outputs a record of this form
>
> <sha1> SP <type> SP <size> LF
>
> for each of the input lines.
>
> * git-cat-file --batch <list-of-sha1
>
> outputs a record of this form
>
> <sha1> SP <type> SP <size> LF <contents> LF
>
> for each of the input lines.
>
> For a missing object, either option gives a record of form:
>
> <sha1> SP missing LF
This patch introduces --batch-check as described above. The next patch introduces --batch.
Documentation/git-cat-file.txt | 31 ++++++++++++++---
builtin-cat-file.c | 74 ++++++++++++++++++++++++++++++++++++++-
t/t1006-cat-file.sh | 52 ++++++++++++++++++++++++++++
3 files changed, 150 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt
index df42cb1..d5821af 100644
--- a/Documentation/git-cat-file.txt
+++ b/Documentation/git-cat-file.txt
@@ -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-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
-------
@@ -46,6 +50,10 @@ OPTIONS
or to ask for a "blob" with <object> being a tag object that
points at it.
+--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>.
@@ -56,9 +64,22 @@ 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-check' is specified, output of the following form is printed for
+each object specified fon stdin:
+
+------------
+<sha1> SP <type> SP <size> LF
+------------
+
+Additionally, 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
------
diff --git a/builtin-cat-file.c b/builtin-cat-file.c
index a76bb16..832cfd1 100644
--- a/builtin-cat-file.c
+++ b/builtin-cat-file.c
@@ -143,11 +143,48 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
return 0;
}
-static const char cat_file_usage[] = "git-cat-file [-t|-s|-e|-p|<type>] <sha1>";
+static int batch_one_object(const char *obj_name)
+{
+ unsigned char sha1[20];
+ enum object_type type;
+ unsigned long size;
+
+ if (!obj_name)
+ return 1;
+
+ if (get_sha1(obj_name, sha1)) {
+ printf("%s missing\n", obj_name);
+ return 0;
+ }
+
+ type = sha1_object_info(sha1, &size);
+ if (type <= 0)
+ return 1;
+
+ printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
+
+ return 0;
+}
+
+static int batch_objects(void)
+{
+ struct strbuf buf;
+
+ strbuf_init(&buf, 0);
+ while (strbuf_getline(&buf, stdin, '\n') != EOF) {
+ int error = batch_one_object(buf.buf);
+ if (error)
+ return error;
+ }
+
+ return 0;
+}
+
+static const char cat_file_usage[] = "git-cat-file [ [-t|-s|-e|-p|<type>] <sha1> | --batch-check < <list_of_sha1s> ]";
int cmd_cat_file(int argc, const char **argv, const char *prefix)
{
- int i, opt = 0;
+ int i, opt = 0, batch_check = 0;
const char *exp_type = NULL, *obj_name = NULL;
git_config(git_default_config);
@@ -155,7 +192,28 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
for (i = 1; i < argc; ++i) {
const char *arg = argv[i];
+ if (!strcmp(arg, "--batch-check")) {
+ if (opt) {
+ error("git-cat-file: Can't use --batch-check with -%c", opt);
+ usage(cat_file_usage);
+ } else if (exp_type) {
+ error("git-cat-file: Can't use --batch-check when a type (\"%s\") is specified", exp_type);
+ usage(cat_file_usage);
+ } else if (obj_name) {
+ error("git-cat-file: Can't use --batch-check when an object (\"%s\") is specified", obj_name);
+ usage(cat_file_usage);
+ }
+
+ batch_check = 1;
+ continue;
+ }
+
if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) {
+ if (batch_check) {
+ error("git-cat-file: Can't use %s with --batch-check", arg);
+ usage(cat_file_usage);
+ }
+
exp_type = arg;
opt = exp_type[1];
continue;
@@ -165,6 +223,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
usage(cat_file_usage);
if (!exp_type) {
+ if (batch_check) {
+ error("git-cat-file: Can't specify a type (\"%s\") with --batch-check", arg);
+ usage(cat_file_usage);
+ }
+
exp_type = arg;
continue;
}
@@ -172,10 +235,17 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
if (obj_name)
usage(cat_file_usage);
+ // We should have hit one of the earlier if (batch_check) cases before
+ // getting here.
+ assert(!batch_check);
+
obj_name = arg;
break;
}
+ if (batch_check)
+ return batch_objects();
+
if (!exp_type || !obj_name)
usage(cat_file_usage);
diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
index 15741d9..46b0f54 100755
--- a/t/t1006-cat-file.sh
+++ b/t/t1006-cat-file.sh
@@ -47,6 +47,9 @@ function run_tests()
test_expect_success \
"Pretty content of $type is correct" \
"test \"\$(maybe_remove_timestamp '$pretty_content' $no_timestamp)\" = \"\$(maybe_remove_timestamp \"\$(git cat-file -p $sha1)\" $no_timestamp)\""
+ test_expect_success \
+ "--batch-check output of $type is correct" \
+ "test \"$sha1 $type $size\" = \"\$(echo_without_newline $sha1 | git cat-file --batch-check)\""
}
hello_content="Hello World"
@@ -98,4 +101,53 @@ test_expect_success \
"Reach a blob from a tag pointing to it" \
"test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
+for opt in t s e p; do
+ test_expect_success \
+ "Passing -$opt with --batch-check fails" \
+ "test_must_fail git cat-file --batch-check -$opt $hello_sha1"
+
+ test_expect_success \
+ "Passing --batch-check with -$opt fails" \
+ "test_must_fail git cat-file -$opt --batch-check $hello_sha1"
+done
+
+test_expect_success \
+ "Passing <type> with --batch-check fails" \
+ "test_must_fail git cat-file --batch-check blob $hello_sha1"
+
+test_expect_success \
+ "Passing --batch-check with <type> fails" \
+ "test_must_fail git cat-file blob --batch-check $hello_sha1"
+
+test_expect_success \
+ "Passing sha1 with --batch-check fails" \
+ "test_must_fail git cat-file --batch-check $hello_sha1"
+
+test_expect_success \
+ "--batch-check for a non-existent object" \
+ "test \"deadbeef missing\" = \"\$(echo_without_newline deadbeef | git cat-file --batch-check)\""
+
+test_expect_success \
+ "--batch-check for an emtpy line" \
+ "test \" missing\" = \"\$(printf \"\\\\n\" | git cat-file --batch-check)\""
+
+batch_check_input="$hello_sha1
+$tree_sha1
+$commit_sha1
+$tag_sha1
+deadbeef
+
+"
+
+batch_check_output="$hello_sha1 blob $hello_size
+$tree_sha1 tree $tree_size
+$commit_sha1 commit $commit_size
+$tag_sha1 tag $tag_size
+deadbeef missing
+ missing"
+
+test_expect_success \
+ "--batch-check with multiple sha1s gives correct format" \
+ "test \"$batch_check_output\" = \"\$(echo_without_newline \"$batch_check_input\" | git cat-file --batch-check)\""
+
test_done
--
1.5.5.1.152.g9aeb7
next prev parent reply other threads:[~2008-04-23 19:19 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-04-23 19:17 Speed up git-svn fetch Adam Roben
2008-04-23 19:17 ` [PATCH 01/11] Add tests for git cat-file Adam Roben
2008-04-23 19:17 ` [PATCH 02/11] git-cat-file: Small refactor of cmd_cat_file Adam Roben
2008-04-23 19:17 ` [PATCH 03/11] git-cat-file: Make option parsing a little more flexible Adam Roben
2008-04-23 19:17 ` Adam Roben [this message]
2008-04-23 19:17 ` [PATCH 05/11] git-cat-file: Add --batch option Adam Roben
2008-04-23 19:17 ` [PATCH 06/11] Move git-hash-object tests from t5303 to t1007 Adam Roben
2008-04-23 19:17 ` [PATCH 07/11] Add more tests for git hash-object Adam Roben
2008-04-23 19:17 ` [PATCH 08/11] git-hash-object: Add --stdin-paths option Adam Roben
2008-04-23 19:17 ` [PATCH 09/11] Git.pm: Add command_bidi_pipe and command_close_bidi_pipe Adam Roben
2008-04-23 19:17 ` [PATCH 10/11] Git.pm: Add hash_and_insert_object and cat_blob Adam Roben
2008-04-23 19:17 ` [PATCH 11/11] git-svn: Speed up fetch Adam Roben
2008-04-25 18:04 ` [PATCH 03/11] git-cat-file: Make option parsing a little more flexible Junio C Hamano
2008-04-25 6:56 ` [PATCH 01/11] Add tests for git cat-file Eric Wong
2008-04-25 18:06 ` Junio C Hamano
2008-04-25 18:03 ` Junio C Hamano
2008-05-06 6:41 ` Junio C Hamano
2008-04-23 19:19 ` Speed up git-svn fetch Adam Roben
2008-04-25 7:15 ` Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1208978273-98146-5-git-send-email-aroben@apple.com \
--to=aroben@apple.com \
--cc=bdowning@lavos.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).