]>
Commit | Line | Data |
---|---|---|
92747a90 | 1 | /* |
ae64bbc1 | 2 | * Copyright (c) 2005, 2006 Rene Scharfe |
92747a90 | 3 | */ |
731ab9cc | 4 | #include "cache.h" |
c3f92812 | 5 | #include "commit.h" |
ae64bbc1 | 6 | #include "tar.h" |
56d1398a | 7 | #include "builtin.h" |
fd88d9c8 | 8 | #include "quote.h" |
731ab9cc | 9 | |
e9dd085d | 10 | static const char builtin_get_tar_commit_id_usage[] = |
33e8fc87 | 11 | "git get-tar-commit-id"; |
e9dd085d | 12 | |
52ba03cb | 13 | /* ustar header + extended global header content */ |
fd88d9c8 | 14 | #define RECORDSIZE (512) |
52ba03cb RS |
15 | #define HEADERSIZE (2 * RECORDSIZE) |
16 | ||
a633fca0 | 17 | int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix) |
52ba03cb RS |
18 | { |
19 | char buffer[HEADERSIZE]; | |
20 | struct ustar_header *header = (struct ustar_header *)buffer; | |
21 | char *content = buffer + RECORDSIZE; | |
e3f1da98 | 22 | const char *comment; |
52ba03cb | 23 | ssize_t n; |
3548726c RS |
24 | long len; |
25 | char *end; | |
52ba03cb | 26 | |
e9dd085d JN |
27 | if (argc != 1) |
28 | usage(builtin_get_tar_commit_id_usage); | |
29 | ||
93d26e4c | 30 | n = read_in_full(0, buffer, HEADERSIZE); |
41dcc4dc JK |
31 | if (n < 0) |
32 | die_errno("git get-tar-commit-id: read error"); | |
61d36330 | 33 | if (n != HEADERSIZE) |
41dcc4dc | 34 | die_errno("git get-tar-commit-id: EOF before reading tar header"); |
52ba03cb RS |
35 | if (header->typeflag[0] != 'g') |
36 | return 1; | |
3548726c RS |
37 | |
38 | len = strtol(content, &end, 10); | |
39 | if (errno == ERANGE || end == content || len < 0) | |
40 | return 1; | |
41 | if (!skip_prefix(end, " comment=", &comment)) | |
42 | return 1; | |
43 | len -= comment - content; | |
87003d2c | 44 | if (len < 1 || !(len % 2) || |
45 | hash_algo_by_length((len - 1) / 2) == GIT_HASH_UNKNOWN) | |
52ba03cb RS |
46 | return 1; |
47 | ||
3548726c | 48 | if (write_in_full(1, comment, len) < 0) |
0721c314 | 49 | die_errno("git get-tar-commit-id: write error"); |
52ba03cb RS |
50 | |
51 | return 0; | |
52 | } |