Imported Upstream version 2.0.1
[platform/upstream/git.git] / builtin / get-tar-commit-id.c
1 /*
2  * Copyright (c) 2005, 2006 Rene Scharfe
3  */
4 #include "cache.h"
5 #include "commit.h"
6 #include "tar.h"
7 #include "builtin.h"
8 #include "quote.h"
9
10 static const char builtin_get_tar_commit_id_usage[] =
11 "git get-tar-commit-id < <tarfile>";
12
13 /* ustar header + extended global header content */
14 #define RECORDSIZE      (512)
15 #define HEADERSIZE (2 * RECORDSIZE)
16
17 int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
18 {
19         char buffer[HEADERSIZE];
20         struct ustar_header *header = (struct ustar_header *)buffer;
21         char *content = buffer + RECORDSIZE;
22         ssize_t n;
23
24         if (argc != 1)
25                 usage(builtin_get_tar_commit_id_usage);
26
27         n = read_in_full(0, buffer, HEADERSIZE);
28         if (n < HEADERSIZE)
29                 die("git get-tar-commit-id: read error");
30         if (header->typeflag[0] != 'g')
31                 return 1;
32         if (memcmp(content, "52 comment=", 11))
33                 return 1;
34
35         n = write_in_full(1, content + 11, 41);
36         if (n < 41)
37                 die_errno("git get-tar-commit-id: write error");
38
39         return 0;
40 }