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