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