Merge branch 'rs/userformat-find-requirements-simplify'
[git/gitster.git] / builtin / get-tar-commit-id.c
blob9303e386cc7031b2111527cafc779df7d23e892a
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "builtin.h"
5 #include "commit.h"
6 #include "tar.h"
7 #include "quote.h"
8 #include "wrapper.h"
10 static const char builtin_get_tar_commit_id_usage[] =
11 "git get-tar-commit-id";
13 /* ustar header + extended global header content */
14 #define RECORDSIZE (512)
15 #define HEADERSIZE (2 * RECORDSIZE)
17 int cmd_get_tar_commit_id(int argc, const char **argv UNUSED, const char *prefix)
19 char buffer[HEADERSIZE];
20 struct ustar_header *header = (struct ustar_header *)buffer;
21 char *content = buffer + RECORDSIZE;
22 const char *comment;
23 ssize_t n;
24 long len;
25 char *end;
27 BUG_ON_NON_EMPTY_PREFIX(prefix);
29 if (argc != 1)
30 usage(builtin_get_tar_commit_id_usage);
32 n = read_in_full(0, buffer, HEADERSIZE);
33 if (n < 0)
34 die_errno("git get-tar-commit-id: read error");
35 if (n != HEADERSIZE)
36 die_errno("git get-tar-commit-id: EOF before reading tar header");
37 if (header->typeflag[0] != TYPEFLAG_GLOBAL_HEADER)
38 return 1;
40 len = strtol(content, &end, 10);
41 if (errno == ERANGE || end == content || len < 0)
42 return 1;
43 if (!skip_prefix(end, " comment=", &comment))
44 return 1;
45 len -= comment - content;
46 if (len < 1 || !(len % 2) ||
47 hash_algo_by_length((len - 1) / 2) == GIT_HASH_UNKNOWN)
48 return 1;
50 if (write_in_full(1, comment, len) < 0)
51 die_errno("git get-tar-commit-id: write error");
53 return 0;