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