Add git-archive
[git.git] / builtin-archive.c
blobf6bc269fdc7e79d601e18cdb53511e6139fb1b7b
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 { "" /* dummy */ },
21 static int run_remote_archiver(struct archiver *ar, int argc,
22 const char **argv)
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]);
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");
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;
66 static int init_archiver(const char *name, struct archiver *ar)
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;
77 return rv;
80 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
82 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
85 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
86 const char *prefix)
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);
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);
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;
157 if (!strncmp(arg, "--format=", 9)) {
158 format = arg + 9;
159 continue;
161 if (!strncmp(arg, "--prefix=", 9)) {
162 base = arg + 9;
163 continue;
165 if (!strncmp(arg, "--remote=", 9)) {
166 remote = arg + 9;
167 continue;
169 if (!strcmp(arg, "--")) {
170 i++;
171 break;
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;
179 break;
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);
188 die("--list and --remote are mutually exclusive");
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));
202 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
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);