Merge branch 'jk/reflog-date' into next
[git] / builtin-tar-tree.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 tar_tree_usage[] =
11 "git tar-tree [--remote=<repo>] <tree-ish> [basedir]\n"
12 "*** Note that this command is now deprecated; use \"git archive\" instead.";
13
14 int cmd_tar_tree(int argc, const char **argv, const char *prefix)
15 {
16         /*
17          * "git tar-tree" is now a wrapper around "git archive --format=tar"
18          *
19          * $0 --remote=<repo> arg... ==>
20          *      git archive --format=tar --remote=<repo> arg...
21          * $0 tree-ish ==>
22          *      git archive --format=tar tree-ish
23          * $0 tree-ish basedir ==>
24          *      git archive --format-tar --prefix=basedir tree-ish
25          */
26         int i;
27         const char **nargv = xcalloc(sizeof(*nargv), argc + 3);
28         char *basedir_arg;
29         int nargc = 0;
30
31         nargv[nargc++] = "archive";
32         nargv[nargc++] = "--format=tar";
33
34         if (2 <= argc && !prefixcmp(argv[1], "--remote=")) {
35                 nargv[nargc++] = argv[1];
36                 argv++;
37                 argc--;
38         }
39
40         /*
41          * Because it's just a compatibility wrapper, tar-tree supports only
42          * the old behaviour of reading attributes from the work tree.
43          */
44         nargv[nargc++] = "--worktree-attributes";
45
46         switch (argc) {
47         default:
48                 usage(tar_tree_usage);
49                 break;
50         case 3:
51                 /* base-path */
52                 basedir_arg = xmalloc(strlen(argv[2]) + 11);
53                 sprintf(basedir_arg, "--prefix=%s/", argv[2]);
54                 nargv[nargc++] = basedir_arg;
55                 /* fallthru */
56         case 2:
57                 /* tree-ish */
58                 nargv[nargc++] = argv[1];
59         }
60         nargv[nargc] = NULL;
61
62         fprintf(stderr,
63                 "*** \"git tar-tree\" is now deprecated.\n"
64                 "*** Running \"git archive\" instead.\n***");
65         for (i = 0; i < nargc; i++) {
66                 fputc(' ', stderr);
67                 sq_quote_print(stderr, nargv[i]);
68         }
69         fputc('\n', stderr);
70         return cmd_archive(nargc, nargv, prefix);
71 }
72
73 /* ustar header + extended global header content */
74 #define RECORDSIZE      (512)
75 #define HEADERSIZE (2 * RECORDSIZE)
76
77 int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
78 {
79         char buffer[HEADERSIZE];
80         struct ustar_header *header = (struct ustar_header *)buffer;
81         char *content = buffer + RECORDSIZE;
82         ssize_t n;
83
84         n = read_in_full(0, buffer, HEADERSIZE);
85         if (n < HEADERSIZE)
86                 die("git get-tar-commit-id: read error");
87         if (header->typeflag[0] != 'g')
88                 return 1;
89         if (memcmp(content, "52 comment=", 11))
90                 return 1;
91
92         n = write_in_full(1, content + 11, 41);
93         if (n < 41)
94                 die_errno("git get-tar-commit-id: write error");
95
96         return 0;
97 }