archive: make zip compression level independent from core git
[git/git.git] / builtin-archive.c
CommitLineData
4df096a5
FBH
1/*
2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
4 */
4df096a5
FBH
5#include "cache.h"
6#include "builtin.h"
7#include "archive.h"
8#include "commit.h"
9#include "tree-walk.h"
10#include "exec_cmd.h"
11#include "pkt-line.h"
23d6d112 12#include "sideband.h"
8460b2fc 13#include "attr.h"
4df096a5
FBH
14
15static const char archive_usage[] = \
1b1dd23f 16"git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
4df096a5 17
489e351e
RS
18#define USES_ZLIB_COMPRESSION 1
19
34533004 20const struct archiver archivers[] = {
489e351e
RS
21 { "tar", write_tar_archive },
22 { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
4df096a5
FBH
23};
24
37f94436 25static int run_remote_archiver(const char *remote, int argc,
4df096a5
FBH
26 const char **argv)
27{
23d6d112 28 char *url, buf[LARGE_PACKET_MAX];
4df096a5 29 int fd[2], i, len, rv;
98158e9c 30 struct child_process *conn;
fe5ab763
JH
31 const char *exec = "git-upload-archive";
32 int exec_at = 0;
4df096a5 33
fe5ab763
JH
34 for (i = 1; i < argc; i++) {
35 const char *arg = argv[i];
599065a3 36 if (!prefixcmp(arg, "--exec=")) {
fe5ab763
JH
37 if (exec_at)
38 die("multiple --exec specified");
39 exec = arg + 7;
40 exec_at = i;
41 break;
42 }
43 }
4df096a5 44
37f94436 45 url = xstrdup(remote);
98158e9c 46 conn = git_connect(fd, url, exec, 0);
4df096a5 47
fe5ab763
JH
48 for (i = 1; i < argc; i++) {
49 if (i == exec_at)
50 continue;
4df096a5 51 packet_write(fd[1], "argument %s\n", argv[i]);
fe5ab763 52 }
4df096a5
FBH
53 packet_flush(fd[1]);
54
55 len = packet_read_line(fd[0], buf, sizeof(buf));
56 if (!len)
57 die("git-archive: expected ACK/NAK, got EOF");
58 if (buf[len-1] == '\n')
59 buf[--len] = 0;
60 if (strcmp(buf, "ACK")) {
cc44c765 61 if (len > 5 && !prefixcmp(buf, "NACK "))
4df096a5
FBH
62 die("git-archive: NACK %s", buf + 5);
63 die("git-archive: protocol error");
64 }
65
66 len = packet_read_line(fd[0], buf, sizeof(buf));
67 if (len)
68 die("git-archive: expected a flush");
69
70 /* Now, start reading from fd[0] and spit it out to stdout */
9ac13ec9 71 rv = recv_sideband("archive", fd[0], 1, 2);
4df096a5 72 close(fd[0]);
ec587fde 73 close(fd[1]);
98158e9c 74 rv |= finish_connect(conn);
4df096a5
FBH
75
76 return !!rv;
77}
78
34533004 79static const struct archiver *lookup_archiver(const char *name)
4df096a5 80{
34533004 81 int i;
4df096a5
FBH
82
83 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
34533004
RS
84 if (!strcmp(name, archivers[i].name))
85 return &archivers[i];
4df096a5 86 }
34533004 87 return NULL;
4df096a5
FBH
88}
89
90void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
91{
92 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
93}
94
95void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
96 const char *prefix)
97{
98 const char *name = argv[0];
99 const unsigned char *commit_sha1;
100 time_t archive_time;
101 struct tree *tree;
8460b2fc 102 const struct commit *commit;
4df096a5
FBH
103 unsigned char sha1[20];
104
105 if (get_sha1(name, sha1))
106 die("Not a valid object name");
107
108 commit = lookup_commit_reference_gently(sha1, 1);
109 if (commit) {
110 commit_sha1 = commit->object.sha1;
111 archive_time = commit->date;
112 } else {
113 commit_sha1 = NULL;
114 archive_time = time(NULL);
115 }
116
117 tree = parse_tree_indirect(sha1);
118 if (tree == NULL)
119 die("not a tree object");
120
121 if (prefix) {
122 unsigned char tree_sha1[20];
123 unsigned int mode;
124 int err;
125
126 err = get_tree_entry(tree->object.sha1, prefix,
127 tree_sha1, &mode);
128 if (err || !S_ISDIR(mode))
129 die("current working directory is untracked");
130
4df096a5
FBH
131 tree = parse_tree_indirect(tree_sha1);
132 }
133 ar_args->tree = tree;
134 ar_args->commit_sha1 = commit_sha1;
8460b2fc 135 ar_args->commit = commit;
4df096a5
FBH
136 ar_args->time = archive_time;
137}
138
34533004
RS
139int parse_archive_args(int argc, const char **argv, const struct archiver **ar,
140 struct archiver_args *args)
4df096a5 141{
8ff21b1a 142 const char *format = "tar";
4df096a5 143 const char *base = "";
489e351e 144 int compression_level = -1;
e0ffb248 145 int verbose = 0;
4df096a5
FBH
146 int i;
147
148 for (i = 1; i < argc; i++) {
149 const char *arg = argv[i];
150
151 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
37f94436
JH
152 for (i = 0; i < ARRAY_SIZE(archivers); i++)
153 printf("%s\n", archivers[i].name);
154 exit(0);
4df096a5 155 }
e0ffb248
JH
156 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
157 verbose = 1;
158 continue;
159 }
cc44c765 160 if (!prefixcmp(arg, "--format=")) {
4df096a5
FBH
161 format = arg + 9;
162 continue;
163 }
cc44c765 164 if (!prefixcmp(arg, "--prefix=")) {
4df096a5
FBH
165 base = arg + 9;
166 continue;
167 }
4df096a5
FBH
168 if (!strcmp(arg, "--")) {
169 i++;
170 break;
171 }
489e351e
RS
172 if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') {
173 compression_level = arg[1] - '0';
4df096a5
FBH
174 continue;
175 }
489e351e
RS
176 if (arg[0] == '-')
177 die("Unknown argument: %s", arg);
4df096a5
FBH
178 break;
179 }
180
37f94436
JH
181 /* We need at least one parameter -- tree-ish */
182 if (argc - 1 < i)
4df096a5 183 usage(archive_usage);
34533004
RS
184 *ar = lookup_archiver(format);
185 if (!*ar)
4df096a5
FBH
186 die("Unknown archive format '%s'", format);
187
3a176c6c 188 args->compression_level = Z_DEFAULT_COMPRESSION;
489e351e
RS
189 if (compression_level != -1) {
190 if ((*ar)->flags & USES_ZLIB_COMPRESSION)
3a176c6c 191 args->compression_level = compression_level;
489e351e
RS
192 else {
193 die("Argument not supported for format '%s': -%d",
194 format, compression_level);
195 }
4df096a5 196 }
34533004
RS
197 args->verbose = verbose;
198 args->base = base;
d53fe818 199 args->baselen = strlen(base);
4df096a5
FBH
200
201 return i;
202}
203
d751864c 204static const char *extract_remote_arg(int *ac, const char **av)
37f94436
JH
205{
206 int ix, iy, cnt = *ac;
207 int no_more_options = 0;
208 const char *remote = NULL;
209
210 for (ix = iy = 1; ix < cnt; ix++) {
211 const char *arg = av[ix];
212 if (!strcmp(arg, "--"))
213 no_more_options = 1;
214 if (!no_more_options) {
cc44c765 215 if (!prefixcmp(arg, "--remote=")) {
37f94436
JH
216 if (remote)
217 die("Multiple --remote specified");
218 remote = arg + 9;
219 continue;
220 }
221 if (arg[0] != '-')
222 no_more_options = 1;
223 }
224 if (ix != iy)
225 av[iy] = arg;
226 iy++;
227 }
228 if (remote) {
229 av[--cnt] = NULL;
230 *ac = cnt;
231 }
232 return remote;
233}
234
4df096a5
FBH
235int cmd_archive(int argc, const char **argv, const char *prefix)
236{
34533004
RS
237 const struct archiver *ar = NULL;
238 struct archiver_args args;
4df096a5 239 int tree_idx;
37f94436 240 const char *remote = NULL;
4df096a5 241
d751864c 242 remote = extract_remote_arg(&argc, argv);
37f94436
JH
243 if (remote)
244 return run_remote_archiver(remote, argc, argv);
4df096a5 245
aa909861 246 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
8142f603 247
34533004 248 tree_idx = parse_archive_args(argc, argv, &ar, &args);
265d5280
RS
249 if (prefix == NULL)
250 prefix = setup_git_directory();
4df096a5
FBH
251
252 argv += tree_idx;
34533004
RS
253 parse_treeish_arg(argv, &args, prefix);
254 parse_pathspec_arg(argv + 1, &args);
4df096a5 255
34533004 256 return ar->write_archive(&args);
4df096a5 257}