blob: f863465a0fa137f3446071a0ec1653adad536fdc [file] [log] [blame]
Franck Bui-Huu4df096a2006-09-07 15:12:02 +02001/*
2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
4 */
Franck Bui-Huu4df096a2006-09-07 15:12:02 +02005#include "cache.h"
6#include "builtin.h"
7#include "archive.h"
Ilari Liusvaarab2367522009-12-09 17:26:33 +02008#include "transport.h"
René Scharfe52e77872009-03-08 19:21:53 +01009#include "parse-options.h"
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020010#include "pkt-line.h"
Junio C Hamano23d6d112006-09-10 03:33:34 -070011#include "sideband.h"
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020012
René Scharfe52e77872009-03-08 19:21:53 +010013static 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)
Ævar Arnfjörð Bjarmason788a3752011-02-22 23:42:19 +000017 die_errno(_("could not create archive file '%s'"), output_file);
René Scharfe52e77872009-03-08 19:21:53 +010018 if (output_fd != 1) {
19 if (dup2(output_fd, 1) < 0)
Ævar Arnfjörð Bjarmason788a3752011-02-22 23:42:19 +000020 die_errno(_("could not redirect output"));
René Scharfe52e77872009-03-08 19:21:53 +010021 else
22 close(output_fd);
23 }
24}
25
26static int run_remote_archiver(int argc, const char **argv,
Jeff King56baa612011-06-21 21:24:48 -040027 const char *remote, const char *exec,
28 const char *name_hint)
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020029{
Jeff King74543a02013-02-20 15:02:57 -050030 char *buf;
31 int fd[2], i, rv;
Ilari Liusvaarab2367522009-12-09 17:26:33 +020032 struct transport *transport;
33 struct remote *_remote;
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020034
Ilari Liusvaarab2367522009-12-09 17:26:33 +020035 _remote = remote_get(remote);
36 if (!_remote->url[0])
Ævar Arnfjörð Bjarmason788a3752011-02-22 23:42:19 +000037 die(_("git archive: Remote with no URL"));
Ilari Liusvaarab2367522009-12-09 17:26:33 +020038 transport = transport_get(_remote, _remote->url[0]);
39 transport_connect(transport, "git-upload-archive", exec, fd);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020040
Jeff King56baa612011-06-21 21:24:48 -040041 /*
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.
46 */
47 if (name_hint) {
48 const char *format = archive_format_from_filename(name_hint);
49 if (format)
Lars Schneider81c634e2016-10-16 16:20:29 -070050 packet_write_fmt(fd[1], "argument --format=%s\n", format);
Jeff King56baa612011-06-21 21:24:48 -040051 }
René Scharfe52e77872009-03-08 19:21:53 +010052 for (i = 1; i < argc; i++)
Lars Schneider81c634e2016-10-16 16:20:29 -070053 packet_write_fmt(fd[1], "argument %s\n", argv[i]);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020054 packet_flush(fd[1]);
55
Jeff King74543a02013-02-20 15:02:57 -050056 buf = packet_read_line(fd[0], NULL);
57 if (!buf)
Ævar Arnfjörð Bjarmason788a3752011-02-22 23:42:19 +000058 die(_("git archive: expected ACK/NAK, got EOF"));
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020059 if (strcmp(buf, "ACK")) {
Christian Couder59556542013-11-30 21:55:40 +010060 if (starts_with(buf, "NACK "))
Ævar Arnfjörð Bjarmason788a3752011-02-22 23:42:19 +000061 die(_("git archive: NACK %s"), buf + 5);
Christian Couder59556542013-11-30 21:55:40 +010062 if (starts_with(buf, "ERR "))
Ilari Liusvaara908aace2011-10-03 14:01:59 +030063 die(_("remote error: %s"), buf + 4);
Ævar Arnfjörð Bjarmason788a3752011-02-22 23:42:19 +000064 die(_("git archive: protocol error"));
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020065 }
66
Jeff King74543a02013-02-20 15:02:57 -050067 if (packet_read_line(fd[0], NULL))
Ævar Arnfjörð Bjarmason788a3752011-02-22 23:42:19 +000068 die(_("git archive: expected a flush"));
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020069
70 /* Now, start reading from fd[0] and spit it out to stdout */
Johannes Sixt34df8ab2009-03-10 22:54:17 +010071 rv = recv_sideband("archive", fd[0], 1);
Ilari Liusvaarab2367522009-12-09 17:26:33 +020072 rv |= transport_disconnect(transport);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020073
74 return !!rv;
75}
76
René Scharfe52e77872009-03-08 19:21:53 +010077#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
78 PARSE_OPT_KEEP_ARGV0 | \
79 PARSE_OPT_KEEP_UNKNOWN | \
80 PARSE_OPT_NO_INTERNAL_HELP )
Junio C Hamano37f94432006-09-09 23:48:03 -070081
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020082int cmd_archive(int argc, const char **argv, const char *prefix)
83{
René Scharfe52e77872009-03-08 19:21:53 +010084 const char *exec = "git-upload-archive";
85 const char *output = NULL;
Junio C Hamano37f94432006-09-09 23:48:03 -070086 const char *remote = NULL;
René Scharfe52e77872009-03-08 19:21:53 +010087 struct option local_opts[] = {
Junio C Hamanoeb0224c2016-11-22 13:37:04 -080088 OPT_FILENAME('o', "output", &output,
89 N_("write the archive to this file")),
Nguyễn Thái Ngọc Duy0012a382012-08-20 19:31:51 +070090 OPT_STRING(0, "remote", &remote, N_("repo"),
91 N_("retrieve the archive from remote repository <repo>")),
Nguyễn Thái Ngọc Duyb0ff9652012-08-20 19:32:54 +070092 OPT_STRING(0, "exec", &exec, N_("command"),
Nguyễn Thái Ngọc Duy0012a382012-08-20 19:31:51 +070093 N_("path to the remote git-upload-archive command")),
René Scharfe52e77872009-03-08 19:21:53 +010094 OPT_END()
95 };
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020096
Stephen Boyd37782922009-05-23 11:53:12 -070097 argc = parse_options(argc, argv, prefix, local_opts, NULL,
98 PARSE_OPT_KEEP_ALL);
René Scharfe52e77872009-03-08 19:21:53 +010099
Jeff King56baa612011-06-21 21:24:48 -0400100 if (output)
René Scharfe52e77872009-03-08 19:21:53 +0100101 create_output_file(output);
102
Junio C Hamano37f94432006-09-09 23:48:03 -0700103 if (remote)
Jeff King56baa612011-06-21 21:24:48 -0400104 return run_remote_archiver(argc, argv, remote, exec, output);
Junio C Hamano37f94432006-09-09 23:48:03 -0700105
Michal Rokosaa909862006-11-21 23:19:28 +0100106 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
Junio C Hamano8142f602006-09-10 04:16:39 -0700107
Junio C Hamanoeb0224c2016-11-22 13:37:04 -0800108 return write_archive(argc, argv, prefix, output, 0);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +0200109}