13eaa22780ad0afbabd3927e52fb4735318bf234
1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
4#include "tag.h"
5#include "object.h"
6#include "commit.h"
7#include "exec_cmd.h"
8#include <signal.h>
9#include <sys/poll.h>
10#include <sys/wait.h>
11
12static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
13
14#define THEY_HAVE (1U << 0)
15#define OUR_REF (1U << 1)
16#define WANTED (1U << 2)
17#define MAX_HAS 256
18#define MAX_NEEDS 256
19static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
20static int use_thin_pack = 0;
21static unsigned char has_sha1[MAX_HAS][20];
22static unsigned char needs_sha1[MAX_NEEDS][20];
23static unsigned int timeout = 0;
24
25static void reset_timeout(void)
26{
27 alarm(timeout);
28}
29
30static int strip(char *line, int len)
31{
32 if (len && line[len-1] == '\n')
33 line[--len] = 0;
34 return len;
35}
36
37static void create_pack_file(void)
38{
39 /* Pipes between rev-list to pack-objects, pack-objects to us
40 * and pack-objects error stream for progress bar.
41 */
42 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
43 pid_t pid_rev_list, pid_pack_objects;
44 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
45 char data[8193], progress[128];
46 int buffered = -1;
47
48 if (pipe(lp_pipe) < 0)
49 die("git-upload-pack: unable to create pipe");
50 pid_rev_list = fork();
51 if (pid_rev_list < 0)
52 die("git-upload-pack: unable to fork git-rev-list");
53
54 if (!pid_rev_list) {
55 int i;
56 int args;
57 const char **argv;
58 char *buf;
59 char **p;
60
61 if (create_full_pack) {
62 args = 10;
63 use_thin_pack = 0; /* no point doing it */
64 }
65 else
66 args = nr_has + nr_needs + 5;
67 p = xmalloc(args * sizeof(char *));
68 argv = (const char **) p;
69 buf = xmalloc(args * 45);
70
71 dup2(lp_pipe[1], 1);
72 close(0);
73 close(lp_pipe[0]);
74 close(lp_pipe[1]);
75 *p++ = "rev-list";
76 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
77 if (create_full_pack || MAX_NEEDS <= nr_needs)
78 *p++ = "--all";
79 else {
80 for (i = 0; i < nr_needs; i++) {
81 *p++ = buf;
82 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
83 buf += 41;
84 }
85 }
86 if (!create_full_pack)
87 for (i = 0; i < nr_has; i++) {
88 *p++ = buf;
89 *buf++ = '^';
90 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
91 buf += 41;
92 }
93 *p++ = NULL;
94 execv_git_cmd(argv);
95 die("git-upload-pack: unable to exec git-rev-list");
96 }
97
98 if (pipe(pu_pipe) < 0)
99 die("git-upload-pack: unable to create pipe");
100 if (pipe(pe_pipe) < 0)
101 die("git-upload-pack: unable to create pipe");
102 pid_pack_objects = fork();
103 if (pid_pack_objects < 0) {
104 /* daemon sets things up to ignore TERM */
105 kill(pid_rev_list, SIGKILL);
106 die("git-upload-pack: unable to fork git-pack-objects");
107 }
108 if (!pid_pack_objects) {
109 dup2(lp_pipe[0], 0);
110 dup2(pu_pipe[1], 1);
111 dup2(pe_pipe[1], 2);
112
113 close(lp_pipe[0]);
114 close(lp_pipe[1]);
115 close(pu_pipe[0]);
116 close(pu_pipe[1]);
117 close(pe_pipe[0]);
118 close(pe_pipe[1]);
119 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL);
120 kill(pid_rev_list, SIGKILL);
121 die("git-upload-pack: unable to exec git-pack-objects");
122 }
123
124 close(lp_pipe[0]);
125 close(lp_pipe[1]);
126
127 /* We read from pe_pipe[0] to capture stderr output for
128 * progress bar, and pu_pipe[0] to capture the pack data.
129 */
130 close(pe_pipe[1]);
131 close(pu_pipe[1]);
132
133 while (1) {
134 const char *who;
135 char *cp;
136 struct pollfd pfd[2];
137 pid_t pid;
138 int status;
139 ssize_t sz;
140 int pe, pu, pollsize;
141
142 pollsize = 0;
143 pe = pu = -1;
144
145 if (0 <= pu_pipe[0]) {
146 pfd[pollsize].fd = pu_pipe[0];
147 pfd[pollsize].events = POLLIN;
148 pu = pollsize;
149 pollsize++;
150 }
151 if (0 <= pe_pipe[0]) {
152 pfd[pollsize].fd = pe_pipe[0];
153 pfd[pollsize].events = POLLIN;
154 pe = pollsize;
155 pollsize++;
156 }
157
158 if (pollsize) {
159 if (poll(pfd, pollsize, -1) < 0) {
160 if (errno != EINTR) {
161 error("poll failed, resuming: %s",
162 strerror(errno));
163 sleep(1);
164 }
165 continue;
166 }
167 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
168 /* Data ready; we keep the last byte
169 * to ourselves in case we detect
170 * broken rev-list, so that we can
171 * leave the stream corrupted. This
172 * is unfortunate -- unpack-objects
173 * would happily accept a valid pack
174 * data with trailing garbage, so
175 * appending garbage after we pass all
176 * the pack data is not good enough to
177 * signal breakage to downstream.
178 */
179 char *cp = data;
180 ssize_t outsz = 0;
181 if (0 <= buffered) {
182 *cp++ = buffered;
183 outsz++;
184 }
185 sz = read(pu_pipe[0], cp,
186 sizeof(data) - outsz);
187 if (0 < sz)
188 ;
189 else if (sz == 0) {
190 close(pu_pipe[0]);
191 pu_pipe[0] = -1;
192 }
193 else
194 goto fail;
195 sz += outsz;
196 if (1 < sz) {
197 buffered = data[sz-1] & 0xFF;
198 sz--;
199 }
200 else
201 buffered = -1;
202 sz = xwrite(1, data, sz);
203 if (sz < 0)
204 goto fail;
205 }
206 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
207 /* Status ready; we do not use it for now,
208 * but later we will add side-band to send it
209 * to the other side.
210 */
211 sz = read(pe_pipe[0], progress,
212 sizeof(progress));
213 if (0 < sz)
214 write(2, progress, sz);
215 else if (sz == 0) {
216 close(pe_pipe[0]);
217 pe_pipe[0] = -1;
218 }
219 else
220 goto fail;
221 }
222 }
223
224 /* See if the children are still there */
225 if (pid_rev_list || pid_pack_objects) {
226 pid = waitpid(-1, &status, WNOHANG);
227 if (!pid)
228 continue;
229 who = ((pid == pid_rev_list) ? "git-rev-list" :
230 (pid == pid_pack_objects) ? "git-pack-objects" :
231 NULL);
232 if (!who) {
233 if (pid < 0) {
234 error("git-upload-pack: %s",
235 strerror(errno));
236 goto fail;
237 }
238 error("git-upload-pack: we weren't "
239 "waiting for %d", pid);
240 continue;
241 }
242 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
243 error("git-upload-pack: %s died with error.",
244 who);
245 goto fail;
246 }
247 if (pid == pid_rev_list)
248 pid_rev_list = 0;
249 if (pid == pid_pack_objects)
250 pid_pack_objects = 0;
251 if (pid_rev_list || pid_pack_objects)
252 continue;
253 }
254
255 /* both died happily */
256 if (pollsize)
257 continue;
258
259 /* flush the data */
260 if (0 <= buffered) {
261 data[0] = buffered;
262 sz = xwrite(1, data, 1);
263 if (sz < 0)
264 goto fail;
265 fprintf(stderr, "flushed.\n");
266 }
267 return;
268 }
269 fail:
270 if (pid_pack_objects)
271 kill(pid_pack_objects, SIGKILL);
272 if (pid_rev_list)
273 kill(pid_rev_list, SIGKILL);
274 die("git-upload-pack: aborting due to possible repository corruption on the remote side.");
275}
276
277static int got_sha1(char *hex, unsigned char *sha1)
278{
279 if (get_sha1_hex(hex, sha1))
280 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
281 if (!has_sha1_file(sha1))
282 return 0;
283 if (nr_has < MAX_HAS) {
284 struct object *o = lookup_object(sha1);
285 if (!(o && o->parsed))
286 o = parse_object(sha1);
287 if (!o)
288 die("oops (%s)", sha1_to_hex(sha1));
289 if (o->type == TYPE_COMMIT) {
290 struct commit_list *parents;
291 if (o->flags & THEY_HAVE)
292 return 0;
293 o->flags |= THEY_HAVE;
294 for (parents = ((struct commit*)o)->parents;
295 parents;
296 parents = parents->next)
297 parents->item->object.flags |= THEY_HAVE;
298 }
299 memcpy(has_sha1[nr_has++], sha1, 20);
300 }
301 return 1;
302}
303
304static int get_common_commits(void)
305{
306 static char line[1000];
307 unsigned char sha1[20], last_sha1[20];
308 int len;
309
310 track_object_refs = 0;
311 save_commit_buffer = 0;
312
313 for(;;) {
314 len = packet_read_line(0, line, sizeof(line));
315 reset_timeout();
316
317 if (!len) {
318 if (nr_has == 0 || multi_ack)
319 packet_write(1, "NAK\n");
320 continue;
321 }
322 len = strip(line, len);
323 if (!strncmp(line, "have ", 5)) {
324 if (got_sha1(line+5, sha1) &&
325 (multi_ack || nr_has == 1)) {
326 if (nr_has >= MAX_HAS)
327 multi_ack = 0;
328 packet_write(1, "ACK %s%s\n",
329 sha1_to_hex(sha1),
330 multi_ack ? " continue" : "");
331 if (multi_ack)
332 memcpy(last_sha1, sha1, 20);
333 }
334 continue;
335 }
336 if (!strcmp(line, "done")) {
337 if (nr_has > 0) {
338 if (multi_ack)
339 packet_write(1, "ACK %s\n",
340 sha1_to_hex(last_sha1));
341 return 0;
342 }
343 packet_write(1, "NAK\n");
344 return -1;
345 }
346 die("git-upload-pack: expected SHA1 list, got '%s'", line);
347 }
348}
349
350static int receive_needs(void)
351{
352 static char line[1000];
353 int len, needs;
354
355 needs = 0;
356 for (;;) {
357 struct object *o;
358 unsigned char dummy[20], *sha1_buf;
359 len = packet_read_line(0, line, sizeof(line));
360 reset_timeout();
361 if (!len)
362 return needs;
363
364 sha1_buf = dummy;
365 if (needs == MAX_NEEDS) {
366 fprintf(stderr,
367 "warning: supporting only a max of %d requests. "
368 "sending everything instead.\n",
369 MAX_NEEDS);
370 }
371 else if (needs < MAX_NEEDS)
372 sha1_buf = needs_sha1[needs];
373
374 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
375 die("git-upload-pack: protocol error, "
376 "expected to get sha, not '%s'", line);
377 if (strstr(line+45, "multi_ack"))
378 multi_ack = 1;
379 if (strstr(line+45, "thin-pack"))
380 use_thin_pack = 1;
381
382 /* We have sent all our refs already, and the other end
383 * should have chosen out of them; otherwise they are
384 * asking for nonsense.
385 *
386 * Hmph. We may later want to allow "want" line that
387 * asks for something like "master~10" (symbolic)...
388 * would it make sense? I don't know.
389 */
390 o = lookup_object(sha1_buf);
391 if (!o || !(o->flags & OUR_REF))
392 die("git-upload-pack: not our ref %s", line+5);
393 if (!(o->flags & WANTED)) {
394 o->flags |= WANTED;
395 needs++;
396 }
397 }
398}
399
400static int send_ref(const char *refname, const unsigned char *sha1)
401{
402 static char *capabilities = "multi_ack thin-pack";
403 struct object *o = parse_object(sha1);
404
405 if (!o)
406 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
407
408 if (capabilities)
409 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
410 0, capabilities);
411 else
412 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
413 capabilities = NULL;
414 if (!(o->flags & OUR_REF)) {
415 o->flags |= OUR_REF;
416 nr_our_refs++;
417 }
418 if (o->type == TYPE_TAG) {
419 o = deref_tag(o, refname, 0);
420 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
421 }
422 return 0;
423}
424
425static int upload_pack(void)
426{
427 reset_timeout();
428 head_ref(send_ref);
429 for_each_ref(send_ref);
430 packet_flush(1);
431 nr_needs = receive_needs();
432 if (!nr_needs)
433 return 0;
434 get_common_commits();
435 create_pack_file();
436 return 0;
437}
438
439int main(int argc, char **argv)
440{
441 char *dir;
442 int i;
443 int strict = 0;
444
445 for (i = 1; i < argc; i++) {
446 char *arg = argv[i];
447
448 if (arg[0] != '-')
449 break;
450 if (!strcmp(arg, "--strict")) {
451 strict = 1;
452 continue;
453 }
454 if (!strncmp(arg, "--timeout=", 10)) {
455 timeout = atoi(arg+10);
456 continue;
457 }
458 if (!strcmp(arg, "--")) {
459 i++;
460 break;
461 }
462 }
463
464 if (i != argc-1)
465 usage(upload_pack_usage);
466 dir = argv[i];
467
468 if (!enter_repo(dir, strict))
469 die("'%s': unable to chdir or not a git archive", dir);
470
471 upload_pack();
472 return 0;
473}