perl/Makefile: update NO_PERL_MAKEMAKER section
[git/git-p4.git] / builtin-archive.c
blobdf9772469681285d69f968b59172c4aaee2eb679
1 /*
2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
4 */
5 #include "cache.h"
6 #include "builtin.h"
7 #include "archive.h"
8 #include "commit.h"
9 #include "tree-walk.h"
10 #include "pkt-line.h"
11 #include "sideband.h"
13 static const char archive_usage[] = \
14 "git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
16 #define USES_ZLIB_COMPRESSION 1
18 const struct archiver archivers[] = {
19 { "tar", write_tar_archive },
20 { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
23 static int run_remote_archiver(const char *remote, int argc,
24 const char **argv)
26 char *url, buf[LARGE_PACKET_MAX];
27 int fd[2], i, len, rv;
28 struct child_process *conn;
29 const char *exec = "git-upload-archive";
30 int exec_at = 0;
32 for (i = 1; i < argc; i++) {
33 const char *arg = argv[i];
34 if (!prefixcmp(arg, "--exec=")) {
35 if (exec_at)
36 die("multiple --exec specified");
37 exec = arg + 7;
38 exec_at = i;
39 break;
43 url = xstrdup(remote);
44 conn = git_connect(fd, url, exec, 0);
46 for (i = 1; i < argc; i++) {
47 if (i == exec_at)
48 continue;
49 packet_write(fd[1], "argument %s\n", argv[i]);
51 packet_flush(fd[1]);
53 len = packet_read_line(fd[0], buf, sizeof(buf));
54 if (!len)
55 die("git-archive: expected ACK/NAK, got EOF");
56 if (buf[len-1] == '\n')
57 buf[--len] = 0;
58 if (strcmp(buf, "ACK")) {
59 if (len > 5 && !prefixcmp(buf, "NACK "))
60 die("git-archive: NACK %s", buf + 5);
61 die("git-archive: protocol error");
64 len = packet_read_line(fd[0], buf, sizeof(buf));
65 if (len)
66 die("git-archive: expected a flush");
68 /* Now, start reading from fd[0] and spit it out to stdout */
69 rv = recv_sideband("archive", fd[0], 1, 2);
70 close(fd[0]);
71 close(fd[1]);
72 rv |= finish_connect(conn);
74 return !!rv;
77 static const struct archiver *lookup_archiver(const char *name)
79 int i;
81 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
82 if (!strcmp(name, archivers[i].name))
83 return &archivers[i];
85 return NULL;
88 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
90 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
93 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
94 const char *prefix)
96 const char *name = argv[0];
97 const unsigned char *commit_sha1;
98 time_t archive_time;
99 struct tree *tree;
100 const struct commit *commit;
101 unsigned char sha1[20];
103 if (get_sha1(name, sha1))
104 die("Not a valid object name");
106 commit = lookup_commit_reference_gently(sha1, 1);
107 if (commit) {
108 commit_sha1 = commit->object.sha1;
109 archive_time = commit->date;
110 } else {
111 commit_sha1 = NULL;
112 archive_time = time(NULL);
115 tree = parse_tree_indirect(sha1);
116 if (tree == NULL)
117 die("not a tree object");
119 if (prefix) {
120 unsigned char tree_sha1[20];
121 unsigned int mode;
122 int err;
124 err = get_tree_entry(tree->object.sha1, prefix,
125 tree_sha1, &mode);
126 if (err || !S_ISDIR(mode))
127 die("current working directory is untracked");
129 tree = parse_tree_indirect(tree_sha1);
131 ar_args->tree = tree;
132 ar_args->commit_sha1 = commit_sha1;
133 ar_args->commit = commit;
134 ar_args->time = archive_time;
137 int parse_archive_args(int argc, const char **argv, const struct archiver **ar,
138 struct archiver_args *args)
140 const char *format = "tar";
141 const char *base = "";
142 int compression_level = -1;
143 int verbose = 0;
144 int i;
146 for (i = 1; i < argc; i++) {
147 const char *arg = argv[i];
149 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
150 for (i = 0; i < ARRAY_SIZE(archivers); i++)
151 printf("%s\n", archivers[i].name);
152 exit(0);
154 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
155 verbose = 1;
156 continue;
158 if (!prefixcmp(arg, "--format=")) {
159 format = arg + 9;
160 continue;
162 if (!prefixcmp(arg, "--prefix=")) {
163 base = arg + 9;
164 continue;
166 if (!strcmp(arg, "--")) {
167 i++;
168 break;
170 if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') {
171 compression_level = arg[1] - '0';
172 continue;
174 if (arg[0] == '-')
175 die("Unknown argument: %s", arg);
176 break;
179 /* We need at least one parameter -- tree-ish */
180 if (argc - 1 < i)
181 usage(archive_usage);
182 *ar = lookup_archiver(format);
183 if (!*ar)
184 die("Unknown archive format '%s'", format);
186 args->compression_level = Z_DEFAULT_COMPRESSION;
187 if (compression_level != -1) {
188 if ((*ar)->flags & USES_ZLIB_COMPRESSION)
189 args->compression_level = compression_level;
190 else {
191 die("Argument not supported for format '%s': -%d",
192 format, compression_level);
195 args->verbose = verbose;
196 args->base = base;
197 args->baselen = strlen(base);
199 return i;
202 static const char *extract_remote_arg(int *ac, const char **av)
204 int ix, iy, cnt = *ac;
205 int no_more_options = 0;
206 const char *remote = NULL;
208 for (ix = iy = 1; ix < cnt; ix++) {
209 const char *arg = av[ix];
210 if (!strcmp(arg, "--"))
211 no_more_options = 1;
212 if (!no_more_options) {
213 if (!prefixcmp(arg, "--remote=")) {
214 if (remote)
215 die("Multiple --remote specified");
216 remote = arg + 9;
217 continue;
219 if (arg[0] != '-')
220 no_more_options = 1;
222 if (ix != iy)
223 av[iy] = arg;
224 iy++;
226 if (remote) {
227 av[--cnt] = NULL;
228 *ac = cnt;
230 return remote;
233 int cmd_archive(int argc, const char **argv, const char *prefix)
235 const struct archiver *ar = NULL;
236 struct archiver_args args;
237 int tree_idx;
238 const char *remote = NULL;
240 remote = extract_remote_arg(&argc, argv);
241 if (remote)
242 return run_remote_archiver(remote, argc, argv);
244 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
246 tree_idx = parse_archive_args(argc, argv, &ar, &args);
247 if (prefix == NULL)
248 prefix = setup_git_directory();
250 argv += tree_idx;
251 parse_treeish_arg(argv, &args, prefix);
252 parse_pathspec_arg(argv + 1, &args);
254 return ar->write_archive(&args);