add 'git credential' plumbing command
[git/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{
b236752a 30 char buf[LARGE_PACKET_MAX];
4df096a5 31 int fd[2], i, len, rv;
b236752a
IL
32 struct transport *transport;
33 struct remote *_remote;
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)
50 packet_write(fd[1], "argument --format=%s\n", format);
51 }
52e77876 52 for (i = 1; i < argc; i++)
4df096a5 53 packet_write(fd[1], "argument %s\n", argv[i]);
4df096a5
FBH
54 packet_flush(fd[1]);
55
56 len = packet_read_line(fd[0], buf, sizeof(buf));
57 if (!len)
788a3755 58 die(_("git archive: expected ACK/NAK, got EOF"));
4df096a5
FBH
59 if (buf[len-1] == '\n')
60 buf[--len] = 0;
61 if (strcmp(buf, "ACK")) {
cc44c765 62 if (len > 5 && !prefixcmp(buf, "NACK "))
788a3755 63 die(_("git archive: NACK %s"), buf + 5);
908aaceb
IL
64 if (len > 4 && !prefixcmp(buf, "ERR "))
65 die(_("remote error: %s"), buf + 4);
788a3755 66 die(_("git archive: protocol error"));
4df096a5
FBH
67 }
68
69 len = packet_read_line(fd[0], buf, sizeof(buf));
70 if (len)
788a3755 71 die(_("git archive: expected a flush"));
4df096a5
FBH
72
73 /* Now, start reading from fd[0] and spit it out to stdout */
34df8aba 74 rv = recv_sideband("archive", fd[0], 1);
b236752a 75 rv |= transport_disconnect(transport);
4df096a5
FBH
76
77 return !!rv;
78}
79
52e77876
RS
80#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
81 PARSE_OPT_KEEP_ARGV0 | \
82 PARSE_OPT_KEEP_UNKNOWN | \
83 PARSE_OPT_NO_INTERNAL_HELP )
37f94436 84
4df096a5
FBH
85int cmd_archive(int argc, const char **argv, const char *prefix)
86{
52e77876
RS
87 const char *exec = "git-upload-archive";
88 const char *output = NULL;
37f94436 89 const char *remote = NULL;
52e77876 90 struct option local_opts[] = {
05d3951e 91 OPT_STRING('o', "output", &output, "file",
52e77876
RS
92 "write the archive to this file"),
93 OPT_STRING(0, "remote", &remote, "repo",
94 "retrieve the archive from remote repository <repo>"),
95 OPT_STRING(0, "exec", &exec, "cmd",
96 "path to the remote git-upload-archive command"),
97 OPT_END()
98 };
99
37782920
SB
100 argc = parse_options(argc, argv, prefix, local_opts, NULL,
101 PARSE_OPT_KEEP_ALL);
52e77876 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
f0c7fd49 111 return write_archive(argc, argv, prefix, 1, output, 0);
4df096a5 112}