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