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