]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-archive.c
Rewrite convert_to_{git,working_tree} to use strbuf's.
[thirdparty/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[] = \
e0ffb248 16"git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
4df096a5 17
6c2f207b
SP
18static struct archiver_desc
19{
20 const char *name;
21 write_archive_fn_t write_archive;
22 parse_extra_args_fn_t parse_extra;
23} archivers[] = {
24 { "tar", write_tar_archive, NULL },
25 { "zip", write_zip_archive, parse_extra_zip_args },
4df096a5
FBH
26};
27
37f94436 28static int run_remote_archiver(const char *remote, int argc,
4df096a5
FBH
29 const char **argv)
30{
23d6d112 31 char *url, buf[LARGE_PACKET_MAX];
4df096a5
FBH
32 int fd[2], i, len, rv;
33 pid_t pid;
fe5ab763
JH
34 const char *exec = "git-upload-archive";
35 int exec_at = 0;
4df096a5 36
fe5ab763
JH
37 for (i = 1; i < argc; i++) {
38 const char *arg = argv[i];
599065a3 39 if (!prefixcmp(arg, "--exec=")) {
fe5ab763
JH
40 if (exec_at)
41 die("multiple --exec specified");
42 exec = arg + 7;
43 exec_at = i;
44 break;
45 }
46 }
4df096a5 47
37f94436 48 url = xstrdup(remote);
7841ce79 49 pid = git_connect(fd, url, exec, 0);
4df096a5
FBH
50 if (pid < 0)
51 return pid;
52
fe5ab763
JH
53 for (i = 1; i < argc; i++) {
54 if (i == exec_at)
55 continue;
4df096a5 56 packet_write(fd[1], "argument %s\n", argv[i]);
fe5ab763 57 }
4df096a5
FBH
58 packet_flush(fd[1]);
59
60 len = packet_read_line(fd[0], buf, sizeof(buf));
61 if (!len)
62 die("git-archive: expected ACK/NAK, got EOF");
63 if (buf[len-1] == '\n')
64 buf[--len] = 0;
65 if (strcmp(buf, "ACK")) {
cc44c765 66 if (len > 5 && !prefixcmp(buf, "NACK "))
4df096a5
FBH
67 die("git-archive: NACK %s", buf + 5);
68 die("git-archive: protocol error");
69 }
70
71 len = packet_read_line(fd[0], buf, sizeof(buf));
72 if (len)
73 die("git-archive: expected a flush");
74
75 /* Now, start reading from fd[0] and spit it out to stdout */
9ac13ec9 76 rv = recv_sideband("archive", fd[0], 1, 2);
4df096a5 77 close(fd[0]);
ec587fde 78 close(fd[1]);
4df096a5
FBH
79 rv |= finish_connect(pid);
80
81 return !!rv;
82}
83
5ecd293d
PH
84static void format_subst(const struct commit *commit,
85 const char *src, size_t len,
86 struct strbuf *buf)
df4a394f 87{
5ecd293d 88 char *to_free = NULL;
674d1727
PH
89 struct strbuf fmt;
90
5ecd293d
PH
91 if (src == buf->buf)
92 to_free = strbuf_detach(buf);
674d1727 93 strbuf_init(&fmt, 0);
df4a394f
RS
94 for (;;) {
95 const char *b, *c;
df4a394f 96
5ecd293d
PH
97 b = memmem(src, len, "$Format:", 8);
98 if (!b || src + len < b + 9)
df4a394f
RS
99 break;
100 c = memchr(b + 8, '$', len - 8);
101 if (!c)
102 break;
103
674d1727
PH
104 strbuf_reset(&fmt);
105 strbuf_add(&fmt, b + 8, c - b - 8);
106
5ecd293d
PH
107 strbuf_add(buf, src, b - src);
108 format_commit_message(commit, fmt.buf, buf);
109 len -= c + 1 - src;
110 src = c + 1;
df4a394f 111 }
5ecd293d 112 strbuf_add(buf, src, len);
674d1727 113 strbuf_release(&fmt);
5ecd293d 114 free(to_free);
df4a394f
RS
115}
116
5ecd293d
PH
117static int convert_to_archive(const char *path,
118 const void *src, size_t len,
119 struct strbuf *buf,
120 const struct commit *commit)
8460b2fc 121{
38c9c9b7 122 static struct git_attr *attr_export_subst;
8460b2fc 123 struct git_attr_check check[1];
8460b2fc
RS
124
125 if (!commit)
5ecd293d 126 return 0;
8460b2fc 127
5ecd293d
PH
128 if (!attr_export_subst)
129 attr_export_subst = git_attr("export-subst", 12);
8460b2fc 130
38c9c9b7 131 check[0].attr = attr_export_subst;
8460b2fc 132 if (git_checkattr(path, ARRAY_SIZE(check), check))
5ecd293d 133 return 0;
8460b2fc 134 if (!ATTR_TRUE(check[0].value))
5ecd293d 135 return 0;
8460b2fc 136
5ecd293d
PH
137 format_subst(commit, src, len, buf);
138 return 1;
8460b2fc
RS
139}
140
141void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
142 unsigned int mode, enum object_type *type,
5ecd293d 143 unsigned long *sizep,
8460b2fc
RS
144 const struct commit *commit)
145{
5ecd293d 146 void *buffer;
8460b2fc 147
5ecd293d 148 buffer = read_sha1_file(sha1, type, sizep);
8460b2fc 149 if (buffer && S_ISREG(mode)) {
5ecd293d
PH
150 struct strbuf buf;
151
152 strbuf_init(&buf, 0);
153 strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
154 convert_to_working_tree(path, buf.buf, buf.len, &buf);
155 convert_to_archive(path, buf.buf, buf.len, &buf, commit);
156 *sizep = buf.len;
157 buffer = buf.buf;
8460b2fc
RS
158 }
159
160 return buffer;
161}
162
4df096a5
FBH
163static int init_archiver(const char *name, struct archiver *ar)
164{
165 int rv = -1, i;
166
167 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
168 if (!strcmp(name, archivers[i].name)) {
6c2f207b
SP
169 memset(ar, 0, sizeof(*ar));
170 ar->name = archivers[i].name;
171 ar->write_archive = archivers[i].write_archive;
172 ar->parse_extra = archivers[i].parse_extra;
4df096a5
FBH
173 rv = 0;
174 break;
175 }
176 }
177 return rv;
178}
179
180void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
181{
182 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
183}
184
185void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
186 const char *prefix)
187{
188 const char *name = argv[0];
189 const unsigned char *commit_sha1;
190 time_t archive_time;
191 struct tree *tree;
8460b2fc 192 const struct commit *commit;
4df096a5
FBH
193 unsigned char sha1[20];
194
195 if (get_sha1(name, sha1))
196 die("Not a valid object name");
197
198 commit = lookup_commit_reference_gently(sha1, 1);
199 if (commit) {
200 commit_sha1 = commit->object.sha1;
201 archive_time = commit->date;
202 } else {
203 commit_sha1 = NULL;
204 archive_time = time(NULL);
205 }
206
207 tree = parse_tree_indirect(sha1);
208 if (tree == NULL)
209 die("not a tree object");
210
211 if (prefix) {
212 unsigned char tree_sha1[20];
213 unsigned int mode;
214 int err;
215
216 err = get_tree_entry(tree->object.sha1, prefix,
217 tree_sha1, &mode);
218 if (err || !S_ISDIR(mode))
219 die("current working directory is untracked");
220
4df096a5
FBH
221 tree = parse_tree_indirect(tree_sha1);
222 }
223 ar_args->tree = tree;
224 ar_args->commit_sha1 = commit_sha1;
8460b2fc 225 ar_args->commit = commit;
4df096a5
FBH
226 ar_args->time = archive_time;
227}
228
4df096a5
FBH
229int parse_archive_args(int argc, const char **argv, struct archiver *ar)
230{
231 const char *extra_argv[MAX_EXTRA_ARGS];
232 int extra_argc = 0;
8ff21b1a 233 const char *format = "tar";
4df096a5 234 const char *base = "";
e0ffb248 235 int verbose = 0;
4df096a5
FBH
236 int i;
237
238 for (i = 1; i < argc; i++) {
239 const char *arg = argv[i];
240
241 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
37f94436
JH
242 for (i = 0; i < ARRAY_SIZE(archivers); i++)
243 printf("%s\n", archivers[i].name);
244 exit(0);
4df096a5 245 }
e0ffb248
JH
246 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
247 verbose = 1;
248 continue;
249 }
cc44c765 250 if (!prefixcmp(arg, "--format=")) {
4df096a5
FBH
251 format = arg + 9;
252 continue;
253 }
cc44c765 254 if (!prefixcmp(arg, "--prefix=")) {
4df096a5
FBH
255 base = arg + 9;
256 continue;
257 }
4df096a5
FBH
258 if (!strcmp(arg, "--")) {
259 i++;
260 break;
261 }
262 if (arg[0] == '-') {
263 if (extra_argc > MAX_EXTRA_ARGS - 1)
264 die("Too many extra options");
265 extra_argv[extra_argc++] = arg;
266 continue;
267 }
268 break;
269 }
270
37f94436
JH
271 /* We need at least one parameter -- tree-ish */
272 if (argc - 1 < i)
4df096a5 273 usage(archive_usage);
4df096a5
FBH
274 if (init_archiver(format, ar) < 0)
275 die("Unknown archive format '%s'", format);
276
37f94436
JH
277 if (extra_argc) {
278 if (!ar->parse_extra)
2232c0c6
RS
279 die("'%s' format does not handle %s",
280 ar->name, extra_argv[0]);
4df096a5
FBH
281 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
282 }
e0ffb248 283 ar->args.verbose = verbose;
4df096a5
FBH
284 ar->args.base = base;
285
286 return i;
287}
288
d751864c 289static const char *extract_remote_arg(int *ac, const char **av)
37f94436
JH
290{
291 int ix, iy, cnt = *ac;
292 int no_more_options = 0;
293 const char *remote = NULL;
294
295 for (ix = iy = 1; ix < cnt; ix++) {
296 const char *arg = av[ix];
297 if (!strcmp(arg, "--"))
298 no_more_options = 1;
299 if (!no_more_options) {
cc44c765 300 if (!prefixcmp(arg, "--remote=")) {
37f94436
JH
301 if (remote)
302 die("Multiple --remote specified");
303 remote = arg + 9;
304 continue;
305 }
306 if (arg[0] != '-')
307 no_more_options = 1;
308 }
309 if (ix != iy)
310 av[iy] = arg;
311 iy++;
312 }
313 if (remote) {
314 av[--cnt] = NULL;
315 *ac = cnt;
316 }
317 return remote;
318}
319
4df096a5
FBH
320int cmd_archive(int argc, const char **argv, const char *prefix)
321{
322 struct archiver ar;
323 int tree_idx;
37f94436 324 const char *remote = NULL;
4df096a5 325
d751864c 326 remote = extract_remote_arg(&argc, argv);
37f94436
JH
327 if (remote)
328 return run_remote_archiver(remote, argc, argv);
4df096a5 329
aa909861 330 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
8142f603 331
37f94436
JH
332 memset(&ar, 0, sizeof(ar));
333 tree_idx = parse_archive_args(argc, argv, &ar);
265d5280
RS
334 if (prefix == NULL)
335 prefix = setup_git_directory();
4df096a5
FBH
336
337 argv += tree_idx;
338 parse_treeish_arg(argv, &ar.args, prefix);
339 parse_pathspec_arg(argv + 1, &ar.args);
340
341 return ar.write_archive(&ar.args);
342}