|
| 1 | +/* |
| 2 | + * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 | + * a Linking Exception. For full terms see the included COPYING file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <git2.h> |
| 9 | +#include "cli.h" |
| 10 | +#include "cmd.h" |
| 11 | + |
| 12 | +#define COMMAND_NAME "cat-file" |
| 13 | + |
| 14 | +typedef enum { |
| 15 | + DISPLAY_CONTENT = 0, |
| 16 | + DISPLAY_EXISTS, |
| 17 | + DISPLAY_PRETTY, |
| 18 | + DISPLAY_SIZE, |
| 19 | + DISPLAY_TYPE |
| 20 | +} display_t; |
| 21 | + |
| 22 | +static int show_help; |
| 23 | +static int display = DISPLAY_CONTENT; |
| 24 | +static char *type_name, *object_spec; |
| 25 | + |
| 26 | +static const cli_opt_spec opts[] = { |
| 27 | + { CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1, |
| 28 | + CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING, NULL, |
| 29 | + "display help about the " COMMAND_NAME " command" }, |
| 30 | + |
| 31 | + { CLI_OPT_TYPE_SWITCH, NULL, 't', &display, DISPLAY_TYPE, |
| 32 | + CLI_OPT_USAGE_REQUIRED, NULL, "display the type of the object" }, |
| 33 | + { CLI_OPT_TYPE_SWITCH, NULL, 's', &display, DISPLAY_SIZE, |
| 34 | + CLI_OPT_USAGE_CHOICE, NULL, "display the size of the object" }, |
| 35 | + { CLI_OPT_TYPE_SWITCH, NULL, 'e', &display, DISPLAY_EXISTS, |
| 36 | + CLI_OPT_USAGE_CHOICE, NULL, "displays nothing unless the object is corrupt" }, |
| 37 | + { CLI_OPT_TYPE_SWITCH, NULL, 'p', &display, DISPLAY_PRETTY, |
| 38 | + CLI_OPT_USAGE_CHOICE, NULL, "pretty-print the object" }, |
| 39 | + { CLI_OPT_TYPE_ARG, "type", 0, &type_name, 0, |
| 40 | + CLI_OPT_USAGE_CHOICE, "type", "the type of object to display" }, |
| 41 | + { CLI_OPT_TYPE_ARG, "object", 0, &object_spec, 0, |
| 42 | + CLI_OPT_USAGE_REQUIRED, "object", "the object to display" }, |
| 43 | + { 0 }, |
| 44 | +}; |
| 45 | + |
| 46 | +static void print_help(void) |
| 47 | +{ |
| 48 | + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts); |
| 49 | + printf("\n"); |
| 50 | + |
| 51 | + printf("Display the content for the given object in the repository.\n"); |
| 52 | + printf("\n"); |
| 53 | + |
| 54 | + printf("Options:\n"); |
| 55 | + |
| 56 | + cli_opt_help_fprint(stdout, opts); |
| 57 | +} |
| 58 | + |
| 59 | +static int print_odb(git_object *object, display_t display) |
| 60 | +{ |
| 61 | + git_odb *odb = NULL; |
| 62 | + git_odb_object *odb_object = NULL; |
| 63 | + const unsigned char *content; |
| 64 | + git_object_size_t size; |
| 65 | + int ret = 0; |
| 66 | + |
| 67 | + /* |
| 68 | + * Our parsed blobs retain the raw content; all other objects are |
| 69 | + * parsed into a working representation. To get the raw content, |
| 70 | + * we need to do an ODB lookup. (Thankfully, this should be cached |
| 71 | + * in-memory from our last call.) |
| 72 | + */ |
| 73 | + if (git_object_type(object) == GIT_OBJECT_BLOB) { |
| 74 | + content = git_blob_rawcontent((git_blob *)object); |
| 75 | + size = git_blob_rawsize((git_blob *)object); |
| 76 | + } else { |
| 77 | + if (git_repository_odb(&odb, git_object_owner(object)) < 0 || |
| 78 | + git_odb_read(&odb_object, odb, git_object_id(object)) < 0) { |
| 79 | + ret = cli_error_git(); |
| 80 | + goto done; |
| 81 | + } |
| 82 | + |
| 83 | + content = git_odb_object_data(odb_object); |
| 84 | + size = git_odb_object_size(odb_object); |
| 85 | + } |
| 86 | + |
| 87 | + switch (display) { |
| 88 | + case DISPLAY_SIZE: |
| 89 | + if (printf("%" PRIu64 "\n", size) < 0) |
| 90 | + ret = cli_error_os(); |
| 91 | + break; |
| 92 | + case DISPLAY_CONTENT: |
| 93 | + if (p_write(fileno(stdout), content, (size_t)size) < 0) |
| 94 | + ret = cli_error_os(); |
| 95 | + break; |
| 96 | + default: |
| 97 | + GIT_ASSERT(0); |
| 98 | + } |
| 99 | + |
| 100 | +done: |
| 101 | + git_odb_object_free(odb_object); |
| 102 | + git_odb_free(odb); |
| 103 | + return ret; |
| 104 | +} |
| 105 | + |
| 106 | +static int print_type(git_object *object) |
| 107 | +{ |
| 108 | + if (printf("%s\n", git_object_type2string(git_object_type(object))) < 0) |
| 109 | + return cli_error_os(); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +static int print_pretty(git_object *object) |
| 115 | +{ |
| 116 | + const git_tree_entry *entry; |
| 117 | + size_t i, count; |
| 118 | + |
| 119 | + /* |
| 120 | + * Only trees are stored in an unreadable format and benefit from |
| 121 | + * pretty-printing. |
| 122 | + */ |
| 123 | + if (git_object_type(object) != GIT_OBJECT_TREE) |
| 124 | + return print_odb(object, DISPLAY_CONTENT); |
| 125 | + |
| 126 | + for (i = 0, count = git_tree_entrycount((git_tree *)object); i < count; i++) { |
| 127 | + entry = git_tree_entry_byindex((git_tree *)object, i); |
| 128 | + |
| 129 | + if (printf("%06o %s %s\t%s\n", |
| 130 | + git_tree_entry_filemode_raw(entry), |
| 131 | + git_object_type2string(git_tree_entry_type(entry)), |
| 132 | + git_oid_tostr_s(git_tree_entry_id(entry)), |
| 133 | + git_tree_entry_name(entry)) < 0) |
| 134 | + return cli_error_os(); |
| 135 | + } |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +int cmd_cat_file(int argc, char **argv) |
| 141 | +{ |
| 142 | + git_repository *repo = NULL; |
| 143 | + git_object *object = NULL; |
| 144 | + git_object_t type; |
| 145 | + cli_opt invalid_opt; |
| 146 | + int giterr, ret = 0; |
| 147 | + |
| 148 | + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) |
| 149 | + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); |
| 150 | + |
| 151 | + if (show_help) { |
| 152 | + print_help(); |
| 153 | + return 0; |
| 154 | + } |
| 155 | + |
| 156 | + if (git_repository_open_ext(&repo, ".", GIT_REPOSITORY_OPEN_FROM_ENV, NULL) < 0) |
| 157 | + return cli_error_git(); |
| 158 | + |
| 159 | + if ((giterr = git_revparse_single(&object, repo, object_spec)) < 0) { |
| 160 | + if (display == DISPLAY_EXISTS && giterr == GIT_ENOTFOUND) |
| 161 | + ret = 1; |
| 162 | + else |
| 163 | + ret = cli_error_git(); |
| 164 | + |
| 165 | + goto done; |
| 166 | + } |
| 167 | + |
| 168 | + if (type_name) { |
| 169 | + git_object *peeled; |
| 170 | + |
| 171 | + if ((type = git_object_string2type(type_name)) == GIT_OBJECT_INVALID) { |
| 172 | + ret = cli_error_usage("invalid object type '%s'", type_name); |
| 173 | + goto done; |
| 174 | + } |
| 175 | + |
| 176 | + if (git_object_peel(&peeled, object, type) < 0) { |
| 177 | + ret = cli_error_git(); |
| 178 | + goto done; |
| 179 | + } |
| 180 | + |
| 181 | + git_object_free(object); |
| 182 | + object = peeled; |
| 183 | + } |
| 184 | + |
| 185 | + switch (display) { |
| 186 | + case DISPLAY_EXISTS: |
| 187 | + ret = 0; |
| 188 | + break; |
| 189 | + case DISPLAY_TYPE: |
| 190 | + ret = print_type(object); |
| 191 | + break; |
| 192 | + case DISPLAY_PRETTY: |
| 193 | + ret = print_pretty(object); |
| 194 | + break; |
| 195 | + default: |
| 196 | + ret = print_odb(object, display); |
| 197 | + break; |
| 198 | + } |
| 199 | + |
| 200 | +done: |
| 201 | + git_object_free(object); |
| 202 | + git_repository_free(repo); |
| 203 | + return ret; |
| 204 | +} |
0 commit comments