cat-file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. */
  6. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  7. #include "cache.h"
  8. #include "config.h"
  9. #include "builtin.h"
  10. #include "diff.h"
  11. #include "parse-options.h"
  12. #include "userdiff.h"
  13. #include "streaming.h"
  14. #include "tree-walk.h"
  15. #include "oid-array.h"
  16. #include "packfile.h"
  17. #include "object-store.h"
  18. #include "promisor-remote.h"
  19. struct batch_options {
  20. int enabled;
  21. int follow_symlinks;
  22. int print_contents;
  23. int buffer_output;
  24. int all_objects;
  25. int unordered;
  26. int cmdmode; /* may be 'w' or 'c' for --filters or --textconv */
  27. const char *format;
  28. };
  29. static const char *force_path;
  30. static int filter_object(const char *path, unsigned mode,
  31. const struct object_id *oid,
  32. char **buf, unsigned long *size)
  33. {
  34. enum object_type type;
  35. *buf = read_object_file(oid, &type, size);
  36. if (!*buf)
  37. return error(_("cannot read object %s '%s'"),
  38. oid_to_hex(oid), path);
  39. if ((type == OBJ_BLOB) && S_ISREG(mode)) {
  40. struct strbuf strbuf = STRBUF_INIT;
  41. struct checkout_metadata meta;
  42. init_checkout_metadata(&meta, NULL, NULL, oid);
  43. if (convert_to_working_tree(&the_index, path, *buf, *size, &strbuf, &meta)) {
  44. free(*buf);
  45. *size = strbuf.len;
  46. *buf = strbuf_detach(&strbuf, NULL);
  47. }
  48. }
  49. return 0;
  50. }
  51. static int stream_blob(const struct object_id *oid)
  52. {
  53. if (stream_blob_to_fd(1, oid, NULL, 0))
  54. die("unable to stream %s to stdout", oid_to_hex(oid));
  55. return 0;
  56. }
  57. static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
  58. int unknown_type)
  59. {
  60. struct object_id oid;
  61. enum object_type type;
  62. char *buf;
  63. unsigned long size;
  64. struct object_context obj_context;
  65. struct object_info oi = OBJECT_INFO_INIT;
  66. struct strbuf sb = STRBUF_INIT;
  67. unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
  68. const char *path = force_path;
  69. if (unknown_type)
  70. flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
  71. if (get_oid_with_context(the_repository, obj_name,
  72. GET_OID_RECORD_PATH,
  73. &oid, &obj_context))
  74. die("Not a valid object name %s", obj_name);
  75. if (!path)
  76. path = obj_context.path;
  77. if (obj_context.mode == S_IFINVALID)
  78. obj_context.mode = 0100644;
  79. buf = NULL;
  80. switch (opt) {
  81. case 't':
  82. oi.type_name = &sb;
  83. if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
  84. die("git cat-file: could not get object info");
  85. if (sb.len) {
  86. printf("%s\n", sb.buf);
  87. strbuf_release(&sb);
  88. return 0;
  89. }
  90. break;
  91. case 's':
  92. oi.sizep = &size;
  93. if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
  94. die("git cat-file: could not get object info");
  95. printf("%"PRIuMAX"\n", (uintmax_t)size);
  96. return 0;
  97. case 'e':
  98. return !has_object_file(&oid);
  99. case 'w':
  100. if (!path)
  101. die("git cat-file --filters %s: <object> must be "
  102. "<sha1:path>", obj_name);
  103. if (filter_object(path, obj_context.mode,
  104. &oid, &buf, &size))
  105. return -1;
  106. break;
  107. case 'c':
  108. if (!path)
  109. die("git cat-file --textconv %s: <object> must be <sha1:path>",
  110. obj_name);
  111. if (textconv_object(the_repository, path, obj_context.mode,
  112. &oid, 1, &buf, &size))
  113. break;
  114. /* else fallthrough */
  115. case 'p':
  116. type = oid_object_info(the_repository, &oid, NULL);
  117. if (type < 0)
  118. die("Not a valid object name %s", obj_name);
  119. /* custom pretty-print here */
  120. if (type == OBJ_TREE) {
  121. const char *ls_args[3] = { NULL };
  122. ls_args[0] = "ls-tree";
  123. ls_args[1] = obj_name;
  124. return cmd_ls_tree(2, ls_args, NULL);
  125. }
  126. if (type == OBJ_BLOB)
  127. return stream_blob(&oid);
  128. buf = read_object_file(&oid, &type, &size);
  129. if (!buf)
  130. die("Cannot read object %s", obj_name);
  131. /* otherwise just spit out the data */
  132. break;
  133. case 0:
  134. if (type_from_string(exp_type) == OBJ_BLOB) {
  135. struct object_id blob_oid;
  136. if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) {
  137. char *buffer = read_object_file(&oid, &type,
  138. &size);
  139. const char *target;
  140. if (!skip_prefix(buffer, "object ", &target) ||
  141. get_oid_hex(target, &blob_oid))
  142. die("%s not a valid tag", oid_to_hex(&oid));
  143. free(buffer);
  144. } else
  145. oidcpy(&blob_oid, &oid);
  146. if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
  147. return stream_blob(&blob_oid);
  148. /*
  149. * we attempted to dereference a tag to a blob
  150. * and failed; there may be new dereference
  151. * mechanisms this code is not aware of.
  152. * fall-back to the usual case.
  153. */
  154. }
  155. buf = read_object_with_reference(the_repository,
  156. &oid, exp_type, &size, NULL);
  157. break;
  158. default:
  159. die("git cat-file: unknown option: %s", exp_type);
  160. }
  161. if (!buf)
  162. die("git cat-file %s: bad file", obj_name);
  163. write_or_die(1, buf, size);
  164. free(buf);
  165. free(obj_context.path);
  166. return 0;
  167. }
  168. struct expand_data {
  169. struct object_id oid;
  170. enum object_type type;
  171. unsigned long size;
  172. off_t disk_size;
  173. const char *rest;
  174. struct object_id delta_base_oid;
  175. /*
  176. * If mark_query is true, we do not expand anything, but rather
  177. * just mark the object_info with items we wish to query.
  178. */
  179. int mark_query;
  180. /*
  181. * Whether to split the input on whitespace before feeding it to
  182. * get_sha1; this is decided during the mark_query phase based on
  183. * whether we have a %(rest) token in our format.
  184. */
  185. int split_on_whitespace;
  186. /*
  187. * After a mark_query run, this object_info is set up to be
  188. * passed to oid_object_info_extended. It will point to the data
  189. * elements above, so you can retrieve the response from there.
  190. */
  191. struct object_info info;
  192. /*
  193. * This flag will be true if the requested batch format and options
  194. * don't require us to call oid_object_info, which can then be
  195. * optimized out.
  196. */
  197. unsigned skip_object_info : 1;
  198. };
  199. static int is_atom(const char *atom, const char *s, int slen)
  200. {
  201. int alen = strlen(atom);
  202. return alen == slen && !memcmp(atom, s, alen);
  203. }
  204. static void expand_atom(struct strbuf *sb, const char *atom, int len,
  205. void *vdata)
  206. {
  207. struct expand_data *data = vdata;
  208. if (is_atom("objectname", atom, len)) {
  209. if (!data->mark_query)
  210. strbuf_addstr(sb, oid_to_hex(&data->oid));
  211. } else if (is_atom("objecttype", atom, len)) {
  212. if (data->mark_query)
  213. data->info.typep = &data->type;
  214. else
  215. strbuf_addstr(sb, type_name(data->type));
  216. } else if (is_atom("objectsize", atom, len)) {
  217. if (data->mark_query)
  218. data->info.sizep = &data->size;
  219. else
  220. strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
  221. } else if (is_atom("objectsize:disk", atom, len)) {
  222. if (data->mark_query)
  223. data->info.disk_sizep = &data->disk_size;
  224. else
  225. strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
  226. } else if (is_atom("rest", atom, len)) {
  227. if (data->mark_query)
  228. data->split_on_whitespace = 1;
  229. else if (data->rest)
  230. strbuf_addstr(sb, data->rest);
  231. } else if (is_atom("deltabase", atom, len)) {
  232. if (data->mark_query)
  233. data->info.delta_base_oid = &data->delta_base_oid;
  234. else
  235. strbuf_addstr(sb,
  236. oid_to_hex(&data->delta_base_oid));
  237. } else
  238. die("unknown format element: %.*s", len, atom);
  239. }
  240. static size_t expand_format(struct strbuf *sb, const char *start, void *data)
  241. {
  242. const char *end;
  243. if (*start != '(')
  244. return 0;
  245. end = strchr(start + 1, ')');
  246. if (!end)
  247. die("format element '%s' does not end in ')'", start);
  248. expand_atom(sb, start + 1, end - start - 1, data);
  249. return end - start + 1;
  250. }
  251. static void batch_write(struct batch_options *opt, const void *data, int len)
  252. {
  253. if (opt->buffer_output) {
  254. if (fwrite(data, 1, len, stdout) != len)
  255. die_errno("unable to write to stdout");
  256. } else
  257. write_or_die(1, data, len);
  258. }
  259. static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
  260. {
  261. const struct object_id *oid = &data->oid;
  262. assert(data->info.typep);
  263. if (data->type == OBJ_BLOB) {
  264. if (opt->buffer_output)
  265. fflush(stdout);
  266. if (opt->cmdmode) {
  267. char *contents;
  268. unsigned long size;
  269. if (!data->rest)
  270. die("missing path for '%s'", oid_to_hex(oid));
  271. if (opt->cmdmode == 'w') {
  272. if (filter_object(data->rest, 0100644, oid,
  273. &contents, &size))
  274. die("could not convert '%s' %s",
  275. oid_to_hex(oid), data->rest);
  276. } else if (opt->cmdmode == 'c') {
  277. enum object_type type;
  278. if (!textconv_object(the_repository,
  279. data->rest, 0100644, oid,
  280. 1, &contents, &size))
  281. contents = read_object_file(oid,
  282. &type,
  283. &size);
  284. if (!contents)
  285. die("could not convert '%s' %s",
  286. oid_to_hex(oid), data->rest);
  287. } else
  288. BUG("invalid cmdmode: %c", opt->cmdmode);
  289. batch_write(opt, contents, size);
  290. free(contents);
  291. } else {
  292. stream_blob(oid);
  293. }
  294. }
  295. else {
  296. enum object_type type;
  297. unsigned long size;
  298. void *contents;
  299. contents = read_object_file(oid, &type, &size);
  300. if (!contents)
  301. die("object %s disappeared", oid_to_hex(oid));
  302. if (type != data->type)
  303. die("object %s changed type!?", oid_to_hex(oid));
  304. if (data->info.sizep && size != data->size)
  305. die("object %s changed size!?", oid_to_hex(oid));
  306. batch_write(opt, contents, size);
  307. free(contents);
  308. }
  309. }
  310. static void batch_object_write(const char *obj_name,
  311. struct strbuf *scratch,
  312. struct batch_options *opt,
  313. struct expand_data *data)
  314. {
  315. if (!data->skip_object_info &&
  316. oid_object_info_extended(the_repository, &data->oid, &data->info,
  317. OBJECT_INFO_LOOKUP_REPLACE) < 0) {
  318. printf("%s missing\n",
  319. obj_name ? obj_name : oid_to_hex(&data->oid));
  320. fflush(stdout);
  321. return;
  322. }
  323. strbuf_reset(scratch);
  324. strbuf_expand(scratch, opt->format, expand_format, data);
  325. strbuf_addch(scratch, '\n');
  326. batch_write(opt, scratch->buf, scratch->len);
  327. if (opt->print_contents) {
  328. print_object_or_die(opt, data);
  329. batch_write(opt, "\n", 1);
  330. }
  331. }
  332. static void batch_one_object(const char *obj_name,
  333. struct strbuf *scratch,
  334. struct batch_options *opt,
  335. struct expand_data *data)
  336. {
  337. struct object_context ctx;
  338. int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
  339. enum get_oid_result result;
  340. result = get_oid_with_context(the_repository, obj_name,
  341. flags, &data->oid, &ctx);
  342. if (result != FOUND) {
  343. switch (result) {
  344. case MISSING_OBJECT:
  345. printf("%s missing\n", obj_name);
  346. break;
  347. case SHORT_NAME_AMBIGUOUS:
  348. printf("%s ambiguous\n", obj_name);
  349. break;
  350. case DANGLING_SYMLINK:
  351. printf("dangling %"PRIuMAX"\n%s\n",
  352. (uintmax_t)strlen(obj_name), obj_name);
  353. break;
  354. case SYMLINK_LOOP:
  355. printf("loop %"PRIuMAX"\n%s\n",
  356. (uintmax_t)strlen(obj_name), obj_name);
  357. break;
  358. case NOT_DIR:
  359. printf("notdir %"PRIuMAX"\n%s\n",
  360. (uintmax_t)strlen(obj_name), obj_name);
  361. break;
  362. default:
  363. BUG("unknown get_sha1_with_context result %d\n",
  364. result);
  365. break;
  366. }
  367. fflush(stdout);
  368. return;
  369. }
  370. if (ctx.mode == 0) {
  371. printf("symlink %"PRIuMAX"\n%s\n",
  372. (uintmax_t)ctx.symlink_path.len,
  373. ctx.symlink_path.buf);
  374. fflush(stdout);
  375. return;
  376. }
  377. batch_object_write(obj_name, scratch, opt, data);
  378. }
  379. struct object_cb_data {
  380. struct batch_options *opt;
  381. struct expand_data *expand;
  382. struct oidset *seen;
  383. struct strbuf *scratch;
  384. };
  385. static int batch_object_cb(const struct object_id *oid, void *vdata)
  386. {
  387. struct object_cb_data *data = vdata;
  388. oidcpy(&data->expand->oid, oid);
  389. batch_object_write(NULL, data->scratch, data->opt, data->expand);
  390. return 0;
  391. }
  392. static int collect_loose_object(const struct object_id *oid,
  393. const char *path,
  394. void *data)
  395. {
  396. oid_array_append(data, oid);
  397. return 0;
  398. }
  399. static int collect_packed_object(const struct object_id *oid,
  400. struct packed_git *pack,
  401. uint32_t pos,
  402. void *data)
  403. {
  404. oid_array_append(data, oid);
  405. return 0;
  406. }
  407. static int batch_unordered_object(const struct object_id *oid, void *vdata)
  408. {
  409. struct object_cb_data *data = vdata;
  410. if (oidset_insert(data->seen, oid))
  411. return 0;
  412. return batch_object_cb(oid, data);
  413. }
  414. static int batch_unordered_loose(const struct object_id *oid,
  415. const char *path,
  416. void *data)
  417. {
  418. return batch_unordered_object(oid, data);
  419. }
  420. static int batch_unordered_packed(const struct object_id *oid,
  421. struct packed_git *pack,
  422. uint32_t pos,
  423. void *data)
  424. {
  425. return batch_unordered_object(oid, data);
  426. }
  427. static int batch_objects(struct batch_options *opt)
  428. {
  429. struct strbuf input = STRBUF_INIT;
  430. struct strbuf output = STRBUF_INIT;
  431. struct expand_data data;
  432. int save_warning;
  433. int retval = 0;
  434. if (!opt->format)
  435. opt->format = "%(objectname) %(objecttype) %(objectsize)";
  436. /*
  437. * Expand once with our special mark_query flag, which will prime the
  438. * object_info to be handed to oid_object_info_extended for each
  439. * object.
  440. */
  441. memset(&data, 0, sizeof(data));
  442. data.mark_query = 1;
  443. strbuf_expand(&output, opt->format, expand_format, &data);
  444. data.mark_query = 0;
  445. strbuf_release(&output);
  446. if (opt->cmdmode)
  447. data.split_on_whitespace = 1;
  448. if (opt->all_objects) {
  449. struct object_info empty = OBJECT_INFO_INIT;
  450. if (!memcmp(&data.info, &empty, sizeof(empty)))
  451. data.skip_object_info = 1;
  452. }
  453. /*
  454. * If we are printing out the object, then always fill in the type,
  455. * since we will want to decide whether or not to stream.
  456. */
  457. if (opt->print_contents)
  458. data.info.typep = &data.type;
  459. if (opt->all_objects) {
  460. struct object_cb_data cb;
  461. if (has_promisor_remote())
  462. warning("This repository uses promisor remotes. Some objects may not be loaded.");
  463. cb.opt = opt;
  464. cb.expand = &data;
  465. cb.scratch = &output;
  466. if (opt->unordered) {
  467. struct oidset seen = OIDSET_INIT;
  468. cb.seen = &seen;
  469. for_each_loose_object(batch_unordered_loose, &cb, 0);
  470. for_each_packed_object(batch_unordered_packed, &cb,
  471. FOR_EACH_OBJECT_PACK_ORDER);
  472. oidset_clear(&seen);
  473. } else {
  474. struct oid_array sa = OID_ARRAY_INIT;
  475. for_each_loose_object(collect_loose_object, &sa, 0);
  476. for_each_packed_object(collect_packed_object, &sa, 0);
  477. oid_array_for_each_unique(&sa, batch_object_cb, &cb);
  478. oid_array_clear(&sa);
  479. }
  480. strbuf_release(&output);
  481. return 0;
  482. }
  483. /*
  484. * We are going to call get_sha1 on a potentially very large number of
  485. * objects. In most large cases, these will be actual object sha1s. The
  486. * cost to double-check that each one is not also a ref (just so we can
  487. * warn) ends up dwarfing the actual cost of the object lookups
  488. * themselves. We can work around it by just turning off the warning.
  489. */
  490. save_warning = warn_on_object_refname_ambiguity;
  491. warn_on_object_refname_ambiguity = 0;
  492. while (strbuf_getline(&input, stdin) != EOF) {
  493. if (data.split_on_whitespace) {
  494. /*
  495. * Split at first whitespace, tying off the beginning
  496. * of the string and saving the remainder (or NULL) in
  497. * data.rest.
  498. */
  499. char *p = strpbrk(input.buf, " \t");
  500. if (p) {
  501. while (*p && strchr(" \t", *p))
  502. *p++ = '\0';
  503. }
  504. data.rest = p;
  505. }
  506. batch_one_object(input.buf, &output, opt, &data);
  507. }
  508. strbuf_release(&input);
  509. strbuf_release(&output);
  510. warn_on_object_refname_ambiguity = save_warning;
  511. return retval;
  512. }
  513. static const char * const cat_file_usage[] = {
  514. N_("git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>"),
  515. N_("git cat-file (--batch[=<format>] | --batch-check[=<format>]) [--follow-symlinks] [--textconv | --filters]"),
  516. NULL
  517. };
  518. static int git_cat_file_config(const char *var, const char *value, void *cb)
  519. {
  520. if (userdiff_config(var, value) < 0)
  521. return -1;
  522. return git_default_config(var, value, cb);
  523. }
  524. static int batch_option_callback(const struct option *opt,
  525. const char *arg,
  526. int unset)
  527. {
  528. struct batch_options *bo = opt->value;
  529. BUG_ON_OPT_NEG(unset);
  530. if (bo->enabled) {
  531. return error(_("only one batch option may be specified"));
  532. }
  533. bo->enabled = 1;
  534. bo->print_contents = !strcmp(opt->long_name, "batch");
  535. bo->format = arg;
  536. return 0;
  537. }
  538. int cmd_cat_file(int argc, const char **argv, const char *prefix)
  539. {
  540. int opt = 0;
  541. const char *exp_type = NULL, *obj_name = NULL;
  542. struct batch_options batch = {0};
  543. int unknown_type = 0;
  544. const struct option options[] = {
  545. OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
  546. OPT_CMDMODE('t', NULL, &opt, N_("show object type"), 't'),
  547. OPT_CMDMODE('s', NULL, &opt, N_("show object size"), 's'),
  548. OPT_CMDMODE('e', NULL, &opt,
  549. N_("exit with zero when there's no error"), 'e'),
  550. OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
  551. OPT_CMDMODE(0, "textconv", &opt,
  552. N_("for blob objects, run textconv on object's content"), 'c'),
  553. OPT_CMDMODE(0, "filters", &opt,
  554. N_("for blob objects, run filters on object's content"), 'w'),
  555. OPT_STRING(0, "path", &force_path, N_("blob"),
  556. N_("use a specific path for --textconv/--filters")),
  557. OPT_BOOL(0, "allow-unknown-type", &unknown_type,
  558. N_("allow -s and -t to work with broken/corrupt objects")),
  559. OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
  560. OPT_CALLBACK_F(0, "batch", &batch, "format",
  561. N_("show info and content of objects fed from the standard input"),
  562. PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
  563. batch_option_callback),
  564. OPT_CALLBACK_F(0, "batch-check", &batch, "format",
  565. N_("show info about objects fed from the standard input"),
  566. PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
  567. batch_option_callback),
  568. OPT_BOOL(0, "follow-symlinks", &batch.follow_symlinks,
  569. N_("follow in-tree symlinks (used with --batch or --batch-check)")),
  570. OPT_BOOL(0, "batch-all-objects", &batch.all_objects,
  571. N_("show all objects with --batch or --batch-check")),
  572. OPT_BOOL(0, "unordered", &batch.unordered,
  573. N_("do not order --batch-all-objects output")),
  574. OPT_END()
  575. };
  576. git_config(git_cat_file_config, NULL);
  577. batch.buffer_output = -1;
  578. argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
  579. if (opt) {
  580. if (batch.enabled && (opt == 'c' || opt == 'w'))
  581. batch.cmdmode = opt;
  582. else if (argc == 1)
  583. obj_name = argv[0];
  584. else
  585. usage_with_options(cat_file_usage, options);
  586. }
  587. if (!opt && !batch.enabled) {
  588. if (argc == 2) {
  589. exp_type = argv[0];
  590. obj_name = argv[1];
  591. } else
  592. usage_with_options(cat_file_usage, options);
  593. }
  594. if (batch.enabled) {
  595. if (batch.cmdmode != opt || argc)
  596. usage_with_options(cat_file_usage, options);
  597. if (batch.cmdmode && batch.all_objects)
  598. die("--batch-all-objects cannot be combined with "
  599. "--textconv nor with --filters");
  600. }
  601. if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
  602. usage_with_options(cat_file_usage, options);
  603. }
  604. if (force_path && opt != 'c' && opt != 'w') {
  605. error("--path=<path> needs --textconv or --filters");
  606. usage_with_options(cat_file_usage, options);
  607. }
  608. if (force_path && batch.enabled) {
  609. error("--path=<path> incompatible with --batch");
  610. usage_with_options(cat_file_usage, options);
  611. }
  612. if (batch.buffer_output < 0)
  613. batch.buffer_output = batch.all_objects;
  614. if (batch.enabled)
  615. return batch_objects(&batch);
  616. if (unknown_type && opt != 't' && opt != 's')
  617. die("git cat-file --allow-unknown-type: use with -s or -t");
  618. return cat_one_file(opt, exp_type, obj_name, unknown_type);
  619. }