]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-archive.c
Update git fsck --full short description to mention packs
[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"
52e77876 8#include "parse-options.h"
4df096a5 9#include "pkt-line.h"
23d6d112 10#include "sideband.h"
4df096a5 11
52e77876
RS
12static 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)
0721c314 16 die_errno("could not create archive file '%s'", output_file);
52e77876
RS
17 if (output_fd != 1) {
18 if (dup2(output_fd, 1) < 0)
0721c314 19 die_errno("could not redirect output");
52e77876
RS
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)
4df096a5 27{
23d6d112 28 char *url, buf[LARGE_PACKET_MAX];
4df096a5 29 int fd[2], i, len, rv;
98158e9c 30 struct child_process *conn;
4df096a5 31
37f94436 32 url = xstrdup(remote);
98158e9c 33 conn = git_connect(fd, url, exec, 0);
4df096a5 34
52e77876 35 for (i = 1; i < argc; i++)
4df096a5 36 packet_write(fd[1], "argument %s\n", argv[i]);
4df096a5
FBH
37 packet_flush(fd[1]);
38
39 len = packet_read_line(fd[0], buf, sizeof(buf));
40 if (!len)
34baebce 41 die("git archive: expected ACK/NAK, got EOF");
4df096a5
FBH
42 if (buf[len-1] == '\n')
43 buf[--len] = 0;
44 if (strcmp(buf, "ACK")) {
cc44c765 45 if (len > 5 && !prefixcmp(buf, "NACK "))
34baebce
HO
46 die("git archive: NACK %s", buf + 5);
47 die("git archive: protocol error");
4df096a5
FBH
48 }
49
50 len = packet_read_line(fd[0], buf, sizeof(buf));
51 if (len)
34baebce 52 die("git archive: expected a flush");
4df096a5
FBH
53
54 /* Now, start reading from fd[0] and spit it out to stdout */
34df8aba 55 rv = recv_sideband("archive", fd[0], 1);
4df096a5 56 close(fd[0]);
ec587fde 57 close(fd[1]);
98158e9c 58 rv |= finish_connect(conn);
4df096a5
FBH
59
60 return !!rv;
61}
62
0f4b377c
DP
63static const char *format_from_name(const char *filename)
64{
65 const char *ext = strrchr(filename, '.');
66 if (!ext)
67 return NULL;
68 ext++;
69 if (!strcasecmp(ext, "zip"))
70 return "zip";
71 return NULL;
72}
73
52e77876
RS
74#define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
75 PARSE_OPT_KEEP_ARGV0 | \
76 PARSE_OPT_KEEP_UNKNOWN | \
77 PARSE_OPT_NO_INTERNAL_HELP )
37f94436 78
4df096a5
FBH
79int cmd_archive(int argc, const char **argv, const char *prefix)
80{
52e77876
RS
81 const char *exec = "git-upload-archive";
82 const char *output = NULL;
37f94436 83 const char *remote = NULL;
0f4b377c 84 const char *format = NULL;
52e77876 85 struct option local_opts[] = {
05d3951e 86 OPT_STRING('o', "output", &output, "file",
52e77876
RS
87 "write the archive to this file"),
88 OPT_STRING(0, "remote", &remote, "repo",
89 "retrieve the archive from remote repository <repo>"),
90 OPT_STRING(0, "exec", &exec, "cmd",
91 "path to the remote git-upload-archive command"),
0f4b377c 92 OPT_STRING(0, "format", &format, "fmt", "archive format"),
52e77876
RS
93 OPT_END()
94 };
0f4b377c 95 char fmt_opt[32];
52e77876 96
37782920
SB
97 argc = parse_options(argc, argv, prefix, local_opts, NULL,
98 PARSE_OPT_KEEP_ALL);
52e77876 99
0f4b377c 100 if (output) {
52e77876 101 create_output_file(output);
0f4b377c
DP
102 if (!format)
103 format = format_from_name(output);
104 }
105
106 if (format) {
107 sprintf(fmt_opt, "--format=%s", format);
108 /*
782a0005
JH
109 * We have enough room in argv[] to muck it in place,
110 * because either --format and/or --output must have
111 * been given on the original command line if we get
112 * to this point, and parse_options() must have eaten
113 * it, i.e. we can add back one element to the array.
114 * But argv[] may contain "--"; we should make it the
115 * first option.
0f4b377c 116 */
782a0005
JH
117 memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
118 argv[1] = fmt_opt;
119 argv[++argc] = NULL;
0f4b377c 120 }
4df096a5 121
37f94436 122 if (remote)
52e77876 123 return run_remote_archiver(argc, argv, remote, exec);
4df096a5 124
aa909861 125 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
8142f603 126
6e94e683 127 return write_archive(argc, argv, prefix, 1);
4df096a5 128}