Code

archive: fix subst file generation
[git.git] / builtin-archive.c
1 /*
2  * Copyright (c) 2006 Franck Bui-Huu
3  * Copyright (c) 2006 Rene Scharfe
4  */
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"
12 #include "sideband.h"
13 #include "attr.h"
15 static const char archive_usage[] = \
16 "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
18 static 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 },
26 };
28 static int run_remote_archiver(const char *remote, int argc,
29                                const char **argv)
30 {
31         char *url, buf[LARGE_PACKET_MAX];
32         int fd[2], i, len, rv;
33         pid_t pid;
34         const char *exec = "git-upload-archive";
35         int exec_at = 0;
37         for (i = 1; i < argc; i++) {
38                 const char *arg = argv[i];
39                 if (!prefixcmp(arg, "--exec=")) {
40                         if (exec_at)
41                                 die("multiple --exec specified");
42                         exec = arg + 7;
43                         exec_at = i;
44                         break;
45                 }
46         }
48         url = xstrdup(remote);
49         pid = git_connect(fd, url, exec, 0);
50         if (pid < 0)
51                 return pid;
53         for (i = 1; i < argc; i++) {
54                 if (i == exec_at)
55                         continue;
56                 packet_write(fd[1], "argument %s\n", argv[i]);
57         }
58         packet_flush(fd[1]);
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")) {
66                 if (len > 5 && !prefixcmp(buf, "NACK "))
67                         die("git-archive: NACK %s", buf + 5);
68                 die("git-archive: protocol error");
69         }
71         len = packet_read_line(fd[0], buf, sizeof(buf));
72         if (len)
73                 die("git-archive: expected a flush");
75         /* Now, start reading from fd[0] and spit it out to stdout */
76         rv = recv_sideband("archive", fd[0], 1, 2);
77         close(fd[0]);
78         close(fd[1]);
79         rv |= finish_connect(pid);
81         return !!rv;
82 }
84 static void *format_subst(const struct commit *commit, const char *format,
85                           unsigned long *sizep)
86 {
87         unsigned long len = *sizep;
88         const char *a = format;
89         struct strbuf result;
90         struct strbuf fmt;
92         strbuf_init(&result, 0);
93         strbuf_init(&fmt, 0);
95         for (;;) {
96                 const char *b, *c;
98                 b = memmem(a, len, "$Format:", 8);
99                 if (!b || a + len < b + 9)
100                         break;
101                 c = memchr(b + 8, '$', len - 8);
102                 if (!c)
103                         break;
105                 strbuf_reset(&fmt);
106                 strbuf_add(&fmt, b + 8, c - b - 8);
108                 strbuf_add(&result, a, b - a);
109                 format_commit_message(commit, fmt.buf, &result);
110                 len -= c + 1 - a;
111                 a = c + 1;
112         }
114         strbuf_add(&result, a, len);
116         *sizep = result.len;
118         strbuf_release(&fmt);
119         return strbuf_detach(&result);
122 static void *convert_to_archive(const char *path,
123                                 const void *src, unsigned long *sizep,
124                                 const struct commit *commit)
126         static struct git_attr *attr_export_subst;
127         struct git_attr_check check[1];
129         if (!commit)
130                 return NULL;
132         if (!attr_export_subst)
133                 attr_export_subst = git_attr("export-subst", 12);
135         check[0].attr = attr_export_subst;
136         if (git_checkattr(path, ARRAY_SIZE(check), check))
137                 return NULL;
138         if (!ATTR_TRUE(check[0].value))
139                 return NULL;
141         return format_subst(commit, src, sizep);
144 void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
145                            unsigned int mode, enum object_type *type,
146                            unsigned long *size,
147                            const struct commit *commit)
149         void *buffer, *converted;
151         buffer = read_sha1_file(sha1, type, size);
152         if (buffer && S_ISREG(mode)) {
153                 converted = convert_to_working_tree(path, buffer, size);
154                 if (converted) {
155                         free(buffer);
156                         buffer = converted;
157                 }
159                 converted = convert_to_archive(path, buffer, size, commit);
160                 if (converted) {
161                         free(buffer);
162                         buffer = converted;
163                 }
164         }
166         return buffer;
169 static int init_archiver(const char *name, struct archiver *ar)
171         int rv = -1, i;
173         for (i = 0; i < ARRAY_SIZE(archivers); i++) {
174                 if (!strcmp(name, archivers[i].name)) {
175                         memset(ar, 0, sizeof(*ar));
176                         ar->name = archivers[i].name;
177                         ar->write_archive = archivers[i].write_archive;
178                         ar->parse_extra = archivers[i].parse_extra;
179                         rv = 0;
180                         break;
181                 }
182         }
183         return rv;
186 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
188         ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
191 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
192                        const char *prefix)
194         const char *name = argv[0];
195         const unsigned char *commit_sha1;
196         time_t archive_time;
197         struct tree *tree;
198         const struct commit *commit;
199         unsigned char sha1[20];
201         if (get_sha1(name, sha1))
202                 die("Not a valid object name");
204         commit = lookup_commit_reference_gently(sha1, 1);
205         if (commit) {
206                 commit_sha1 = commit->object.sha1;
207                 archive_time = commit->date;
208         } else {
209                 commit_sha1 = NULL;
210                 archive_time = time(NULL);
211         }
213         tree = parse_tree_indirect(sha1);
214         if (tree == NULL)
215                 die("not a tree object");
217         if (prefix) {
218                 unsigned char tree_sha1[20];
219                 unsigned int mode;
220                 int err;
222                 err = get_tree_entry(tree->object.sha1, prefix,
223                                      tree_sha1, &mode);
224                 if (err || !S_ISDIR(mode))
225                         die("current working directory is untracked");
227                 tree = parse_tree_indirect(tree_sha1);
228         }
229         ar_args->tree = tree;
230         ar_args->commit_sha1 = commit_sha1;
231         ar_args->commit = commit;
232         ar_args->time = archive_time;
235 int parse_archive_args(int argc, const char **argv, struct archiver *ar)
237         const char *extra_argv[MAX_EXTRA_ARGS];
238         int extra_argc = 0;
239         const char *format = "tar";
240         const char *base = "";
241         int verbose = 0;
242         int i;
244         for (i = 1; i < argc; i++) {
245                 const char *arg = argv[i];
247                 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
248                         for (i = 0; i < ARRAY_SIZE(archivers); i++)
249                                 printf("%s\n", archivers[i].name);
250                         exit(0);
251                 }
252                 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
253                         verbose = 1;
254                         continue;
255                 }
256                 if (!prefixcmp(arg, "--format=")) {
257                         format = arg + 9;
258                         continue;
259                 }
260                 if (!prefixcmp(arg, "--prefix=")) {
261                         base = arg + 9;
262                         continue;
263                 }
264                 if (!strcmp(arg, "--")) {
265                         i++;
266                         break;
267                 }
268                 if (arg[0] == '-') {
269                         if (extra_argc > MAX_EXTRA_ARGS - 1)
270                                 die("Too many extra options");
271                         extra_argv[extra_argc++] = arg;
272                         continue;
273                 }
274                 break;
275         }
277         /* We need at least one parameter -- tree-ish */
278         if (argc - 1 < i)
279                 usage(archive_usage);
280         if (init_archiver(format, ar) < 0)
281                 die("Unknown archive format '%s'", format);
283         if (extra_argc) {
284                 if (!ar->parse_extra)
285                         die("'%s' format does not handle %s",
286                             ar->name, extra_argv[0]);
287                 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
288         }
289         ar->args.verbose = verbose;
290         ar->args.base = base;
292         return i;
295 static const char *extract_remote_arg(int *ac, const char **av)
297         int ix, iy, cnt = *ac;
298         int no_more_options = 0;
299         const char *remote = NULL;
301         for (ix = iy = 1; ix < cnt; ix++) {
302                 const char *arg = av[ix];
303                 if (!strcmp(arg, "--"))
304                         no_more_options = 1;
305                 if (!no_more_options) {
306                         if (!prefixcmp(arg, "--remote=")) {
307                                 if (remote)
308                                         die("Multiple --remote specified");
309                                 remote = arg + 9;
310                                 continue;
311                         }
312                         if (arg[0] != '-')
313                                 no_more_options = 1;
314                 }
315                 if (ix != iy)
316                         av[iy] = arg;
317                 iy++;
318         }
319         if (remote) {
320                 av[--cnt] = NULL;
321                 *ac = cnt;
322         }
323         return remote;
326 int cmd_archive(int argc, const char **argv, const char *prefix)
328         struct archiver ar;
329         int tree_idx;
330         const char *remote = NULL;
332         remote = extract_remote_arg(&argc, argv);
333         if (remote)
334                 return run_remote_archiver(remote, argc, argv);
336         setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
338         memset(&ar, 0, sizeof(ar));
339         tree_idx = parse_archive_args(argc, argv, &ar);
340         if (prefix == NULL)
341                 prefix = setup_git_directory();
343         argv += tree_idx;
344         parse_treeish_arg(argv, &ar.args, prefix);
345         parse_pathspec_arg(argv + 1, &ar.args);
347         return ar.write_archive(&ar.args);