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