Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Franck Bui-Huu |
| 3 | * Copyright (c) 2006 Rene Scharfe |
| 4 | */ |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 5 | #include "cache.h" |
| 6 | #include "builtin.h" |
| 7 | #include "archive.h" |
Ilari Liusvaara | b236752 | 2009-12-09 17:26:33 +0200 | [diff] [blame] | 8 | #include "transport.h" |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 9 | #include "parse-options.h" |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 10 | #include "pkt-line.h" |
Junio C Hamano | 23d6d11 | 2006-09-10 03:33:34 -0700 | [diff] [blame] | 11 | #include "sideband.h" |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 12 | |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 13 | static 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ð Bjarmason | 788a375 | 2011-02-22 23:42:19 +0000 | [diff] [blame] | 17 | die_errno(_("could not create archive file '%s'"), output_file); |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 18 | if (output_fd != 1) { |
| 19 | if (dup2(output_fd, 1) < 0) |
Ævar Arnfjörð Bjarmason | 788a375 | 2011-02-22 23:42:19 +0000 | [diff] [blame] | 20 | die_errno(_("could not redirect output")); |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 21 | else |
| 22 | close(output_fd); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | static int run_remote_archiver(int argc, const char **argv, |
Jeff King | 56baa61 | 2011-06-21 21:24:48 -0400 | [diff] [blame] | 27 | const char *remote, const char *exec, |
| 28 | const char *name_hint) |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 29 | { |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 30 | char *buf; |
| 31 | int fd[2], i, rv; |
Ilari Liusvaara | b236752 | 2009-12-09 17:26:33 +0200 | [diff] [blame] | 32 | struct transport *transport; |
| 33 | struct remote *_remote; |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 34 | |
Ilari Liusvaara | b236752 | 2009-12-09 17:26:33 +0200 | [diff] [blame] | 35 | _remote = remote_get(remote); |
| 36 | if (!_remote->url[0]) |
Ævar Arnfjörð Bjarmason | 788a375 | 2011-02-22 23:42:19 +0000 | [diff] [blame] | 37 | die(_("git archive: Remote with no URL")); |
Ilari Liusvaara | b236752 | 2009-12-09 17:26:33 +0200 | [diff] [blame] | 38 | transport = transport_get(_remote, _remote->url[0]); |
| 39 | transport_connect(transport, "git-upload-archive", exec, fd); |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 40 | |
Jeff King | 56baa61 | 2011-06-21 21:24:48 -0400 | [diff] [blame] | 41 | /* |
| 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 Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 50 | packet_write_fmt(fd[1], "argument --format=%s\n", format); |
Jeff King | 56baa61 | 2011-06-21 21:24:48 -0400 | [diff] [blame] | 51 | } |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 52 | for (i = 1; i < argc; i++) |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 53 | packet_write_fmt(fd[1], "argument %s\n", argv[i]); |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 54 | packet_flush(fd[1]); |
| 55 | |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 56 | buf = packet_read_line(fd[0], NULL); |
| 57 | if (!buf) |
Ævar Arnfjörð Bjarmason | 788a375 | 2011-02-22 23:42:19 +0000 | [diff] [blame] | 58 | die(_("git archive: expected ACK/NAK, got EOF")); |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 59 | if (strcmp(buf, "ACK")) { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 60 | if (starts_with(buf, "NACK ")) |
Ævar Arnfjörð Bjarmason | 788a375 | 2011-02-22 23:42:19 +0000 | [diff] [blame] | 61 | die(_("git archive: NACK %s"), buf + 5); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 62 | if (starts_with(buf, "ERR ")) |
Ilari Liusvaara | 908aace | 2011-10-03 14:01:59 +0300 | [diff] [blame] | 63 | die(_("remote error: %s"), buf + 4); |
Ævar Arnfjörð Bjarmason | 788a375 | 2011-02-22 23:42:19 +0000 | [diff] [blame] | 64 | die(_("git archive: protocol error")); |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 67 | if (packet_read_line(fd[0], NULL)) |
Ævar Arnfjörð Bjarmason | 788a375 | 2011-02-22 23:42:19 +0000 | [diff] [blame] | 68 | die(_("git archive: expected a flush")); |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 69 | |
| 70 | /* Now, start reading from fd[0] and spit it out to stdout */ |
Johannes Sixt | 34df8ab | 2009-03-10 22:54:17 +0100 | [diff] [blame] | 71 | rv = recv_sideband("archive", fd[0], 1); |
Ilari Liusvaara | b236752 | 2009-12-09 17:26:33 +0200 | [diff] [blame] | 72 | rv |= transport_disconnect(transport); |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 73 | |
| 74 | return !!rv; |
| 75 | } |
| 76 | |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 77 | #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 Hamano | 37f9443 | 2006-09-09 23:48:03 -0700 | [diff] [blame] | 81 | |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 82 | int cmd_archive(int argc, const char **argv, const char *prefix) |
| 83 | { |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 84 | const char *exec = "git-upload-archive"; |
| 85 | const char *output = NULL; |
Junio C Hamano | 37f9443 | 2006-09-09 23:48:03 -0700 | [diff] [blame] | 86 | const char *remote = NULL; |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 87 | struct option local_opts[] = { |
Junio C Hamano | eb0224c | 2016-11-22 13:37:04 -0800 | [diff] [blame] | 88 | OPT_FILENAME('o', "output", &output, |
| 89 | N_("write the archive to this file")), |
Nguyễn Thái Ngọc Duy | 0012a38 | 2012-08-20 19:31:51 +0700 | [diff] [blame] | 90 | OPT_STRING(0, "remote", &remote, N_("repo"), |
| 91 | N_("retrieve the archive from remote repository <repo>")), |
Nguyễn Thái Ngọc Duy | b0ff965 | 2012-08-20 19:32:54 +0700 | [diff] [blame] | 92 | OPT_STRING(0, "exec", &exec, N_("command"), |
Nguyễn Thái Ngọc Duy | 0012a38 | 2012-08-20 19:31:51 +0700 | [diff] [blame] | 93 | N_("path to the remote git-upload-archive command")), |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 94 | OPT_END() |
| 95 | }; |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 96 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 97 | argc = parse_options(argc, argv, prefix, local_opts, NULL, |
| 98 | PARSE_OPT_KEEP_ALL); |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 99 | |
Jeff King | 56baa61 | 2011-06-21 21:24:48 -0400 | [diff] [blame] | 100 | if (output) |
René Scharfe | 52e7787 | 2009-03-08 19:21:53 +0100 | [diff] [blame] | 101 | create_output_file(output); |
| 102 | |
Junio C Hamano | 37f9443 | 2006-09-09 23:48:03 -0700 | [diff] [blame] | 103 | if (remote) |
Jeff King | 56baa61 | 2011-06-21 21:24:48 -0400 | [diff] [blame] | 104 | return run_remote_archiver(argc, argv, remote, exec, output); |
Junio C Hamano | 37f9443 | 2006-09-09 23:48:03 -0700 | [diff] [blame] | 105 | |
Michal Rokos | aa90986 | 2006-11-21 23:19:28 +0100 | [diff] [blame] | 106 | setvbuf(stderr, NULL, _IOLBF, BUFSIZ); |
Junio C Hamano | 8142f60 | 2006-09-10 04:16:39 -0700 | [diff] [blame] | 107 | |
Junio C Hamano | eb0224c | 2016-11-22 13:37:04 -0800 | [diff] [blame] | 108 | return write_archive(argc, argv, prefix, output, 0); |
Franck Bui-Huu | 4df096a | 2006-09-07 15:12:02 +0200 | [diff] [blame] | 109 | } |