Linus Torvalds | 8bc9a0c | 2005-04-07 22:16:10 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | |
| 8 | int main(int argc, char **argv) |
| 9 | { |
| 10 | unsigned char sha1[20]; |
| 11 | char type[20]; |
| 12 | void *buf; |
| 13 | unsigned long size; |
Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 14 | |
Junio C Hamano | 5da22bc | 2005-08-30 16:07:50 | [diff] [blame] | 15 | setup_git_directory(); |
Linus Torvalds | 3c249c9 | 2005-05-01 23:36:56 | [diff] [blame] | 16 | if (argc != 3 || get_sha1(argv[2], sha1)) |
Junio C Hamano | f16ebbd | 2005-07-14 03:23:40 | [diff] [blame] | 17 | usage("git-cat-file [-t | -s | <type>] <sha1>"); |
Linus Torvalds | 11e7d5c | 2005-05-02 02:28:18 | [diff] [blame] | 18 | |
Junio C Hamano | 62bb996 | 2005-06-28 06:59:18 | [diff] [blame] | 19 | if (!strcmp("-t", argv[1]) || !strcmp("-s", argv[1])) { |
Junio C Hamano | c62266f | 2005-07-01 00:13:07 | [diff] [blame] | 20 | if (!sha1_object_info(sha1, type, |
| 21 | argv[1][1] == 's' ? &size : NULL)) { |
Junio C Hamano | 62bb996 | 2005-06-28 06:59:18 | [diff] [blame] | 22 | switch (argv[1][1]) { |
| 23 | case 't': |
| 24 | printf("%s\n", type); |
| 25 | break; |
| 26 | case 's': |
| 27 | printf("%lu\n", size); |
| 28 | break; |
| 29 | } |
Junio C Hamano | f2a0633 | 2005-06-28 06:58:45 | [diff] [blame] | 30 | return 0; |
Linus Torvalds | 11e7d5c | 2005-05-02 02:28:18 | [diff] [blame] | 31 | } |
Junio C Hamano | f2a0633 | 2005-06-28 06:58:45 | [diff] [blame] | 32 | buf = NULL; |
Linus Torvalds | 11e7d5c | 2005-05-02 02:28:18 | [diff] [blame] | 33 | } else { |
| 34 | buf = read_object_with_reference(sha1, argv[1], &size, NULL); |
| 35 | } |
| 36 | |
Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 37 | if (!buf) |
Alexey Nezhdanov | bab5583 | 2005-05-02 04:23:04 | [diff] [blame] | 38 | die("git-cat-file %s: bad file", argv[2]); |
Linus Torvalds | bf0c6e8 | 2005-04-08 16:16:38 | [diff] [blame] | 39 | |
| 40 | while (size > 0) { |
| 41 | long ret = write(1, buf, size); |
| 42 | if (ret < 0) { |
| 43 | if (errno == EAGAIN) |
| 44 | continue; |
| 45 | /* Ignore epipe */ |
| 46 | if (errno == EPIPE) |
| 47 | break; |
Alexey Nezhdanov | bab5583 | 2005-05-02 04:23:04 | [diff] [blame] | 48 | die("git-cat-file: %s", strerror(errno)); |
Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 49 | } else if (!ret) { |
Alexey Nezhdanov | bab5583 | 2005-05-02 04:23:04 | [diff] [blame] | 50 | die("git-cat-file: disk full?"); |
Linus Torvalds | bf0c6e8 | 2005-04-08 16:16:38 | [diff] [blame] | 51 | } |
| 52 | size -= ret; |
| 53 | buf += ret; |
| 54 | } |
| 55 | return 0; |
Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 56 | } |