| 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; |
| H. Peter Anvin | 7950571 | 2005-12-04 01:57:48 | [diff] [blame] | 14 | int opt; |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 15 | |
| Junio C Hamano | 5da22bc | 2005-08-30 16:07:50 | [diff] [blame] | 16 | setup_git_directory(); |
| Linus Torvalds | 3c249c9 | 2005-05-01 23:36:56 | [diff] [blame] | 17 | if (argc != 3 || get_sha1(argv[2], sha1)) |
| H. Peter Anvin | 7950571 | 2005-12-04 01:57:48 | [diff] [blame] | 18 | usage("git-cat-file [-t|-s|-e|<type>] <sha1>"); |
| Linus Torvalds | 11e7d5c | 2005-05-02 02:28:18 | [diff] [blame] | 19 | |
| H. Peter Anvin | 7950571 | 2005-12-04 01:57:48 | [diff] [blame] | 20 | opt = 0; |
| 21 | if ( argv[1][0] == '-' ) { |
| 22 | opt = argv[1][1]; |
| 23 | if ( !opt || argv[1][2] ) |
| 24 | opt = -1; /* Not a single character option */ |
| 25 | } |
| 26 | |
| 27 | buf = NULL; |
| 28 | switch (opt) { |
| 29 | case 't': |
| 30 | if (!sha1_object_info(sha1, type, NULL)) { |
| 31 | printf("%s\n", type); |
| Junio C Hamano | f2a0633 | 2005-06-28 06:58:45 | [diff] [blame] | 32 | return 0; |
| Linus Torvalds | 11e7d5c | 2005-05-02 02:28:18 | [diff] [blame] | 33 | } |
| H. Peter Anvin | 7950571 | 2005-12-04 01:57:48 | [diff] [blame] | 34 | break; |
| 35 | |
| 36 | case 's': |
| 37 | if (!sha1_object_info(sha1, type, &size)) { |
| 38 | printf("%lu\n", size); |
| 39 | return 0; |
| 40 | } |
| 41 | break; |
| 42 | |
| 43 | case 'e': |
| 44 | return !has_sha1_file(sha1); |
| 45 | |
| 46 | case 0: |
| Linus Torvalds | 11e7d5c | 2005-05-02 02:28:18 | [diff] [blame] | 47 | buf = read_object_with_reference(sha1, argv[1], &size, NULL); |
| H. Peter Anvin | 7950571 | 2005-12-04 01:57:48 | [diff] [blame] | 48 | break; |
| 49 | |
| 50 | default: |
| 51 | die("git-cat-file: unknown option: %s\n", argv[1]); |
| Linus Torvalds | 11e7d5c | 2005-05-02 02:28:18 | [diff] [blame] | 52 | } |
| 53 | |
| Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 54 | if (!buf) |
| Alexey Nezhdanov | bab5583 | 2005-05-02 04:23:04 | [diff] [blame] | 55 | die("git-cat-file %s: bad file", argv[2]); |
| Linus Torvalds | bf0c6e8 | 2005-04-08 16:16:38 | [diff] [blame] | 56 | |
| 57 | while (size > 0) { |
| Junio C Hamano | 1c15afb | 2005-12-20 00:18:28 | [diff] [blame] | 58 | long ret = xwrite(1, buf, size); |
| Linus Torvalds | bf0c6e8 | 2005-04-08 16:16:38 | [diff] [blame] | 59 | if (ret < 0) { |
| Linus Torvalds | bf0c6e8 | 2005-04-08 16:16:38 | [diff] [blame] | 60 | /* Ignore epipe */ |
| 61 | if (errno == EPIPE) |
| 62 | break; |
| Alexey Nezhdanov | bab5583 | 2005-05-02 04:23:04 | [diff] [blame] | 63 | die("git-cat-file: %s", strerror(errno)); |
| Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 64 | } else if (!ret) { |
| Alexey Nezhdanov | bab5583 | 2005-05-02 04:23:04 | [diff] [blame] | 65 | die("git-cat-file: disk full?"); |
| Linus Torvalds | bf0c6e8 | 2005-04-08 16:16:38 | [diff] [blame] | 66 | } |
| 67 | size -= ret; |
| 68 | buf += ret; |
| 69 | } |
| 70 | return 0; |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 71 | } |