2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
9 #include "parse-options.h"
11 #include "repository.h"
13 static void create_output_file(const char *output_file
)
15 int output_fd
= xopen(output_file
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
17 if (dup2(output_fd
, 1) < 0)
18 die_errno(_("could not redirect output"));
24 static int run_remote_archiver(int argc
, const char **argv
,
25 const char *remote
, const char *exec
,
26 const char *name_hint
)
29 struct transport
*transport
;
30 struct remote
*_remote
;
31 struct packet_reader reader
;
33 _remote
= remote_get(remote
);
34 transport
= transport_get(_remote
, _remote
->url
.v
[0]);
35 transport_connect(transport
, "git-upload-archive", exec
, fd
);
38 * Inject a fake --format field at the beginning of the
39 * arguments, with the format inferred from our output
40 * filename. This way explicit --format options can override
44 const char *format
= archive_format_from_filename(name_hint
);
46 packet_write_fmt(fd
[1], "argument --format=%s\n", format
);
48 for (i
= 1; i
< argc
; i
++)
49 packet_write_fmt(fd
[1], "argument %s\n", argv
[i
]);
52 packet_reader_init(&reader
, fd
[0], NULL
, 0,
53 PACKET_READ_CHOMP_NEWLINE
|
54 PACKET_READ_DIE_ON_ERR_PACKET
);
56 if (packet_reader_read(&reader
) != PACKET_READ_NORMAL
)
57 die(_("git archive: expected ACK/NAK, got a flush packet"));
58 if (strcmp(reader
.line
, "ACK")) {
59 if (starts_with(reader
.line
, "NACK "))
60 die(_("git archive: NACK %s"), reader
.line
+ 5);
61 die(_("git archive: protocol error"));
64 if (packet_reader_read(&reader
) != PACKET_READ_FLUSH
)
65 die(_("git archive: expected a flush"));
67 /* Now, start reading from fd[0] and spit it out to stdout */
68 rv
= recv_sideband("archive", fd
[0], 1);
69 rv
|= transport_disconnect(transport
);
74 #define PARSE_OPT_KEEP_ALL ( PARSE_OPT_KEEP_DASHDASH | \
75 PARSE_OPT_KEEP_ARGV0 | \
76 PARSE_OPT_KEEP_UNKNOWN_OPT | \
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";
83 const char *remote
= NULL
;
84 struct option local_opts
[] = {
85 OPT_FILENAME('o', "output", &output
,
86 N_("write the archive to this file")),
87 OPT_STRING(0, "remote", &remote
, N_("repo"),
88 N_("retrieve the archive from remote repository <repo>")),
89 OPT_STRING(0, "exec", &exec
, N_("command"),
90 N_("path to the remote git-upload-archive command")),
95 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 ret
= write_archive(argc
, argv
, prefix
, the_repository
, output
, 0);