]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/get-tar-commit-id.c
mv: fix error for moving directory to another
[thirdparty/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 #include "wrapper.h"
10
11 static const char builtin_get_tar_commit_id_usage[] =
12 "git get-tar-commit-id";
13
14 /* ustar header + extended global header content */
15 #define RECORDSIZE (512)
16 #define HEADERSIZE (2 * RECORDSIZE)
17
18 int cmd_get_tar_commit_id(int argc, const char **argv UNUSED, const char *prefix)
19 {
20 char buffer[HEADERSIZE];
21 struct ustar_header *header = (struct ustar_header *)buffer;
22 char *content = buffer + RECORDSIZE;
23 const char *comment;
24 ssize_t n;
25 long len;
26 char *end;
27
28 BUG_ON_NON_EMPTY_PREFIX(prefix);
29
30 if (argc != 1)
31 usage(builtin_get_tar_commit_id_usage);
32
33 n = read_in_full(0, buffer, HEADERSIZE);
34 if (n < 0)
35 die_errno("git get-tar-commit-id: read error");
36 if (n != HEADERSIZE)
37 die_errno("git get-tar-commit-id: EOF before reading tar header");
38 if (header->typeflag[0] != TYPEFLAG_GLOBAL_HEADER)
39 return 1;
40
41 len = strtol(content, &end, 10);
42 if (errno == ERANGE || end == content || len < 0)
43 return 1;
44 if (!skip_prefix(end, " comment=", &comment))
45 return 1;
46 len -= comment - content;
47 if (len < 1 || !(len % 2) ||
48 hash_algo_by_length((len - 1) / 2) == GIT_HASH_UNKNOWN)
49 return 1;
50
51 if (write_in_full(1, comment, len) < 0)
52 die_errno("git get-tar-commit-id: write error");
53
54 return 0;
55 }