]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-archive.c
archive: specfile syntax change: "$Format:%PLCHLDR$" instead of just "%PLCHLDR" ...
[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
df4a394f
RS
84static void *format_specfile(const struct commit *commit, const char *format,
85 unsigned long *sizep)
86{
87 unsigned long len = *sizep, result_len = 0;
88 const char *a = format;
89 char *result = NULL;
90
91 for (;;) {
92 const char *b, *c;
93 char *fmt, *formatted = NULL;
94 unsigned long a_len, fmt_len, formatted_len, allocated = 0;
95
96 b = memmem(a, len, "$Format:", 8);
97 if (!b || a + len < b + 9)
98 break;
99 c = memchr(b + 8, '$', len - 8);
100 if (!c)
101 break;
102
103 a_len = b - a;
104 fmt_len = c - b - 8;
105 fmt = xmalloc(fmt_len + 1);
106 memcpy(fmt, b + 8, fmt_len);
107 fmt[fmt_len] = '\0';
108
109 formatted_len = format_commit_message(commit, fmt, &formatted,
110 &allocated);
111 result = xrealloc(result, result_len + a_len + formatted_len);
112 memcpy(result + result_len, a, a_len);
113 memcpy(result + result_len + a_len, formatted, formatted_len);
114 result_len += a_len + formatted_len;
115 len -= c + 1 - a;
116 a = c + 1;
117 }
118
119 if (result && len) {
120 result = xrealloc(result, result_len + len);
121 memcpy(result + result_len, a, len);
122 result_len += len;
123 }
124
125 *sizep = result_len;
126
127 return result;
128}
129
8460b2fc
RS
130static void *convert_to_archive(const char *path,
131 const void *src, unsigned long *sizep,
132 const struct commit *commit)
133{
134 static struct git_attr *attr_specfile;
135 struct git_attr_check check[1];
8460b2fc
RS
136
137 if (!commit)
138 return NULL;
139
140 if (!attr_specfile)
141 attr_specfile = git_attr("specfile", 8);
142
143 check[0].attr = attr_specfile;
144 if (git_checkattr(path, ARRAY_SIZE(check), check))
145 return NULL;
146 if (!ATTR_TRUE(check[0].value))
147 return NULL;
148
df4a394f 149 return format_specfile(commit, src, sizep);
8460b2fc
RS
150}
151
152void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
153 unsigned int mode, enum object_type *type,
154 unsigned long *size,
155 const struct commit *commit)
156{
157 void *buffer, *converted;
158
159 buffer = read_sha1_file(sha1, type, size);
160 if (buffer && S_ISREG(mode)) {
161 converted = convert_to_working_tree(path, buffer, size);
162 if (converted) {
163 free(buffer);
164 buffer = converted;
165 }
166
167 converted = convert_to_archive(path, buffer, size, commit);
168 if (converted) {
169 free(buffer);
170 buffer = converted;
171 }
172 }
173
174 return buffer;
175}
176
4df096a5
FBH
177static int init_archiver(const char *name, struct archiver *ar)
178{
179 int rv = -1, i;
180
181 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
182 if (!strcmp(name, archivers[i].name)) {
6c2f207b
SP
183 memset(ar, 0, sizeof(*ar));
184 ar->name = archivers[i].name;
185 ar->write_archive = archivers[i].write_archive;
186 ar->parse_extra = archivers[i].parse_extra;
4df096a5
FBH
187 rv = 0;
188 break;
189 }
190 }
191 return rv;
192}
193
194void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
195{
196 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
197}
198
199void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
200 const char *prefix)
201{
202 const char *name = argv[0];
203 const unsigned char *commit_sha1;
204 time_t archive_time;
205 struct tree *tree;
8460b2fc 206 const struct commit *commit;
4df096a5
FBH
207 unsigned char sha1[20];
208
209 if (get_sha1(name, sha1))
210 die("Not a valid object name");
211
212 commit = lookup_commit_reference_gently(sha1, 1);
213 if (commit) {
214 commit_sha1 = commit->object.sha1;
215 archive_time = commit->date;
216 } else {
217 commit_sha1 = NULL;
218 archive_time = time(NULL);
219 }
220
221 tree = parse_tree_indirect(sha1);
222 if (tree == NULL)
223 die("not a tree object");
224
225 if (prefix) {
226 unsigned char tree_sha1[20];
227 unsigned int mode;
228 int err;
229
230 err = get_tree_entry(tree->object.sha1, prefix,
231 tree_sha1, &mode);
232 if (err || !S_ISDIR(mode))
233 die("current working directory is untracked");
234
4df096a5
FBH
235 tree = parse_tree_indirect(tree_sha1);
236 }
237 ar_args->tree = tree;
238 ar_args->commit_sha1 = commit_sha1;
8460b2fc 239 ar_args->commit = commit;
4df096a5
FBH
240 ar_args->time = archive_time;
241}
242
4df096a5
FBH
243int parse_archive_args(int argc, const char **argv, struct archiver *ar)
244{
245 const char *extra_argv[MAX_EXTRA_ARGS];
246 int extra_argc = 0;
8ff21b1a 247 const char *format = "tar";
4df096a5 248 const char *base = "";
e0ffb248 249 int verbose = 0;
4df096a5
FBH
250 int i;
251
252 for (i = 1; i < argc; i++) {
253 const char *arg = argv[i];
254
255 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
37f94436
JH
256 for (i = 0; i < ARRAY_SIZE(archivers); i++)
257 printf("%s\n", archivers[i].name);
258 exit(0);
4df096a5 259 }
e0ffb248
JH
260 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
261 verbose = 1;
262 continue;
263 }
cc44c765 264 if (!prefixcmp(arg, "--format=")) {
4df096a5
FBH
265 format = arg + 9;
266 continue;
267 }
cc44c765 268 if (!prefixcmp(arg, "--prefix=")) {
4df096a5
FBH
269 base = arg + 9;
270 continue;
271 }
4df096a5
FBH
272 if (!strcmp(arg, "--")) {
273 i++;
274 break;
275 }
276 if (arg[0] == '-') {
277 if (extra_argc > MAX_EXTRA_ARGS - 1)
278 die("Too many extra options");
279 extra_argv[extra_argc++] = arg;
280 continue;
281 }
282 break;
283 }
284
37f94436
JH
285 /* We need at least one parameter -- tree-ish */
286 if (argc - 1 < i)
4df096a5 287 usage(archive_usage);
4df096a5
FBH
288 if (init_archiver(format, ar) < 0)
289 die("Unknown archive format '%s'", format);
290
37f94436
JH
291 if (extra_argc) {
292 if (!ar->parse_extra)
2232c0c6
RS
293 die("'%s' format does not handle %s",
294 ar->name, extra_argv[0]);
4df096a5
FBH
295 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
296 }
e0ffb248 297 ar->args.verbose = verbose;
4df096a5
FBH
298 ar->args.base = base;
299
300 return i;
301}
302
d751864c 303static const char *extract_remote_arg(int *ac, const char **av)
37f94436
JH
304{
305 int ix, iy, cnt = *ac;
306 int no_more_options = 0;
307 const char *remote = NULL;
308
309 for (ix = iy = 1; ix < cnt; ix++) {
310 const char *arg = av[ix];
311 if (!strcmp(arg, "--"))
312 no_more_options = 1;
313 if (!no_more_options) {
cc44c765 314 if (!prefixcmp(arg, "--remote=")) {
37f94436
JH
315 if (remote)
316 die("Multiple --remote specified");
317 remote = arg + 9;
318 continue;
319 }
320 if (arg[0] != '-')
321 no_more_options = 1;
322 }
323 if (ix != iy)
324 av[iy] = arg;
325 iy++;
326 }
327 if (remote) {
328 av[--cnt] = NULL;
329 *ac = cnt;
330 }
331 return remote;
332}
333
4df096a5
FBH
334int cmd_archive(int argc, const char **argv, const char *prefix)
335{
336 struct archiver ar;
337 int tree_idx;
37f94436 338 const char *remote = NULL;
4df096a5 339
d751864c 340 remote = extract_remote_arg(&argc, argv);
37f94436
JH
341 if (remote)
342 return run_remote_archiver(remote, argc, argv);
4df096a5 343
aa909861 344 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
8142f603 345
37f94436
JH
346 memset(&ar, 0, sizeof(ar));
347 tree_idx = parse_archive_args(argc, argv, &ar);
265d5280
RS
348 if (prefix == NULL)
349 prefix = setup_git_directory();
4df096a5
FBH
350
351 argv += tree_idx;
352 parse_treeish_arg(argv, &ar.args, prefix);
353 parse_pathspec_arg(argv + 1, &ar.args);
354
355 return ar.write_archive(&ar.args);
356}