make sure packs to be replaced are closed beforehand
[git/spearce.git] / builtin-archive.c
blob5ceec433fd590e8bf6a51700ea69c37f9af30fa7
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 "pkt-line.h"
9 #include "sideband.h"
11 static int run_remote_archiver(const char *remote, int argc,
12 const char **argv)
14 char *url, buf[LARGE_PACKET_MAX];
15 int fd[2], i, len, rv;
16 struct child_process *conn;
17 const char *exec = "git-upload-archive";
18 int exec_at = 0, exec_value_at = 0;
20 for (i = 1; i < argc; i++) {
21 const char *arg = argv[i];
22 if (!prefixcmp(arg, "--exec=")) {
23 if (exec_at)
24 die("multiple --exec specified");
25 exec = arg + 7;
26 exec_at = i;
27 } else if (!strcmp(arg, "--exec")) {
28 if (exec_at)
29 die("multiple --exec specified");
30 if (i + 1 >= argc)
31 die("option --exec requires a value");
32 exec = argv[i + 1];
33 exec_at = i;
34 exec_value_at = ++i;
38 url = xstrdup(remote);
39 conn = git_connect(fd, url, exec, 0);
41 for (i = 1; i < argc; i++) {
42 if (i == exec_at || i == exec_value_at)
43 continue;
44 packet_write(fd[1], "argument %s\n", argv[i]);
46 packet_flush(fd[1]);
48 len = packet_read_line(fd[0], buf, sizeof(buf));
49 if (!len)
50 die("git archive: expected ACK/NAK, got EOF");
51 if (buf[len-1] == '\n')
52 buf[--len] = 0;
53 if (strcmp(buf, "ACK")) {
54 if (len > 5 && !prefixcmp(buf, "NACK "))
55 die("git archive: NACK %s", buf + 5);
56 die("git archive: protocol error");
59 len = packet_read_line(fd[0], buf, sizeof(buf));
60 if (len)
61 die("git archive: expected a flush");
63 /* Now, start reading from fd[0] and spit it out to stdout */
64 rv = recv_sideband("archive", fd[0], 1, 2);
65 close(fd[0]);
66 close(fd[1]);
67 rv |= finish_connect(conn);
69 return !!rv;
72 static const char *extract_remote_arg(int *ac, const char **av)
74 int ix, iy, cnt = *ac;
75 int no_more_options = 0;
76 const char *remote = NULL;
78 for (ix = iy = 1; ix < cnt; ix++) {
79 const char *arg = av[ix];
80 if (!strcmp(arg, "--"))
81 no_more_options = 1;
82 if (!no_more_options) {
83 if (!prefixcmp(arg, "--remote=")) {
84 if (remote)
85 die("Multiple --remote specified");
86 remote = arg + 9;
87 continue;
88 } else if (!strcmp(arg, "--remote")) {
89 if (remote)
90 die("Multiple --remote specified");
91 if (++ix >= cnt)
92 die("option --remote requires a value");
93 remote = av[ix];
94 continue;
96 if (arg[0] != '-')
97 no_more_options = 1;
99 if (ix != iy)
100 av[iy] = arg;
101 iy++;
103 if (remote) {
104 av[--cnt] = NULL;
105 *ac = cnt;
107 return remote;
110 int cmd_archive(int argc, const char **argv, const char *prefix)
112 const char *remote = NULL;
114 remote = extract_remote_arg(&argc, argv);
115 if (remote)
116 return run_remote_archiver(remote, argc, argv);
118 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
120 return write_archive(argc, argv, prefix, 1);