Code

Support ERR in remote archive like in fetch/push
[git.git] / builtin / archive.c
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 "transport.h"
9 #include "parse-options.h"
10 #include "pkt-line.h"
11 #include "sideband.h"
13 static void create_output_file(const char *output_file)
14 {
15         int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
16         if (output_fd < 0)
17                 die_errno(_("could not create archive file '%s'"), output_file);
18         if (output_fd != 1) {
19                 if (dup2(output_fd, 1) < 0)
20                         die_errno(_("could not redirect output"));
21                 else
22                         close(output_fd);
23         }
24 }
26 static int run_remote_archiver(int argc, const char **argv,
27                                const char *remote, const char *exec)
28 {
29         char buf[LARGE_PACKET_MAX];
30         int fd[2], i, len, rv;
31         struct transport *transport;
32         struct remote *_remote;
34         _remote = remote_get(remote);
35         if (!_remote->url[0])
36                 die(_("git archive: Remote with no URL"));
37         transport = transport_get(_remote, _remote->url[0]);
38         transport_connect(transport, "git-upload-archive", exec, fd);
40         for (i = 1; i < argc; i++)
41                 packet_write(fd[1], "argument %s\n", argv[i]);
42         packet_flush(fd[1]);
44         len = packet_read_line(fd[0], buf, sizeof(buf));
45         if (!len)
46                 die(_("git archive: expected ACK/NAK, got EOF"));
47         if (buf[len-1] == '\n')
48                 buf[--len] = 0;
49         if (strcmp(buf, "ACK")) {
50                 if (len > 5 && !prefixcmp(buf, "NACK "))
51                         die(_("git archive: NACK %s"), buf + 5);
52                 if (len > 4 && !prefixcmp(buf, "ERR "))
53                         die(_("remote error: %s"), buf + 4);
54                 die(_("git archive: protocol error"));
55         }
57         len = packet_read_line(fd[0], buf, sizeof(buf));
58         if (len)
59                 die(_("git archive: expected a flush"));
61         /* Now, start reading from fd[0] and spit it out to stdout */
62         rv = recv_sideband("archive", fd[0], 1);
63         rv |= transport_disconnect(transport);
65         return !!rv;
66 }
68 static const char *format_from_name(const char *filename)
69 {
70         const char *ext = strrchr(filename, '.');
71         if (!ext)
72                 return NULL;
73         ext++;
74         if (!strcasecmp(ext, "zip"))
75                 return "--format=zip";
76         return NULL;
77 }
79 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH |  \
80                              PARSE_OPT_KEEP_ARGV0 |     \
81                              PARSE_OPT_KEEP_UNKNOWN |   \
82                              PARSE_OPT_NO_INTERNAL_HELP )
84 int cmd_archive(int argc, const char **argv, const char *prefix)
85 {
86         const char *exec = "git-upload-archive";
87         const char *output = NULL;
88         const char *remote = NULL;
89         const char *format_option = NULL;
90         struct option local_opts[] = {
91                 OPT_STRING('o', "output", &output, "file",
92                         "write the archive to this file"),
93                 OPT_STRING(0, "remote", &remote, "repo",
94                         "retrieve the archive from remote repository <repo>"),
95                 OPT_STRING(0, "exec", &exec, "cmd",
96                         "path to the remote git-upload-archive command"),
97                 OPT_END()
98         };
100         argc = parse_options(argc, argv, prefix, local_opts, NULL,
101                              PARSE_OPT_KEEP_ALL);
103         if (output) {
104                 create_output_file(output);
105                 format_option = format_from_name(output);
106         }
108         /*
109          * We have enough room in argv[] to muck it in place, because
110          * --output must have been given on the original command line
111          * if we get to this point, and parse_options() must have eaten
112          * it, i.e. we can add back one element to the array.
113          *
114          * We add a fake --format option at the beginning, with the
115          * format inferred from our output filename.  This way explicit
116          * --format options can override it, and the fake option is
117          * inserted before any "--" that might have been given.
118          */
119         if (format_option) {
120                 memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
121                 argv[1] = format_option;
122                 argv[++argc] = NULL;
123         }
125         if (remote)
126                 return run_remote_archiver(argc, argv, remote, exec);
128         setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
130         return write_archive(argc, argv, prefix, 1);