Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 6 | #include "cache.h" |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 7 | #include "exec_cmd.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 8 | #include "tag.h" |
| 9 | #include "tree.h" |
Timo Hirvonen | f81daef | 2006-05-24 14:08:46 +0300 | [diff] [blame] | 10 | #include "builtin.h" |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 11 | #include "parse-options.h" |
| 12 | |
| 13 | #define BATCH 1 |
| 14 | #define BATCH_CHECK 2 |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 15 | |
David Rientjes | eddd1c8 | 2006-08-14 13:19:15 -0700 | [diff] [blame] | 16 | static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size) |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 17 | { |
| 18 | /* the parser in tag.c is useless here. */ |
| 19 | const char *endp = buf + size; |
| 20 | const char *cp = buf; |
| 21 | |
| 22 | while (cp < endp) { |
| 23 | char c = *cp++; |
| 24 | if (c != '\n') |
| 25 | continue; |
| 26 | if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) { |
| 27 | const char *tagger = cp; |
| 28 | |
| 29 | /* Found the tagger line. Copy out the contents |
| 30 | * of the buffer so far. |
| 31 | */ |
Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 32 | write_or_die(1, buf, cp - buf); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Do something intelligent, like pretty-printing |
| 36 | * the date. |
| 37 | */ |
| 38 | while (cp < endp) { |
| 39 | if (*cp++ == '\n') { |
| 40 | /* tagger to cp is a line |
| 41 | * that has ident and time. |
| 42 | */ |
| 43 | const char *sp = tagger; |
| 44 | char *ep; |
| 45 | unsigned long date; |
| 46 | long tz; |
| 47 | while (sp < cp && *sp != '>') |
| 48 | sp++; |
| 49 | if (sp == cp) { |
| 50 | /* give up */ |
Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 51 | write_or_die(1, tagger, |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 52 | cp - tagger); |
| 53 | break; |
| 54 | } |
| 55 | while (sp < cp && |
| 56 | !('0' <= *sp && *sp <= '9')) |
| 57 | sp++; |
Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 58 | write_or_die(1, tagger, sp - tagger); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 59 | date = strtoul(sp, &ep, 10); |
| 60 | tz = strtol(ep, NULL, 10); |
Linus Torvalds | 9a8e35e | 2006-08-26 15:45:26 -0700 | [diff] [blame] | 61 | sp = show_date(date, tz, 0); |
Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 62 | write_or_die(1, sp, strlen(sp)); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 63 | xwrite(1, "\n", 1); |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | break; |
| 68 | } |
| 69 | if (cp < endp && *cp == '\n') |
| 70 | /* end of header */ |
| 71 | break; |
| 72 | } |
| 73 | /* At this point, we have copied out the header up to the end of |
| 74 | * the tagger line and cp points at one past \n. It could be the |
| 75 | * next header line after the tagger line, or it could be another |
| 76 | * \n that marks the end of the headers. We need to copy out the |
| 77 | * remainder as is. |
| 78 | */ |
| 79 | if (cp < endp) |
Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 80 | write_or_die(1, cp, endp - cp); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 81 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 82 | |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 83 | static int cat_one_file(int opt, const char *exp_type, const char *obj_name) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 84 | { |
| 85 | unsigned char sha1[20]; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 86 | enum object_type type; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 87 | void *buf; |
| 88 | unsigned long size; |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 89 | |
| 90 | if (get_sha1(obj_name, sha1)) |
| 91 | die("Not a valid object name %s", obj_name); |
Linus Torvalds | 11e7d5c | 2005-05-01 19:28:18 -0700 | [diff] [blame] | 92 | |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 93 | buf = NULL; |
| 94 | switch (opt) { |
| 95 | case 't': |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 96 | type = sha1_object_info(sha1, NULL); |
| 97 | if (type > 0) { |
| 98 | printf("%s\n", typename(type)); |
Junio C Hamano | f2a0633 | 2005-06-27 23:58:45 -0700 | [diff] [blame] | 99 | return 0; |
Linus Torvalds | 11e7d5c | 2005-05-01 19:28:18 -0700 | [diff] [blame] | 100 | } |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 101 | break; |
| 102 | |
| 103 | case 's': |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 104 | type = sha1_object_info(sha1, &size); |
| 105 | if (type > 0) { |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 106 | printf("%lu\n", size); |
| 107 | return 0; |
| 108 | } |
| 109 | break; |
| 110 | |
| 111 | case 'e': |
| 112 | return !has_sha1_file(sha1); |
| 113 | |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 114 | case 'p': |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 115 | type = sha1_object_info(sha1, NULL); |
| 116 | if (type < 0) |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 117 | die("Not a valid object name %s", obj_name); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 118 | |
| 119 | /* custom pretty-print here */ |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 120 | if (type == OBJ_TREE) { |
| 121 | const char *ls_args[3] = {"ls-tree", obj_name, NULL}; |
| 122 | return cmd_ls_tree(2, ls_args, NULL); |
| 123 | } |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 124 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 125 | buf = read_sha1_file(sha1, &type, &size); |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 126 | if (!buf) |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 127 | die("Cannot read object %s", obj_name); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 128 | if (type == OBJ_TAG) { |
David Rientjes | eddd1c8 | 2006-08-14 13:19:15 -0700 | [diff] [blame] | 129 | pprint_tag(sha1, buf, size); |
| 130 | return 0; |
| 131 | } |
Junio C Hamano | a0f15fa | 2006-03-01 16:43:19 -0800 | [diff] [blame] | 132 | |
| 133 | /* otherwise just spit out the data */ |
| 134 | break; |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 135 | case 0: |
Shawn O. Pearce | 2b6854c | 2007-04-21 21:14:39 -0400 | [diff] [blame] | 136 | buf = read_object_with_reference(sha1, exp_type, &size, NULL); |
H. Peter Anvin | 7950571 | 2005-12-03 17:57:48 -0800 | [diff] [blame] | 137 | break; |
| 138 | |
| 139 | default: |
Heikki Orsila | 34baebc | 2008-08-30 14:12:53 +0300 | [diff] [blame] | 140 | die("git cat-file: unknown option: %s\n", exp_type); |
Linus Torvalds | 11e7d5c | 2005-05-01 19:28:18 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 143 | if (!buf) |
Heikki Orsila | 34baebc | 2008-08-30 14:12:53 +0300 | [diff] [blame] | 144 | die("git cat-file %s: bad file", obj_name); |
Linus Torvalds | bf0c6e8 | 2005-04-08 09:16:38 -0700 | [diff] [blame] | 145 | |
Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 146 | write_or_die(1, buf, size); |
Linus Torvalds | bf0c6e8 | 2005-04-08 09:16:38 -0700 | [diff] [blame] | 147 | return 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 148 | } |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 149 | |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 150 | static int batch_one_object(const char *obj_name, int print_contents) |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 151 | { |
| 152 | unsigned char sha1[20]; |
Lea Wiemann | 3c076db | 2008-06-09 02:02:21 +0200 | [diff] [blame] | 153 | enum object_type type = 0; |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 154 | unsigned long size; |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 155 | void *contents = contents; |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 156 | |
| 157 | if (!obj_name) |
| 158 | return 1; |
| 159 | |
| 160 | if (get_sha1(obj_name, sha1)) { |
| 161 | printf("%s missing\n", obj_name); |
Lea Wiemann | 422b206 | 2008-06-03 20:34:17 +0200 | [diff] [blame] | 162 | fflush(stdout); |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 166 | if (print_contents == BATCH) |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 167 | contents = read_sha1_file(sha1, &type, &size); |
| 168 | else |
| 169 | type = sha1_object_info(sha1, &size); |
| 170 | |
Lea Wiemann | 3c076db | 2008-06-09 02:02:21 +0200 | [diff] [blame] | 171 | if (type <= 0) { |
| 172 | printf("%s missing\n", obj_name); |
| 173 | fflush(stdout); |
| 174 | return 0; |
| 175 | } |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 176 | |
| 177 | printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size); |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 178 | fflush(stdout); |
| 179 | |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 180 | if (print_contents == BATCH) { |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 181 | write_or_die(1, contents, size); |
| 182 | printf("\n"); |
| 183 | fflush(stdout); |
Björn Steinbrink | 5b8a94b | 2008-06-29 03:21:25 +0200 | [diff] [blame] | 184 | free(contents); |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 185 | } |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 190 | static int batch_objects(int print_contents) |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 191 | { |
| 192 | struct strbuf buf; |
| 193 | |
| 194 | strbuf_init(&buf, 0); |
| 195 | while (strbuf_getline(&buf, stdin, '\n') != EOF) { |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 196 | int error = batch_one_object(buf.buf, print_contents); |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 197 | if (error) |
| 198 | return error; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 204 | static const char * const cat_file_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 205 | "git cat-file [-t|-s|-e|-p|<type>] <sha1>", |
| 206 | "git cat-file [--batch|--batch-check] < <list_of_sha1s>", |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 207 | NULL |
| 208 | }; |
Adam Roben | 4814dbe | 2008-04-23 15:17:45 -0400 | [diff] [blame] | 209 | |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 210 | int cmd_cat_file(int argc, const char **argv, const char *prefix) |
| 211 | { |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 212 | int opt = 0, batch = 0; |
Adam Roben | 4814dbe | 2008-04-23 15:17:45 -0400 | [diff] [blame] | 213 | const char *exp_type = NULL, *obj_name = NULL; |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 214 | |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 215 | const struct option options[] = { |
| 216 | OPT_GROUP("<type> can be one of: blob, tree, commit, tag"), |
| 217 | OPT_SET_INT('t', NULL, &opt, "show object type", 't'), |
| 218 | OPT_SET_INT('s', NULL, &opt, "show object size", 's'), |
| 219 | OPT_SET_INT('e', NULL, &opt, |
| 220 | "exit with zero when there's no error", 'e'), |
| 221 | OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'), |
| 222 | OPT_SET_INT(0, "batch", &batch, |
| 223 | "show info and content of objects feeded on stdin", BATCH), |
| 224 | OPT_SET_INT(0, "batch-check", &batch, |
| 225 | "show info about objects feeded on stdin", |
| 226 | BATCH_CHECK), |
| 227 | OPT_END() |
| 228 | }; |
| 229 | |
Junio C Hamano | 9bd81e4 | 2008-05-25 14:25:02 -0700 | [diff] [blame] | 230 | git_config(git_default_config, NULL); |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 231 | |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 232 | if (argc != 3 && argc != 2) |
| 233 | usage_with_options(cat_file_usage, options); |
Adam Roben | 4814dbe | 2008-04-23 15:17:45 -0400 | [diff] [blame] | 234 | |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 235 | argc = parse_options(argc, argv, options, cat_file_usage, 0); |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 236 | |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 237 | if (opt) { |
| 238 | if (argc == 1) |
| 239 | obj_name = argv[0]; |
| 240 | else |
| 241 | usage_with_options(cat_file_usage, options); |
| 242 | } |
| 243 | if (!opt && !batch) { |
| 244 | if (argc == 2) { |
| 245 | exp_type = argv[0]; |
| 246 | obj_name = argv[1]; |
| 247 | } else |
| 248 | usage_with_options(cat_file_usage, options); |
| 249 | } |
| 250 | if (batch && (opt || argc)) { |
| 251 | usage_with_options(cat_file_usage, options); |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Michele Ballabio | 15d8e56 | 2008-05-23 16:19:42 +0200 | [diff] [blame] | 254 | if (batch) |
Adam Roben | a8128ed | 2008-04-23 15:17:47 -0400 | [diff] [blame] | 255 | return batch_objects(batch); |
Adam Roben | 05d5667 | 2008-04-23 15:17:46 -0400 | [diff] [blame] | 256 | |
Adam Roben | 9cf71b1 | 2008-04-23 15:17:44 -0400 | [diff] [blame] | 257 | return cat_one_file(opt, exp_type, obj_name); |
| 258 | } |