git-cat-file: Add --batch option
--batch is similar to --batch-check, except that the contents of each object is also printed. The output's form is: <sha1> SP <type> SP <size> LF <contents> LF Signed-off-by: Adam Roben <aroben@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Adam Roben committed
Apr 23, 2008 at 15:17 UTC
a8128ed62858063e29edc066b14b8b0fa6257cc2
3 files changed
+117
-38
Documentation/git-cat-file.txt
+15
-3
@@ -9,7 +9,7 @@ git-cat-file - Provide content or type/size information for repository objects
9
SYNOPSIS
10
--------
11
'git-cat-file' [-t | -s | -e | -p | <type>] <object>
12
-'git-cat-file' --batch-check < <list-of-objects>
12
+'git-cat-file' [--batch | --batch-check] < <list-of-objects>
13
14
DESCRIPTION
15
-----------
@@ -50,6 +50,10 @@ OPTIONS
50
or to ask for a "blob" with <object> being a tag object that
51
points at it.
52
53
+--batch::
54
+ Print the SHA1, type, size, and contents of each object provided on
55
+ stdin. May not be combined with any other options or arguments.
56
+
57
--batch-check::
58
Print the SHA1, type, and size of each object provided on stdin. May not be
59
combined with any other options or arguments.
@@ -67,6 +71,14 @@ If '-p' is specified, the contents of <object> are pretty-printed.
71
If <type> is specified, the raw (though uncompressed) contents of the <object>
72
will be returned.
73
74
+If '--batch' is specified, output of the following form is printed for each
75
+object specified on stdin:
76
+
77
+------------
78
+<sha1> SP <type> SP <size> LF
79
+<contents> LF
80
+------------
81
+
82
If '--batch-check' is specified, output of the following form is printed for
83
each object specified fon stdin:
84
@@ -74,8 +86,8 @@ each object specified fon stdin:
86
<sha1> SP <type> SP <size> LF
87
------------
88
77
-Additionally, output of the following form is printed for each object specified
78
-on stdin that does not exist in the repository:
89
+For both '--batch' and '--batch-check', output of the following form is printed
90
+for each object specified on stdin that does not exist in the repository:
91
92
------------
93
<object> SP missing LF
builtin-cat-file.c
+45
-18
@@ -143,11 +143,12 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
143
return 0;
144
}
145
146
-static int batch_one_object(const char *obj_name)
146
+static int batch_one_object(const char *obj_name, int print_contents)
147
{
148
unsigned char sha1[20];
149
enum object_type type;
150
unsigned long size;
151
+ void *contents = contents;
152
153
if (!obj_name)
154
return 1;
@@ -157,22 +158,33 @@ static int batch_one_object(const char *obj_name)
158
return 0;
159
}
160
160
- type = sha1_object_info(sha1, &size);
161
+ if (print_contents)
162
+ contents = read_sha1_file(sha1, &type, &size);
163
+ else
164
+ type = sha1_object_info(sha1, &size);
165
+
166
if (type <= 0)
167
return 1;
168
169
printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
170
+ fflush(stdout);
171
+
172
+ if (print_contents) {
173
+ write_or_die(1, contents, size);
174
+ printf("\n");
175
+ fflush(stdout);
176
+ }
177
178
return 0;
179
}
180
169
-static int batch_objects(void)
181
+static int batch_objects(int print_contents)
182
{
183
struct strbuf buf;
184
185
strbuf_init(&buf, 0);
186
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
175
- int error = batch_one_object(buf.buf);
187
+ int error = batch_one_object(buf.buf, print_contents);
188
if (error)
189
return error;
190
}
@@ -180,37 +192,51 @@ static int batch_objects(void)
192
return 0;
193
}
194
183
-static const char cat_file_usage[] = "git-cat-file [ [-t|-s|-e|-p|<type>] <sha1> | --batch-check < <list_of_sha1s> ]";
195
+static const char cat_file_usage[] = "git-cat-file [ [-t|-s|-e|-p|<type>] <sha1> | [--batch|--batch-check] < <list_of_sha1s> ]";
196
197
int cmd_cat_file(int argc, const char **argv, const char *prefix)
198
{
187
- int i, opt = 0, batch_check = 0;
199
+ int i, opt = 0, batch = 0, batch_check = 0;
200
const char *exp_type = NULL, *obj_name = NULL;
201
202
git_config(git_default_config);
203
204
for (i = 1; i < argc; ++i) {
205
const char *arg = argv[i];
206
+ int is_batch = 0, is_batch_check = 0;
207
+
208
+ is_batch = !strcmp(arg, "--batch");
209
+ if (!is_batch)
210
+ is_batch_check = !strcmp(arg, "--batch-check");
211
195
- if (!strcmp(arg, "--batch-check")) {
212
+ if (is_batch || is_batch_check) {
213
if (opt) {
197
- error("git-cat-file: Can't use --batch-check with -%c", opt);
214
+ error("git-cat-file: Can't use %s with -%c", arg, opt);
215
usage(cat_file_usage);
216
} else if (exp_type) {
200
- error("git-cat-file: Can't use --batch-check when a type (\"%s\") is specified", exp_type);
217
+ error("git-cat-file: Can't use %s when a type (\"%s\") is specified", arg, exp_type);
218
usage(cat_file_usage);
219
} else if (obj_name) {
203
- error("git-cat-file: Can't use --batch-check when an object (\"%s\") is specified", obj_name);
220
+ error("git-cat-file: Can't use %s when an object (\"%s\") is specified", arg, obj_name);
221
usage(cat_file_usage);
222
}
223
207
- batch_check = 1;
224
+ if ((is_batch && batch_check) || (is_batch_check && batch)) {
225
+ error("git-cat-file: Can't use %s with %s", arg, is_batch ? "--batch-check" : "--batch");
226
+ usage(cat_file_usage);
227
+ }
228
+
229
+ if (is_batch)
230
+ batch = 1;
231
+ else
232
+ batch_check = 1;
233
+
234
continue;
235
}
236
237
if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) {
212
- if (batch_check) {
213
- error("git-cat-file: Can't use %s with --batch-check", arg);
238
+ if (batch || batch_check) {
239
+ error("git-cat-file: Can't use %s with %s", arg, batch ? "--batch" : "--batch-check");
240
usage(cat_file_usage);
241
}
242
@@ -223,8 +249,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
249
usage(cat_file_usage);
250
251
if (!exp_type) {
226
- if (batch_check) {
227
- error("git-cat-file: Can't specify a type (\"%s\") with --batch-check", arg);
252
+ if (batch || batch_check) {
253
+ error("git-cat-file: Can't specify a type (\"%s\") with %s", arg, batch ? "--batch" : "--batch-check");
254
usage(cat_file_usage);
255
}
256
@@ -235,16 +261,17 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
261
if (obj_name)
262
usage(cat_file_usage);
263
238
- // We should have hit one of the earlier if (batch_check) cases before
264
+ // We should have hit one of the earlier if (batch || batch_check) cases before
265
// getting here.
266
+ assert(!batch);
267
assert(!batch_check);
268
269
obj_name = arg;
270
break;
271
}
272
246
- if (batch_check)
247
- return batch_objects();
273
+ if (batch || batch_check)
274
+ return batch_objects(batch);
275
276
if (!exp_type || !obj_name)
277
usage(cat_file_usage);
t/t1006-cat-file.sh
+57
-17
@@ -28,6 +28,9 @@ run_tests () {
28
pretty_content=$5
29
no_ts=$6
30
31
+ batch_output="$sha1 $type $size
32
+$content"
33
+
34
test_expect_success "$type exists" '
35
git cat-file -e $sha1
36
'
@@ -68,6 +71,20 @@ run_tests () {
71
fi
72
'
73
74
+ test -z "$content" ||
75
+ test_expect_success "--batch output of $type is correct" '
76
+ expect="$(maybe_remove_timestamp "$batch_output" $no_ts)"
77
+ actual="$(maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" no_ts)"
78
+ if test "z$expect" = "z$actual"
79
+ then
80
+ : happy
81
+ else
82
+ echo "Oops: expected $expect"
83
+ echo "but got $actual"
84
+ false
85
+ fi
86
+ '
87
+
88
test_expect_success "--batch-check output of $type is correct" '
89
expect="$sha1 $type $size"
90
actual="$(echo_without_newline $sha1 | git cat-file --batch-check)"
@@ -131,28 +148,31 @@ test_expect_success \
148
"Reach a blob from a tag pointing to it" \
149
"test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
150
134
-for opt in t s e p
151
+for batch in batch batch-check
152
do
136
- test_expect_success "Passing -$opt with --batch-check fails" '
137
- test_must_fail git cat-file --batch-check -$opt $hello_sha1
153
+ for opt in t s e p
154
+ do
155
+ test_expect_success "Passing -$opt with --$batch fails" '
156
+ test_must_fail git cat-file --$batch -$opt $hello_sha1
157
+ '
158
+
159
+ test_expect_success "Passing --$batch with -$opt fails" '
160
+ test_must_fail git cat-file -$opt --$batch $hello_sha1
161
+ '
162
+ done
163
+
164
+ test_expect_success "Passing <type> with --$batch fails" '
165
+ test_must_fail git cat-file --$batch blob $hello_sha1
166
'
167
140
- test_expect_success "Passing --batch-check with -$opt fails" '
141
- test_must_fail git cat-file -$opt --batch-check $hello_sha1
168
+ test_expect_success "Passing --$batch with <type> fails" '
169
+ test_must_fail git cat-file blob --$batch $hello_sha1
170
'
143
-done
144
-
145
-test_expect_success "Passing <type> with --batch-check fails" '
146
- test_must_fail git cat-file --batch-check blob $hello_sha1
147
-'
148
-
149
-test_expect_success "Passing --batch-check with <type> fails" '
150
- test_must_fail git cat-file blob --batch-check $hello_sha1
151
-'
171
153
-test_expect_success "Passing sha1 with --batch-check fails" '
154
- test_must_fail git cat-file --batch-check $hello_sha1
155
-'
172
+ test_expect_success "Passing sha1 with --$batch fails" '
173
+ test_must_fail git cat-file --$batch $hello_sha1
174
+ '
175
+done
176
177
test_expect_success "--batch-check for a non-existent object" '
178
test "deadbeef missing" = \
@@ -163,6 +183,26 @@ test_expect_success "--batch-check for an emtpy line" '
183
test " missing" = "$(echo | git cat-file --batch-check)"
184
'
185
186
+batch_input="$hello_sha1
187
+$commit_sha1
188
+$tag_sha1
189
+deadbeef
190
+
191
+"
192
+
193
+batch_output="$hello_sha1 blob $hello_size
194
+$hello_content
195
+$commit_sha1 commit $commit_size
196
+$commit_content
197
+$tag_sha1 tag $tag_size
198
+$tag_content
199
+deadbeef missing
200
+ missing"
201
+
202
+test_expect_success \
203
+ "--batch with multiple sha1s gives correct format" \
204
+ "test \"\$(maybe_remove_timestamp \"$batch_output\" 1)\" = \"\$(maybe_remove_timestamp \"\$(echo_without_newline \"$batch_input\" | git cat-file --batch)\" 1)\""
205
+
206
batch_check_input="$hello_sha1
207
$tree_sha1
208
$commit_sha1