]>
Commit | Line | Data |
---|---|---|
4df096a5 FBH |
1 | /* |
2 | * Copyright (c) 2006 Franck Bui-Huu | |
3 | * Copyright (c) 2006 Rene Scharfe | |
4 | */ | |
4df096a5 FBH |
5 | #include "builtin.h" |
6 | #include "archive.h" | |
f394e093 | 7 | #include "gettext.h" |
b236752a | 8 | #include "transport.h" |
52e77876 | 9 | #include "parse-options.h" |
4df096a5 | 10 | #include "pkt-line.h" |
d1cbe1e6 | 11 | #include "repository.h" |
4df096a5 | 12 | |
52e77876 RS |
13 | static void create_output_file(const char *output_file) |
14 | { | |
66e905b7 | 15 | int output_fd = xopen(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666); |
52e77876 RS |
16 | if (output_fd != 1) { |
17 | if (dup2(output_fd, 1) < 0) | |
788a3755 | 18 | die_errno(_("could not redirect output")); |
52e77876 RS |
19 | else |
20 | close(output_fd); | |
21 | } | |
22 | } | |
23 | ||
24 | static int run_remote_archiver(int argc, const char **argv, | |
56baa61d JK |
25 | const char *remote, const char *exec, |
26 | const char *name_hint) | |
4df096a5 | 27 | { |
74543a04 | 28 | int fd[2], i, rv; |
b236752a IL |
29 | struct transport *transport; |
30 | struct remote *_remote; | |
01f9ec64 | 31 | struct packet_reader reader; |
4df096a5 | 32 | |
b236752a IL |
33 | _remote = remote_get(remote); |
34 | if (!_remote->url[0]) | |
788a3755 | 35 | die(_("git archive: Remote with no URL")); |
b236752a IL |
36 | transport = transport_get(_remote, _remote->url[0]); |
37 | transport_connect(transport, "git-upload-archive", exec, fd); | |
4df096a5 | 38 | |
56baa61d JK |
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) | |
81c634e9 | 48 | packet_write_fmt(fd[1], "argument --format=%s\n", format); |
56baa61d | 49 | } |
52e77876 | 50 | for (i = 1; i < argc; i++) |
81c634e9 | 51 | packet_write_fmt(fd[1], "argument %s\n", argv[i]); |
4df096a5 FBH |
52 | packet_flush(fd[1]); |
53 | ||
2d103c31 MS |
54 | packet_reader_init(&reader, fd[0], NULL, 0, |
55 | PACKET_READ_CHOMP_NEWLINE | | |
56 | PACKET_READ_DIE_ON_ERR_PACKET); | |
01f9ec64 MS |
57 | |
58 | if (packet_reader_read(&reader) != PACKET_READ_NORMAL) | |
bc9d4dc5 | 59 | die(_("git archive: expected ACK/NAK, got a flush packet")); |
01f9ec64 MS |
60 | if (strcmp(reader.line, "ACK")) { |
61 | if (starts_with(reader.line, "NACK ")) | |
62 | die(_("git archive: NACK %s"), reader.line + 5); | |
788a3755 | 63 | die(_("git archive: protocol error")); |
4df096a5 FBH |
64 | } |
65 | ||
01f9ec64 | 66 | if (packet_reader_read(&reader) != PACKET_READ_FLUSH) |
788a3755 | 67 | die(_("git archive: expected a flush")); |
4df096a5 FBH |
68 | |
69 | /* Now, start reading from fd[0] and spit it out to stdout */ | |
34df8aba | 70 | rv = recv_sideband("archive", fd[0], 1); |
b236752a | 71 | rv |= transport_disconnect(transport); |
4df096a5 FBH |
72 | |
73 | return !!rv; | |
74 | } | |
75 | ||
52e77876 RS |
76 | #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \ |
77 | PARSE_OPT_KEEP_ARGV0 | \ | |
99d86d60 | 78 | PARSE_OPT_KEEP_UNKNOWN_OPT | \ |
52e77876 | 79 | PARSE_OPT_NO_INTERNAL_HELP ) |
37f94436 | 80 | |
4df096a5 FBH |
81 | int cmd_archive(int argc, const char **argv, const char *prefix) |
82 | { | |
52e77876 | 83 | const char *exec = "git-upload-archive"; |
7ce4088a | 84 | char *output = NULL; |
37f94436 | 85 | const char *remote = NULL; |
52e77876 | 86 | struct option local_opts[] = { |
eb0224c6 JH |
87 | OPT_FILENAME('o', "output", &output, |
88 | N_("write the archive to this file")), | |
0012a387 NTND |
89 | OPT_STRING(0, "remote", &remote, N_("repo"), |
90 | N_("retrieve the archive from remote repository <repo>")), | |
b0ff9654 | 91 | OPT_STRING(0, "exec", &exec, N_("command"), |
0012a387 | 92 | N_("path to the remote git-upload-archive command")), |
52e77876 RS |
93 | OPT_END() |
94 | }; | |
95 | ||
37782920 SB |
96 | argc = parse_options(argc, argv, prefix, local_opts, NULL, |
97 | PARSE_OPT_KEEP_ALL); | |
52e77876 | 98 | |
00436bf1 JS |
99 | init_archivers(); |
100 | ||
56baa61d | 101 | if (output) |
52e77876 | 102 | create_output_file(output); |
4df096a5 | 103 | |
37f94436 | 104 | if (remote) |
56baa61d | 105 | return run_remote_archiver(argc, argv, remote, exec, output); |
4df096a5 | 106 | |
aa909861 | 107 | setvbuf(stderr, NULL, _IOLBF, BUFSIZ); |
8142f603 | 108 | |
7ce4088a | 109 | UNLEAK(output); |
b612ee20 | 110 | return write_archive(argc, argv, prefix, the_repository, output, 0); |
4df096a5 | 111 | } |