]>
Commit | Line | Data |
---|---|---|
4df096a5 FBH |
1 | /* |
2 | * Copyright (c) 2006 Franck Bui-Huu | |
3 | * Copyright (c) 2006 Rene Scharfe | |
4 | */ | |
4df096a5 FBH |
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" | |
23d6d112 | 12 | #include "sideband.h" |
4df096a5 FBH |
13 | |
14 | static const char archive_usage[] = \ | |
e0ffb248 | 15 | "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]"; |
4df096a5 | 16 | |
6c2f207b SP |
17 | static struct archiver_desc |
18 | { | |
19 | const char *name; | |
20 | write_archive_fn_t write_archive; | |
21 | parse_extra_args_fn_t parse_extra; | |
22 | } archivers[] = { | |
23 | { "tar", write_tar_archive, NULL }, | |
24 | { "zip", write_zip_archive, parse_extra_zip_args }, | |
4df096a5 FBH |
25 | }; |
26 | ||
37f94436 | 27 | static int run_remote_archiver(const char *remote, int argc, |
4df096a5 FBH |
28 | const char **argv) |
29 | { | |
23d6d112 | 30 | char *url, buf[LARGE_PACKET_MAX]; |
4df096a5 FBH |
31 | int fd[2], i, len, rv; |
32 | pid_t pid; | |
fe5ab763 JH |
33 | const char *exec = "git-upload-archive"; |
34 | int exec_at = 0; | |
4df096a5 | 35 | |
fe5ab763 JH |
36 | for (i = 1; i < argc; i++) { |
37 | const char *arg = argv[i]; | |
38 | if (!strncmp("--exec=", arg, 7)) { | |
39 | if (exec_at) | |
40 | die("multiple --exec specified"); | |
41 | exec = arg + 7; | |
42 | exec_at = i; | |
43 | break; | |
44 | } | |
45 | } | |
4df096a5 | 46 | |
37f94436 | 47 | url = xstrdup(remote); |
fe5ab763 | 48 | pid = git_connect(fd, url, exec); |
4df096a5 FBH |
49 | if (pid < 0) |
50 | return pid; | |
51 | ||
fe5ab763 JH |
52 | for (i = 1; i < argc; i++) { |
53 | if (i == exec_at) | |
54 | continue; | |
4df096a5 | 55 | packet_write(fd[1], "argument %s\n", argv[i]); |
fe5ab763 | 56 | } |
4df096a5 FBH |
57 | packet_flush(fd[1]); |
58 | ||
59 | len = packet_read_line(fd[0], buf, sizeof(buf)); | |
60 | if (!len) | |
61 | die("git-archive: expected ACK/NAK, got EOF"); | |
62 | if (buf[len-1] == '\n') | |
63 | buf[--len] = 0; | |
64 | if (strcmp(buf, "ACK")) { | |
65 | if (len > 5 && !strncmp(buf, "NACK ", 5)) | |
66 | die("git-archive: NACK %s", buf + 5); | |
67 | die("git-archive: protocol error"); | |
68 | } | |
69 | ||
70 | len = packet_read_line(fd[0], buf, sizeof(buf)); | |
71 | if (len) | |
72 | die("git-archive: expected a flush"); | |
73 | ||
74 | /* Now, start reading from fd[0] and spit it out to stdout */ | |
9ac13ec9 | 75 | rv = recv_sideband("archive", fd[0], 1, 2); |
4df096a5 FBH |
76 | close(fd[0]); |
77 | rv |= finish_connect(pid); | |
78 | ||
79 | return !!rv; | |
80 | } | |
81 | ||
82 | static int init_archiver(const char *name, struct archiver *ar) | |
83 | { | |
84 | int rv = -1, i; | |
85 | ||
86 | for (i = 0; i < ARRAY_SIZE(archivers); i++) { | |
87 | if (!strcmp(name, archivers[i].name)) { | |
6c2f207b SP |
88 | memset(ar, 0, sizeof(*ar)); |
89 | ar->name = archivers[i].name; | |
90 | ar->write_archive = archivers[i].write_archive; | |
91 | ar->parse_extra = archivers[i].parse_extra; | |
4df096a5 FBH |
92 | rv = 0; |
93 | break; | |
94 | } | |
95 | } | |
96 | return rv; | |
97 | } | |
98 | ||
99 | void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args) | |
100 | { | |
101 | ar_args->pathspec = get_pathspec(ar_args->base, pathspec); | |
102 | } | |
103 | ||
104 | void parse_treeish_arg(const char **argv, struct archiver_args *ar_args, | |
105 | const char *prefix) | |
106 | { | |
107 | const char *name = argv[0]; | |
108 | const unsigned char *commit_sha1; | |
109 | time_t archive_time; | |
110 | struct tree *tree; | |
111 | struct commit *commit; | |
112 | unsigned char sha1[20]; | |
113 | ||
114 | if (get_sha1(name, sha1)) | |
115 | die("Not a valid object name"); | |
116 | ||
117 | commit = lookup_commit_reference_gently(sha1, 1); | |
118 | if (commit) { | |
119 | commit_sha1 = commit->object.sha1; | |
120 | archive_time = commit->date; | |
121 | } else { | |
122 | commit_sha1 = NULL; | |
123 | archive_time = time(NULL); | |
124 | } | |
125 | ||
126 | tree = parse_tree_indirect(sha1); | |
127 | if (tree == NULL) | |
128 | die("not a tree object"); | |
129 | ||
130 | if (prefix) { | |
131 | unsigned char tree_sha1[20]; | |
132 | unsigned int mode; | |
133 | int err; | |
134 | ||
135 | err = get_tree_entry(tree->object.sha1, prefix, | |
136 | tree_sha1, &mode); | |
137 | if (err || !S_ISDIR(mode)) | |
138 | die("current working directory is untracked"); | |
139 | ||
4df096a5 FBH |
140 | tree = parse_tree_indirect(tree_sha1); |
141 | } | |
142 | ar_args->tree = tree; | |
143 | ar_args->commit_sha1 = commit_sha1; | |
144 | ar_args->time = archive_time; | |
145 | } | |
146 | ||
4df096a5 FBH |
147 | int parse_archive_args(int argc, const char **argv, struct archiver *ar) |
148 | { | |
149 | const char *extra_argv[MAX_EXTRA_ARGS]; | |
150 | int extra_argc = 0; | |
151 | const char *format = NULL; /* might want to default to "tar" */ | |
4df096a5 | 152 | const char *base = ""; |
e0ffb248 | 153 | int verbose = 0; |
4df096a5 FBH |
154 | int i; |
155 | ||
156 | for (i = 1; i < argc; i++) { | |
157 | const char *arg = argv[i]; | |
158 | ||
159 | if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) { | |
37f94436 JH |
160 | for (i = 0; i < ARRAY_SIZE(archivers); i++) |
161 | printf("%s\n", archivers[i].name); | |
162 | exit(0); | |
4df096a5 | 163 | } |
e0ffb248 JH |
164 | if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) { |
165 | verbose = 1; | |
166 | continue; | |
167 | } | |
4df096a5 FBH |
168 | if (!strncmp(arg, "--format=", 9)) { |
169 | format = arg + 9; | |
170 | continue; | |
171 | } | |
172 | if (!strncmp(arg, "--prefix=", 9)) { | |
173 | base = arg + 9; | |
174 | continue; | |
175 | } | |
4df096a5 FBH |
176 | if (!strcmp(arg, "--")) { |
177 | i++; | |
178 | break; | |
179 | } | |
180 | if (arg[0] == '-') { | |
181 | if (extra_argc > MAX_EXTRA_ARGS - 1) | |
182 | die("Too many extra options"); | |
183 | extra_argv[extra_argc++] = arg; | |
184 | continue; | |
185 | } | |
186 | break; | |
187 | } | |
188 | ||
37f94436 JH |
189 | /* We need at least one parameter -- tree-ish */ |
190 | if (argc - 1 < i) | |
4df096a5 FBH |
191 | usage(archive_usage); |
192 | if (!format) | |
193 | die("You must specify an archive format"); | |
194 | if (init_archiver(format, ar) < 0) | |
195 | die("Unknown archive format '%s'", format); | |
196 | ||
37f94436 JH |
197 | if (extra_argc) { |
198 | if (!ar->parse_extra) | |
2232c0c6 RS |
199 | die("'%s' format does not handle %s", |
200 | ar->name, extra_argv[0]); | |
4df096a5 FBH |
201 | ar->args.extra = ar->parse_extra(extra_argc, extra_argv); |
202 | } | |
e0ffb248 | 203 | ar->args.verbose = verbose; |
4df096a5 FBH |
204 | ar->args.base = base; |
205 | ||
206 | return i; | |
207 | } | |
208 | ||
d751864c | 209 | static const char *extract_remote_arg(int *ac, const char **av) |
37f94436 JH |
210 | { |
211 | int ix, iy, cnt = *ac; | |
212 | int no_more_options = 0; | |
213 | const char *remote = NULL; | |
214 | ||
215 | for (ix = iy = 1; ix < cnt; ix++) { | |
216 | const char *arg = av[ix]; | |
217 | if (!strcmp(arg, "--")) | |
218 | no_more_options = 1; | |
219 | if (!no_more_options) { | |
220 | if (!strncmp(arg, "--remote=", 9)) { | |
221 | if (remote) | |
222 | die("Multiple --remote specified"); | |
223 | remote = arg + 9; | |
224 | continue; | |
225 | } | |
226 | if (arg[0] != '-') | |
227 | no_more_options = 1; | |
228 | } | |
229 | if (ix != iy) | |
230 | av[iy] = arg; | |
231 | iy++; | |
232 | } | |
233 | if (remote) { | |
234 | av[--cnt] = NULL; | |
235 | *ac = cnt; | |
236 | } | |
237 | return remote; | |
238 | } | |
239 | ||
4df096a5 FBH |
240 | int cmd_archive(int argc, const char **argv, const char *prefix) |
241 | { | |
242 | struct archiver ar; | |
243 | int tree_idx; | |
37f94436 | 244 | const char *remote = NULL; |
4df096a5 | 245 | |
d751864c | 246 | remote = extract_remote_arg(&argc, argv); |
37f94436 JH |
247 | if (remote) |
248 | return run_remote_archiver(remote, argc, argv); | |
4df096a5 | 249 | |
aa909861 | 250 | setvbuf(stderr, NULL, _IOLBF, BUFSIZ); |
8142f603 | 251 | |
37f94436 JH |
252 | memset(&ar, 0, sizeof(ar)); |
253 | tree_idx = parse_archive_args(argc, argv, &ar); | |
4df096a5 FBH |
254 | if (prefix == NULL) |
255 | prefix = setup_git_directory(); | |
256 | ||
257 | argv += tree_idx; | |
258 | parse_treeish_arg(argv, &ar.args, prefix); | |
259 | parse_pathspec_arg(argv + 1, &ar.args); | |
260 | ||
261 | return ar.write_archive(&ar.args); | |
262 | } |