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