blob: 3c5a5a7822afebf87ef6eb1f89c7c54d76844161 [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"
René Scharfe52e77872009-03-08 19:21:53 +01008#include "parse-options.h"
Franck Bui-Huu4df096a2006-09-07 15:12:02 +02009#include "pkt-line.h"
Junio C Hamano23d6d112006-09-10 03:33:34 -070010#include "sideband.h"
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020011
René Scharfe52e77872009-03-08 19:21:53 +010012static void create_output_file(const char *output_file)
13{
14 int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
15 if (output_fd < 0)
16 die("could not create archive file: %s ", output_file);
17 if (output_fd != 1) {
18 if (dup2(output_fd, 1) < 0)
19 die("could not redirect output");
20 else
21 close(output_fd);
22 }
23}
24
25static int run_remote_archiver(int argc, const char **argv,
26 const char *remote, const char *exec)
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020027{
Junio C Hamano23d6d112006-09-10 03:33:34 -070028 char *url, buf[LARGE_PACKET_MAX];
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020029 int fd[2], i, len, rv;
Johannes Sixt98158e92007-10-19 21:47:53 +020030 struct child_process *conn;
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020031
Junio C Hamano37f94432006-09-09 23:48:03 -070032 url = xstrdup(remote);
Johannes Sixt98158e92007-10-19 21:47:53 +020033 conn = git_connect(fd, url, exec, 0);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020034
René Scharfe52e77872009-03-08 19:21:53 +010035 for (i = 1; i < argc; i++)
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020036 packet_write(fd[1], "argument %s\n", argv[i]);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020037 packet_flush(fd[1]);
38
39 len = packet_read_line(fd[0], buf, sizeof(buf));
40 if (!len)
Heikki Orsila34baebc2008-08-30 14:12:53 +030041 die("git archive: expected ACK/NAK, got EOF");
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020042 if (buf[len-1] == '\n')
43 buf[--len] = 0;
44 if (strcmp(buf, "ACK")) {
Junio C Hamanocc44c762007-02-20 01:53:29 -080045 if (len > 5 && !prefixcmp(buf, "NACK "))
Heikki Orsila34baebc2008-08-30 14:12:53 +030046 die("git archive: NACK %s", buf + 5);
47 die("git archive: protocol error");
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020048 }
49
50 len = packet_read_line(fd[0], buf, sizeof(buf));
51 if (len)
Heikki Orsila34baebc2008-08-30 14:12:53 +030052 die("git archive: expected a flush");
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020053
54 /* Now, start reading from fd[0] and spit it out to stdout */
Johannes Sixt34df8ab2009-03-10 22:54:17 +010055 rv = recv_sideband("archive", fd[0], 1);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020056 close(fd[0]);
Junio C Hamanoec587fd2007-01-21 17:10:51 -080057 close(fd[1]);
Johannes Sixt98158e92007-10-19 21:47:53 +020058 rv |= finish_connect(conn);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020059
60 return !!rv;
61}
62
René Scharfe52e77872009-03-08 19:21:53 +010063#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
64 PARSE_OPT_KEEP_ARGV0 | \
65 PARSE_OPT_KEEP_UNKNOWN | \
66 PARSE_OPT_NO_INTERNAL_HELP )
Junio C Hamano37f94432006-09-09 23:48:03 -070067
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020068int cmd_archive(int argc, const char **argv, const char *prefix)
69{
René Scharfe52e77872009-03-08 19:21:53 +010070 const char *exec = "git-upload-archive";
71 const char *output = NULL;
Junio C Hamano37f94432006-09-09 23:48:03 -070072 const char *remote = NULL;
René Scharfe52e77872009-03-08 19:21:53 +010073 struct option local_opts[] = {
74 OPT_STRING(0, "output", &output, "file",
75 "write the archive to this file"),
76 OPT_STRING(0, "remote", &remote, "repo",
77 "retrieve the archive from remote repository <repo>"),
78 OPT_STRING(0, "exec", &exec, "cmd",
79 "path to the remote git-upload-archive command"),
80 OPT_END()
81 };
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020082
Stephen Boyd37782922009-05-23 11:53:12 -070083 argc = parse_options(argc, argv, prefix, local_opts, NULL,
84 PARSE_OPT_KEEP_ALL);
René Scharfe52e77872009-03-08 19:21:53 +010085
86 if (output)
87 create_output_file(output);
88
Junio C Hamano37f94432006-09-09 23:48:03 -070089 if (remote)
René Scharfe52e77872009-03-08 19:21:53 +010090 return run_remote_archiver(argc, argv, remote, exec);
Junio C Hamano37f94432006-09-09 23:48:03 -070091
Michal Rokosaa909862006-11-21 23:19:28 +010092 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
Junio C Hamano8142f602006-09-10 04:16:39 -070093
Rene Scharfe6e94e682008-07-25 12:41:21 +020094 return write_archive(argc, argv, prefix, 1);
Franck Bui-Huu4df096a2006-09-07 15:12:02 +020095}