Code

pack-objects: learn about pack index version 2
[git.git] / builtin-archive.c
1 /*
2  * Copyright (c) 2006 Franck Bui-Huu
3  * Copyright (c) 2006 Rene Scharfe
4  */
5 #include <time.h>
6 #include "cache.h"
7 #include "builtin.h"
8 #include "archive.h"
9 #include "commit.h"
10 #include "tree-walk.h"
11 #include "exec_cmd.h"
12 #include "pkt-line.h"
13 #include "sideband.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 (!strncmp("--exec=", arg, 7)) {
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);
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 && !strncmp(buf, "NACK ", 5))
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         rv |= finish_connect(pid);
80         return !!rv;
81 }
83 static int init_archiver(const char *name, struct archiver *ar)
84 {
85         int rv = -1, i;
87         for (i = 0; i < ARRAY_SIZE(archivers); i++) {
88                 if (!strcmp(name, archivers[i].name)) {
89                         memset(ar, 0, sizeof(*ar));
90                         ar->name = archivers[i].name;
91                         ar->write_archive = archivers[i].write_archive;
92                         ar->parse_extra = archivers[i].parse_extra;
93                         rv = 0;
94                         break;
95                 }
96         }
97         return rv;
98 }
100 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
102         ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
105 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
106                        const char *prefix)
108         const char *name = argv[0];
109         const unsigned char *commit_sha1;
110         time_t archive_time;
111         struct tree *tree;
112         struct commit *commit;
113         unsigned char sha1[20];
115         if (get_sha1(name, sha1))
116                 die("Not a valid object name");
118         commit = lookup_commit_reference_gently(sha1, 1);
119         if (commit) {
120                 commit_sha1 = commit->object.sha1;
121                 archive_time = commit->date;
122         } else {
123                 commit_sha1 = NULL;
124                 archive_time = time(NULL);
125         }
127         tree = parse_tree_indirect(sha1);
128         if (tree == NULL)
129                 die("not a tree object");
131         if (prefix) {
132                 unsigned char tree_sha1[20];
133                 unsigned int mode;
134                 int err;
136                 err = get_tree_entry(tree->object.sha1, prefix,
137                                      tree_sha1, &mode);
138                 if (err || !S_ISDIR(mode))
139                         die("current working directory is untracked");
141                 free(tree);
142                 tree = parse_tree_indirect(tree_sha1);
143         }
144         ar_args->tree = tree;
145         ar_args->commit_sha1 = commit_sha1;
146         ar_args->time = archive_time;
149 int parse_archive_args(int argc, const char **argv, struct archiver *ar)
151         const char *extra_argv[MAX_EXTRA_ARGS];
152         int extra_argc = 0;
153         const char *format = NULL; /* might want to default to "tar" */
154         const char *base = "";
155         int verbose = 0;
156         int i;
158         for (i = 1; i < argc; i++) {
159                 const char *arg = argv[i];
161                 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
162                         for (i = 0; i < ARRAY_SIZE(archivers); i++)
163                                 printf("%s\n", archivers[i].name);
164                         exit(0);
165                 }
166                 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
167                         verbose = 1;
168                         continue;
169                 }
170                 if (!strncmp(arg, "--format=", 9)) {
171                         format = arg + 9;
172                         continue;
173                 }
174                 if (!strncmp(arg, "--prefix=", 9)) {
175                         base = arg + 9;
176                         continue;
177                 }
178                 if (!strcmp(arg, "--")) {
179                         i++;
180                         break;
181                 }
182                 if (arg[0] == '-') {
183                         if (extra_argc > MAX_EXTRA_ARGS - 1)
184                                 die("Too many extra options");
185                         extra_argv[extra_argc++] = arg;
186                         continue;
187                 }
188                 break;
189         }
191         /* We need at least one parameter -- tree-ish */
192         if (argc - 1 < i)
193                 usage(archive_usage);
194         if (!format)
195                 die("You must specify an archive format");
196         if (init_archiver(format, ar) < 0)
197                 die("Unknown archive format '%s'", format);
199         if (extra_argc) {
200                 if (!ar->parse_extra)
201                         die("'%s' format does not handle %s",
202                             ar->name, extra_argv[0]);
203                 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
204         }
205         ar->args.verbose = verbose;
206         ar->args.base = base;
208         return i;
211 static const char *extract_remote_arg(int *ac, const char **av)
213         int ix, iy, cnt = *ac;
214         int no_more_options = 0;
215         const char *remote = NULL;
217         for (ix = iy = 1; ix < cnt; ix++) {
218                 const char *arg = av[ix];
219                 if (!strcmp(arg, "--"))
220                         no_more_options = 1;
221                 if (!no_more_options) {
222                         if (!strncmp(arg, "--remote=", 9)) {
223                                 if (remote)
224                                         die("Multiple --remote specified");
225                                 remote = arg + 9;
226                                 continue;
227                         }
228                         if (arg[0] != '-')
229                                 no_more_options = 1;
230                 }
231                 if (ix != iy)
232                         av[iy] = arg;
233                 iy++;
234         }
235         if (remote) {
236                 av[--cnt] = NULL;
237                 *ac = cnt;
238         }
239         return remote;
242 int cmd_archive(int argc, const char **argv, const char *prefix)
244         struct archiver ar;
245         int tree_idx;
246         const char *remote = NULL;
248         remote = extract_remote_arg(&argc, argv);
249         if (remote)
250                 return run_remote_archiver(remote, argc, argv);
252         setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
254         memset(&ar, 0, sizeof(ar));
255         tree_idx = parse_archive_args(argc, argv, &ar);
256         if (prefix == NULL)
257                 prefix = setup_git_directory();
259         argv += tree_idx;
260         parse_treeish_arg(argv, &ar.args, prefix);
261         parse_pathspec_arg(argv + 1, &ar.args);
263         return ar.write_archive(&ar.args);