1/*2* Copyright (c) 2006 Franck Bui-Huu3* Copyright (c) 2006 Rene Scharfe4*/5#include "cache.h"6#include "builtin.h"7#include "archive.h"8#include "transport.h"9#include "parse-options.h"10#include "pkt-line.h"11#include "sideband.h"1213static void create_output_file(const char *output_file)14{15int output_fd = open(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);16if (output_fd < 0)17die_errno("could not create archive file '%s'", output_file);18if (output_fd != 1) {19if (dup2(output_fd, 1) < 0)20die_errno("could not redirect output");21else22close(output_fd);23}24}2526static int run_remote_archiver(int argc, const char **argv,27const char *remote, const char *exec)28{29char buf[LARGE_PACKET_MAX];30int fd[2], i, len, rv;31struct transport *transport;32struct remote *_remote;3334_remote = remote_get(remote);35if (!_remote->url[0])36die("git archive: Remote with no URL");37transport = transport_get(_remote, _remote->url[0]);38transport_connect(transport, "git-upload-archive", exec, fd);3940for (i = 1; i < argc; i++)41packet_write(fd[1], "argument %s\n", argv[i]);42packet_flush(fd[1]);4344len = packet_read_line(fd[0], buf, sizeof(buf));45if (!len)46die("git archive: expected ACK/NAK, got EOF");47if (buf[len-1] == '\n')48buf[--len] = 0;49if (strcmp(buf, "ACK")) {50if (len > 5 && !prefixcmp(buf, "NACK "))51die("git archive: NACK %s", buf + 5);52die("git archive: protocol error");53}5455len = packet_read_line(fd[0], buf, sizeof(buf));56if (len)57die("git archive: expected a flush");5859/* Now, start reading from fd[0] and spit it out to stdout */60rv = recv_sideband("archive", fd[0], 1);61rv |= transport_disconnect(transport);6263return !!rv;64}6566static const char *format_from_name(const char *filename)67{68const char *ext = strrchr(filename, '.');69if (!ext)70return NULL;71ext++;72if (!strcasecmp(ext, "zip"))73return "--format=zip";74return NULL;75}7677#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \78PARSE_OPT_KEEP_ARGV0 | \79PARSE_OPT_KEEP_UNKNOWN | \80PARSE_OPT_NO_INTERNAL_HELP )8182int cmd_archive(int argc, const char **argv, const char *prefix)83{84const char *exec = "git-upload-archive";85const char *output = NULL;86const char *remote = NULL;87const char *format_option = NULL;88struct option local_opts[] = {89OPT_STRING('o', "output", &output, "file",90"write the archive to this file"),91OPT_STRING(0, "remote", &remote, "repo",92"retrieve the archive from remote repository <repo>"),93OPT_STRING(0, "exec", &exec, "cmd",94"path to the remote git-upload-archive command"),95OPT_END()96};9798argc = parse_options(argc, argv, prefix, local_opts, NULL,99PARSE_OPT_KEEP_ALL);100101if (output) {102create_output_file(output);103format_option = format_from_name(output);104}105106/*107* We have enough room in argv[] to muck it in place, because108* --output must have been given on the original command line109* if we get to this point, and parse_options() must have eaten110* it, i.e. we can add back one element to the array.111*112* We add a fake --format option at the beginning, with the113* format inferred from our output filename. This way explicit114* --format options can override it, and the fake option is115* inserted before any "--" that might have been given.116*/117if (format_option) {118memmove(argv + 2, argv + 1, sizeof(*argv) * argc);119argv[1] = format_option;120argv[++argc] = NULL;121}122123if (remote)124return run_remote_archiver(argc, argv, remote, exec);125126setvbuf(stderr, NULL, _IOLBF, BUFSIZ);127128return write_archive(argc, argv, prefix, 1);129}