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