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
)
29 char buf
[LARGE_PACKET_MAX
];
30 int fd
[2], i
, len
, rv
;
31 struct transport
*transport
;
32 struct remote
*_remote
;
34 _remote
= remote_get(remote
);
36 die("git archive: Remote with no URL");
37 transport
= transport_get(_remote
, _remote
->url
[0]);
38 transport_connect(transport
, "git-upload-archive", exec
, fd
);
40 for (i
= 1; i
< argc
; i
++)
41 packet_write(fd
[1], "argument %s\n", argv
[i
]);
44 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
46 die("git archive: expected ACK/NAK, got EOF");
47 if (buf
[len
-1] == '\n')
49 if (strcmp(buf
, "ACK")) {
50 if (len
> 5 && !prefixcmp(buf
, "NACK "))
51 die("git archive: NACK %s", buf
+ 5);
52 die("git archive: protocol error");
55 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
57 die("git archive: expected a flush");
59 /* Now, start reading from fd[0] and spit it out to stdout */
60 rv
= recv_sideband("archive", fd
[0], 1);
61 rv
|= transport_disconnect(transport
);
66 static const char *format_from_name(const char *filename
)
68 const char *ext
= strrchr(filename
, '.');
72 if (!strcasecmp(ext
, "zip"))
73 return "--format=zip";
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 const char *format_option
= NULL
;
88 struct option local_opts
[] = {
89 OPT_STRING('o', "output", &output
, "file",
90 "write the archive to this file"),
91 OPT_STRING(0, "remote", &remote
, "repo",
92 "retrieve the archive from remote repository <repo>"),
93 OPT_STRING(0, "exec", &exec
, "cmd",
94 "path to the remote git-upload-archive command"),
98 argc
= parse_options(argc
, argv
, prefix
, local_opts
, NULL
,
102 create_output_file(output
);
103 format_option
= format_from_name(output
);
107 * We have enough room in argv[] to muck it in place, because
108 * --output must have been given on the original command line
109 * if we get to this point, and parse_options() must have eaten
110 * it, i.e. we can add back one element to the array.
112 * We add a fake --format option at the beginning, with the
113 * format inferred from our output filename. This way explicit
114 * --format options can override it, and the fake option is
115 * inserted before any "--" that might have been given.
118 memmove(argv
+ 2, argv
+ 1, sizeof(*argv
) * argc
);
119 argv
[1] = format_option
;
124 return run_remote_archiver(argc
, argv
, remote
, exec
);
126 setvbuf(stderr
, NULL
, _IOLBF
, BUFSIZ
);
128 return write_archive(argc
, argv
, prefix
, 1);