upload-pack: make have_obj not global
[git/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"
829a3215 27#include "commit-graph.h"
ba3ca1ed 28#include "commit-reach.h"
def88e9a 29
208acbfb 30/* Remember to update object flag allocation in object.h */
937a515a
JH
31#define THEY_HAVE (1u << 11)
32#define OUR_REF (1u << 12)
33#define WANTED (1u << 13)
34#define COMMON_KNOWN (1u << 14)
937a515a 35
f53514bc
JS
36#define SHALLOW (1u << 16)
37#define NOT_SHALLOW (1u << 17)
38#define CLIENT_SHALLOW (1u << 18)
390eb36b 39#define HIDDEN_REF (1u << 19)
f53514bc 40
dddbad72 41static timestamp_t oldest_have;
937a515a 42
cccf74e2 43static int deepen_relative;
3f1da57f 44static int multi_ack;
4e10cf9a 45static int no_done;
348e390b 46static int use_thin_pack, use_ofs_delta, use_include_tag;
9462e3f5 47static int no_progress, daemon_mode;
7199c093
FM
48/* Allow specifying sha1 if it is a ref tip. */
49#define ALLOW_TIP_SHA1 01
68ee6289
FM
50/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
51#define ALLOW_REACHABLE_SHA1 02
f8edeaa0
DT
52/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
53#define ALLOW_ANY_SHA1 07
7199c093 54static unsigned int allow_unadvertised_object_request;
f0cea83f 55static int shallow_nr;
b1e9fff7 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
0b9333ff 102static void create_pack_file(const struct object_array *have_obj)
fb9040cc 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 166 fprintf(pipe_fd, "--not\n");
0b9333ff 167 for (i = 0; i < have_obj->nr; i++)
cdab4858 168 fprintf(pipe_fd, "%s\n",
0b9333ff 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
0b9333ff
JT
306static int got_oid(const char *hex, struct object_id *oid,
307 struct object_array *have_obj)
def88e9a 308{
b1e9fff7 309 struct object *o;
937a515a 310 int we_knew_they_have = 0;
b1e9fff7 311
cf93982f 312 if (get_oid_hex(hex, oid))
7e44c935 313 die("git upload-pack: expected SHA1 object, got '%s'", hex);
cf93982f 314 if (!has_object_file(oid))
937a515a 315 return -1;
b1e9fff7 316
109cd76d 317 o = parse_object(the_repository, oid);
b1e9fff7 318 if (!o)
cf93982f 319 die("oops (%s)", oid_to_hex(oid));
182a8dab 320 if (o->type == OBJ_COMMIT) {
b1e9fff7 321 struct commit_list *parents;
937a515a 322 struct commit *commit = (struct commit *)o;
b1e9fff7 323 if (o->flags & THEY_HAVE)
937a515a
JH
324 we_knew_they_have = 1;
325 else
326 o->flags |= THEY_HAVE;
327 if (!oldest_have || (commit->date < oldest_have))
328 oldest_have = commit->date;
329 for (parents = commit->parents;
b1e9fff7
JH
330 parents;
331 parents = parents->next)
332 parents->item->object.flags |= THEY_HAVE;
fb9040cc 333 }
937a515a 334 if (!we_knew_they_have) {
0b9333ff 335 add_object_array(o, NULL, have_obj);
937a515a
JH
336 return 1;
337 }
338 return 0;
339}
340
0b9333ff 341static int ok_to_give_up(const struct object_array *have_obj)
937a515a 342{
4fbcca4e 343 uint32_t min_generation = GENERATION_NUMBER_ZERO;
937a515a 344
0b9333ff 345 if (!have_obj->nr)
937a515a
JH
346 return 0;
347
118be578 348 return can_all_from_reach_with_flag(&want_obj, THEY_HAVE,
4fbcca4e
DS
349 COMMON_KNOWN, oldest_have,
350 min_generation);
def88e9a
LT
351}
352
0b9333ff 353static int get_common_commits(struct object_array *have_obj)
def88e9a 354{
cf93982f 355 struct object_id oid;
356 char last_hex[GIT_MAX_HEXSZ + 1];
49bee717
SP
357 int got_common = 0;
358 int got_other = 0;
4e10cf9a 359 int sent_ready = 0;
def88e9a 360
f0243f26
JS
361 save_commit_buffer = 0;
362
eeefa7c9 363 for (;;) {
74543a04 364 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
365 const char *arg;
366
960deccb 367 reset_timeout();
def88e9a 368
74543a04 369 if (!line) {
49bee717 370 if (multi_ack == 2 && got_common
0b9333ff 371 && !got_other && ok_to_give_up(have_obj)) {
4e10cf9a 372 sent_ready = 1;
81c634e9 373 packet_write_fmt(1, "ACK %s ready\n", last_hex);
4e10cf9a 374 }
0b9333ff 375 if (have_obj->nr == 0 || multi_ack)
81c634e9 376 packet_write_fmt(1, "NAK\n");
4e10cf9a
JH
377
378 if (no_done && sent_ready) {
81c634e9 379 packet_write_fmt(1, "ACK %s\n", last_hex);
4e10cf9a
JH
380 return 0;
381 }
42526b47
SP
382 if (stateless_rpc)
383 exit(0);
49bee717
SP
384 got_common = 0;
385 got_other = 0;
def88e9a
LT
386 continue;
387 }
8bf3b758 388 if (skip_prefix(line, "have ", &arg)) {
0b9333ff 389 switch (got_oid(arg, &oid, have_obj)) {
937a515a 390 case -1: /* they have what we do not */
49bee717 391 got_other = 1;
0b9333ff 392 if (multi_ack && ok_to_give_up(have_obj)) {
cf93982f 393 const char *hex = oid_to_hex(&oid);
4e10cf9a
JH
394 if (multi_ack == 2) {
395 sent_ready = 1;
81c634e9 396 packet_write_fmt(1, "ACK %s ready\n", hex);
4e10cf9a 397 } else
81c634e9 398 packet_write_fmt(1, "ACK %s continue\n", hex);
78affc49 399 }
937a515a
JH
400 break;
401 default:
49bee717 402 got_common = 1;
55dc227d 403 oid_to_hex_r(last_hex, &oid);
78affc49 404 if (multi_ack == 2)
81c634e9 405 packet_write_fmt(1, "ACK %s common\n", last_hex);
78affc49 406 else if (multi_ack)
81c634e9 407 packet_write_fmt(1, "ACK %s continue\n", last_hex);
0b9333ff 408 else if (have_obj->nr == 1)
81c634e9 409 packet_write_fmt(1, "ACK %s\n", last_hex);
937a515a 410 break;
af2d3aa4 411 }
def88e9a
LT
412 continue;
413 }
414 if (!strcmp(line, "done")) {
0b9333ff 415 if (have_obj->nr > 0) {
1bd8c8f0 416 if (multi_ack)
81c634e9 417 packet_write_fmt(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
418 return 0;
419 }
81c634e9 420 packet_write_fmt(1, "NAK\n");
def88e9a
LT
421 return -1;
422 }
7e44c935 423 die("git upload-pack: expected SHA1 list, got '%s'", line);
def88e9a 424 }
def88e9a
LT
425}
426
390eb36b
JH
427static int is_our_ref(struct object *o)
428{
68ee6289
FM
429 int allow_hidden_ref = (allow_unadvertised_object_request &
430 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
7199c093 431 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
390eb36b
JH
432}
433
2997178e
NTND
434/*
435 * on successful case, it's up to the caller to close cmd->out
436 */
437static int do_reachable_revlist(struct child_process *cmd,
079aa97e
NTND
438 struct object_array *src,
439 struct object_array *reachable)
051e4005
JH
440{
441 static const char *argv[] = {
442 "rev-list", "--stdin", NULL,
443 };
051e4005 444 struct object *o;
55dc227d 445 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
051e4005
JH
446 int i;
447
2997178e
NTND
448 cmd->argv = argv;
449 cmd->git_cmd = 1;
450 cmd->no_stderr = 1;
451 cmd->in = -1;
452 cmd->out = -1;
051e4005
JH
453
454 /*
7fcbd37f
NTND
455 * If the next rev-list --stdin encounters an unknown commit,
456 * it terminates, which will cause SIGPIPE in the write loop
051e4005
JH
457 * below.
458 */
459 sigchain_push(SIGPIPE, SIG_IGN);
460
2997178e 461 if (start_command(cmd))
7fcbd37f
NTND
462 goto error;
463
051e4005 464 namebuf[0] = '^';
cf93982f 465 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
051e4005
JH
466 for (i = get_max_object_index(); 0 < i; ) {
467 o = get_indexed_object(--i);
2a745324
BH
468 if (!o)
469 continue;
079aa97e
NTND
470 if (reachable && o->type == OBJ_COMMIT)
471 o->flags &= ~TMP_MARK;
390eb36b 472 if (!is_our_ref(o))
051e4005 473 continue;
f2fd0760 474 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
cf93982f 475 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
051e4005
JH
476 goto error;
477 }
cf93982f 478 namebuf[GIT_SHA1_HEXSZ] = '\n';
3f0f6624
NTND
479 for (i = 0; i < src->nr; i++) {
480 o = src->objects[i].item;
079aa97e
NTND
481 if (is_our_ref(o)) {
482 if (reachable)
483 add_object_array(o, NULL, reachable);
051e4005 484 continue;
079aa97e
NTND
485 }
486 if (reachable && o->type == OBJ_COMMIT)
487 o->flags |= TMP_MARK;
f2fd0760 488 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
cf93982f 489 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
051e4005
JH
490 goto error;
491 }
2997178e
NTND
492 close(cmd->in);
493 cmd->in = -1;
494 sigchain_pop(SIGPIPE);
051e4005 495
2997178e
NTND
496 return 0;
497
498error:
051e4005
JH
499 sigchain_pop(SIGPIPE);
500
2997178e
NTND
501 if (cmd->in >= 0)
502 close(cmd->in);
503 if (cmd->out >= 0)
504 close(cmd->out);
505 return -1;
506}
507
079aa97e
NTND
508static int get_reachable_list(struct object_array *src,
509 struct object_array *reachable)
510{
511 struct child_process cmd = CHILD_PROCESS_INIT;
512 int i;
513 struct object *o;
55dc227d 514 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
515 const unsigned hexsz = the_hash_algo->hexsz;
079aa97e
NTND
516
517 if (do_reachable_revlist(&cmd, src, reachable) < 0)
518 return -1;
519
55dc227d 520 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
079aa97e 521 struct object_id sha1;
55dc227d 522 const char *p;
079aa97e 523
55dc227d 524 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
079aa97e
NTND
525 break;
526
5abddd1e 527 o = lookup_object(the_repository, sha1.hash);
079aa97e
NTND
528 if (o && o->type == OBJ_COMMIT) {
529 o->flags &= ~TMP_MARK;
530 }
531 }
532 for (i = get_max_object_index(); 0 < i; i--) {
533 o = get_indexed_object(i - 1);
534 if (o && o->type == OBJ_COMMIT &&
535 (o->flags & TMP_MARK)) {
536 add_object_array(o, NULL, reachable);
537 o->flags &= ~TMP_MARK;
538 }
539 }
540 close(cmd.out);
541
542 if (finish_command(&cmd))
543 return -1;
544
545 return 0;
546}
547
2997178e
NTND
548static int has_unreachable(struct object_array *src)
549{
550 struct child_process cmd = CHILD_PROCESS_INIT;
551 char buf[1];
552 int i;
553
079aa97e 554 if (do_reachable_revlist(&cmd, src, NULL) < 0)
2997178e 555 return 1;
051e4005
JH
556
557 /*
558 * The commits out of the rev-list are not ancestors of
559 * our ref.
560 */
2997178e 561 i = read_in_full(cmd.out, buf, 1);
051e4005
JH
562 if (i)
563 goto error;
564 close(cmd.out);
7fcbd37f 565 cmd.out = -1;
051e4005
JH
566
567 /*
568 * rev-list may have died by encountering a bad commit
569 * in the history, in which case we do want to bail out
570 * even when it showed no commit.
571 */
572 if (finish_command(&cmd))
573 goto error;
574
575 /* All the non-tip ones are ancestors of what we advertised */
3f0f6624 576 return 0;
051e4005
JH
577
578error:
7fcbd37f 579 sigchain_pop(SIGPIPE);
7fcbd37f
NTND
580 if (cmd.out >= 0)
581 close(cmd.out);
3f0f6624
NTND
582 return 1;
583}
7fcbd37f 584
3f0f6624
NTND
585static void check_non_tip(void)
586{
587 int i;
588
589 /*
590 * In the normal in-process case without
591 * uploadpack.allowReachableSHA1InWant,
592 * non-tip requests can never happen.
593 */
594 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
595 goto error;
596 if (!has_unreachable(&want_obj))
597 /* All the non-tip ones are ancestors of what we advertised */
598 return;
051e4005
JH
599
600error:
601 /* Pick one of them (we know there at least is one) */
602 for (i = 0; i < want_obj.nr; i++) {
3f0f6624 603 struct object *o = want_obj.objects[i].item;
390eb36b 604 if (!is_our_ref(o))
051e4005 605 die("git upload-pack: not our ref %s",
f2fd0760 606 oid_to_hex(&o->oid));
051e4005
JH
607 }
608}
609
5c24cdea
NTND
610static void send_shallow(struct commit_list *result)
611{
612 while (result) {
613 struct object *object = &result->item->object;
614 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
dbaa6bdc
JH
615 packet_write_fmt(1, "shallow %s",
616 oid_to_hex(&object->oid));
19143f13 617 register_shallow(the_repository, &object->oid);
5c24cdea
NTND
618 shallow_nr++;
619 }
620 result = result->next;
621 }
622}
623
873700c9 624static void send_unshallow(const struct object_array *shallows)
e8e44de7 625{
e8e44de7 626 int i;
873700c9 627
e8e44de7
NTND
628 for (i = 0; i < shallows->nr; i++) {
629 struct object *object = shallows->objects[i].item;
630 if (object->flags & NOT_SHALLOW) {
631 struct commit_list *parents;
dbaa6bdc
JH
632 packet_write_fmt(1, "unshallow %s",
633 oid_to_hex(&object->oid));
e8e44de7 634 object->flags &= ~CLIENT_SHALLOW;
873700c9
NTND
635 /*
636 * We want to _register_ "object" as shallow, but we
637 * also need to traverse object's parents to deepen a
638 * shallow clone. Unregister it for now so we can
639 * parse and add the parents to the want list, then
640 * re-register it.
641 */
e92b848c 642 unregister_shallow(&object->oid);
e8e44de7
NTND
643 object->parsed = 0;
644 parse_commit_or_die((struct commit *)object);
645 parents = ((struct commit *)object)->parents;
646 while (parents) {
647 add_object_array(&parents->item->object,
648 NULL, &want_obj);
649 parents = parents->next;
650 }
651 add_object_array(object, NULL, &extra_edge_obj);
652 }
653 /* make sure commit traversal conforms to client */
19143f13 654 register_shallow(the_repository, &object->oid);
e8e44de7 655 }
873700c9
NTND
656}
657
cccf74e2
NTND
658static void deepen(int depth, int deepen_relative,
659 struct object_array *shallows)
873700c9 660{
c8813487 661 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
873700c9
NTND
662 int i;
663
664 for (i = 0; i < shallows->nr; i++) {
665 struct object *object = shallows->objects[i].item;
666 object->flags |= NOT_SHALLOW;
667 }
cccf74e2
NTND
668 } else if (deepen_relative) {
669 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
670 struct commit_list *result;
671
672 get_reachable_list(shallows, &reachable_shallows);
673 result = get_shallow_commits(&reachable_shallows,
674 depth + 1,
675 SHALLOW, NOT_SHALLOW);
676 send_shallow(result);
677 free_commit_list(result);
678 object_array_clear(&reachable_shallows);
873700c9
NTND
679 } else {
680 struct commit_list *result;
681
682 result = get_shallow_commits(&want_obj, depth,
683 SHALLOW, NOT_SHALLOW);
684 send_shallow(result);
685 free_commit_list(result);
686 }
687
688 send_unshallow(shallows);
e8e44de7
NTND
689}
690
569e554b
NTND
691static void deepen_by_rev_list(int ac, const char **av,
692 struct object_array *shallows)
693{
694 struct commit_list *result;
695
829a3215 696 close_commit_graph(the_repository);
569e554b
NTND
697 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
698 send_shallow(result);
699 free_commit_list(result);
700 send_unshallow(shallows);
685fbd32
BW
701}
702
703/* Returns 1 if a shallow list is sent or 0 otherwise */
704static int send_shallow_list(int depth, int deepen_rev_list,
705 timestamp_t deepen_since,
706 struct string_list *deepen_not,
707 struct object_array *shallows)
708{
709 int ret = 0;
710
711 if (depth > 0 && deepen_rev_list)
712 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
713 if (depth > 0) {
714 deepen(depth, deepen_relative, shallows);
715 ret = 1;
716 } else if (deepen_rev_list) {
717 struct argv_array av = ARGV_ARRAY_INIT;
718 int i;
719
720 argv_array_push(&av, "rev-list");
721 if (deepen_since)
722 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
723 if (deepen_not->nr) {
724 argv_array_push(&av, "--not");
725 for (i = 0; i < deepen_not->nr; i++) {
726 struct string_list_item *s = deepen_not->items + i;
727 argv_array_push(&av, s->string);
728 }
729 argv_array_push(&av, "--not");
730 }
731 for (i = 0; i < want_obj.nr; i++) {
732 struct object *o = want_obj.objects[i].item;
733 argv_array_push(&av, oid_to_hex(&o->oid));
734 }
735 deepen_by_rev_list(av.argc, av.argv, shallows);
736 argv_array_clear(&av);
737 ret = 1;
738 } else {
739 if (shallows->nr > 0) {
740 int i;
741 for (i = 0; i < shallows->nr; i++)
00624d60
JH
742 register_shallow(the_repository,
743 &shallows->objects[i].item->oid);
685fbd32
BW
744 }
745 }
746
747 shallow_nr += shallows->nr;
748 return ret;
569e554b
NTND
749}
750
ae2948f3
BW
751static int process_shallow(const char *line, struct object_array *shallows)
752{
753 const char *arg;
754 if (skip_prefix(line, "shallow ", &arg)) {
755 struct object_id oid;
756 struct object *object;
757 if (get_oid_hex(arg, &oid))
758 die("invalid shallow line: %s", line);
109cd76d 759 object = parse_object(the_repository, &oid);
ae2948f3
BW
760 if (!object)
761 return 1;
762 if (object->type != OBJ_COMMIT)
763 die("invalid shallow object %s", oid_to_hex(&oid));
764 if (!(object->flags & CLIENT_SHALLOW)) {
765 object->flags |= CLIENT_SHALLOW;
766 add_object_array(object, NULL, shallows);
767 }
768 return 1;
769 }
770
771 return 0;
772}
773
774static int process_deepen(const char *line, int *depth)
775{
776 const char *arg;
777 if (skip_prefix(line, "deepen ", &arg)) {
778 char *end = NULL;
779 *depth = (int)strtol(arg, &end, 0);
780 if (!end || *end || *depth <= 0)
781 die("Invalid deepen: %s", line);
782 return 1;
783 }
784
785 return 0;
786}
787
788static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
789{
790 const char *arg;
791 if (skip_prefix(line, "deepen-since ", &arg)) {
792 char *end = NULL;
793 *deepen_since = parse_timestamp(arg, &end, 0);
794 if (!end || *end || !deepen_since ||
795 /* revisions.c's max_age -1 is special */
796 *deepen_since == -1)
797 die("Invalid deepen-since: %s", line);
798 *deepen_rev_list = 1;
799 return 1;
800 }
801 return 0;
802}
803
804static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
805{
806 const char *arg;
807 if (skip_prefix(line, "deepen-not ", &arg)) {
808 char *ref = NULL;
809 struct object_id oid;
810 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
811 die("git upload-pack: ambiguous deepen-not: %s", line);
812 string_list_append(deepen_not, ref);
813 free(ref);
814 *deepen_rev_list = 1;
815 return 1;
816 }
817 return 0;
569e554b
NTND
818}
819
b1e9fff7 820static void receive_needs(void)
fb9040cc 821{
3cd47459 822 struct object_array shallows = OBJECT_ARRAY_INIT;
269a7a83 823 struct string_list deepen_not = STRING_LIST_INIT_DUP;
74543a04 824 int depth = 0;
051e4005 825 int has_non_tip = 0;
dddbad72 826 timestamp_t deepen_since = 0;
569e554b 827 int deepen_rev_list = 0;
fb9040cc 828
f0cea83f 829 shallow_nr = 0;
fb9040cc 830 for (;;) {
565ebbf7 831 struct object *o;
f47182c8 832 const char *features;
cf93982f 833 struct object_id oid_buf;
74543a04 834 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
835 const char *arg;
836
960deccb 837 reset_timeout();
74543a04 838 if (!line)
ed09aef0 839 break;
e091eb93 840
ae2948f3 841 if (process_shallow(line, &shallows))
ed09aef0 842 continue;
ae2948f3 843 if (process_deepen(line, &depth))
016e6ccb 844 continue;
ae2948f3 845 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
569e554b 846 continue;
ae2948f3 847 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
269a7a83 848 continue;
ae2948f3 849
10ac85c7
JH
850 if (skip_prefix(line, "filter ", &arg)) {
851 if (!filter_capability_requested)
852 die("git upload-pack: filtering capability not negotiated");
853 parse_list_objects_filter(&filter_options, arg);
854 continue;
855 }
9bfa0f9b 856
8bf3b758 857 if (!skip_prefix(line, "want ", &arg) ||
55dc227d 858 parse_oid_hex(arg, &oid_buf, &features))
7e44c935 859 die("git upload-pack: protocol error, "
55dc227d 860 "expected to get object ID, not '%s'", line);
f47182c8 861
cccf74e2
NTND
862 if (parse_feature_request(features, "deepen-relative"))
863 deepen_relative = 1;
f47182c8 864 if (parse_feature_request(features, "multi_ack_detailed"))
78affc49 865 multi_ack = 2;
f47182c8 866 else if (parse_feature_request(features, "multi_ack"))
1bd8c8f0 867 multi_ack = 1;
f47182c8 868 if (parse_feature_request(features, "no-done"))
4e10cf9a 869 no_done = 1;
f47182c8 870 if (parse_feature_request(features, "thin-pack"))
b19696c2 871 use_thin_pack = 1;
f47182c8 872 if (parse_feature_request(features, "ofs-delta"))
e4fe4b8e 873 use_ofs_delta = 1;
f47182c8 874 if (parse_feature_request(features, "side-band-64k"))
d47f3db7 875 use_sideband = LARGE_PACKET_MAX;
f47182c8 876 else if (parse_feature_request(features, "side-band"))
d47f3db7 877 use_sideband = DEFAULT_PACKET_MAX;
f47182c8 878 if (parse_feature_request(features, "no-progress"))
b0e90897 879 no_progress = 1;
f47182c8 880 if (parse_feature_request(features, "include-tag"))
348e390b 881 use_include_tag = 1;
c7620bd0 882 if (allow_filter && parse_feature_request(features, "filter"))
10ac85c7 883 filter_capability_requested = 1;
565ebbf7 884
109cd76d 885 o = parse_object(the_repository, &oid_buf);
bdb31ead
JT
886 if (!o) {
887 packet_write_fmt(1,
888 "ERR upload-pack: not our ref %s",
cf93982f 889 oid_to_hex(&oid_buf));
9f9aa761 890 die("git upload-pack: not our ref %s",
cf93982f 891 oid_to_hex(&oid_buf));
bdb31ead 892 }
565ebbf7
JH
893 if (!(o->flags & WANTED)) {
894 o->flags |= WANTED;
f8edeaa0
DT
895 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
896 || is_our_ref(o)))
051e4005 897 has_non_tip = 1;
b1e9fff7 898 add_object_array(o, NULL, &want_obj);
565ebbf7 899 }
fb9040cc 900 }
9462e3f5 901
051e4005
JH
902 /*
903 * We have sent all our refs already, and the other end
904 * should have chosen out of them. When we are operating
905 * in the stateless RPC mode, however, their choice may
906 * have been based on the set of older refs advertised
907 * by another process that handled the initial request.
908 */
909 if (has_non_tip)
910 check_non_tip();
911
9462e3f5
JS
912 if (!use_sideband && daemon_mode)
913 no_progress = 1;
914
569e554b 915 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
f53514bc 916 return;
569e554b 917
685fbd32
BW
918 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
919 &deepen_not, &shallows))
920 packet_flush(1);
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);
10ac85c7 973 packet_write_fmt(1, "%s %s%c%s%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,
c7620bd0 982 allow_filter ? " filter" : "",
ff5effdf 983 git_user_agent_sanitized());
7171d8c1
JH
984 strbuf_release(&symref_info);
985 } else {
81c634e9 986 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
7171d8c1 987 }
1f5881bb 988 capabilities = NULL;
b420d909 989 if (!peel_ref(refname, &peeled))
81c634e9 990 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
def88e9a
LT
991 return 0;
992}
993
7dabd056
MH
994static int find_symref(const char *refname, const struct object_id *oid,
995 int flag, void *cb_data)
7171d8c1
JH
996{
997 const char *symref_target;
998 struct string_list_item *item;
7171d8c1
JH
999
1000 if ((flag & REF_ISSYMREF) == 0)
1001 return 0;
744c040b 1002 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
7171d8c1
JH
1003 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1004 die("'%s' is a symref but it is not?", refname);
1005 item = string_list_append(cb_data, refname);
1006 item->util = xstrdup(symref_target);
1007 return 0;
1008}
1009
daebaa78
JH
1010static int upload_pack_config(const char *var, const char *value, void *unused)
1011{
7199c093
FM
1012 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1013 if (git_config_bool(var, value))
1014 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1015 else
1016 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
68ee6289
FM
1017 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1018 if (git_config_bool(var, value))
1019 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1020 else
1021 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
f8edeaa0
DT
1022 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1023 if (git_config_bool(var, value))
1024 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1025 else
1026 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
7199c093 1027 } else if (!strcmp("uploadpack.keepalive", var)) {
05e95155
JK
1028 keepalive = git_config_int(var, value);
1029 if (!keepalive)
1030 keepalive = -1;
20b20a22
JK
1031 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1032 if (!strcmp("uploadpack.packobjectshook", var))
1033 return git_config_string(&pack_objects_hook, var, value);
10ac85c7 1034 } else if (!strcmp("uploadpack.allowfilter", var)) {
c7620bd0 1035 allow_filter = git_config_bool(var, value);
516e2b76
BW
1036 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1037 allow_ref_in_want = git_config_bool(var, value);
05e95155 1038 }
daebaa78
JH
1039 return parse_hide_refs_config(var, value, "uploadpack");
1040}
1041
a3d6b53e 1042void upload_pack(struct upload_pack_options *options)
def88e9a 1043{
a3d6b53e 1044 struct string_list symref = STRING_LIST_INIT_DUP;
960deccb 1045
a3d6b53e
BW
1046 stateless_rpc = options->stateless_rpc;
1047 timeout = options->timeout;
1048 daemon_mode = options->daemon_mode;
2fb3f6db 1049
daebaa78 1050 git_config(upload_pack_config, NULL);
960deccb 1051
a3d6b53e 1052 head_ref_namespaced(find_symref, &symref);
a6080a0a 1053
a3d6b53e
BW
1054 if (options->advertise_refs || !stateless_rpc) {
1055 reset_timeout();
1056 head_ref_namespaced(send_ref, &symref);
1057 for_each_namespaced_ref(send_ref, &symref);
1058 advertise_shallow_grafts(1);
1059 packet_flush(1);
1060 } else {
1061 head_ref_namespaced(check_ref, NULL);
1062 for_each_namespaced_ref(check_ref, NULL);
aa9bab29 1063 }
a3d6b53e
BW
1064 string_list_clear(&symref, 1);
1065 if (options->advertise_refs)
1066 return;
04b33055 1067
a3d6b53e
BW
1068 receive_needs();
1069 if (want_obj.nr) {
0b9333ff
JT
1070 struct object_array have_obj = OBJECT_ARRAY_INIT;
1071 get_common_commits(&have_obj);
1072 create_pack_file(&have_obj);
a3d6b53e 1073 }
def88e9a 1074}
04b33055 1075
3145ea95
BW
1076struct upload_pack_data {
1077 struct object_array wants;
516e2b76 1078 struct string_list wanted_refs;
3145ea95 1079 struct oid_array haves;
113b9475 1080
685fbd32
BW
1081 struct object_array shallows;
1082 struct string_list deepen_not;
1083 int depth;
1084 timestamp_t deepen_since;
1085 int deepen_rev_list;
1086 int deepen_relative;
ad491366 1087
3145ea95 1088 unsigned stateless_rpc : 1;
aa9bab29 1089
3145ea95
BW
1090 unsigned use_thin_pack : 1;
1091 unsigned use_ofs_delta : 1;
1092 unsigned no_progress : 1;
1093 unsigned use_include_tag : 1;
1094 unsigned done : 1;
1095};
1096
1097static void upload_pack_data_init(struct upload_pack_data *data)
1098{
1099 struct object_array wants = OBJECT_ARRAY_INIT;
516e2b76 1100 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
3145ea95 1101 struct oid_array haves = OID_ARRAY_INIT;
685fbd32
BW
1102 struct object_array shallows = OBJECT_ARRAY_INIT;
1103 struct string_list deepen_not = STRING_LIST_INIT_DUP;
3145ea95
BW
1104
1105 memset(data, 0, sizeof(*data));
1106 data->wants = wants;
516e2b76 1107 data->wanted_refs = wanted_refs;
3145ea95 1108 data->haves = haves;
685fbd32
BW
1109 data->shallows = shallows;
1110 data->deepen_not = deepen_not;
3145ea95
BW
1111}
1112
1113static void upload_pack_data_clear(struct upload_pack_data *data)
1114{
1115 object_array_clear(&data->wants);
516e2b76 1116 string_list_clear(&data->wanted_refs, 1);
3145ea95 1117 oid_array_clear(&data->haves);
685fbd32
BW
1118 object_array_clear(&data->shallows);
1119 string_list_clear(&data->deepen_not, 0);
3145ea95
BW
1120}
1121
1122static int parse_want(const char *line)
1123{
1124 const char *arg;
1125 if (skip_prefix(line, "want ", &arg)) {
1126 struct object_id oid;
1127 struct object *o;
1128
1129 if (get_oid_hex(arg, &oid))
1130 die("git upload-pack: protocol error, "
1131 "expected to get oid, not '%s'", line);
1132
109cd76d 1133 o = parse_object(the_repository, &oid);
3145ea95
BW
1134 if (!o) {
1135 packet_write_fmt(1,
1136 "ERR upload-pack: not our ref %s",
1137 oid_to_hex(&oid));
1138 die("git upload-pack: not our ref %s",
1139 oid_to_hex(&oid));
1140 }
1141
1142 if (!(o->flags & WANTED)) {
1143 o->flags |= WANTED;
1144 add_object_array(o, NULL, &want_obj);
1145 }
1146
1147 return 1;
1148 }
1149
1150 return 0;
1151}
1152
516e2b76
BW
1153static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1154{
1155 const char *arg;
1156 if (skip_prefix(line, "want-ref ", &arg)) {
1157 struct object_id oid;
1158 struct string_list_item *item;
1159 struct object *o;
1160
1161 if (read_ref(arg, &oid)) {
1162 packet_write_fmt(1, "ERR unknown ref %s", arg);
1163 die("unknown ref %s", arg);
1164 }
1165
1166 item = string_list_append(wanted_refs, arg);
1167 item->util = oiddup(&oid);
1168
1169 o = parse_object_or_die(&oid, arg);
1170 if (!(o->flags & WANTED)) {
1171 o->flags |= WANTED;
1172 add_object_array(o, NULL, &want_obj);
1173 }
1174
1175 return 1;
1176 }
1177
1178 return 0;
1179}
1180
3145ea95
BW
1181static int parse_have(const char *line, struct oid_array *haves)
1182{
1183 const char *arg;
1184 if (skip_prefix(line, "have ", &arg)) {
1185 struct object_id oid;
1186
1187 if (get_oid_hex(arg, &oid))
1188 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1189 oid_array_append(haves, &oid);
1190 return 1;
aa9bab29
BW
1191 }
1192
def88e9a
LT
1193 return 0;
1194}
3145ea95
BW
1195
1196static void process_args(struct packet_reader *request,
1197 struct upload_pack_data *data)
1198{
1199 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1200 const char *arg = request->line;
ba95710a 1201 const char *p;
3145ea95
BW
1202
1203 /* process want */
1204 if (parse_want(arg))
1205 continue;
516e2b76
BW
1206 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1207 continue;
3145ea95
BW
1208 /* process have line */
1209 if (parse_have(arg, &data->haves))
1210 continue;
1211
1212 /* process args like thin-pack */
1213 if (!strcmp(arg, "thin-pack")) {
1214 use_thin_pack = 1;
1215 continue;
1216 }
1217 if (!strcmp(arg, "ofs-delta")) {
1218 use_ofs_delta = 1;
1219 continue;
1220 }
1221 if (!strcmp(arg, "no-progress")) {
1222 no_progress = 1;
1223 continue;
1224 }
1225 if (!strcmp(arg, "include-tag")) {
1226 use_include_tag = 1;
1227 continue;
1228 }
1229 if (!strcmp(arg, "done")) {
1230 data->done = 1;
1231 continue;
1232 }
1233
685fbd32
BW
1234 /* Shallow related arguments */
1235 if (process_shallow(arg, &data->shallows))
1236 continue;
1237 if (process_deepen(arg, &data->depth))
1238 continue;
1239 if (process_deepen_since(arg, &data->deepen_since,
1240 &data->deepen_rev_list))
1241 continue;
1242 if (process_deepen_not(arg, &data->deepen_not,
1243 &data->deepen_rev_list))
1244 continue;
1245 if (!strcmp(arg, "deepen-relative")) {
1246 data->deepen_relative = 1;
1247 continue;
1248 }
1249
ba95710a
JT
1250 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1251 parse_list_objects_filter(&filter_options, p);
1252 continue;
1253 }
1254
3145ea95 1255 /* ignore unknown lines maybe? */
7cc6ed2d 1256 die("unexpected line: '%s'", arg);
3145ea95
BW
1257 }
1258}
1259
0b9333ff
JT
1260static int process_haves(struct oid_array *haves, struct oid_array *common,
1261 struct object_array *have_obj)
3145ea95
BW
1262{
1263 int i;
1264
1265 /* Process haves */
1266 for (i = 0; i < haves->nr; i++) {
1267 const struct object_id *oid = &haves->oid[i];
1268 struct object *o;
1269 int we_knew_they_have = 0;
1270
1271 if (!has_object_file(oid))
1272 continue;
1273
1274 oid_array_append(common, oid);
1275
109cd76d 1276 o = parse_object(the_repository, oid);
3145ea95
BW
1277 if (!o)
1278 die("oops (%s)", oid_to_hex(oid));
1279 if (o->type == OBJ_COMMIT) {
1280 struct commit_list *parents;
1281 struct commit *commit = (struct commit *)o;
1282 if (o->flags & THEY_HAVE)
1283 we_knew_they_have = 1;
1284 else
1285 o->flags |= THEY_HAVE;
1286 if (!oldest_have || (commit->date < oldest_have))
1287 oldest_have = commit->date;
1288 for (parents = commit->parents;
1289 parents;
1290 parents = parents->next)
1291 parents->item->object.flags |= THEY_HAVE;
1292 }
1293 if (!we_knew_they_have)
0b9333ff 1294 add_object_array(o, NULL, have_obj);
3145ea95
BW
1295 }
1296
1297 return 0;
1298}
1299
0b9333ff
JT
1300static int send_acks(struct oid_array *acks, struct strbuf *response,
1301 const struct object_array *have_obj)
3145ea95
BW
1302{
1303 int i;
1304
1305 packet_buf_write(response, "acknowledgments\n");
1306
1307 /* Send Acks */
1308 if (!acks->nr)
1309 packet_buf_write(response, "NAK\n");
1310
1311 for (i = 0; i < acks->nr; i++) {
1312 packet_buf_write(response, "ACK %s\n",
1313 oid_to_hex(&acks->oid[i]));
1314 }
1315
0b9333ff 1316 if (ok_to_give_up(have_obj)) {
3145ea95
BW
1317 /* Send Ready */
1318 packet_buf_write(response, "ready\n");
1319 return 1;
1320 }
1321
1322 return 0;
1323}
1324
0b9333ff
JT
1325static int process_haves_and_send_acks(struct upload_pack_data *data,
1326 struct object_array *have_obj)
3145ea95
BW
1327{
1328 struct oid_array common = OID_ARRAY_INIT;
1329 struct strbuf response = STRBUF_INIT;
1330 int ret = 0;
1331
0b9333ff 1332 process_haves(&data->haves, &common, have_obj);
3145ea95
BW
1333 if (data->done) {
1334 ret = 1;
0b9333ff 1335 } else if (send_acks(&common, &response, have_obj)) {
3145ea95
BW
1336 packet_buf_delim(&response);
1337 ret = 1;
1338 } else {
1339 /* Add Flush */
1340 packet_buf_flush(&response);
1341 ret = 0;
1342 }
1343
1344 /* Send response */
1345 write_or_die(1, response.buf, response.len);
1346 strbuf_release(&response);
1347
1348 oid_array_clear(&data->haves);
1349 oid_array_clear(&common);
1350 return ret;
1351}
1352
516e2b76
BW
1353static void send_wanted_ref_info(struct upload_pack_data *data)
1354{
1355 const struct string_list_item *item;
1356
1357 if (!data->wanted_refs.nr)
1358 return;
1359
1360 packet_write_fmt(1, "wanted-refs\n");
1361
1362 for_each_string_list_item(item, &data->wanted_refs) {
1363 packet_write_fmt(1, "%s %s\n",
1364 oid_to_hex(item->util),
1365 item->string);
1366 }
1367
1368 packet_delim(1);
1369}
1370
685fbd32
BW
1371static void send_shallow_info(struct upload_pack_data *data)
1372{
1373 /* No shallow info needs to be sent */
1374 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
00624d60 1375 !is_repository_shallow(the_repository))
685fbd32
BW
1376 return;
1377
1378 packet_write_fmt(1, "shallow-info\n");
1379
1380 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1381 data->deepen_since, &data->deepen_not,
00624d60
JH
1382 &data->shallows) &&
1383 is_repository_shallow(the_repository))
685fbd32
BW
1384 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1385
1386 packet_delim(1);
1387}
1388
3145ea95
BW
1389enum fetch_state {
1390 FETCH_PROCESS_ARGS = 0,
1391 FETCH_SEND_ACKS,
1392 FETCH_SEND_PACK,
1393 FETCH_DONE,
1394};
1395
1396int upload_pack_v2(struct repository *r, struct argv_array *keys,
1397 struct packet_reader *request)
1398{
1399 enum fetch_state state = FETCH_PROCESS_ARGS;
1400 struct upload_pack_data data;
0b9333ff
JT
1401 /* NEEDSWORK: make this non-static */
1402 static struct object_array have_obj;
3145ea95 1403
54592687
JT
1404 git_config(upload_pack_config, NULL);
1405
3145ea95
BW
1406 upload_pack_data_init(&data);
1407 use_sideband = LARGE_PACKET_MAX;
1408
1409 while (state != FETCH_DONE) {
1410 switch (state) {
1411 case FETCH_PROCESS_ARGS:
1412 process_args(request, &data);
1413
1414 if (!want_obj.nr) {
1415 /*
1416 * Request didn't contain any 'want' lines,
1417 * guess they didn't want anything.
1418 */
1419 state = FETCH_DONE;
1420 } else if (data.haves.nr) {
1421 /*
1422 * Request had 'have' lines, so lets ACK them.
1423 */
1424 state = FETCH_SEND_ACKS;
1425 } else {
1426 /*
1427 * Request had 'want's but no 'have's so we can
1428 * immedietly go to construct and send a pack.
1429 */
1430 state = FETCH_SEND_PACK;
1431 }
1432 break;
1433 case FETCH_SEND_ACKS:
0b9333ff 1434 if (process_haves_and_send_acks(&data, &have_obj))
3145ea95
BW
1435 state = FETCH_SEND_PACK;
1436 else
1437 state = FETCH_DONE;
1438 break;
1439 case FETCH_SEND_PACK:
516e2b76 1440 send_wanted_ref_info(&data);
685fbd32
BW
1441 send_shallow_info(&data);
1442
3145ea95 1443 packet_write_fmt(1, "packfile\n");
0b9333ff 1444 create_pack_file(&have_obj);
3145ea95
BW
1445 state = FETCH_DONE;
1446 break;
1447 case FETCH_DONE:
1448 continue;
1449 }
1450 }
1451
1452 upload_pack_data_clear(&data);
1453 return 0;
1454}
685fbd32
BW
1455
1456int upload_pack_advertise(struct repository *r,
1457 struct strbuf *value)
1458{
ba95710a
JT
1459 if (value) {
1460 int allow_filter_value;
516e2b76
BW
1461 int allow_ref_in_want;
1462
685fbd32 1463 strbuf_addstr(value, "shallow");
516e2b76 1464
ba95710a
JT
1465 if (!repo_config_get_bool(the_repository,
1466 "uploadpack.allowfilter",
1467 &allow_filter_value) &&
1468 allow_filter_value)
1469 strbuf_addstr(value, " filter");
516e2b76
BW
1470
1471 if (!repo_config_get_bool(the_repository,
1472 "uploadpack.allowrefinwant",
1473 &allow_ref_in_want) &&
1474 allow_ref_in_want)
1475 strbuf_addstr(value, " ref-in-want");
ba95710a 1476 }
516e2b76 1477
685fbd32
BW
1478 return 1;
1479}