Merge branch 'jc/maint-reflog-bad-timestamp' into maint
[git/dscho.git] / builtin-archive.c
blobfaf4554d5edbb21dbd043daec669bc684bf056cc
1 /*
2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
4 */
5 #include "cache.h"
6 #include "builtin.h"
7 #include "archive.h"
8 #include "parse-options.h"
9 #include "pkt-line.h"
10 #include "sideband.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);
15 if (output_fd < 0)
16 die_errno("could not create archive file '%s'", output_file);
17 if (output_fd != 1) {
18 if (dup2(output_fd, 1) < 0)
19 die_errno("could not redirect output");
20 else
21 close(output_fd);
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]);
37 packet_flush(fd[1]);
39 len = packet_read_line(fd[0], buf, sizeof(buf));
40 if (!len)
41 die("git archive: expected ACK/NAK, got EOF");
42 if (buf[len-1] == '\n')
43 buf[--len] = 0;
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));
51 if (len)
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);
56 close(fd[0]);
57 close(fd[1]);
58 rv |= finish_connect(conn);
60 return !!rv;
63 static const char *format_from_name(const char *filename)
65 const char *ext = strrchr(filename, '.');
66 if (!ext)
67 return NULL;
68 ext++;
69 if (!strcasecmp(ext, "zip"))
70 return "--format=zip";
71 return NULL;
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 )
79 int cmd_archive(int argc, const char **argv, const char *prefix)
81 const char *exec = "git-upload-archive";
82 const char *output = NULL;
83 const char *remote = NULL;
84 const char *format_option = NULL;
85 struct option local_opts[] = {
86 OPT_STRING('o', "output", &output, "file",
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"),
92 OPT_END()
95 argc = parse_options(argc, argv, prefix, local_opts, NULL,
96 PARSE_OPT_KEEP_ALL);
98 if (output) {
99 create_output_file(output);
100 format_option = format_from_name(output);
104 * We have enough room in argv[] to muck it in place, because
105 * --output must have been given on the original command line
106 * if we get to this point, and parse_options() must have eaten
107 * it, i.e. we can add back one element to the array.
109 * We add a fake --format option at the beginning, with the
110 * format inferred from our output filename. This way explicit
111 * --format options can override it, and the fake option is
112 * inserted before any "--" that might have been given.
114 if (format_option) {
115 memmove(argv + 2, argv + 1, sizeof(*argv) * argc);
116 argv[1] = format_option;
117 argv[++argc] = NULL;
120 if (remote)
121 return run_remote_archiver(argc, argv, remote, exec);
123 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
125 return write_archive(argc, argv, prefix, 1);