]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-archive.c
archive: specfile support (--pretty=format: in archive files)
[thirdparty/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"
14
15 static const char archive_usage[] = \
16 "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
17
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 };
27
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;
36
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 }
47
48 url = xstrdup(remote);
49 pid = git_connect(fd, url, exec, 0);
50 if (pid < 0)
51 return pid;
52
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]);
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")) {
66 if (len > 5 && !prefixcmp(buf, "NACK "))
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 */
76 rv = recv_sideband("archive", fd[0], 1, 2);
77 close(fd[0]);
78 close(fd[1]);
79 rv |= finish_connect(pid);
80
81 return !!rv;
82 }
83
84 static 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
110 void *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
135 static 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)) {
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;
145 rv = 0;
146 break;
147 }
148 }
149 return rv;
150 }
151
152 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
153 {
154 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
155 }
156
157 void 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;
164 const struct commit *commit;
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
193 tree = parse_tree_indirect(tree_sha1);
194 }
195 ar_args->tree = tree;
196 ar_args->commit_sha1 = commit_sha1;
197 ar_args->commit = commit;
198 ar_args->time = archive_time;
199 }
200
201 int 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;
205 const char *format = "tar";
206 const char *base = "";
207 int verbose = 0;
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")) {
214 for (i = 0; i < ARRAY_SIZE(archivers); i++)
215 printf("%s\n", archivers[i].name);
216 exit(0);
217 }
218 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
219 verbose = 1;
220 continue;
221 }
222 if (!prefixcmp(arg, "--format=")) {
223 format = arg + 9;
224 continue;
225 }
226 if (!prefixcmp(arg, "--prefix=")) {
227 base = arg + 9;
228 continue;
229 }
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
243 /* We need at least one parameter -- tree-ish */
244 if (argc - 1 < i)
245 usage(archive_usage);
246 if (init_archiver(format, ar) < 0)
247 die("Unknown archive format '%s'", format);
248
249 if (extra_argc) {
250 if (!ar->parse_extra)
251 die("'%s' format does not handle %s",
252 ar->name, extra_argv[0]);
253 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
254 }
255 ar->args.verbose = verbose;
256 ar->args.base = base;
257
258 return i;
259 }
260
261 static const char *extract_remote_arg(int *ac, const char **av)
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) {
272 if (!prefixcmp(arg, "--remote=")) {
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
292 int cmd_archive(int argc, const char **argv, const char *prefix)
293 {
294 struct archiver ar;
295 int tree_idx;
296 const char *remote = NULL;
297
298 remote = extract_remote_arg(&argc, argv);
299 if (remote)
300 return run_remote_archiver(remote, argc, argv);
301
302 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
303
304 memset(&ar, 0, sizeof(ar));
305 tree_idx = parse_archive_args(argc, argv, &ar);
306 if (prefix == NULL)
307 prefix = setup_git_directory();
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 }