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