2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
8 #include "parse-options.h"
12 static void create_output_file(const char *output_file)
14 int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
16 die_errno("could not create archive file '%s'", output_file);
18 if (dup2(output_fd, 1) < 0)
19 die_errno("could not redirect output");
25 static int run_remote_archiver(int argc, const char **argv,
26 const char *remote, const char *exec)
28 char *url, buf[LARGE_PACKET_MAX];
29 int fd[2], i, len, rv;
30 struct child_process *conn;
32 url = xstrdup(remote);
33 conn = git_connect(fd, url, exec, 0);
35 for (i = 1; i < argc; i++)
36 packet_write(fd[1], "argument %s\n", argv[i]);
39 len = packet_read_line(fd[0], buf, sizeof(buf));
41 die("git archive: expected ACK/NAK, got EOF");
42 if (buf[len-1] == '\n')
44 if (strcmp(buf, "ACK")) {
45 if (len > 5 && !prefixcmp(buf, "NACK "))
46 die("git archive: NACK %s", buf + 5);
47 die("git archive: protocol error");
50 len = packet_read_line(fd[0], buf, sizeof(buf));
52 die("git archive: expected a flush");
54 /* Now, start reading from fd[0] and spit it out to stdout */
55 rv = recv_sideband("archive", fd[0], 1);
58 rv |= finish_connect(conn);
63 #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 )
68 int cmd_archive(int argc, const char **argv, const char *prefix)
70 const char *exec = "git-upload-archive";
71 const char *output = NULL;
72 const char *remote = NULL;
73 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"),
83 argc = parse_options(argc, argv, prefix, local_opts, NULL,
87 create_output_file(output);
90 return run_remote_archiver(argc, argv, remote, exec);
92 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
94 return write_archive(argc, argv, prefix, 1);