]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/get-tar-commit-id.c
Start the 2.46 cycle
[thirdparty/git.git] / builtin / get-tar-commit-id.c
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "builtin.h"
5 #include "commit.h"
6 #include "tar.h"
7
8 static const char builtin_get_tar_commit_id_usage[] =
9 "git get-tar-commit-id";
10
11 /* ustar header + extended global header content */
12 #define RECORDSIZE (512)
13 #define HEADERSIZE (2 * RECORDSIZE)
14
15 int cmd_get_tar_commit_id(int argc, const char **argv UNUSED, const char *prefix)
16 {
17 char buffer[HEADERSIZE];
18 struct ustar_header *header = (struct ustar_header *)buffer;
19 char *content = buffer + RECORDSIZE;
20 const char *comment;
21 ssize_t n;
22 long len;
23 char *end;
24
25 BUG_ON_NON_EMPTY_PREFIX(prefix);
26
27 if (argc != 1)
28 usage(builtin_get_tar_commit_id_usage);
29
30 n = read_in_full(0, buffer, HEADERSIZE);
31 if (n < 0)
32 die_errno("git get-tar-commit-id: read error");
33 if (n != HEADERSIZE)
34 die_errno("git get-tar-commit-id: EOF before reading tar header");
35 if (header->typeflag[0] != TYPEFLAG_GLOBAL_HEADER)
36 return 1;
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;
44 if (len < 1 || !(len % 2) ||
45 hash_algo_by_length((len - 1) / 2) == GIT_HASH_UNKNOWN)
46 return 1;
47
48 if (write_in_full(1, comment, len) < 0)
49 die_errno("git get-tar-commit-id: write error");
50
51 return 0;
52 }