2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
10 #include "tree-walk.h"
15 static const char archive_usage
[] = \
16 "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
18 struct archiver archivers
[] = {
21 .write_archive
= write_tar_archive
,
25 .write_archive
= write_zip_archive
,
26 .parse_extra
= parse_extra_zip_args
,
30 static int run_remote_archiver(const char *remote
, int argc
,
33 char *url
, buf
[LARGE_PACKET_MAX
];
34 int fd
[2], i
, len
, rv
;
36 const char *exec
= "git-upload-archive";
39 for (i
= 1; i
< argc
; i
++) {
40 const char *arg
= argv
[i
];
41 if (!strncmp("--exec=", arg
, 7)) {
43 die("multiple --exec specified");
50 url
= xstrdup(remote
);
51 pid
= git_connect(fd
, url
, exec
);
55 for (i
= 1; i
< argc
; i
++) {
58 packet_write(fd
[1], "argument %s\n", argv
[i
]);
62 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
64 die("git-archive: expected ACK/NAK, got EOF");
65 if (buf
[len
-1] == '\n')
67 if (strcmp(buf
, "ACK")) {
68 if (len
> 5 && !strncmp(buf
, "NACK ", 5))
69 die("git-archive: NACK %s", buf
+ 5);
70 die("git-archive: protocol error");
73 len
= packet_read_line(fd
[0], buf
, sizeof(buf
));
75 die("git-archive: expected a flush");
77 /* Now, start reading from fd[0] and spit it out to stdout */
78 rv
= recv_sideband("archive", fd
[0], 1, 2, buf
, sizeof(buf
));
80 rv
|= finish_connect(pid
);
85 static int init_archiver(const char *name
, struct archiver
*ar
)
89 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++) {
90 if (!strcmp(name
, archivers
[i
].name
)) {
91 memcpy(ar
, &archivers
[i
], sizeof(struct archiver
));
99 void parse_pathspec_arg(const char **pathspec
, struct archiver_args
*ar_args
)
101 ar_args
->pathspec
= get_pathspec(ar_args
->base
, pathspec
);
104 void parse_treeish_arg(const char **argv
, struct archiver_args
*ar_args
,
107 const char *name
= argv
[0];
108 const unsigned char *commit_sha1
;
111 struct commit
*commit
;
112 unsigned char sha1
[20];
114 if (get_sha1(name
, sha1
))
115 die("Not a valid object name");
117 commit
= lookup_commit_reference_gently(sha1
, 1);
119 commit_sha1
= commit
->object
.sha1
;
120 archive_time
= commit
->date
;
123 archive_time
= time(NULL
);
126 tree
= parse_tree_indirect(sha1
);
128 die("not a tree object");
131 unsigned char tree_sha1
[20];
135 err
= get_tree_entry(tree
->object
.sha1
, prefix
,
137 if (err
|| !S_ISDIR(mode
))
138 die("current working directory is untracked");
141 tree
= parse_tree_indirect(tree_sha1
);
143 ar_args
->tree
= tree
;
144 ar_args
->commit_sha1
= commit_sha1
;
145 ar_args
->time
= archive_time
;
148 int parse_archive_args(int argc
, const char **argv
, struct archiver
*ar
)
150 const char *extra_argv
[MAX_EXTRA_ARGS
];
152 const char *format
= NULL
; /* might want to default to "tar" */
153 const char *base
= "";
157 for (i
= 1; i
< argc
; i
++) {
158 const char *arg
= argv
[i
];
160 if (!strcmp(arg
, "--list") || !strcmp(arg
, "-l")) {
161 for (i
= 0; i
< ARRAY_SIZE(archivers
); i
++)
162 printf("%s\n", archivers
[i
].name
);
165 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
169 if (!strncmp(arg
, "--format=", 9)) {
173 if (!strncmp(arg
, "--prefix=", 9)) {
177 if (!strcmp(arg
, "--")) {
182 if (extra_argc
> MAX_EXTRA_ARGS
- 1)
183 die("Too many extra options");
184 extra_argv
[extra_argc
++] = arg
;
190 /* We need at least one parameter -- tree-ish */
192 usage(archive_usage
);
194 die("You must specify an archive format");
195 if (init_archiver(format
, ar
) < 0)
196 die("Unknown archive format '%s'", format
);
199 if (!ar
->parse_extra
)
200 die("'%s' format does not handle %s",
201 ar
->name
, extra_argv
[0]);
202 ar
->args
.extra
= ar
->parse_extra(extra_argc
, extra_argv
);
204 ar
->args
.verbose
= verbose
;
205 ar
->args
.base
= base
;
210 static const char *extract_remote_arg(int *ac
, const char **av
)
212 int ix
, iy
, cnt
= *ac
;
213 int no_more_options
= 0;
214 const char *remote
= NULL
;
216 for (ix
= iy
= 1; ix
< cnt
; ix
++) {
217 const char *arg
= av
[ix
];
218 if (!strcmp(arg
, "--"))
220 if (!no_more_options
) {
221 if (!strncmp(arg
, "--remote=", 9)) {
223 die("Multiple --remote specified");
241 int cmd_archive(int argc
, const char **argv
, const char *prefix
)
245 const char *remote
= NULL
;
247 remote
= extract_remote_arg(&argc
, argv
);
249 return run_remote_archiver(remote
, argc
, argv
);
253 memset(&ar
, 0, sizeof(ar
));
254 tree_idx
= parse_archive_args(argc
, argv
, &ar
);
256 prefix
= setup_git_directory();
259 parse_treeish_arg(argv
, &ar
.args
, prefix
);
260 parse_pathspec_arg(argv
+ 1, &ar
.args
);
262 return ar
.write_archive(&ar
.args
);