]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-archive.c
fast-import: clarify documentation of "feature" command
[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)
0721c314 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)
0721c314 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,
27 const char *remote, const char *exec)
4df096a5 28{
b236752a 29 char buf[LARGE_PACKET_MAX];
4df096a5 30 int fd[2], i, len, rv;
b236752a
IL
31 struct transport *transport;
32 struct remote *_remote;
4df096a5 33
b236752a
IL
34 _remote = remote_get(remote);
35 if (!_remote->url[0])
36 die("git archive: Remote with no URL");
37 transport = transport_get(_remote, _remote->url[0]);
38 transport_connect(transport, "git-upload-archive", exec, fd);
4df096a5 39
52e77876 40 for (i = 1; i < argc; i++)
4df096a5 41 packet_write(fd[1], "argument %s\n", argv[i]);
4df096a5
FBH
42 packet_flush(fd[1]);
43
44 len = packet_read_line(fd[0], buf, sizeof(buf));
45 if (!len)
34baebce 46 die("git archive: expected ACK/NAK, got EOF");
4df096a5
FBH
47 if (buf[len-1] == '\n')
48 buf[--len] = 0;
49 if (strcmp(buf, "ACK")) {
cc44c765 50 if (len > 5 && !prefixcmp(buf, "NACK "))
34baebce
HO
51 die("git archive: NACK %s", buf + 5);
52 die("git archive: protocol error");
4df096a5
FBH
53 }
54
55 len = packet_read_line(fd[0], buf, sizeof(buf));
56 if (len)
34baebce 57 die("git archive: expected a flush");
4df096a5
FBH
58
59 /* Now, start reading from fd[0] and spit it out to stdout */
34df8aba 60 rv = recv_sideband("archive", fd[0], 1);
b236752a 61 rv |= transport_disconnect(transport);
4df096a5
FBH
62
63 return !!rv;
64}
65
0f4b377c
DP
66static const char *format_from_name(const char *filename)
67{
68 const char *ext = strrchr(filename, '.');
69 if (!ext)
70 return NULL;
71 ext++;
72 if (!strcasecmp(ext, "zip"))
fe12d8e8 73 return "--format=zip";
0f4b377c
DP
74 return NULL;
75}
76
52e77876
RS
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 )
37f94436 81
4df096a5
FBH
82int cmd_archive(int argc, const char **argv, const char *prefix)
83{
52e77876
RS
84 const char *exec = "git-upload-archive";
85 const char *output = NULL;
37f94436 86 const char *remote = NULL;
fe12d8e8 87 const char *format_option = NULL;
52e77876 88 struct option local_opts[] = {
05d3951e 89 OPT_STRING('o', "output", &output, "file",
52e77876
RS
90 "write the archive to this file"),
91 OPT_STRING(0, "remote", &remote, "repo",
92 "retrieve the archive from remote repository <repo>"),
93 OPT_STRING(0, "exec", &exec, "cmd",
94 "path to the remote git-upload-archive command"),
95 OPT_END()
96 };
97
37782920
SB
98 argc = parse_options(argc, argv, prefix, local_opts, NULL,
99 PARSE_OPT_KEEP_ALL);
52e77876 100
0f4b377c 101 if (output) {
52e77876 102 create_output_file(output);
fe12d8e8 103 format_option = format_from_name(output);
0f4b377c
DP
104 }
105
fe12d8e8
RS
106 /*
107 * We have enough room in argv[] to muck it in place, because
108 * --output must have been given on the original command line
109 * if we get to this point, and parse_options() must have eaten
110 * it, i.e. we can add back one element to the array.
111 *
112 * We add a fake --format option at the beginning, with the
113 * format inferred from our output filename. This way explicit
114 * --format options can override it, and the fake option is
115 * inserted before any "--" that might have been given.
116 */
117 if (format_option) {
782a0005 118 memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
fe12d8e8 119 argv[1] = format_option;
782a0005 120 argv[++argc] = NULL;
0f4b377c 121 }
4df096a5 122
37f94436 123 if (remote)
52e77876 124 return run_remote_archiver(argc, argv, remote, exec);
4df096a5 125
aa909861 126 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
8142f603 127
6e94e683 128 return write_archive(argc, argv, prefix, 1);
4df096a5 129}