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