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