2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
9 #include "parse-options.h"
13 static void create_output_file(const char *output_file
)
15 int output_fd
= open(output_file
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
17 die_errno(_("could not create archive file '%s'"), output_file
);
19 if (dup2(output_fd
, 1) < 0)
20 die_errno(_("could not redirect output"));
26 static int run_remote_archiver(int argc
, const char **argv
,
27 const char *remote
, const char *exec
,
28 const char *name_hint
)
32 struct transport
*transport
;
33 struct remote
*_remote
;
35 _remote
= remote_get(remote
);
37 die(_("git archive: Remote with no URL"));
38 transport
= transport_get(_remote
, _remote
->url
[0]);
39 transport_connect(transport
, "git-upload-archive", exec
, fd
);
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
48 const char *format
= archive_format_from_filename(name_hint
);
50 packet_write(fd
[1], "argument --format=%s\n", format
);
52 for (i
= 1; i
< argc
; i
++)
53 packet_write(fd
[1], "argument %s\n", argv
[i
]);
56 buf
= packet_read_line(fd
[0], NULL
);
58 die(_("git archive: expected ACK/NAK, got EOF"));
59 if (strcmp(buf
, "ACK")) {
60 if (!prefixcmp(buf
, "NACK "))
61 die(_("git archive: NACK %s"), buf
+ 5);
62 if (!prefixcmp(buf
, "ERR "))
63 die(_("remote error: %s"), buf
+ 4);
64 die(_("git archive: protocol error"));
67 if (packet_read_line(fd
[0], NULL
))
68 die(_("git archive: expected a flush"));
70 /* Now, start reading from fd[0] and spit it out to stdout */
71 rv
= recv_sideband("archive", fd
[0], 1);
72 rv
|= transport_disconnect(transport
);
77 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
78 PARSE_OPT_KEEP_ARGV0 | \
79 PARSE_OPT_KEEP_UNKNOWN | \
80 PARSE_OPT_NO_INTERNAL_HELP )
82 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
84 const char *exec
= "git-upload-archive";
85 const char *output
= NULL
;
86 const char *remote
= NULL
;
87 struct option local_opts
[] = {
88 OPT_STRING('o', "output", &output
, N_("file"),
89 N_("write the archive to this file")),
90 OPT_STRING(0, "remote", &remote
, N_("repo"),
91 N_("retrieve the archive from remote repository <repo>")),
92 OPT_STRING(0, "exec", &exec
, N_("command"),
93 N_("path to the remote git-upload-archive command")),
97 argc
= parse_options(argc
, argv
, prefix
, local_opts
, NULL
,
101 create_output_file(output
);
104 return run_remote_archiver(argc
, argv
, remote
, exec
, output
);
106 setvbuf(stderr
, NULL
, _IOLBF
, BUFSIZ
);
108 return write_archive(argc
, argv
, prefix
, 1, output
, 0);