]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-archive.c
add memmem()
[thirdparty/git.git] / builtin-archive.c
CommitLineData
4df096a5
FBH
1/*
2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
4 */
4df096a5
FBH
5#include "cache.h"
6#include "builtin.h"
7#include "archive.h"
8#include "commit.h"
9#include "tree-walk.h"
10#include "exec_cmd.h"
11#include "pkt-line.h"
23d6d112 12#include "sideband.h"
8460b2fc 13#include "attr.h"
4df096a5
FBH
14
15static const char archive_usage[] = \
e0ffb248 16"git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
4df096a5 17
6c2f207b
SP
18static struct archiver_desc
19{
20 const char *name;
21 write_archive_fn_t write_archive;
22 parse_extra_args_fn_t parse_extra;
23} archivers[] = {
24 { "tar", write_tar_archive, NULL },
25 { "zip", write_zip_archive, parse_extra_zip_args },
4df096a5
FBH
26};
27
37f94436 28static int run_remote_archiver(const char *remote, int argc,
4df096a5
FBH
29 const char **argv)
30{
23d6d112 31 char *url, buf[LARGE_PACKET_MAX];
4df096a5
FBH
32 int fd[2], i, len, rv;
33 pid_t pid;
fe5ab763
JH
34 const char *exec = "git-upload-archive";
35 int exec_at = 0;
4df096a5 36
fe5ab763
JH
37 for (i = 1; i < argc; i++) {
38 const char *arg = argv[i];
599065a3 39 if (!prefixcmp(arg, "--exec=")) {
fe5ab763
JH
40 if (exec_at)
41 die("multiple --exec specified");
42 exec = arg + 7;
43 exec_at = i;
44 break;
45 }
46 }
4df096a5 47
37f94436 48 url = xstrdup(remote);
7841ce79 49 pid = git_connect(fd, url, exec, 0);
4df096a5
FBH
50 if (pid < 0)
51 return pid;
52
fe5ab763
JH
53 for (i = 1; i < argc; i++) {
54 if (i == exec_at)
55 continue;
4df096a5 56 packet_write(fd[1], "argument %s\n", argv[i]);
fe5ab763 57 }
4df096a5
FBH
58 packet_flush(fd[1]);
59
60 len = packet_read_line(fd[0], buf, sizeof(buf));
61 if (!len)
62 die("git-archive: expected ACK/NAK, got EOF");
63 if (buf[len-1] == '\n')
64 buf[--len] = 0;
65 if (strcmp(buf, "ACK")) {
cc44c765 66 if (len > 5 && !prefixcmp(buf, "NACK "))
4df096a5
FBH
67 die("git-archive: NACK %s", buf + 5);
68 die("git-archive: protocol error");
69 }
70
71 len = packet_read_line(fd[0], buf, sizeof(buf));
72 if (len)
73 die("git-archive: expected a flush");
74
75 /* Now, start reading from fd[0] and spit it out to stdout */
9ac13ec9 76 rv = recv_sideband("archive", fd[0], 1, 2);
4df096a5 77 close(fd[0]);
ec587fde 78 close(fd[1]);
4df096a5
FBH
79 rv |= finish_connect(pid);
80
81 return !!rv;
82}
83
8460b2fc
RS
84static void *convert_to_archive(const char *path,
85 const void *src, unsigned long *sizep,
86 const struct commit *commit)
87{
88 static struct git_attr *attr_specfile;
89 struct git_attr_check check[1];
90 char *interpolated = NULL;
91 unsigned long allocated = 0;
92
93 if (!commit)
94 return NULL;
95
96 if (!attr_specfile)
97 attr_specfile = git_attr("specfile", 8);
98
99 check[0].attr = attr_specfile;
100 if (git_checkattr(path, ARRAY_SIZE(check), check))
101 return NULL;
102 if (!ATTR_TRUE(check[0].value))
103 return NULL;
104
105 *sizep = format_commit_message(commit, src, &interpolated, &allocated);
106
107 return interpolated;
108}
109
110void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
111 unsigned int mode, enum object_type *type,
112 unsigned long *size,
113 const struct commit *commit)
114{
115 void *buffer, *converted;
116
117 buffer = read_sha1_file(sha1, type, size);
118 if (buffer && S_ISREG(mode)) {
119 converted = convert_to_working_tree(path, buffer, size);
120 if (converted) {
121 free(buffer);
122 buffer = converted;
123 }
124
125 converted = convert_to_archive(path, buffer, size, commit);
126 if (converted) {
127 free(buffer);
128 buffer = converted;
129 }
130 }
131
132 return buffer;
133}
134
4df096a5
FBH
135static int init_archiver(const char *name, struct archiver *ar)
136{
137 int rv = -1, i;
138
139 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
140 if (!strcmp(name, archivers[i].name)) {
6c2f207b
SP
141 memset(ar, 0, sizeof(*ar));
142 ar->name = archivers[i].name;
143 ar->write_archive = archivers[i].write_archive;
144 ar->parse_extra = archivers[i].parse_extra;
4df096a5
FBH
145 rv = 0;
146 break;
147 }
148 }
149 return rv;
150}
151
152void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
153{
154 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
155}
156
157void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
158 const char *prefix)
159{
160 const char *name = argv[0];
161 const unsigned char *commit_sha1;
162 time_t archive_time;
163 struct tree *tree;
8460b2fc 164 const struct commit *commit;
4df096a5
FBH
165 unsigned char sha1[20];
166
167 if (get_sha1(name, sha1))
168 die("Not a valid object name");
169
170 commit = lookup_commit_reference_gently(sha1, 1);
171 if (commit) {
172 commit_sha1 = commit->object.sha1;
173 archive_time = commit->date;
174 } else {
175 commit_sha1 = NULL;
176 archive_time = time(NULL);
177 }
178
179 tree = parse_tree_indirect(sha1);
180 if (tree == NULL)
181 die("not a tree object");
182
183 if (prefix) {
184 unsigned char tree_sha1[20];
185 unsigned int mode;
186 int err;
187
188 err = get_tree_entry(tree->object.sha1, prefix,
189 tree_sha1, &mode);
190 if (err || !S_ISDIR(mode))
191 die("current working directory is untracked");
192
4df096a5
FBH
193 tree = parse_tree_indirect(tree_sha1);
194 }
195 ar_args->tree = tree;
196 ar_args->commit_sha1 = commit_sha1;
8460b2fc 197 ar_args->commit = commit;
4df096a5
FBH
198 ar_args->time = archive_time;
199}
200
4df096a5
FBH
201int parse_archive_args(int argc, const char **argv, struct archiver *ar)
202{
203 const char *extra_argv[MAX_EXTRA_ARGS];
204 int extra_argc = 0;
8ff21b1a 205 const char *format = "tar";
4df096a5 206 const char *base = "";
e0ffb248 207 int verbose = 0;
4df096a5
FBH
208 int i;
209
210 for (i = 1; i < argc; i++) {
211 const char *arg = argv[i];
212
213 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
37f94436
JH
214 for (i = 0; i < ARRAY_SIZE(archivers); i++)
215 printf("%s\n", archivers[i].name);
216 exit(0);
4df096a5 217 }
e0ffb248
JH
218 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
219 verbose = 1;
220 continue;
221 }
cc44c765 222 if (!prefixcmp(arg, "--format=")) {
4df096a5
FBH
223 format = arg + 9;
224 continue;
225 }
cc44c765 226 if (!prefixcmp(arg, "--prefix=")) {
4df096a5
FBH
227 base = arg + 9;
228 continue;
229 }
4df096a5
FBH
230 if (!strcmp(arg, "--")) {
231 i++;
232 break;
233 }
234 if (arg[0] == '-') {
235 if (extra_argc > MAX_EXTRA_ARGS - 1)
236 die("Too many extra options");
237 extra_argv[extra_argc++] = arg;
238 continue;
239 }
240 break;
241 }
242
37f94436
JH
243 /* We need at least one parameter -- tree-ish */
244 if (argc - 1 < i)
4df096a5 245 usage(archive_usage);
4df096a5
FBH
246 if (init_archiver(format, ar) < 0)
247 die("Unknown archive format '%s'", format);
248
37f94436
JH
249 if (extra_argc) {
250 if (!ar->parse_extra)
2232c0c6
RS
251 die("'%s' format does not handle %s",
252 ar->name, extra_argv[0]);
4df096a5
FBH
253 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
254 }
e0ffb248 255 ar->args.verbose = verbose;
4df096a5
FBH
256 ar->args.base = base;
257
258 return i;
259}
260
d751864c 261static const char *extract_remote_arg(int *ac, const char **av)
37f94436
JH
262{
263 int ix, iy, cnt = *ac;
264 int no_more_options = 0;
265 const char *remote = NULL;
266
267 for (ix = iy = 1; ix < cnt; ix++) {
268 const char *arg = av[ix];
269 if (!strcmp(arg, "--"))
270 no_more_options = 1;
271 if (!no_more_options) {
cc44c765 272 if (!prefixcmp(arg, "--remote=")) {
37f94436
JH
273 if (remote)
274 die("Multiple --remote specified");
275 remote = arg + 9;
276 continue;
277 }
278 if (arg[0] != '-')
279 no_more_options = 1;
280 }
281 if (ix != iy)
282 av[iy] = arg;
283 iy++;
284 }
285 if (remote) {
286 av[--cnt] = NULL;
287 *ac = cnt;
288 }
289 return remote;
290}
291
4df096a5
FBH
292int cmd_archive(int argc, const char **argv, const char *prefix)
293{
294 struct archiver ar;
295 int tree_idx;
37f94436 296 const char *remote = NULL;
4df096a5 297
d751864c 298 remote = extract_remote_arg(&argc, argv);
37f94436
JH
299 if (remote)
300 return run_remote_archiver(remote, argc, argv);
4df096a5 301
aa909861 302 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
8142f603 303
37f94436
JH
304 memset(&ar, 0, sizeof(ar));
305 tree_idx = parse_archive_args(argc, argv, &ar);
265d5280
RS
306 if (prefix == NULL)
307 prefix = setup_git_directory();
4df096a5
FBH
308
309 argv += tree_idx;
310 parse_treeish_arg(argv, &ar.args, prefix);
311 parse_pathspec_arg(argv + 1, &ar.args);
312
313 return ar.write_archive(&ar.args);
314}