Code

c6423b9c48b4d84269343e30c790d5c82dc9cd83
[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"
14 static const char archive_usage[] = \
15 "git-archive --format=<fmt> [--prefix=<prefix>/] [<extra>] <tree-ish> [path...]";
17 struct archiver archivers[] = {
18         { .name = "tar", .write_archive = write_tar_archive },
19 };
21 static int run_remote_archiver(struct archiver *ar, int argc,
22                                const char **argv)
23 {
24         char *url, buf[1024];
25         int fd[2], i, len, rv;
26         pid_t pid;
28         sprintf(buf, "git-upload-archive");
30         url = xstrdup(ar->remote);
31         pid = git_connect(fd, url, buf);
32         if (pid < 0)
33                 return pid;
35         for (i = 1; i < argc; i++) {
36                 if (!strncmp(argv[i], "--remote=", 9))
37                         continue;
38                 packet_write(fd[1], "argument %s\n", argv[i]);
39         }
40         packet_flush(fd[1]);
42         len = packet_read_line(fd[0], buf, sizeof(buf));
43         if (!len)
44                 die("git-archive: expected ACK/NAK, got EOF");
45         if (buf[len-1] == '\n')
46                 buf[--len] = 0;
47         if (strcmp(buf, "ACK")) {
48                 if (len > 5 && !strncmp(buf, "NACK ", 5))
49                         die("git-archive: NACK %s", buf + 5);
50                 die("git-archive: protocol error");
51         }
53         len = packet_read_line(fd[0], buf, sizeof(buf));
54         if (len)
55                 die("git-archive: expected a flush");
57         /* Now, start reading from fd[0] and spit it out to stdout */
58         rv = copy_fd(fd[0], 1);
60         close(fd[0]);
61         rv |= finish_connect(pid);
63         return !!rv;
64 }
66 static int init_archiver(const char *name, struct archiver *ar)
67 {
68         int rv = -1, i;
70         for (i = 0; i < ARRAY_SIZE(archivers); i++) {
71                 if (!strcmp(name, archivers[i].name)) {
72                         memcpy(ar, &archivers[i], sizeof(struct archiver));
73                         rv = 0;
74                         break;
75                 }
76         }
77         return rv;
78 }
80 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
81 {
82         ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
83 }
85 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
86                        const char *prefix)
87 {
88         const char *name = argv[0];
89         const unsigned char *commit_sha1;
90         time_t archive_time;
91         struct tree *tree;
92         struct commit *commit;
93         unsigned char sha1[20];
95         if (get_sha1(name, sha1))
96                 die("Not a valid object name");
98         commit = lookup_commit_reference_gently(sha1, 1);
99         if (commit) {
100                 commit_sha1 = commit->object.sha1;
101                 archive_time = commit->date;
102         } else {
103                 commit_sha1 = NULL;
104                 archive_time = time(NULL);
105         }
107         tree = parse_tree_indirect(sha1);
108         if (tree == NULL)
109                 die("not a tree object");
111         if (prefix) {
112                 unsigned char tree_sha1[20];
113                 unsigned int mode;
114                 int err;
116                 err = get_tree_entry(tree->object.sha1, prefix,
117                                      tree_sha1, &mode);
118                 if (err || !S_ISDIR(mode))
119                         die("current working directory is untracked");
121                 free(tree);
122                 tree = parse_tree_indirect(tree_sha1);
123         }
124         ar_args->tree = tree;
125         ar_args->commit_sha1 = commit_sha1;
126         ar_args->time = archive_time;
129 static const char *default_parse_extra(struct archiver *ar,
130                                        const char **argv)
132         static char msg[64];
134         snprintf(msg, sizeof(msg) - 4, "'%s' format does not handle %s",
135                  ar->name, *argv);
137         return strcat(msg, "...");
140 int parse_archive_args(int argc, const char **argv, struct archiver *ar)
142         const char *extra_argv[MAX_EXTRA_ARGS];
143         int extra_argc = 0;
144         const char *format = NULL; /* might want to default to "tar" */
145         const char *remote = NULL;
146         const char *base = "";
147         int list = 0;
148         int i;
150         for (i = 1; i < argc; i++) {
151                 const char *arg = argv[i];
153                 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
154                         list = 1;
155                         continue;
156                 }
157                 if (!strncmp(arg, "--format=", 9)) {
158                         format = arg + 9;
159                         continue;
160                 }
161                 if (!strncmp(arg, "--prefix=", 9)) {
162                         base = arg + 9;
163                         continue;
164                 }
165                 if (!strncmp(arg, "--remote=", 9)) {
166                         remote = arg + 9;
167                         continue;
168                 }
169                 if (!strcmp(arg, "--")) {
170                         i++;
171                         break;
172                 }
173                 if (arg[0] == '-') {
174                         if (extra_argc > MAX_EXTRA_ARGS - 1)
175                                 die("Too many extra options");
176                         extra_argv[extra_argc++] = arg;
177                         continue;
178                 }
179                 break;
180         }
182         if (list) {
183                 if (!remote) {
184                         for (i = 0; i < ARRAY_SIZE(archivers); i++)
185                                 printf("%s\n", archivers[i].name);
186                         exit(0);
187                 }
188                 die("--list and --remote are mutually exclusive");
189         }
191         if (argc - i < 1)
192                 usage(archive_usage);
193         if (!format)
194                 die("You must specify an archive format");
195         if (init_archiver(format, ar) < 0)
196                 die("Unknown archive format '%s'", format);
198         if (extra_argc && !remote) {
199                 if (!ar->parse_extra) {
200                         die("%s", default_parse_extra(ar, extra_argv));
201                 }
202                 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
203         }
204         ar->remote = remote;
205         ar->args.base = base;
207         return i;
210 int cmd_archive(int argc, const char **argv, const char *prefix)
212         struct archiver ar;
213         int tree_idx;
215         tree_idx = parse_archive_args(argc, argv, &ar);
217         if (ar.remote)
218                 return run_remote_archiver(&ar, argc, argv);
220         if (prefix == NULL)
221                 prefix = setup_git_directory();
223         argv += tree_idx;
224         parse_treeish_arg(argv, &ar.args, prefix);
225         parse_pathspec_arg(argv + 1, &ar.args);
227         return ar.write_archive(&ar.args);