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