Git fork
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
12static void create_output_file(const char *output_file)
13{
14 int output_fd = xopen(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
15 if (output_fd != 1) {
16 if (dup2(output_fd, 1) < 0)
17 die_errno(_("could not redirect output"));
18 else
19 close(output_fd);
20 }
21}
22
23static int run_remote_archiver(int argc, const char **argv,
24 const char *remote, const char *exec,
25 const char *name_hint)
26{
27 int fd[2], i, rv;
28 struct transport *transport;
29 struct remote *_remote;
30 struct packet_reader reader;
31
32 _remote = remote_get(remote);
33 transport = transport_get(_remote, _remote->url.v[0]);
34 transport_connect(transport, "git-upload-archive", exec, fd);
35
36 /*
37 * Inject a fake --format field at the beginning of the
38 * arguments, with the format inferred from our output
39 * filename. This way explicit --format options can override
40 * it.
41 */
42 if (name_hint) {
43 const char *format = archive_format_from_filename(name_hint);
44 if (format)
45 packet_write_fmt(fd[1], "argument --format=%s\n", format);
46 }
47 for (i = 1; i < argc; i++)
48 packet_write_fmt(fd[1], "argument %s\n", argv[i]);
49 packet_flush(fd[1]);
50
51 packet_reader_init(&reader, fd[0], NULL, 0,
52 PACKET_READ_CHOMP_NEWLINE |
53 PACKET_READ_DIE_ON_ERR_PACKET);
54
55 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
56 die(_("git archive: expected ACK/NAK, got a flush packet"));
57 if (strcmp(reader.line, "ACK")) {
58 if (starts_with(reader.line, "NACK "))
59 die(_("git archive: NACK %s"), reader.line + 5);
60 die(_("git archive: protocol error"));
61 }
62
63 if (packet_reader_read(&reader) != PACKET_READ_FLUSH)
64 die(_("git archive: expected a flush"));
65
66 /* Now, start reading from fd[0] and spit it out to stdout */
67 rv = recv_sideband("archive", fd[0], 1);
68 rv |= transport_disconnect(transport);
69
70 return !!rv;
71}
72
73#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
74 PARSE_OPT_KEEP_ARGV0 | \
75 PARSE_OPT_KEEP_UNKNOWN_OPT | \
76 PARSE_OPT_NO_INTERNAL_HELP )
77
78int cmd_archive(int argc,
79 const char **argv,
80 const char *prefix,
81 struct repository *repo)
82{
83 const char *exec = "git-upload-archive";
84 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 int ret;
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 ret = run_remote_archiver(argc, argv, remote, exec, output);
107 goto out;
108 }
109
110 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
111
112 ret = write_archive(argc, argv, prefix, repo, output, 0);
113
114out:
115 free(output);
116 return ret;
117}