]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/archive.c
parse-options: simplify positivation handling
[thirdparty/git.git] / builtin / archive.c
1 /*
2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
4 */
5 #include "builtin.h"
6 #include "archive.h"
7 #include "gettext.h"
8 #include "transport.h"
9 #include "parse-options.h"
10 #include "pkt-line.h"
11 #include "repository.h"
12 #include "sideband.h"
13
14 static void create_output_file(const char *output_file)
15 {
16 int output_fd = xopen(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
17 if (output_fd != 1) {
18 if (dup2(output_fd, 1) < 0)
19 die_errno(_("could not redirect output"));
20 else
21 close(output_fd);
22 }
23 }
24
25 static int run_remote_archiver(int argc, const char **argv,
26 const char *remote, const char *exec,
27 const char *name_hint)
28 {
29 int fd[2], i, rv;
30 struct transport *transport;
31 struct remote *_remote;
32 struct packet_reader reader;
33
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);
39
40 /*
41 * Inject a fake --format field at the beginning of the
42 * arguments, with the format inferred from our output
43 * filename. This way explicit --format options can override
44 * it.
45 */
46 if (name_hint) {
47 const char *format = archive_format_from_filename(name_hint);
48 if (format)
49 packet_write_fmt(fd[1], "argument --format=%s\n", format);
50 }
51 for (i = 1; i < argc; i++)
52 packet_write_fmt(fd[1], "argument %s\n", argv[i]);
53 packet_flush(fd[1]);
54
55 packet_reader_init(&reader, fd[0], NULL, 0,
56 PACKET_READ_CHOMP_NEWLINE |
57 PACKET_READ_DIE_ON_ERR_PACKET);
58
59 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
60 die(_("git archive: expected ACK/NAK, got a flush packet"));
61 if (strcmp(reader.line, "ACK")) {
62 if (starts_with(reader.line, "NACK "))
63 die(_("git archive: NACK %s"), reader.line + 5);
64 die(_("git archive: protocol error"));
65 }
66
67 if (packet_reader_read(&reader) != PACKET_READ_FLUSH)
68 die(_("git archive: expected a flush"));
69
70 /* Now, start reading from fd[0] and spit it out to stdout */
71 rv = recv_sideband("archive", fd[0], 1);
72 rv |= transport_disconnect(transport);
73
74 return !!rv;
75 }
76
77 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
78 PARSE_OPT_KEEP_ARGV0 | \
79 PARSE_OPT_KEEP_UNKNOWN_OPT | \
80 PARSE_OPT_NO_INTERNAL_HELP )
81
82 int cmd_archive(int argc, const char **argv, const char *prefix)
83 {
84 const char *exec = "git-upload-archive";
85 char *output = NULL;
86 const char *remote = NULL;
87 struct option local_opts[] = {
88 OPT_FILENAME('o', "output", &output,
89 N_("write the archive to this file")),
90 OPT_STRING(0, "remote", &remote, N_("repo"),
91 N_("retrieve the archive from remote repository <repo>")),
92 OPT_STRING(0, "exec", &exec, N_("command"),
93 N_("path to the remote git-upload-archive command")),
94 OPT_END()
95 };
96
97 argc = parse_options(argc, argv, prefix, local_opts, NULL,
98 PARSE_OPT_KEEP_ALL);
99
100 init_archivers();
101
102 if (output)
103 create_output_file(output);
104
105 if (remote)
106 return run_remote_archiver(argc, argv, remote, exec, output);
107
108 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
109
110 UNLEAK(output);
111 return write_archive(argc, argv, prefix, the_repository, output, 0);
112 }