cat-file: pass expand_data to print_object_or_die

We currently individually pass the sha1, type, and size fields calculated by sha1_object_info. However, if we pass the whole struct, the called function can make more intelligent decisions about which fields were actually filled by sha1_object_info. This patch takes that first refactoring step, passing the whole struct, so further patches can make those decisions with less noise in their diffs. There should be no functional change to this patch (aside from a minor typo fix in the error message). As a side effect, we can rename the local variables in the function to "type" and "size", since the names are no longer taken. Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 12, 2013 at 07:01 UTC 370c9268d13ef54a05ed84b92baa81b75930e58f
1 file changed +11 -10
builtin/cat-file.c
+11 -10
@@ -193,25 +193,26 @@ static size_t expand_format(struct strbuf *sb, const char *start, void *data)
193 return end - start + 1;
194 }
195
196 -static void print_object_or_die(int fd, const unsigned char *sha1,
197 - enum object_type type, unsigned long size)
196 +static void print_object_or_die(int fd, struct expand_data *data)
197 {
199 - if (type == OBJ_BLOB) {
198 + const unsigned char *sha1 = data->sha1;
199 +
200 + if (data->type == OBJ_BLOB) {
201 if (stream_blob_to_fd(fd, sha1, NULL, 0) < 0)
202 die("unable to stream %s to stdout", sha1_to_hex(sha1));
203 }
204 else {
204 - enum object_type rtype;
205 - unsigned long rsize;
205 + enum object_type type;
206 + unsigned long size;
207 void *contents;
208
208 - contents = read_sha1_file(sha1, &rtype, &rsize);
209 + contents = read_sha1_file(sha1, &type, &size);
210 if (!contents)
211 die("object %s disappeared", sha1_to_hex(sha1));
211 - if (rtype != type)
212 + if (type != data->type)
213 die("object %s changed type!?", sha1_to_hex(sha1));
213 - if (rsize != size)
214 - die("object %s change size!?", sha1_to_hex(sha1));
214 + if (size != data->size)
215 + die("object %s changed size!?", sha1_to_hex(sha1));
216
217 write_or_die(fd, contents, size);
218 free(contents);
@@ -250,7 +251,7 @@ static int batch_one_object(const char *obj_name, struct batch_options *opt,
251 strbuf_release(&buf);
252
253 if (opt->print_contents) {
253 - print_object_or_die(1, data->sha1, data->type, data->size);
254 + print_object_or_die(1, data);
255 write_or_die(1, "\n", 1);
256 }
257 return 0;