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