]> git.ipfire.org Git - thirdparty/git.git/blame - upload-pack.c
Merge branch 'sb/object-store-grafts' into sb/object-store-lookup
[thirdparty/git.git] / upload-pack.c
CommitLineData
def88e9a 1#include "cache.h"
b2141fc1 2#include "config.h"
def88e9a
LT
3#include "refs.h"
4#include "pkt-line.h"
958c24b1 5#include "sideband.h"
cbd53a21 6#include "object-store.h"
f6b42a81
JH
7#include "tag.h"
8#include "object.h"
f0243f26 9#include "commit.h"
9b8dc263
JS
10#include "diff.h"
11#include "revision.h"
12#include "list-objects.h"
10ac85c7
JH
13#include "list-objects-filter.h"
14#include "list-objects-filter-options.h"
cc41fa8d 15#include "run-command.h"
47a59185 16#include "connect.h"
051e4005 17#include "sigchain.h"
ff5effdf 18#include "version.h"
daebaa78 19#include "string-list.h"
569e554b 20#include "argv-array.h"
5411b10c 21#include "prio-queue.h"
aa9bab29 22#include "protocol.h"
10ac85c7 23#include "quote.h"
a3d6b53e 24#include "upload-pack.h"
3145ea95 25#include "serve.h"
def88e9a 26
208acbfb 27/* Remember to update object flag allocation in object.h */
937a515a
JH
28#define THEY_HAVE (1u << 11)
29#define OUR_REF (1u << 12)
30#define WANTED (1u << 13)
31#define COMMON_KNOWN (1u << 14)
32#define REACHABLE (1u << 15)
33
f53514bc
JS
34#define SHALLOW (1u << 16)
35#define NOT_SHALLOW (1u << 17)
36#define CLIENT_SHALLOW (1u << 18)
390eb36b 37#define HIDDEN_REF (1u << 19)
f53514bc 38
dddbad72 39static timestamp_t oldest_have;
937a515a 40
cccf74e2 41static int deepen_relative;
3f1da57f 42static int multi_ack;
4e10cf9a 43static int no_done;
348e390b 44static int use_thin_pack, use_ofs_delta, use_include_tag;
9462e3f5 45static int no_progress, daemon_mode;
7199c093
FM
46/* Allow specifying sha1 if it is a ref tip. */
47#define ALLOW_TIP_SHA1 01
68ee6289
FM
48/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
49#define ALLOW_REACHABLE_SHA1 02
f8edeaa0
DT
50/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
51#define ALLOW_ANY_SHA1 07
7199c093 52static unsigned int allow_unadvertised_object_request;
f0cea83f 53static int shallow_nr;
b1e9fff7
JH
54static struct object_array have_obj;
55static struct object_array want_obj;
6523078b 56static struct object_array extra_edge_obj;
96f1e58f 57static unsigned int timeout;
115dedd7 58static int keepalive = 5;
d47f3db7
JH
59/* 0 for no sideband,
60 * otherwise maximum packet size (up to 65520 bytes).
61 */
96f1e58f 62static int use_sideband;
42526b47 63static int stateless_rpc;
20b20a22 64static const char *pack_objects_hook;
960deccb 65
10ac85c7 66static int filter_capability_requested;
c7620bd0 67static int allow_filter;
10ac85c7
JH
68static struct list_objects_filter_options filter_options;
69
960deccb
PA
70static void reset_timeout(void)
71{
72 alarm(timeout);
73}
fb9040cc 74
fcf0fe9e 75static void send_client_data(int fd, const char *data, ssize_t sz)
583b7ea3 76{
4c4b7d1d
LF
77 if (use_sideband) {
78 send_sideband(1, fd, data, sz, use_sideband);
fcf0fe9e 79 return;
4c4b7d1d 80 }
958c24b1
JH
81 if (fd == 3)
82 /* emergency quit */
83 fd = 2;
84 if (fd == 2) {
93822c22 85 /* XXX: are we happy to lose stuff here? */
958c24b1 86 xwrite(fd, data, sz);
fcf0fe9e 87 return;
583b7ea3 88 }
cdf4fb8e 89 write_or_die(fd, data, sz);
583b7ea3
JH
90}
91
b790e0f6
NTND
92static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
93{
94 FILE *fp = cb_data;
95 if (graft->nr_parent == -1)
7683e2e6 96 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
b790e0f6
NTND
97 return 0;
98}
99
fb9040cc
LT
100static void create_pack_file(void)
101{
d3180279 102 struct child_process pack_objects = CHILD_PROCESS_INIT;
363b7817 103 char data[8193], progress[128];
583b7ea3
JH
104 char abort_msg[] = "aborting due to possible repository "
105 "corruption on the remote side.";
b1c71b72 106 int buffered = -1;
1456b043 107 ssize_t sz;
65a3629e 108 int i;
cdab4858 109 FILE *pipe_fd;
75bfc6c2 110
20b20a22
JK
111 if (!pack_objects_hook)
112 pack_objects.git_cmd = 1;
113 else {
114 argv_array_push(&pack_objects.args, pack_objects_hook);
115 argv_array_push(&pack_objects.args, "git");
116 pack_objects.use_shell = 1;
117 }
118
cdab4858 119 if (shallow_nr) {
65a3629e
MP
120 argv_array_push(&pack_objects.args, "--shallow-file");
121 argv_array_push(&pack_objects.args, "");
f0cea83f 122 }
65a3629e
MP
123 argv_array_push(&pack_objects.args, "pack-objects");
124 argv_array_push(&pack_objects.args, "--revs");
cdab4858 125 if (use_thin_pack)
65a3629e 126 argv_array_push(&pack_objects.args, "--thin");
75bfc6c2 127
65a3629e 128 argv_array_push(&pack_objects.args, "--stdout");
2dacf26d 129 if (shallow_nr)
65a3629e 130 argv_array_push(&pack_objects.args, "--shallow");
cc41fa8d 131 if (!no_progress)
65a3629e 132 argv_array_push(&pack_objects.args, "--progress");
cc41fa8d 133 if (use_ofs_delta)
65a3629e 134 argv_array_push(&pack_objects.args, "--delta-base-offset");
348e390b 135 if (use_include_tag)
65a3629e 136 argv_array_push(&pack_objects.args, "--include-tag");
10ac85c7 137 if (filter_options.filter_spec) {
0b6069fe
JT
138 if (pack_objects.use_shell) {
139 struct strbuf buf = STRBUF_INIT;
140 sq_quote_buf(&buf, filter_options.filter_spec);
141 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
142 strbuf_release(&buf);
143 } else {
144 argv_array_pushf(&pack_objects.args, "--filter=%s",
145 filter_options.filter_spec);
146 }
10ac85c7 147 }
cc41fa8d 148
b9612197 149 pack_objects.in = -1;
cc41fa8d
JS
150 pack_objects.out = -1;
151 pack_objects.err = -1;
21edd3f1 152
4c324c00 153 if (start_command(&pack_objects))
7e44c935 154 die("git upload-pack: unable to fork git-pack-objects");
b1c71b72 155
cdab4858
NTND
156 pipe_fd = xfdopen(pack_objects.in, "w");
157
b790e0f6
NTND
158 if (shallow_nr)
159 for_each_commit_graft(write_one_shallow, pipe_fd);
160
cdab4858
NTND
161 for (i = 0; i < want_obj.nr; i++)
162 fprintf(pipe_fd, "%s\n",
f2fd0760 163 oid_to_hex(&want_obj.objects[i].item->oid));
cdab4858
NTND
164 fprintf(pipe_fd, "--not\n");
165 for (i = 0; i < have_obj.nr; i++)
166 fprintf(pipe_fd, "%s\n",
f2fd0760 167 oid_to_hex(&have_obj.objects[i].item->oid));
cdab4858
NTND
168 for (i = 0; i < extra_edge_obj.nr; i++)
169 fprintf(pipe_fd, "%s\n",
f2fd0760 170 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
cdab4858
NTND
171 fprintf(pipe_fd, "\n");
172 fflush(pipe_fd);
173 fclose(pipe_fd);
f0cea83f 174
cc41fa8d
JS
175 /* We read from pack_objects.err to capture stderr output for
176 * progress bar, and pack_objects.out to capture the pack data.
b1c71b72 177 */
b1c71b72
JH
178
179 while (1) {
b1c71b72 180 struct pollfd pfd[2];
363b7817 181 int pe, pu, pollsize;
05e95155 182 int ret;
b1c71b72 183
0d516ada
ML
184 reset_timeout();
185
b1c71b72 186 pollsize = 0;
363b7817 187 pe = pu = -1;
b1c71b72 188
cc41fa8d
JS
189 if (0 <= pack_objects.out) {
190 pfd[pollsize].fd = pack_objects.out;
b1c71b72
JH
191 pfd[pollsize].events = POLLIN;
192 pu = pollsize;
193 pollsize++;
194 }
cc41fa8d
JS
195 if (0 <= pack_objects.err) {
196 pfd[pollsize].fd = pack_objects.err;
363b7817
JH
197 pfd[pollsize].events = POLLIN;
198 pe = pollsize;
199 pollsize++;
200 }
b1c71b72 201
4c324c00
JS
202 if (!pollsize)
203 break;
204
6c71f8b0
ET
205 ret = poll(pfd, pollsize,
206 keepalive < 0 ? -1 : 1000 * keepalive);
207
05e95155 208 if (ret < 0) {
4c324c00 209 if (errno != EINTR) {
d2b6afa2 210 error_errno("poll failed, resuming");
4c324c00 211 sleep(1);
b1c71b72 212 }
4c324c00
JS
213 continue;
214 }
6b59f51b
NP
215 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
216 /* Status ready; we ship that in the side-band
217 * or dump to the standard error.
218 */
219 sz = xread(pack_objects.err, progress,
220 sizeof(progress));
221 if (0 < sz)
222 send_client_data(2, progress, sz);
223 else if (sz == 0) {
224 close(pack_objects.err);
225 pack_objects.err = -1;
226 }
227 else
228 goto fail;
229 /* give priority to status messages */
230 continue;
231 }
4c324c00
JS
232 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
233 /* Data ready; we keep the last byte to ourselves
234 * in case we detect broken rev-list, so that we
235 * can leave the stream corrupted. This is
236 * unfortunate -- unpack-objects would happily
237 * accept a valid packdata with trailing garbage,
238 * so appending garbage after we pass all the
239 * pack data is not good enough to signal
240 * breakage to downstream.
241 */
242 char *cp = data;
243 ssize_t outsz = 0;
244 if (0 <= buffered) {
245 *cp++ = buffered;
246 outsz++;
b1c71b72 247 }
4c324c00
JS
248 sz = xread(pack_objects.out, cp,
249 sizeof(data) - outsz);
250 if (0 < sz)
1456b043 251 ;
4c324c00
JS
252 else if (sz == 0) {
253 close(pack_objects.out);
254 pack_objects.out = -1;
363b7817 255 }
4c324c00
JS
256 else
257 goto fail;
258 sz += outsz;
259 if (1 < sz) {
260 buffered = data[sz-1] & 0xFF;
261 sz--;
b1c71b72 262 }
4c324c00
JS
263 else
264 buffered = -1;
fcf0fe9e 265 send_client_data(1, data, sz);
4c324c00 266 }
05e95155
JK
267
268 /*
269 * We hit the keepalive timeout without saying anything; send
270 * an empty message on the data sideband just to let the other
271 * side know we're still working on it, but don't have any data
272 * yet.
273 *
274 * If we don't have a sideband channel, there's no room in the
275 * protocol to say anything, so those clients are just out of
276 * luck.
277 */
278 if (!ret && use_sideband) {
279 static const char buf[] = "0005\1";
280 write_or_die(1, buf, 5);
281 }
4c324c00 282 }
b1c71b72 283
4c324c00 284 if (finish_command(&pack_objects)) {
7e44c935 285 error("git upload-pack: git-pack-objects died with error.");
4c324c00
JS
286 goto fail;
287 }
b1c71b72 288
4c324c00
JS
289 /* flush the data */
290 if (0 <= buffered) {
291 data[0] = buffered;
fcf0fe9e 292 send_client_data(1, data, 1);
4c324c00 293 fprintf(stderr, "flushed.\n");
b1c71b72 294 }
4c324c00
JS
295 if (use_sideband)
296 packet_flush(1);
297 return;
298
b1c71b72 299 fail:
583b7ea3 300 send_client_data(3, abort_msg, sizeof(abort_msg));
7e44c935 301 die("git upload-pack: %s", abort_msg);
fb9040cc
LT
302}
303
cf93982f 304static int got_oid(const char *hex, struct object_id *oid)
def88e9a 305{
b1e9fff7 306 struct object *o;
937a515a 307 int we_knew_they_have = 0;
b1e9fff7 308
cf93982f 309 if (get_oid_hex(hex, oid))
7e44c935 310 die("git upload-pack: expected SHA1 object, got '%s'", hex);
cf93982f 311 if (!has_object_file(oid))
937a515a 312 return -1;
b1e9fff7 313
c251c83d 314 o = parse_object(oid);
b1e9fff7 315 if (!o)
cf93982f 316 die("oops (%s)", oid_to_hex(oid));
182a8dab 317 if (o->type == OBJ_COMMIT) {
b1e9fff7 318 struct commit_list *parents;
937a515a 319 struct commit *commit = (struct commit *)o;
b1e9fff7 320 if (o->flags & THEY_HAVE)
937a515a
JH
321 we_knew_they_have = 1;
322 else
323 o->flags |= THEY_HAVE;
324 if (!oldest_have || (commit->date < oldest_have))
325 oldest_have = commit->date;
326 for (parents = commit->parents;
b1e9fff7
JH
327 parents;
328 parents = parents->next)
329 parents->item->object.flags |= THEY_HAVE;
fb9040cc 330 }
937a515a
JH
331 if (!we_knew_they_have) {
332 add_object_array(o, NULL, &have_obj);
333 return 1;
334 }
335 return 0;
336}
337
338static int reachable(struct commit *want)
339{
5411b10c 340 struct prio_queue work = { compare_commits_by_commit_date };
937a515a 341
5411b10c
JK
342 prio_queue_put(&work, want);
343 while (work.nr) {
e510ab89 344 struct commit_list *list;
5411b10c 345 struct commit *commit = prio_queue_get(&work);
937a515a
JH
346
347 if (commit->object.flags & THEY_HAVE) {
348 want->object.flags |= COMMON_KNOWN;
349 break;
350 }
351 if (!commit->object.parsed)
c251c83d 352 parse_object(&commit->object.oid);
937a515a
JH
353 if (commit->object.flags & REACHABLE)
354 continue;
355 commit->object.flags |= REACHABLE;
356 if (commit->date < oldest_have)
357 continue;
358 for (list = commit->parents; list; list = list->next) {
359 struct commit *parent = list->item;
360 if (!(parent->object.flags & REACHABLE))
5411b10c 361 prio_queue_put(&work, parent);
937a515a
JH
362 }
363 }
364 want->object.flags |= REACHABLE;
365 clear_commit_marks(want, REACHABLE);
5411b10c 366 clear_prio_queue(&work);
937a515a
JH
367 return (want->object.flags & COMMON_KNOWN);
368}
369
370static int ok_to_give_up(void)
371{
372 int i;
373
374 if (!have_obj.nr)
375 return 0;
376
377 for (i = 0; i < want_obj.nr; i++) {
378 struct object *want = want_obj.objects[i].item;
379
380 if (want->flags & COMMON_KNOWN)
381 continue;
382 want = deref_tag(want, "a want line", 0);
383 if (!want || want->type != OBJ_COMMIT) {
384 /* no way to tell if this is reachable by
385 * looking at the ancestry chain alone, so
386 * leave a note to ourselves not to worry about
387 * this object anymore.
388 */
389 want_obj.objects[i].item->flags |= COMMON_KNOWN;
390 continue;
391 }
392 if (!reachable((struct commit *)want))
393 return 0;
394 }
fb9040cc 395 return 1;
def88e9a
LT
396}
397
398static int get_common_commits(void)
399{
cf93982f 400 struct object_id oid;
401 char last_hex[GIT_MAX_HEXSZ + 1];
49bee717
SP
402 int got_common = 0;
403 int got_other = 0;
4e10cf9a 404 int sent_ready = 0;
def88e9a 405
f0243f26
JS
406 save_commit_buffer = 0;
407
eeefa7c9 408 for (;;) {
74543a04 409 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
410 const char *arg;
411
960deccb 412 reset_timeout();
def88e9a 413
74543a04 414 if (!line) {
49bee717 415 if (multi_ack == 2 && got_common
4e10cf9a
JH
416 && !got_other && ok_to_give_up()) {
417 sent_ready = 1;
81c634e9 418 packet_write_fmt(1, "ACK %s ready\n", last_hex);
4e10cf9a 419 }
b1e9fff7 420 if (have_obj.nr == 0 || multi_ack)
81c634e9 421 packet_write_fmt(1, "NAK\n");
4e10cf9a
JH
422
423 if (no_done && sent_ready) {
81c634e9 424 packet_write_fmt(1, "ACK %s\n", last_hex);
4e10cf9a
JH
425 return 0;
426 }
42526b47
SP
427 if (stateless_rpc)
428 exit(0);
49bee717
SP
429 got_common = 0;
430 got_other = 0;
def88e9a
LT
431 continue;
432 }
8bf3b758 433 if (skip_prefix(line, "have ", &arg)) {
cf93982f 434 switch (got_oid(arg, &oid)) {
937a515a 435 case -1: /* they have what we do not */
49bee717 436 got_other = 1;
78affc49 437 if (multi_ack && ok_to_give_up()) {
cf93982f 438 const char *hex = oid_to_hex(&oid);
4e10cf9a
JH
439 if (multi_ack == 2) {
440 sent_ready = 1;
81c634e9 441 packet_write_fmt(1, "ACK %s ready\n", hex);
4e10cf9a 442 } else
81c634e9 443 packet_write_fmt(1, "ACK %s continue\n", hex);
78affc49 444 }
937a515a
JH
445 break;
446 default:
49bee717 447 got_common = 1;
55dc227d 448 oid_to_hex_r(last_hex, &oid);
78affc49 449 if (multi_ack == 2)
81c634e9 450 packet_write_fmt(1, "ACK %s common\n", last_hex);
78affc49 451 else if (multi_ack)
81c634e9 452 packet_write_fmt(1, "ACK %s continue\n", last_hex);
c04c4e57 453 else if (have_obj.nr == 1)
81c634e9 454 packet_write_fmt(1, "ACK %s\n", last_hex);
937a515a 455 break;
af2d3aa4 456 }
def88e9a
LT
457 continue;
458 }
459 if (!strcmp(line, "done")) {
b1e9fff7 460 if (have_obj.nr > 0) {
1bd8c8f0 461 if (multi_ack)
81c634e9 462 packet_write_fmt(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
463 return 0;
464 }
81c634e9 465 packet_write_fmt(1, "NAK\n");
def88e9a
LT
466 return -1;
467 }
7e44c935 468 die("git upload-pack: expected SHA1 list, got '%s'", line);
def88e9a 469 }
def88e9a
LT
470}
471
390eb36b
JH
472static int is_our_ref(struct object *o)
473{
68ee6289
FM
474 int allow_hidden_ref = (allow_unadvertised_object_request &
475 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
7199c093 476 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
390eb36b
JH
477}
478
2997178e
NTND
479/*
480 * on successful case, it's up to the caller to close cmd->out
481 */
482static int do_reachable_revlist(struct child_process *cmd,
079aa97e
NTND
483 struct object_array *src,
484 struct object_array *reachable)
051e4005
JH
485{
486 static const char *argv[] = {
487 "rev-list", "--stdin", NULL,
488 };
051e4005 489 struct object *o;
55dc227d 490 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
051e4005
JH
491 int i;
492
2997178e
NTND
493 cmd->argv = argv;
494 cmd->git_cmd = 1;
495 cmd->no_stderr = 1;
496 cmd->in = -1;
497 cmd->out = -1;
051e4005
JH
498
499 /*
7fcbd37f
NTND
500 * If the next rev-list --stdin encounters an unknown commit,
501 * it terminates, which will cause SIGPIPE in the write loop
051e4005
JH
502 * below.
503 */
504 sigchain_push(SIGPIPE, SIG_IGN);
505
2997178e 506 if (start_command(cmd))
7fcbd37f
NTND
507 goto error;
508
051e4005 509 namebuf[0] = '^';
cf93982f 510 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
051e4005
JH
511 for (i = get_max_object_index(); 0 < i; ) {
512 o = get_indexed_object(--i);
2a745324
BH
513 if (!o)
514 continue;
079aa97e
NTND
515 if (reachable && o->type == OBJ_COMMIT)
516 o->flags &= ~TMP_MARK;
390eb36b 517 if (!is_our_ref(o))
051e4005 518 continue;
f2fd0760 519 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
cf93982f 520 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
051e4005
JH
521 goto error;
522 }
cf93982f 523 namebuf[GIT_SHA1_HEXSZ] = '\n';
3f0f6624
NTND
524 for (i = 0; i < src->nr; i++) {
525 o = src->objects[i].item;
079aa97e
NTND
526 if (is_our_ref(o)) {
527 if (reachable)
528 add_object_array(o, NULL, reachable);
051e4005 529 continue;
079aa97e
NTND
530 }
531 if (reachable && o->type == OBJ_COMMIT)
532 o->flags |= TMP_MARK;
f2fd0760 533 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
cf93982f 534 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
051e4005
JH
535 goto error;
536 }
2997178e
NTND
537 close(cmd->in);
538 cmd->in = -1;
539 sigchain_pop(SIGPIPE);
051e4005 540
2997178e
NTND
541 return 0;
542
543error:
051e4005
JH
544 sigchain_pop(SIGPIPE);
545
2997178e
NTND
546 if (cmd->in >= 0)
547 close(cmd->in);
548 if (cmd->out >= 0)
549 close(cmd->out);
550 return -1;
551}
552
079aa97e
NTND
553static int get_reachable_list(struct object_array *src,
554 struct object_array *reachable)
555{
556 struct child_process cmd = CHILD_PROCESS_INIT;
557 int i;
558 struct object *o;
55dc227d 559 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
560 const unsigned hexsz = the_hash_algo->hexsz;
079aa97e
NTND
561
562 if (do_reachable_revlist(&cmd, src, reachable) < 0)
563 return -1;
564
55dc227d 565 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
079aa97e 566 struct object_id sha1;
55dc227d 567 const char *p;
079aa97e 568
55dc227d 569 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
079aa97e
NTND
570 break;
571
572 o = lookup_object(sha1.hash);
573 if (o && o->type == OBJ_COMMIT) {
574 o->flags &= ~TMP_MARK;
575 }
576 }
577 for (i = get_max_object_index(); 0 < i; i--) {
578 o = get_indexed_object(i - 1);
579 if (o && o->type == OBJ_COMMIT &&
580 (o->flags & TMP_MARK)) {
581 add_object_array(o, NULL, reachable);
582 o->flags &= ~TMP_MARK;
583 }
584 }
585 close(cmd.out);
586
587 if (finish_command(&cmd))
588 return -1;
589
590 return 0;
591}
592
2997178e
NTND
593static int has_unreachable(struct object_array *src)
594{
595 struct child_process cmd = CHILD_PROCESS_INIT;
596 char buf[1];
597 int i;
598
079aa97e 599 if (do_reachable_revlist(&cmd, src, NULL) < 0)
2997178e 600 return 1;
051e4005
JH
601
602 /*
603 * The commits out of the rev-list are not ancestors of
604 * our ref.
605 */
2997178e 606 i = read_in_full(cmd.out, buf, 1);
051e4005
JH
607 if (i)
608 goto error;
609 close(cmd.out);
7fcbd37f 610 cmd.out = -1;
051e4005
JH
611
612 /*
613 * rev-list may have died by encountering a bad commit
614 * in the history, in which case we do want to bail out
615 * even when it showed no commit.
616 */
617 if (finish_command(&cmd))
618 goto error;
619
620 /* All the non-tip ones are ancestors of what we advertised */
3f0f6624 621 return 0;
051e4005
JH
622
623error:
7fcbd37f 624 sigchain_pop(SIGPIPE);
7fcbd37f
NTND
625 if (cmd.out >= 0)
626 close(cmd.out);
3f0f6624
NTND
627 return 1;
628}
7fcbd37f 629
3f0f6624
NTND
630static void check_non_tip(void)
631{
632 int i;
633
634 /*
635 * In the normal in-process case without
636 * uploadpack.allowReachableSHA1InWant,
637 * non-tip requests can never happen.
638 */
639 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
640 goto error;
641 if (!has_unreachable(&want_obj))
642 /* All the non-tip ones are ancestors of what we advertised */
643 return;
051e4005
JH
644
645error:
646 /* Pick one of them (we know there at least is one) */
647 for (i = 0; i < want_obj.nr; i++) {
3f0f6624 648 struct object *o = want_obj.objects[i].item;
390eb36b 649 if (!is_our_ref(o))
051e4005 650 die("git upload-pack: not our ref %s",
f2fd0760 651 oid_to_hex(&o->oid));
051e4005
JH
652 }
653}
654
5c24cdea
NTND
655static void send_shallow(struct commit_list *result)
656{
657 while (result) {
658 struct object *object = &result->item->object;
659 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
dbaa6bdc
JH
660 packet_write_fmt(1, "shallow %s",
661 oid_to_hex(&object->oid));
19143f13 662 register_shallow(the_repository, &object->oid);
5c24cdea
NTND
663 shallow_nr++;
664 }
665 result = result->next;
666 }
667}
668
873700c9 669static void send_unshallow(const struct object_array *shallows)
e8e44de7 670{
e8e44de7 671 int i;
873700c9 672
e8e44de7
NTND
673 for (i = 0; i < shallows->nr; i++) {
674 struct object *object = shallows->objects[i].item;
675 if (object->flags & NOT_SHALLOW) {
676 struct commit_list *parents;
dbaa6bdc
JH
677 packet_write_fmt(1, "unshallow %s",
678 oid_to_hex(&object->oid));
e8e44de7 679 object->flags &= ~CLIENT_SHALLOW;
873700c9
NTND
680 /*
681 * We want to _register_ "object" as shallow, but we
682 * also need to traverse object's parents to deepen a
683 * shallow clone. Unregister it for now so we can
684 * parse and add the parents to the want list, then
685 * re-register it.
686 */
e92b848c 687 unregister_shallow(&object->oid);
e8e44de7
NTND
688 object->parsed = 0;
689 parse_commit_or_die((struct commit *)object);
690 parents = ((struct commit *)object)->parents;
691 while (parents) {
692 add_object_array(&parents->item->object,
693 NULL, &want_obj);
694 parents = parents->next;
695 }
696 add_object_array(object, NULL, &extra_edge_obj);
697 }
698 /* make sure commit traversal conforms to client */
19143f13 699 register_shallow(the_repository, &object->oid);
e8e44de7 700 }
873700c9
NTND
701}
702
cccf74e2
NTND
703static void deepen(int depth, int deepen_relative,
704 struct object_array *shallows)
873700c9 705{
c8813487 706 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
873700c9
NTND
707 int i;
708
709 for (i = 0; i < shallows->nr; i++) {
710 struct object *object = shallows->objects[i].item;
711 object->flags |= NOT_SHALLOW;
712 }
cccf74e2
NTND
713 } else if (deepen_relative) {
714 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
715 struct commit_list *result;
716
717 get_reachable_list(shallows, &reachable_shallows);
718 result = get_shallow_commits(&reachable_shallows,
719 depth + 1,
720 SHALLOW, NOT_SHALLOW);
721 send_shallow(result);
722 free_commit_list(result);
723 object_array_clear(&reachable_shallows);
873700c9
NTND
724 } else {
725 struct commit_list *result;
726
727 result = get_shallow_commits(&want_obj, depth,
728 SHALLOW, NOT_SHALLOW);
729 send_shallow(result);
730 free_commit_list(result);
731 }
732
733 send_unshallow(shallows);
e8e44de7
NTND
734}
735
569e554b
NTND
736static void deepen_by_rev_list(int ac, const char **av,
737 struct object_array *shallows)
738{
739 struct commit_list *result;
740
741 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
742 send_shallow(result);
743 free_commit_list(result);
744 send_unshallow(shallows);
685fbd32
BW
745}
746
747/* Returns 1 if a shallow list is sent or 0 otherwise */
748static int send_shallow_list(int depth, int deepen_rev_list,
749 timestamp_t deepen_since,
750 struct string_list *deepen_not,
751 struct object_array *shallows)
752{
753 int ret = 0;
754
755 if (depth > 0 && deepen_rev_list)
756 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
757 if (depth > 0) {
758 deepen(depth, deepen_relative, shallows);
759 ret = 1;
760 } else if (deepen_rev_list) {
761 struct argv_array av = ARGV_ARRAY_INIT;
762 int i;
763
764 argv_array_push(&av, "rev-list");
765 if (deepen_since)
766 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
767 if (deepen_not->nr) {
768 argv_array_push(&av, "--not");
769 for (i = 0; i < deepen_not->nr; i++) {
770 struct string_list_item *s = deepen_not->items + i;
771 argv_array_push(&av, s->string);
772 }
773 argv_array_push(&av, "--not");
774 }
775 for (i = 0; i < want_obj.nr; i++) {
776 struct object *o = want_obj.objects[i].item;
777 argv_array_push(&av, oid_to_hex(&o->oid));
778 }
779 deepen_by_rev_list(av.argc, av.argv, shallows);
780 argv_array_clear(&av);
781 ret = 1;
782 } else {
783 if (shallows->nr > 0) {
784 int i;
785 for (i = 0; i < shallows->nr; i++)
b16b60f7
JH
786 register_shallow(the_repository,
787 &shallows->objects[i].item->oid);
685fbd32
BW
788 }
789 }
790
791 shallow_nr += shallows->nr;
792 return ret;
569e554b
NTND
793}
794
ae2948f3
BW
795static int process_shallow(const char *line, struct object_array *shallows)
796{
797 const char *arg;
798 if (skip_prefix(line, "shallow ", &arg)) {
799 struct object_id oid;
800 struct object *object;
801 if (get_oid_hex(arg, &oid))
802 die("invalid shallow line: %s", line);
803 object = parse_object(&oid);
804 if (!object)
805 return 1;
806 if (object->type != OBJ_COMMIT)
807 die("invalid shallow object %s", oid_to_hex(&oid));
808 if (!(object->flags & CLIENT_SHALLOW)) {
809 object->flags |= CLIENT_SHALLOW;
810 add_object_array(object, NULL, shallows);
811 }
812 return 1;
813 }
814
815 return 0;
816}
817
818static int process_deepen(const char *line, int *depth)
819{
820 const char *arg;
821 if (skip_prefix(line, "deepen ", &arg)) {
822 char *end = NULL;
823 *depth = (int)strtol(arg, &end, 0);
824 if (!end || *end || *depth <= 0)
825 die("Invalid deepen: %s", line);
826 return 1;
827 }
828
829 return 0;
830}
831
832static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
833{
834 const char *arg;
835 if (skip_prefix(line, "deepen-since ", &arg)) {
836 char *end = NULL;
837 *deepen_since = parse_timestamp(arg, &end, 0);
838 if (!end || *end || !deepen_since ||
839 /* revisions.c's max_age -1 is special */
840 *deepen_since == -1)
841 die("Invalid deepen-since: %s", line);
842 *deepen_rev_list = 1;
843 return 1;
844 }
845 return 0;
846}
847
848static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
849{
850 const char *arg;
851 if (skip_prefix(line, "deepen-not ", &arg)) {
852 char *ref = NULL;
853 struct object_id oid;
854 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
855 die("git upload-pack: ambiguous deepen-not: %s", line);
856 string_list_append(deepen_not, ref);
857 free(ref);
858 *deepen_rev_list = 1;
859 return 1;
860 }
861 return 0;
569e554b
NTND
862}
863
b1e9fff7 864static void receive_needs(void)
fb9040cc 865{
3cd47459 866 struct object_array shallows = OBJECT_ARRAY_INIT;
269a7a83 867 struct string_list deepen_not = STRING_LIST_INIT_DUP;
74543a04 868 int depth = 0;
051e4005 869 int has_non_tip = 0;
dddbad72 870 timestamp_t deepen_since = 0;
569e554b 871 int deepen_rev_list = 0;
fb9040cc 872
f0cea83f 873 shallow_nr = 0;
fb9040cc 874 for (;;) {
565ebbf7 875 struct object *o;
f47182c8 876 const char *features;
cf93982f 877 struct object_id oid_buf;
74543a04 878 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
879 const char *arg;
880
960deccb 881 reset_timeout();
74543a04 882 if (!line)
ed09aef0 883 break;
e091eb93 884
ae2948f3 885 if (process_shallow(line, &shallows))
ed09aef0 886 continue;
ae2948f3 887 if (process_deepen(line, &depth))
016e6ccb 888 continue;
ae2948f3 889 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
569e554b 890 continue;
ae2948f3 891 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
269a7a83 892 continue;
ae2948f3 893
10ac85c7
JH
894 if (skip_prefix(line, "filter ", &arg)) {
895 if (!filter_capability_requested)
896 die("git upload-pack: filtering capability not negotiated");
897 parse_list_objects_filter(&filter_options, arg);
898 continue;
899 }
9bfa0f9b 900
8bf3b758 901 if (!skip_prefix(line, "want ", &arg) ||
55dc227d 902 parse_oid_hex(arg, &oid_buf, &features))
7e44c935 903 die("git upload-pack: protocol error, "
55dc227d 904 "expected to get object ID, not '%s'", line);
f47182c8 905
cccf74e2
NTND
906 if (parse_feature_request(features, "deepen-relative"))
907 deepen_relative = 1;
f47182c8 908 if (parse_feature_request(features, "multi_ack_detailed"))
78affc49 909 multi_ack = 2;
f47182c8 910 else if (parse_feature_request(features, "multi_ack"))
1bd8c8f0 911 multi_ack = 1;
f47182c8 912 if (parse_feature_request(features, "no-done"))
4e10cf9a 913 no_done = 1;
f47182c8 914 if (parse_feature_request(features, "thin-pack"))
b19696c2 915 use_thin_pack = 1;
f47182c8 916 if (parse_feature_request(features, "ofs-delta"))
e4fe4b8e 917 use_ofs_delta = 1;
f47182c8 918 if (parse_feature_request(features, "side-band-64k"))
d47f3db7 919 use_sideband = LARGE_PACKET_MAX;
f47182c8 920 else if (parse_feature_request(features, "side-band"))
d47f3db7 921 use_sideband = DEFAULT_PACKET_MAX;
f47182c8 922 if (parse_feature_request(features, "no-progress"))
b0e90897 923 no_progress = 1;
f47182c8 924 if (parse_feature_request(features, "include-tag"))
348e390b 925 use_include_tag = 1;
c7620bd0 926 if (allow_filter && parse_feature_request(features, "filter"))
10ac85c7 927 filter_capability_requested = 1;
565ebbf7 928
c251c83d 929 o = parse_object(&oid_buf);
bdb31ead
JT
930 if (!o) {
931 packet_write_fmt(1,
932 "ERR upload-pack: not our ref %s",
cf93982f 933 oid_to_hex(&oid_buf));
9f9aa761 934 die("git upload-pack: not our ref %s",
cf93982f 935 oid_to_hex(&oid_buf));
bdb31ead 936 }
565ebbf7
JH
937 if (!(o->flags & WANTED)) {
938 o->flags |= WANTED;
f8edeaa0
DT
939 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
940 || is_our_ref(o)))
051e4005 941 has_non_tip = 1;
b1e9fff7 942 add_object_array(o, NULL, &want_obj);
565ebbf7 943 }
fb9040cc 944 }
9462e3f5 945
051e4005
JH
946 /*
947 * We have sent all our refs already, and the other end
948 * should have chosen out of them. When we are operating
949 * in the stateless RPC mode, however, their choice may
950 * have been based on the set of older refs advertised
951 * by another process that handled the initial request.
952 */
953 if (has_non_tip)
954 check_non_tip();
955
9462e3f5
JS
956 if (!use_sideband && daemon_mode)
957 no_progress = 1;
958
569e554b 959 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
f53514bc 960 return;
569e554b 961
685fbd32
BW
962 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
963 &deepen_not, &shallows))
964 packet_flush(1);
dcb572ab 965 object_array_clear(&shallows);
fb9040cc
LT
966}
967
daebaa78 968/* return non-zero if the ref is hidden, otherwise 0 */
78a766ab
LF
969static int mark_our_ref(const char *refname, const char *refname_full,
970 const struct object_id *oid)
cbbe50db 971{
363e98bf 972 struct object *o = lookup_unknown_object(oid->hash);
daebaa78 973
78a766ab 974 if (ref_is_hidden(refname, refname_full)) {
390eb36b 975 o->flags |= HIDDEN_REF;
daebaa78 976 return 1;
390eb36b 977 }
3f1da57f 978 o->flags |= OUR_REF;
cbbe50db
JH
979 return 0;
980}
981
78a766ab 982static int check_ref(const char *refname_full, const struct object_id *oid,
363e98bf 983 int flag, void *cb_data)
e172755b 984{
78a766ab
LF
985 const char *refname = strip_namespace(refname_full);
986
987 mark_our_ref(refname, refname_full, oid);
e172755b
JK
988 return 0;
989}
990
7171d8c1
JH
991static void format_symref_info(struct strbuf *buf, struct string_list *symref)
992{
993 struct string_list_item *item;
994
995 if (!symref->nr)
996 return;
997 for_each_string_list_item(item, symref)
998 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
999}
1000
363e98bf
MH
1001static int send_ref(const char *refname, const struct object_id *oid,
1002 int flag, void *cb_data)
def88e9a 1003{
ed09aef0 1004 static const char *capabilities = "multi_ack thin-pack side-band"
cccf74e2
NTND
1005 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1006 " deepen-relative no-progress include-tag multi_ack_detailed";
6b01ecfe 1007 const char *refname_nons = strip_namespace(refname);
21758aff 1008 struct object_id peeled;
b5b16990 1009
78a766ab 1010 if (mark_our_ref(refname_nons, refname, oid))
daebaa78 1011 return 0;
cbbe50db 1012
7171d8c1
JH
1013 if (capabilities) {
1014 struct strbuf symref_info = STRBUF_INIT;
1015
1016 format_symref_info(&symref_info, cb_data);
10ac85c7 1017 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
363e98bf 1018 oid_to_hex(oid), refname_nons,
cf2ad8e6 1019 0, capabilities,
7199c093
FM
1020 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1021 " allow-tip-sha1-in-want" : "",
68ee6289
FM
1022 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1023 " allow-reachable-sha1-in-want" : "",
ff5effdf 1024 stateless_rpc ? " no-done" : "",
7171d8c1 1025 symref_info.buf,
c7620bd0 1026 allow_filter ? " filter" : "",
ff5effdf 1027 git_user_agent_sanitized());
7171d8c1
JH
1028 strbuf_release(&symref_info);
1029 } else {
81c634e9 1030 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
7171d8c1 1031 }
1f5881bb 1032 capabilities = NULL;
b420d909 1033 if (!peel_ref(refname, &peeled))
81c634e9 1034 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
def88e9a
LT
1035 return 0;
1036}
1037
7dabd056
MH
1038static int find_symref(const char *refname, const struct object_id *oid,
1039 int flag, void *cb_data)
7171d8c1
JH
1040{
1041 const char *symref_target;
1042 struct string_list_item *item;
7171d8c1
JH
1043
1044 if ((flag & REF_ISSYMREF) == 0)
1045 return 0;
744c040b 1046 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
7171d8c1
JH
1047 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1048 die("'%s' is a symref but it is not?", refname);
1049 item = string_list_append(cb_data, refname);
1050 item->util = xstrdup(symref_target);
1051 return 0;
1052}
1053
daebaa78
JH
1054static int upload_pack_config(const char *var, const char *value, void *unused)
1055{
7199c093
FM
1056 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1057 if (git_config_bool(var, value))
1058 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1059 else
1060 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
68ee6289
FM
1061 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1062 if (git_config_bool(var, value))
1063 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1064 else
1065 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
f8edeaa0
DT
1066 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1067 if (git_config_bool(var, value))
1068 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1069 else
1070 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
7199c093 1071 } else if (!strcmp("uploadpack.keepalive", var)) {
05e95155
JK
1072 keepalive = git_config_int(var, value);
1073 if (!keepalive)
1074 keepalive = -1;
20b20a22
JK
1075 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1076 if (!strcmp("uploadpack.packobjectshook", var))
1077 return git_config_string(&pack_objects_hook, var, value);
10ac85c7 1078 } else if (!strcmp("uploadpack.allowfilter", var)) {
c7620bd0 1079 allow_filter = git_config_bool(var, value);
05e95155 1080 }
daebaa78
JH
1081 return parse_hide_refs_config(var, value, "uploadpack");
1082}
1083
a3d6b53e 1084void upload_pack(struct upload_pack_options *options)
def88e9a 1085{
a3d6b53e 1086 struct string_list symref = STRING_LIST_INIT_DUP;
960deccb 1087
a3d6b53e
BW
1088 stateless_rpc = options->stateless_rpc;
1089 timeout = options->timeout;
1090 daemon_mode = options->daemon_mode;
2fb3f6db 1091
daebaa78 1092 git_config(upload_pack_config, NULL);
960deccb 1093
a3d6b53e 1094 head_ref_namespaced(find_symref, &symref);
a6080a0a 1095
a3d6b53e
BW
1096 if (options->advertise_refs || !stateless_rpc) {
1097 reset_timeout();
1098 head_ref_namespaced(send_ref, &symref);
1099 for_each_namespaced_ref(send_ref, &symref);
1100 advertise_shallow_grafts(1);
1101 packet_flush(1);
1102 } else {
1103 head_ref_namespaced(check_ref, NULL);
1104 for_each_namespaced_ref(check_ref, NULL);
aa9bab29 1105 }
a3d6b53e
BW
1106 string_list_clear(&symref, 1);
1107 if (options->advertise_refs)
1108 return;
04b33055 1109
a3d6b53e
BW
1110 receive_needs();
1111 if (want_obj.nr) {
1112 get_common_commits();
1113 create_pack_file();
1114 }
def88e9a 1115}
04b33055 1116
3145ea95
BW
1117struct upload_pack_data {
1118 struct object_array wants;
1119 struct oid_array haves;
113b9475 1120
685fbd32
BW
1121 struct object_array shallows;
1122 struct string_list deepen_not;
1123 int depth;
1124 timestamp_t deepen_since;
1125 int deepen_rev_list;
1126 int deepen_relative;
ad491366 1127
3145ea95 1128 unsigned stateless_rpc : 1;
aa9bab29 1129
3145ea95
BW
1130 unsigned use_thin_pack : 1;
1131 unsigned use_ofs_delta : 1;
1132 unsigned no_progress : 1;
1133 unsigned use_include_tag : 1;
1134 unsigned done : 1;
1135};
1136
1137static void upload_pack_data_init(struct upload_pack_data *data)
1138{
1139 struct object_array wants = OBJECT_ARRAY_INIT;
1140 struct oid_array haves = OID_ARRAY_INIT;
685fbd32
BW
1141 struct object_array shallows = OBJECT_ARRAY_INIT;
1142 struct string_list deepen_not = STRING_LIST_INIT_DUP;
3145ea95
BW
1143
1144 memset(data, 0, sizeof(*data));
1145 data->wants = wants;
1146 data->haves = haves;
685fbd32
BW
1147 data->shallows = shallows;
1148 data->deepen_not = deepen_not;
3145ea95
BW
1149}
1150
1151static void upload_pack_data_clear(struct upload_pack_data *data)
1152{
1153 object_array_clear(&data->wants);
1154 oid_array_clear(&data->haves);
685fbd32
BW
1155 object_array_clear(&data->shallows);
1156 string_list_clear(&data->deepen_not, 0);
3145ea95
BW
1157}
1158
1159static int parse_want(const char *line)
1160{
1161 const char *arg;
1162 if (skip_prefix(line, "want ", &arg)) {
1163 struct object_id oid;
1164 struct object *o;
1165
1166 if (get_oid_hex(arg, &oid))
1167 die("git upload-pack: protocol error, "
1168 "expected to get oid, not '%s'", line);
1169
1170 o = parse_object(&oid);
1171 if (!o) {
1172 packet_write_fmt(1,
1173 "ERR upload-pack: not our ref %s",
1174 oid_to_hex(&oid));
1175 die("git upload-pack: not our ref %s",
1176 oid_to_hex(&oid));
1177 }
1178
1179 if (!(o->flags & WANTED)) {
1180 o->flags |= WANTED;
1181 add_object_array(o, NULL, &want_obj);
1182 }
1183
1184 return 1;
1185 }
1186
1187 return 0;
1188}
1189
1190static int parse_have(const char *line, struct oid_array *haves)
1191{
1192 const char *arg;
1193 if (skip_prefix(line, "have ", &arg)) {
1194 struct object_id oid;
1195
1196 if (get_oid_hex(arg, &oid))
1197 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1198 oid_array_append(haves, &oid);
1199 return 1;
aa9bab29
BW
1200 }
1201
def88e9a
LT
1202 return 0;
1203}
3145ea95
BW
1204
1205static void process_args(struct packet_reader *request,
1206 struct upload_pack_data *data)
1207{
1208 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1209 const char *arg = request->line;
ba95710a 1210 const char *p;
3145ea95
BW
1211
1212 /* process want */
1213 if (parse_want(arg))
1214 continue;
1215 /* process have line */
1216 if (parse_have(arg, &data->haves))
1217 continue;
1218
1219 /* process args like thin-pack */
1220 if (!strcmp(arg, "thin-pack")) {
1221 use_thin_pack = 1;
1222 continue;
1223 }
1224 if (!strcmp(arg, "ofs-delta")) {
1225 use_ofs_delta = 1;
1226 continue;
1227 }
1228 if (!strcmp(arg, "no-progress")) {
1229 no_progress = 1;
1230 continue;
1231 }
1232 if (!strcmp(arg, "include-tag")) {
1233 use_include_tag = 1;
1234 continue;
1235 }
1236 if (!strcmp(arg, "done")) {
1237 data->done = 1;
1238 continue;
1239 }
1240
685fbd32
BW
1241 /* Shallow related arguments */
1242 if (process_shallow(arg, &data->shallows))
1243 continue;
1244 if (process_deepen(arg, &data->depth))
1245 continue;
1246 if (process_deepen_since(arg, &data->deepen_since,
1247 &data->deepen_rev_list))
1248 continue;
1249 if (process_deepen_not(arg, &data->deepen_not,
1250 &data->deepen_rev_list))
1251 continue;
1252 if (!strcmp(arg, "deepen-relative")) {
1253 data->deepen_relative = 1;
1254 continue;
1255 }
1256
ba95710a
JT
1257 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1258 parse_list_objects_filter(&filter_options, p);
1259 continue;
1260 }
1261
3145ea95 1262 /* ignore unknown lines maybe? */
7cc6ed2d 1263 die("unexpected line: '%s'", arg);
3145ea95
BW
1264 }
1265}
1266
1267static int process_haves(struct oid_array *haves, struct oid_array *common)
1268{
1269 int i;
1270
1271 /* Process haves */
1272 for (i = 0; i < haves->nr; i++) {
1273 const struct object_id *oid = &haves->oid[i];
1274 struct object *o;
1275 int we_knew_they_have = 0;
1276
1277 if (!has_object_file(oid))
1278 continue;
1279
1280 oid_array_append(common, oid);
1281
1282 o = parse_object(oid);
1283 if (!o)
1284 die("oops (%s)", oid_to_hex(oid));
1285 if (o->type == OBJ_COMMIT) {
1286 struct commit_list *parents;
1287 struct commit *commit = (struct commit *)o;
1288 if (o->flags & THEY_HAVE)
1289 we_knew_they_have = 1;
1290 else
1291 o->flags |= THEY_HAVE;
1292 if (!oldest_have || (commit->date < oldest_have))
1293 oldest_have = commit->date;
1294 for (parents = commit->parents;
1295 parents;
1296 parents = parents->next)
1297 parents->item->object.flags |= THEY_HAVE;
1298 }
1299 if (!we_knew_they_have)
1300 add_object_array(o, NULL, &have_obj);
1301 }
1302
1303 return 0;
1304}
1305
1306static int send_acks(struct oid_array *acks, struct strbuf *response)
1307{
1308 int i;
1309
1310 packet_buf_write(response, "acknowledgments\n");
1311
1312 /* Send Acks */
1313 if (!acks->nr)
1314 packet_buf_write(response, "NAK\n");
1315
1316 for (i = 0; i < acks->nr; i++) {
1317 packet_buf_write(response, "ACK %s\n",
1318 oid_to_hex(&acks->oid[i]));
1319 }
1320
1321 if (ok_to_give_up()) {
1322 /* Send Ready */
1323 packet_buf_write(response, "ready\n");
1324 return 1;
1325 }
1326
1327 return 0;
1328}
1329
1330static int process_haves_and_send_acks(struct upload_pack_data *data)
1331{
1332 struct oid_array common = OID_ARRAY_INIT;
1333 struct strbuf response = STRBUF_INIT;
1334 int ret = 0;
1335
1336 process_haves(&data->haves, &common);
1337 if (data->done) {
1338 ret = 1;
1339 } else if (send_acks(&common, &response)) {
1340 packet_buf_delim(&response);
1341 ret = 1;
1342 } else {
1343 /* Add Flush */
1344 packet_buf_flush(&response);
1345 ret = 0;
1346 }
1347
1348 /* Send response */
1349 write_or_die(1, response.buf, response.len);
1350 strbuf_release(&response);
1351
1352 oid_array_clear(&data->haves);
1353 oid_array_clear(&common);
1354 return ret;
1355}
1356
685fbd32
BW
1357static void send_shallow_info(struct upload_pack_data *data)
1358{
1359 /* No shallow info needs to be sent */
1360 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
b16b60f7 1361 !is_repository_shallow(the_repository))
685fbd32
BW
1362 return;
1363
1364 packet_write_fmt(1, "shallow-info\n");
1365
1366 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1367 data->deepen_since, &data->deepen_not,
b16b60f7
JH
1368 &data->shallows) &&
1369 is_repository_shallow(the_repository))
685fbd32
BW
1370 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1371
1372 packet_delim(1);
1373}
1374
3145ea95
BW
1375enum fetch_state {
1376 FETCH_PROCESS_ARGS = 0,
1377 FETCH_SEND_ACKS,
1378 FETCH_SEND_PACK,
1379 FETCH_DONE,
1380};
1381
1382int upload_pack_v2(struct repository *r, struct argv_array *keys,
1383 struct packet_reader *request)
1384{
1385 enum fetch_state state = FETCH_PROCESS_ARGS;
1386 struct upload_pack_data data;
1387
54592687
JT
1388 git_config(upload_pack_config, NULL);
1389
3145ea95
BW
1390 upload_pack_data_init(&data);
1391 use_sideband = LARGE_PACKET_MAX;
1392
1393 while (state != FETCH_DONE) {
1394 switch (state) {
1395 case FETCH_PROCESS_ARGS:
1396 process_args(request, &data);
1397
1398 if (!want_obj.nr) {
1399 /*
1400 * Request didn't contain any 'want' lines,
1401 * guess they didn't want anything.
1402 */
1403 state = FETCH_DONE;
1404 } else if (data.haves.nr) {
1405 /*
1406 * Request had 'have' lines, so lets ACK them.
1407 */
1408 state = FETCH_SEND_ACKS;
1409 } else {
1410 /*
1411 * Request had 'want's but no 'have's so we can
1412 * immedietly go to construct and send a pack.
1413 */
1414 state = FETCH_SEND_PACK;
1415 }
1416 break;
1417 case FETCH_SEND_ACKS:
1418 if (process_haves_and_send_acks(&data))
1419 state = FETCH_SEND_PACK;
1420 else
1421 state = FETCH_DONE;
1422 break;
1423 case FETCH_SEND_PACK:
685fbd32
BW
1424 send_shallow_info(&data);
1425
3145ea95
BW
1426 packet_write_fmt(1, "packfile\n");
1427 create_pack_file();
1428 state = FETCH_DONE;
1429 break;
1430 case FETCH_DONE:
1431 continue;
1432 }
1433 }
1434
1435 upload_pack_data_clear(&data);
1436 return 0;
1437}
685fbd32
BW
1438
1439int upload_pack_advertise(struct repository *r,
1440 struct strbuf *value)
1441{
ba95710a
JT
1442 if (value) {
1443 int allow_filter_value;
685fbd32 1444 strbuf_addstr(value, "shallow");
ba95710a
JT
1445 if (!repo_config_get_bool(the_repository,
1446 "uploadpack.allowfilter",
1447 &allow_filter_value) &&
1448 allow_filter_value)
1449 strbuf_addstr(value, " filter");
1450 }
685fbd32
BW
1451 return 1;
1452}