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