pkt-line: move LARGE_PACKET_MAX definition from sideband
[git/git.git] / upload-pack.c
CommitLineData
def88e9a
LT
1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
958c24b1 4#include "sideband.h"
f6b42a81
JH
5#include "tag.h"
6#include "object.h"
f0243f26 7#include "commit.h"
77cb17e9 8#include "exec_cmd.h"
9b8dc263
JS
9#include "diff.h"
10#include "revision.h"
11#include "list-objects.h"
cc41fa8d 12#include "run-command.h"
051e4005 13#include "sigchain.h"
ff5effdf 14#include "version.h"
daebaa78 15#include "string-list.h"
def88e9a 16
62b4698e 17static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
def88e9a 18
937a515a
JH
19/* bits #0..7 in revision.h, #8..10 in commit.c */
20#define THEY_HAVE (1u << 11)
21#define OUR_REF (1u << 12)
22#define WANTED (1u << 13)
23#define COMMON_KNOWN (1u << 14)
24#define REACHABLE (1u << 15)
25
f53514bc
JS
26#define SHALLOW (1u << 16)
27#define NOT_SHALLOW (1u << 17)
28#define CLIENT_SHALLOW (1u << 18)
29
3fbe2d54 30static unsigned long oldest_have;
937a515a 31
3f1da57f 32static int multi_ack;
4e10cf9a 33static int no_done;
348e390b 34static int use_thin_pack, use_ofs_delta, use_include_tag;
9462e3f5 35static int no_progress, daemon_mode;
f0cea83f 36static int shallow_nr;
b1e9fff7
JH
37static struct object_array have_obj;
38static struct object_array want_obj;
6523078b 39static struct object_array extra_edge_obj;
96f1e58f 40static unsigned int timeout;
d47f3db7
JH
41/* 0 for no sideband,
42 * otherwise maximum packet size (up to 65520 bytes).
43 */
96f1e58f 44static int use_sideband;
42526b47
SP
45static int advertise_refs;
46static int stateless_rpc;
960deccb
PA
47
48static void reset_timeout(void)
49{
50 alarm(timeout);
51}
fb9040cc 52
583b7ea3
JH
53static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
54{
958c24b1 55 if (use_sideband)
d47f3db7 56 return send_sideband(1, fd, data, sz, use_sideband);
958c24b1
JH
57 if (fd == 3)
58 /* emergency quit */
59 fd = 2;
60 if (fd == 2) {
93822c22 61 /* XXX: are we happy to lose stuff here? */
958c24b1
JH
62 xwrite(fd, data, sz);
63 return sz;
583b7ea3 64 }
cdf4fb8e
JK
65 write_or_die(fd, data, sz);
66 return sz;
583b7ea3
JH
67}
68
16befb8b 69static FILE *pack_pipe = NULL;
11c211fa 70static void show_commit(struct commit *commit, void *data)
9b8dc263
JS
71{
72 if (commit->object.flags & BOUNDARY)
73 fputc('-', pack_pipe);
74 if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0)
75 die("broken output pipe");
76 fputc('\n', pack_pipe);
77 fflush(pack_pipe);
78 free(commit->buffer);
79 commit->buffer = NULL;
80}
81
49473672
JH
82static void show_object(struct object *obj,
83 const struct name_path *path, const char *component,
84 void *cb_data)
9b8dc263 85{
91f17516 86 show_object_with_name(pack_pipe, obj, path, component);
9b8dc263
JS
87}
88
89static void show_edge(struct commit *commit)
90{
91 fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1));
92}
93
1e39d7de 94static int do_rev_list(int in, int out, void *user_data)
80ccaa78
JS
95{
96 int i;
97 struct rev_info revs;
98
ae6a5609 99 pack_pipe = xfdopen(out, "w");
80ccaa78
JS
100 init_revisions(&revs, NULL);
101 revs.tag_objects = 1;
102 revs.tree_objects = 1;
103 revs.blob_objects = 1;
104 if (use_thin_pack)
105 revs.edge_hint = 1;
106
1e39d7de
NTND
107 for (i = 0; i < want_obj.nr; i++) {
108 struct object *o = want_obj.objects[i].item;
109 /* why??? */
110 o->flags &= ~UNINTERESTING;
111 add_pending_object(&revs, o, NULL);
80ccaa78 112 }
1e39d7de
NTND
113 for (i = 0; i < have_obj.nr; i++) {
114 struct object *o = have_obj.objects[i].item;
115 o->flags |= UNINTERESTING;
116 add_pending_object(&revs, o, NULL);
117 }
118 setup_revisions(0, NULL, &revs, NULL);
3d51e1b5
MK
119 if (prepare_revision_walk(&revs))
120 die("revision walk setup failed");
80ccaa78 121 mark_edges_uninteresting(revs.commits, &revs, show_edge);
6523078b
NP
122 if (use_thin_pack)
123 for (i = 0; i < extra_edge_obj.nr; i++)
124 fprintf(pack_pipe, "-%s\n", sha1_to_hex(
125 extra_edge_obj.objects[i].item->sha1));
11c211fa 126 traverse_commit_list(&revs, show_commit, show_object, NULL);
618ebe9f
JS
127 fflush(pack_pipe);
128 fclose(pack_pipe);
21edd3f1 129 return 0;
80ccaa78
JS
130}
131
fb9040cc
LT
132static void create_pack_file(void)
133{
21edd3f1 134 struct async rev_list;
cc41fa8d 135 struct child_process pack_objects;
363b7817 136 char data[8193], progress[128];
583b7ea3
JH
137 char abort_msg[] = "aborting due to possible repository "
138 "corruption on the remote side.";
b1c71b72 139 int buffered = -1;
1456b043 140 ssize_t sz;
cc41fa8d
JS
141 const char *argv[10];
142 int arg = 0;
75bfc6c2 143
b9612197
JK
144 argv[arg++] = "pack-objects";
145 if (!shallow_nr) {
f0cea83f 146 argv[arg++] = "--revs";
3f1da57f 147 if (use_thin_pack)
f0cea83f
NE
148 argv[arg++] = "--thin";
149 }
75bfc6c2 150
cc41fa8d
JS
151 argv[arg++] = "--stdout";
152 if (!no_progress)
153 argv[arg++] = "--progress";
154 if (use_ofs_delta)
155 argv[arg++] = "--delta-base-offset";
348e390b
SP
156 if (use_include_tag)
157 argv[arg++] = "--include-tag";
cc41fa8d
JS
158 argv[arg++] = NULL;
159
160 memset(&pack_objects, 0, sizeof(pack_objects));
b9612197 161 pack_objects.in = -1;
cc41fa8d
JS
162 pack_objects.out = -1;
163 pack_objects.err = -1;
164 pack_objects.git_cmd = 1;
165 pack_objects.argv = argv;
21edd3f1 166
4c324c00 167 if (start_command(&pack_objects))
7e44c935 168 die("git upload-pack: unable to fork git-pack-objects");
b1c71b72 169
b9612197
JK
170 if (shallow_nr) {
171 memset(&rev_list, 0, sizeof(rev_list));
172 rev_list.proc = do_rev_list;
173 rev_list.out = pack_objects.in;
174 if (start_async(&rev_list))
175 die("git upload-pack: unable to fork git-rev-list");
176 }
177 else {
41698375 178 FILE *pipe_fd = xfdopen(pack_objects.in, "w");
3f1da57f 179 int i;
f0cea83f 180
3f1da57f
JH
181 for (i = 0; i < want_obj.nr; i++)
182 fprintf(pipe_fd, "%s\n",
183 sha1_to_hex(want_obj.objects[i].item->sha1));
184 fprintf(pipe_fd, "--not\n");
185 for (i = 0; i < have_obj.nr; i++)
186 fprintf(pipe_fd, "%s\n",
187 sha1_to_hex(have_obj.objects[i].item->sha1));
f0cea83f
NE
188 fprintf(pipe_fd, "\n");
189 fflush(pipe_fd);
190 fclose(pipe_fd);
191 }
192
193
cc41fa8d
JS
194 /* We read from pack_objects.err to capture stderr output for
195 * progress bar, and pack_objects.out to capture the pack data.
b1c71b72 196 */
b1c71b72
JH
197
198 while (1) {
b1c71b72 199 struct pollfd pfd[2];
363b7817 200 int pe, pu, pollsize;
b1c71b72 201
0d516ada
ML
202 reset_timeout();
203
b1c71b72 204 pollsize = 0;
363b7817 205 pe = pu = -1;
b1c71b72 206
cc41fa8d
JS
207 if (0 <= pack_objects.out) {
208 pfd[pollsize].fd = pack_objects.out;
b1c71b72
JH
209 pfd[pollsize].events = POLLIN;
210 pu = pollsize;
211 pollsize++;
212 }
cc41fa8d
JS
213 if (0 <= pack_objects.err) {
214 pfd[pollsize].fd = pack_objects.err;
363b7817
JH
215 pfd[pollsize].events = POLLIN;
216 pe = pollsize;
217 pollsize++;
218 }
b1c71b72 219
4c324c00
JS
220 if (!pollsize)
221 break;
222
223 if (poll(pfd, pollsize, -1) < 0) {
224 if (errno != EINTR) {
225 error("poll failed, resuming: %s",
226 strerror(errno));
227 sleep(1);
b1c71b72 228 }
4c324c00
JS
229 continue;
230 }
6b59f51b
NP
231 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
232 /* Status ready; we ship that in the side-band
233 * or dump to the standard error.
234 */
235 sz = xread(pack_objects.err, progress,
236 sizeof(progress));
237 if (0 < sz)
238 send_client_data(2, progress, sz);
239 else if (sz == 0) {
240 close(pack_objects.err);
241 pack_objects.err = -1;
242 }
243 else
244 goto fail;
245 /* give priority to status messages */
246 continue;
247 }
4c324c00
JS
248 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
249 /* Data ready; we keep the last byte to ourselves
250 * in case we detect broken rev-list, so that we
251 * can leave the stream corrupted. This is
252 * unfortunate -- unpack-objects would happily
253 * accept a valid packdata with trailing garbage,
254 * so appending garbage after we pass all the
255 * pack data is not good enough to signal
256 * breakage to downstream.
257 */
258 char *cp = data;
259 ssize_t outsz = 0;
260 if (0 <= buffered) {
261 *cp++ = buffered;
262 outsz++;
b1c71b72 263 }
4c324c00
JS
264 sz = xread(pack_objects.out, cp,
265 sizeof(data) - outsz);
266 if (0 < sz)
1456b043 267 ;
4c324c00
JS
268 else if (sz == 0) {
269 close(pack_objects.out);
270 pack_objects.out = -1;
363b7817 271 }
4c324c00
JS
272 else
273 goto fail;
274 sz += outsz;
275 if (1 < sz) {
276 buffered = data[sz-1] & 0xFF;
277 sz--;
b1c71b72 278 }
4c324c00
JS
279 else
280 buffered = -1;
281 sz = send_client_data(1, data, sz);
282 if (sz < 0)
b1c71b72 283 goto fail;
4c324c00 284 }
4c324c00 285 }
b1c71b72 286
4c324c00 287 if (finish_command(&pack_objects)) {
7e44c935 288 error("git upload-pack: git-pack-objects died with error.");
4c324c00
JS
289 goto fail;
290 }
f0cea83f 291 if (shallow_nr && finish_async(&rev_list))
4c324c00 292 goto fail; /* error was already reported */
b1c71b72 293
4c324c00
JS
294 /* flush the data */
295 if (0 <= buffered) {
296 data[0] = buffered;
297 sz = send_client_data(1, data, 1);
298 if (sz < 0)
299 goto fail;
300 fprintf(stderr, "flushed.\n");
b1c71b72 301 }
4c324c00
JS
302 if (use_sideband)
303 packet_flush(1);
304 return;
305
b1c71b72 306 fail:
583b7ea3 307 send_client_data(3, abort_msg, sizeof(abort_msg));
7e44c935 308 die("git upload-pack: %s", abort_msg);
fb9040cc
LT
309}
310
def88e9a
LT
311static int got_sha1(char *hex, unsigned char *sha1)
312{
b1e9fff7 313 struct object *o;
937a515a 314 int we_knew_they_have = 0;
b1e9fff7 315
def88e9a 316 if (get_sha1_hex(hex, sha1))
7e44c935 317 die("git upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc 318 if (!has_sha1_file(sha1))
937a515a 319 return -1;
b1e9fff7
JH
320
321 o = lookup_object(sha1);
322 if (!(o && o->parsed))
323 o = parse_object(sha1);
324 if (!o)
325 die("oops (%s)", sha1_to_hex(sha1));
182a8dab 326 if (o->type == OBJ_COMMIT) {
b1e9fff7 327 struct commit_list *parents;
937a515a 328 struct commit *commit = (struct commit *)o;
b1e9fff7 329 if (o->flags & THEY_HAVE)
937a515a
JH
330 we_knew_they_have = 1;
331 else
332 o->flags |= THEY_HAVE;
333 if (!oldest_have || (commit->date < oldest_have))
334 oldest_have = commit->date;
335 for (parents = commit->parents;
b1e9fff7
JH
336 parents;
337 parents = parents->next)
338 parents->item->object.flags |= THEY_HAVE;
fb9040cc 339 }
937a515a
JH
340 if (!we_knew_they_have) {
341 add_object_array(o, NULL, &have_obj);
342 return 1;
343 }
344 return 0;
345}
346
347static int reachable(struct commit *want)
348{
349 struct commit_list *work = NULL;
350
47e44ed1 351 commit_list_insert_by_date(want, &work);
937a515a
JH
352 while (work) {
353 struct commit_list *list = work->next;
354 struct commit *commit = work->item;
355 free(work);
356 work = list;
357
358 if (commit->object.flags & THEY_HAVE) {
359 want->object.flags |= COMMON_KNOWN;
360 break;
361 }
362 if (!commit->object.parsed)
363 parse_object(commit->object.sha1);
364 if (commit->object.flags & REACHABLE)
365 continue;
366 commit->object.flags |= REACHABLE;
367 if (commit->date < oldest_have)
368 continue;
369 for (list = commit->parents; list; list = list->next) {
370 struct commit *parent = list->item;
371 if (!(parent->object.flags & REACHABLE))
47e44ed1 372 commit_list_insert_by_date(parent, &work);
937a515a
JH
373 }
374 }
375 want->object.flags |= REACHABLE;
376 clear_commit_marks(want, REACHABLE);
377 free_commit_list(work);
378 return (want->object.flags & COMMON_KNOWN);
379}
380
381static int ok_to_give_up(void)
382{
383 int i;
384
385 if (!have_obj.nr)
386 return 0;
387
388 for (i = 0; i < want_obj.nr; i++) {
389 struct object *want = want_obj.objects[i].item;
390
391 if (want->flags & COMMON_KNOWN)
392 continue;
393 want = deref_tag(want, "a want line", 0);
394 if (!want || want->type != OBJ_COMMIT) {
395 /* no way to tell if this is reachable by
396 * looking at the ancestry chain alone, so
397 * leave a note to ourselves not to worry about
398 * this object anymore.
399 */
400 want_obj.objects[i].item->flags |= COMMON_KNOWN;
401 continue;
402 }
403 if (!reachable((struct commit *)want))
404 return 0;
405 }
fb9040cc 406 return 1;
def88e9a
LT
407}
408
409static int get_common_commits(void)
410{
411 static char line[1000];
c04c4e57 412 unsigned char sha1[20];
78affc49 413 char last_hex[41];
49bee717
SP
414 int got_common = 0;
415 int got_other = 0;
4e10cf9a 416 int sent_ready = 0;
def88e9a 417
f0243f26
JS
418 save_commit_buffer = 0;
419
eeefa7c9 420 for (;;) {
fd13b21f 421 int len = packet_read_line(0, line, sizeof(line));
960deccb 422 reset_timeout();
def88e9a
LT
423
424 if (!len) {
49bee717 425 if (multi_ack == 2 && got_common
4e10cf9a
JH
426 && !got_other && ok_to_give_up()) {
427 sent_ready = 1;
49bee717 428 packet_write(1, "ACK %s ready\n", last_hex);
4e10cf9a 429 }
b1e9fff7 430 if (have_obj.nr == 0 || multi_ack)
1bd8c8f0 431 packet_write(1, "NAK\n");
4e10cf9a
JH
432
433 if (no_done && sent_ready) {
434 packet_write(1, "ACK %s\n", last_hex);
435 return 0;
436 }
42526b47
SP
437 if (stateless_rpc)
438 exit(0);
49bee717
SP
439 got_common = 0;
440 got_other = 0;
def88e9a
LT
441 continue;
442 }
cc44c765 443 if (!prefixcmp(line, "have ")) {
937a515a
JH
444 switch (got_sha1(line+5, sha1)) {
445 case -1: /* they have what we do not */
49bee717 446 got_other = 1;
78affc49
SP
447 if (multi_ack && ok_to_give_up()) {
448 const char *hex = sha1_to_hex(sha1);
4e10cf9a
JH
449 if (multi_ack == 2) {
450 sent_ready = 1;
78affc49 451 packet_write(1, "ACK %s ready\n", hex);
4e10cf9a 452 } else
78affc49
SP
453 packet_write(1, "ACK %s continue\n", hex);
454 }
937a515a
JH
455 break;
456 default:
49bee717 457 got_common = 1;
78affc49
SP
458 memcpy(last_hex, sha1_to_hex(sha1), 41);
459 if (multi_ack == 2)
460 packet_write(1, "ACK %s common\n", last_hex);
461 else if (multi_ack)
462 packet_write(1, "ACK %s continue\n", last_hex);
c04c4e57 463 else if (have_obj.nr == 1)
78affc49 464 packet_write(1, "ACK %s\n", last_hex);
937a515a 465 break;
af2d3aa4 466 }
def88e9a
LT
467 continue;
468 }
469 if (!strcmp(line, "done")) {
b1e9fff7 470 if (have_obj.nr > 0) {
1bd8c8f0 471 if (multi_ack)
c04c4e57 472 packet_write(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
473 return 0;
474 }
def88e9a
LT
475 packet_write(1, "NAK\n");
476 return -1;
477 }
7e44c935 478 die("git upload-pack: expected SHA1 list, got '%s'", line);
def88e9a 479 }
def88e9a
LT
480}
481
051e4005
JH
482static void check_non_tip(void)
483{
484 static const char *argv[] = {
485 "rev-list", "--stdin", NULL,
486 };
487 static struct child_process cmd;
488 struct object *o;
489 char namebuf[42]; /* ^ + SHA-1 + LF */
490 int i;
491
492 /* In the normal in-process case non-tip request can never happen */
493 if (!stateless_rpc)
494 goto error;
495
496 cmd.argv = argv;
497 cmd.git_cmd = 1;
498 cmd.no_stderr = 1;
499 cmd.in = -1;
500 cmd.out = -1;
501
502 if (start_command(&cmd))
503 goto error;
504
505 /*
506 * If rev-list --stdin encounters an unknown commit, it
507 * terminates, which will cause SIGPIPE in the write loop
508 * below.
509 */
510 sigchain_push(SIGPIPE, SIG_IGN);
511
512 namebuf[0] = '^';
513 namebuf[41] = '\n';
514 for (i = get_max_object_index(); 0 < i; ) {
515 o = get_indexed_object(--i);
2a745324
BH
516 if (!o)
517 continue;
051e4005
JH
518 if (!(o->flags & OUR_REF))
519 continue;
520 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
521 if (write_in_full(cmd.in, namebuf, 42) < 0)
522 goto error;
523 }
524 namebuf[40] = '\n';
525 for (i = 0; i < want_obj.nr; i++) {
526 o = want_obj.objects[i].item;
527 if (o->flags & OUR_REF)
528 continue;
529 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
530 if (write_in_full(cmd.in, namebuf, 41) < 0)
531 goto error;
532 }
533 close(cmd.in);
534
535 sigchain_pop(SIGPIPE);
536
537 /*
538 * The commits out of the rev-list are not ancestors of
539 * our ref.
540 */
541 i = read_in_full(cmd.out, namebuf, 1);
542 if (i)
543 goto error;
544 close(cmd.out);
545
546 /*
547 * rev-list may have died by encountering a bad commit
548 * in the history, in which case we do want to bail out
549 * even when it showed no commit.
550 */
551 if (finish_command(&cmd))
552 goto error;
553
554 /* All the non-tip ones are ancestors of what we advertised */
555 return;
556
557error:
558 /* Pick one of them (we know there at least is one) */
559 for (i = 0; i < want_obj.nr; i++) {
560 o = want_obj.objects[i].item;
561 if (!(o->flags & OUR_REF))
562 die("git upload-pack: not our ref %s",
563 sha1_to_hex(o->sha1));
564 }
565}
566
b1e9fff7 567static void receive_needs(void)
fb9040cc 568{
3cd47459 569 struct object_array shallows = OBJECT_ARRAY_INIT;
fb9040cc 570 static char line[1000];
016e6ccb 571 int len, depth = 0;
051e4005 572 int has_non_tip = 0;
fb9040cc 573
f0cea83f 574 shallow_nr = 0;
fb9040cc 575 for (;;) {
565ebbf7 576 struct object *o;
f47182c8 577 const char *features;
6ece0d30 578 unsigned char sha1_buf[20];
fb9040cc 579 len = packet_read_line(0, line, sizeof(line));
960deccb 580 reset_timeout();
fb9040cc 581 if (!len)
ed09aef0 582 break;
e091eb93 583
599065a3 584 if (!prefixcmp(line, "shallow ")) {
ed09aef0
JS
585 unsigned char sha1[20];
586 struct object *object;
b7b02170 587 if (get_sha1_hex(line + 8, sha1))
ed09aef0
JS
588 die("invalid shallow line: %s", line);
589 object = parse_object(sha1);
590 if (!object)
591 die("did not find object for %s", line);
6293ded3
NTND
592 if (object->type != OBJ_COMMIT)
593 die("invalid shallow object %s", sha1_to_hex(sha1));
e58e57e4
JK
594 if (!(object->flags & CLIENT_SHALLOW)) {
595 object->flags |= CLIENT_SHALLOW;
596 add_object_array(object, NULL, &shallows);
597 }
ed09aef0
JS
598 continue;
599 }
599065a3 600 if (!prefixcmp(line, "deepen ")) {
016e6ccb
JS
601 char *end;
602 depth = strtol(line + 7, &end, 0);
603 if (end == line + 7 || depth <= 0)
604 die("Invalid deepen: %s", line);
605 continue;
606 }
599065a3 607 if (prefixcmp(line, "want ") ||
6ece0d30 608 get_sha1_hex(line+5, sha1_buf))
7e44c935 609 die("git upload-pack: protocol error, "
e091eb93 610 "expected to get sha, not '%s'", line);
f47182c8
JH
611
612 features = line + 45;
613
614 if (parse_feature_request(features, "multi_ack_detailed"))
78affc49 615 multi_ack = 2;
f47182c8 616 else if (parse_feature_request(features, "multi_ack"))
1bd8c8f0 617 multi_ack = 1;
f47182c8 618 if (parse_feature_request(features, "no-done"))
4e10cf9a 619 no_done = 1;
f47182c8 620 if (parse_feature_request(features, "thin-pack"))
b19696c2 621 use_thin_pack = 1;
f47182c8 622 if (parse_feature_request(features, "ofs-delta"))
e4fe4b8e 623 use_ofs_delta = 1;
f47182c8 624 if (parse_feature_request(features, "side-band-64k"))
d47f3db7 625 use_sideband = LARGE_PACKET_MAX;
f47182c8 626 else if (parse_feature_request(features, "side-band"))
d47f3db7 627 use_sideband = DEFAULT_PACKET_MAX;
f47182c8 628 if (parse_feature_request(features, "no-progress"))
b0e90897 629 no_progress = 1;
f47182c8 630 if (parse_feature_request(features, "include-tag"))
348e390b 631 use_include_tag = 1;
565ebbf7 632
565ebbf7 633 o = lookup_object(sha1_buf);
051e4005 634 if (!o)
9f9aa761
EN
635 die("git upload-pack: not our ref %s",
636 sha1_to_hex(sha1_buf));
565ebbf7
JH
637 if (!(o->flags & WANTED)) {
638 o->flags |= WANTED;
051e4005
JH
639 if (!(o->flags & OUR_REF))
640 has_non_tip = 1;
b1e9fff7 641 add_object_array(o, NULL, &want_obj);
565ebbf7 642 }
fb9040cc 643 }
9462e3f5 644
051e4005
JH
645 /*
646 * We have sent all our refs already, and the other end
647 * should have chosen out of them. When we are operating
648 * in the stateless RPC mode, however, their choice may
649 * have been based on the set of older refs advertised
650 * by another process that handled the initial request.
651 */
652 if (has_non_tip)
653 check_non_tip();
654
9462e3f5
JS
655 if (!use_sideband && daemon_mode)
656 no_progress = 1;
657
f53514bc
JS
658 if (depth == 0 && shallows.nr == 0)
659 return;
016e6ccb 660 if (depth > 0) {
4dcb167f 661 struct commit_list *result = NULL, *backup = NULL;
f53514bc 662 int i;
4dcb167f
NTND
663 if (depth == INFINITE_DEPTH)
664 for (i = 0; i < shallows.nr; i++) {
665 struct object *object = shallows.objects[i].item;
666 object->flags |= NOT_SHALLOW;
667 }
668 else
669 backup = result =
670 get_shallow_commits(&want_obj, depth,
671 SHALLOW, NOT_SHALLOW);
016e6ccb 672 while (result) {
f53514bc 673 struct object *object = &result->item->object;
1f2de769 674 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
f53514bc
JS
675 packet_write(1, "shallow %s",
676 sha1_to_hex(object->sha1));
677 register_shallow(object->sha1);
f0cea83f 678 shallow_nr++;
f53514bc 679 }
016e6ccb
JS
680 result = result->next;
681 }
682 free_commit_list(backup);
f53514bc
JS
683 for (i = 0; i < shallows.nr; i++) {
684 struct object *object = shallows.objects[i].item;
685 if (object->flags & NOT_SHALLOW) {
686 struct commit_list *parents;
687 packet_write(1, "unshallow %s",
688 sha1_to_hex(object->sha1));
689 object->flags &= ~CLIENT_SHALLOW;
690 /* make sure the real parents are parsed */
691 unregister_shallow(object->sha1);
176d45cb 692 object->parsed = 0;
dec38c81
MK
693 if (parse_commit((struct commit *)object))
694 die("invalid commit");
f53514bc
JS
695 parents = ((struct commit *)object)->parents;
696 while (parents) {
697 add_object_array(&parents->item->object,
698 NULL, &want_obj);
699 parents = parents->next;
700 }
6523078b 701 add_object_array(object, NULL, &extra_edge_obj);
f53514bc
JS
702 }
703 /* make sure commit traversal conforms to client */
704 register_shallow(object->sha1);
705 }
706 packet_flush(1);
707 } else
708 if (shallows.nr > 0) {
709 int i;
710 for (i = 0; i < shallows.nr; i++)
711 register_shallow(shallows.objects[i].item->sha1);
712 }
f0cea83f
NE
713
714 shallow_nr += shallows.nr;
f53514bc 715 free(shallows.objects);
fb9040cc
LT
716}
717
daebaa78 718/* return non-zero if the ref is hidden, otherwise 0 */
cbbe50db
JH
719static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
720{
721 struct object *o = lookup_unknown_object(sha1);
daebaa78
JH
722
723 if (ref_is_hidden(refname))
724 return 1;
cbbe50db
JH
725 if (!o)
726 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
3f1da57f 727 o->flags |= OUR_REF;
cbbe50db
JH
728 return 0;
729}
730
8da19775 731static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
def88e9a 732{
ed09aef0 733 static const char *capabilities = "multi_ack thin-pack side-band"
348e390b 734 " side-band-64k ofs-delta shallow no-progress"
78affc49 735 " include-tag multi_ack_detailed";
6b01ecfe 736 const char *refname_nons = strip_namespace(refname);
435c8332 737 unsigned char peeled[20];
b5b16990 738
daebaa78
JH
739 if (mark_our_ref(refname, sha1, flag, cb_data))
740 return 0;
cbbe50db 741
1f5881bb 742 if (capabilities)
ff5effdf
JK
743 packet_write(1, "%s %s%c%s%s agent=%s\n",
744 sha1_to_hex(sha1), refname_nons,
cf2ad8e6 745 0, capabilities,
ff5effdf
JK
746 stateless_rpc ? " no-done" : "",
747 git_user_agent_sanitized());
1f5881bb 748 else
6b01ecfe 749 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
1f5881bb 750 capabilities = NULL;
435c8332
JK
751 if (!peel_ref(refname, peeled))
752 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
def88e9a
LT
753 return 0;
754}
755
59076eba 756static void upload_pack(void)
def88e9a 757{
42526b47
SP
758 if (advertise_refs || !stateless_rpc) {
759 reset_timeout();
6b01ecfe
JT
760 head_ref_namespaced(send_ref, NULL);
761 for_each_namespaced_ref(send_ref, NULL);
42526b47
SP
762 packet_flush(1);
763 } else {
6b01ecfe
JT
764 head_ref_namespaced(mark_our_ref, NULL);
765 for_each_namespaced_ref(mark_our_ref, NULL);
42526b47
SP
766 }
767 if (advertise_refs)
768 return;
769
b1e9fff7 770 receive_needs();
59076eba
DR
771 if (want_obj.nr) {
772 get_common_commits();
773 create_pack_file();
774 }
def88e9a
LT
775}
776
daebaa78
JH
777static int upload_pack_config(const char *var, const char *value, void *unused)
778{
779 return parse_hide_refs_config(var, value, "uploadpack");
780}
781
def88e9a
LT
782int main(int argc, char **argv)
783{
8d630132 784 char *dir;
960deccb
PA
785 int i;
786 int strict = 0;
787
5e9637c6
ÆAB
788 git_setup_gettext();
789
bbc30f99 790 packet_trace_identity("upload-pack");
2fb3f6db 791 git_extract_argv0_path(argv[0]);
dae556bd 792 read_replace_refs = 0;
2fb3f6db 793
960deccb
PA
794 for (i = 1; i < argc; i++) {
795 char *arg = argv[i];
796
797 if (arg[0] != '-')
798 break;
42526b47
SP
799 if (!strcmp(arg, "--advertise-refs")) {
800 advertise_refs = 1;
801 continue;
802 }
803 if (!strcmp(arg, "--stateless-rpc")) {
804 stateless_rpc = 1;
805 continue;
806 }
960deccb
PA
807 if (!strcmp(arg, "--strict")) {
808 strict = 1;
809 continue;
810 }
cc44c765 811 if (!prefixcmp(arg, "--timeout=")) {
960deccb 812 timeout = atoi(arg+10);
9462e3f5 813 daemon_mode = 1;
960deccb
PA
814 continue;
815 }
816 if (!strcmp(arg, "--")) {
817 i++;
818 break;
819 }
820 }
a6080a0a 821
960deccb 822 if (i != argc-1)
def88e9a 823 usage(upload_pack_usage);
04b33055 824
e1464ca7 825 setup_path();
04b33055 826
960deccb 827 dir = argv[i];
113b9475 828
8d630132 829 if (!enter_repo(dir, strict))
05ac6b34 830 die("'%s' does not appear to be a git repository", dir);
a0022eeb
JH
831 if (is_repository_shallow())
832 die("attempt to fetch/clone from a shallow repository");
daebaa78 833 git_config(upload_pack_config, NULL);
def88e9a
LT
834 upload_pack();
835 return 0;
836}