Revert "MinGW: Add missing file mode bit defines"
[git/dscho.git] / builtin / archive.c
blobe405566c5eabe03160e77dec4e7843f35f4e78fb
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)
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);
26 static int run_remote_archiver(int argc, const char **argv,
27 const char *remote, const char *exec,
28 const char *name_hint)
30 char buf[LARGE_PACKET_MAX];
31 int fd[2], i, len, rv;
32 struct transport *transport;
33 struct remote *_remote;
35 _remote = remote_get(remote);
36 if (!_remote->url[0])
37 die(_("git archive: Remote with no URL"));
38 transport = transport_get(_remote, _remote->url[0]);
39 transport_connect(transport, "git-upload-archive", exec, fd);
42 * Inject a fake --format field at the beginning of the
43 * arguments, with the format inferred from our output
44 * filename. This way explicit --format options can override
45 * it.
47 if (name_hint) {
48 const char *format = archive_format_from_filename(name_hint);
49 if (format)
50 packet_write(fd[1], "argument --format=%s\n", format);
52 for (i = 1; i < argc; i++)
53 packet_write(fd[1], "argument %s\n", argv[i]);
54 packet_flush(fd[1]);
56 len = packet_read_line(fd[0], buf, sizeof(buf));
57 if (!len)
58 die(_("git archive: expected ACK/NAK, got EOF"));
59 if (buf[len-1] == '\n')
60 buf[--len] = 0;
61 if (strcmp(buf, "ACK")) {
62 if (len > 5 && !prefixcmp(buf, "NACK "))
63 die(_("git archive: NACK %s"), buf + 5);
64 if (len > 4 && !prefixcmp(buf, "ERR "))
65 die(_("remote error: %s"), buf + 4);
66 die(_("git archive: protocol error"));
69 len = packet_read_line(fd[0], buf, sizeof(buf));
70 if (len)
71 die(_("git archive: expected a flush"));
73 /* Now, start reading from fd[0] and spit it out to stdout */
74 rv = recv_sideband("archive", fd[0], 1);
75 rv |= transport_disconnect(transport);
77 return !!rv;
80 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
81 PARSE_OPT_KEEP_ARGV0 | \
82 PARSE_OPT_KEEP_UNKNOWN | \
83 PARSE_OPT_NO_INTERNAL_HELP )
85 int cmd_archive(int argc, const char **argv, const char *prefix)
87 const char *exec = "git-upload-archive";
88 const char *output = NULL;
89 const char *remote = NULL;
90 int is_remote = 0;
91 struct option local_opts[] = {
92 OPT_STRING('o', "output", &output, "file",
93 "write the archive to this file"),
94 OPT_STRING(0, "remote", &remote, "repo",
95 "retrieve the archive from remote repository <repo>"),
96 OPT_STRING(0, "exec", &exec, "cmd",
97 "path to the remote git-upload-archive command"),
98 { OPTION_BOOLEAN, 0, "remote-request", &is_remote, NULL,
99 "indicate we are serving a remote request",
100 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
101 OPT_END()
104 argc = parse_options(argc, argv, prefix, local_opts, NULL,
105 PARSE_OPT_KEEP_ALL);
107 if (output)
108 create_output_file(output);
110 if (remote)
111 return run_remote_archiver(argc, argv, remote, exec, output);
113 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
115 return write_archive(argc, argv, prefix, 1, output, is_remote);