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