]> git.ipfire.org Git - thirdparty/git.git/blame - upload-pack.c
unpack-trees: release oid_array after use in check_updates()
[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"
f6b42a81
JH
6#include "tag.h"
7#include "object.h"
f0243f26 8#include "commit.h"
77cb17e9 9#include "exec_cmd.h"
9b8dc263
JS
10#include "diff.h"
11#include "revision.h"
12#include "list-objects.h"
10ac85c7
JH
13#include "list-objects-filter.h"
14#include "list-objects-filter-options.h"
cc41fa8d 15#include "run-command.h"
47a59185 16#include "connect.h"
051e4005 17#include "sigchain.h"
ff5effdf 18#include "version.h"
daebaa78 19#include "string-list.h"
9812f213 20#include "parse-options.h"
569e554b 21#include "argv-array.h"
5411b10c 22#include "prio-queue.h"
10ac85c7 23#include "quote.h"
def88e9a 24
9812f213
AQ
25static const char * const upload_pack_usage[] = {
26 N_("git upload-pack [<options>] <dir>"),
27 NULL
28};
def88e9a 29
208acbfb 30/* Remember to update object flag allocation in object.h */
937a515a
JH
31#define THEY_HAVE (1u << 11)
32#define OUR_REF (1u << 12)
33#define WANTED (1u << 13)
34#define COMMON_KNOWN (1u << 14)
35#define REACHABLE (1u << 15)
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
dddbad72 42static timestamp_t oldest_have;
937a515a 43
cccf74e2 44static int deepen_relative;
3f1da57f 45static int multi_ack;
4e10cf9a 46static int no_done;
348e390b 47static int use_thin_pack, use_ofs_delta, use_include_tag;
9462e3f5 48static int no_progress, daemon_mode;
7199c093
FM
49/* Allow specifying sha1 if it is a ref tip. */
50#define ALLOW_TIP_SHA1 01
68ee6289
FM
51/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
52#define ALLOW_REACHABLE_SHA1 02
f8edeaa0
DT
53/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
54#define ALLOW_ANY_SHA1 07
7199c093 55static unsigned int allow_unadvertised_object_request;
f0cea83f 56static int shallow_nr;
b1e9fff7
JH
57static struct object_array have_obj;
58static struct object_array want_obj;
6523078b 59static struct object_array extra_edge_obj;
96f1e58f 60static unsigned int timeout;
115dedd7 61static int keepalive = 5;
d47f3db7
JH
62/* 0 for no sideband,
63 * otherwise maximum packet size (up to 65520 bytes).
64 */
96f1e58f 65static int use_sideband;
42526b47
SP
66static int advertise_refs;
67static int stateless_rpc;
20b20a22 68static const char *pack_objects_hook;
960deccb 69
10ac85c7
JH
70static int filter_capability_requested;
71static int filter_advertise;
72static struct list_objects_filter_options filter_options;
73
960deccb
PA
74static void reset_timeout(void)
75{
76 alarm(timeout);
77}
fb9040cc 78
fcf0fe9e 79static void send_client_data(int fd, const char *data, ssize_t sz)
583b7ea3 80{
4c4b7d1d
LF
81 if (use_sideband) {
82 send_sideband(1, fd, data, sz, use_sideband);
fcf0fe9e 83 return;
4c4b7d1d 84 }
958c24b1
JH
85 if (fd == 3)
86 /* emergency quit */
87 fd = 2;
88 if (fd == 2) {
93822c22 89 /* XXX: are we happy to lose stuff here? */
958c24b1 90 xwrite(fd, data, sz);
fcf0fe9e 91 return;
583b7ea3 92 }
cdf4fb8e 93 write_or_die(fd, data, sz);
583b7ea3
JH
94}
95
b790e0f6
NTND
96static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
97{
98 FILE *fp = cb_data;
99 if (graft->nr_parent == -1)
7683e2e6 100 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
b790e0f6
NTND
101 return 0;
102}
103
fb9040cc
LT
104static void create_pack_file(void)
105{
d3180279 106 struct child_process pack_objects = CHILD_PROCESS_INIT;
363b7817 107 char data[8193], progress[128];
583b7ea3
JH
108 char abort_msg[] = "aborting due to possible repository "
109 "corruption on the remote side.";
b1c71b72 110 int buffered = -1;
1456b043 111 ssize_t sz;
65a3629e 112 int i;
cdab4858 113 FILE *pipe_fd;
75bfc6c2 114
20b20a22
JK
115 if (!pack_objects_hook)
116 pack_objects.git_cmd = 1;
117 else {
118 argv_array_push(&pack_objects.args, pack_objects_hook);
119 argv_array_push(&pack_objects.args, "git");
120 pack_objects.use_shell = 1;
121 }
122
cdab4858 123 if (shallow_nr) {
65a3629e
MP
124 argv_array_push(&pack_objects.args, "--shallow-file");
125 argv_array_push(&pack_objects.args, "");
f0cea83f 126 }
65a3629e
MP
127 argv_array_push(&pack_objects.args, "pack-objects");
128 argv_array_push(&pack_objects.args, "--revs");
cdab4858 129 if (use_thin_pack)
65a3629e 130 argv_array_push(&pack_objects.args, "--thin");
75bfc6c2 131
65a3629e 132 argv_array_push(&pack_objects.args, "--stdout");
2dacf26d 133 if (shallow_nr)
65a3629e 134 argv_array_push(&pack_objects.args, "--shallow");
cc41fa8d 135 if (!no_progress)
65a3629e 136 argv_array_push(&pack_objects.args, "--progress");
cc41fa8d 137 if (use_ofs_delta)
65a3629e 138 argv_array_push(&pack_objects.args, "--delta-base-offset");
348e390b 139 if (use_include_tag)
65a3629e 140 argv_array_push(&pack_objects.args, "--include-tag");
10ac85c7 141 if (filter_options.filter_spec) {
0b6069fe
JT
142 if (pack_objects.use_shell) {
143 struct strbuf buf = STRBUF_INIT;
144 sq_quote_buf(&buf, filter_options.filter_spec);
145 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
146 strbuf_release(&buf);
147 } else {
148 argv_array_pushf(&pack_objects.args, "--filter=%s",
149 filter_options.filter_spec);
150 }
10ac85c7 151 }
cc41fa8d 152
b9612197 153 pack_objects.in = -1;
cc41fa8d
JS
154 pack_objects.out = -1;
155 pack_objects.err = -1;
21edd3f1 156
4c324c00 157 if (start_command(&pack_objects))
7e44c935 158 die("git upload-pack: unable to fork git-pack-objects");
b1c71b72 159
cdab4858
NTND
160 pipe_fd = xfdopen(pack_objects.in, "w");
161
b790e0f6
NTND
162 if (shallow_nr)
163 for_each_commit_graft(write_one_shallow, pipe_fd);
164
cdab4858
NTND
165 for (i = 0; i < want_obj.nr; i++)
166 fprintf(pipe_fd, "%s\n",
f2fd0760 167 oid_to_hex(&want_obj.objects[i].item->oid));
cdab4858
NTND
168 fprintf(pipe_fd, "--not\n");
169 for (i = 0; i < have_obj.nr; i++)
170 fprintf(pipe_fd, "%s\n",
f2fd0760 171 oid_to_hex(&have_obj.objects[i].item->oid));
cdab4858
NTND
172 for (i = 0; i < extra_edge_obj.nr; i++)
173 fprintf(pipe_fd, "%s\n",
f2fd0760 174 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
cdab4858
NTND
175 fprintf(pipe_fd, "\n");
176 fflush(pipe_fd);
177 fclose(pipe_fd);
f0cea83f 178
cc41fa8d
JS
179 /* We read from pack_objects.err to capture stderr output for
180 * progress bar, and pack_objects.out to capture the pack data.
b1c71b72 181 */
b1c71b72
JH
182
183 while (1) {
b1c71b72 184 struct pollfd pfd[2];
363b7817 185 int pe, pu, pollsize;
05e95155 186 int ret;
b1c71b72 187
0d516ada
ML
188 reset_timeout();
189
b1c71b72 190 pollsize = 0;
363b7817 191 pe = pu = -1;
b1c71b72 192
cc41fa8d
JS
193 if (0 <= pack_objects.out) {
194 pfd[pollsize].fd = pack_objects.out;
b1c71b72
JH
195 pfd[pollsize].events = POLLIN;
196 pu = pollsize;
197 pollsize++;
198 }
cc41fa8d
JS
199 if (0 <= pack_objects.err) {
200 pfd[pollsize].fd = pack_objects.err;
363b7817
JH
201 pfd[pollsize].events = POLLIN;
202 pe = pollsize;
203 pollsize++;
204 }
b1c71b72 205
4c324c00
JS
206 if (!pollsize)
207 break;
208
6c71f8b0
ET
209 ret = poll(pfd, pollsize,
210 keepalive < 0 ? -1 : 1000 * keepalive);
211
05e95155 212 if (ret < 0) {
4c324c00 213 if (errno != EINTR) {
d2b6afa2 214 error_errno("poll failed, resuming");
4c324c00 215 sleep(1);
b1c71b72 216 }
4c324c00
JS
217 continue;
218 }
6b59f51b
NP
219 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
220 /* Status ready; we ship that in the side-band
221 * or dump to the standard error.
222 */
223 sz = xread(pack_objects.err, progress,
224 sizeof(progress));
225 if (0 < sz)
226 send_client_data(2, progress, sz);
227 else if (sz == 0) {
228 close(pack_objects.err);
229 pack_objects.err = -1;
230 }
231 else
232 goto fail;
233 /* give priority to status messages */
234 continue;
235 }
4c324c00
JS
236 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
237 /* Data ready; we keep the last byte to ourselves
238 * in case we detect broken rev-list, so that we
239 * can leave the stream corrupted. This is
240 * unfortunate -- unpack-objects would happily
241 * accept a valid packdata with trailing garbage,
242 * so appending garbage after we pass all the
243 * pack data is not good enough to signal
244 * breakage to downstream.
245 */
246 char *cp = data;
247 ssize_t outsz = 0;
248 if (0 <= buffered) {
249 *cp++ = buffered;
250 outsz++;
b1c71b72 251 }
4c324c00
JS
252 sz = xread(pack_objects.out, cp,
253 sizeof(data) - outsz);
254 if (0 < sz)
1456b043 255 ;
4c324c00
JS
256 else if (sz == 0) {
257 close(pack_objects.out);
258 pack_objects.out = -1;
363b7817 259 }
4c324c00
JS
260 else
261 goto fail;
262 sz += outsz;
263 if (1 < sz) {
264 buffered = data[sz-1] & 0xFF;
265 sz--;
b1c71b72 266 }
4c324c00
JS
267 else
268 buffered = -1;
fcf0fe9e 269 send_client_data(1, data, sz);
4c324c00 270 }
05e95155
JK
271
272 /*
273 * We hit the keepalive timeout without saying anything; send
274 * an empty message on the data sideband just to let the other
275 * side know we're still working on it, but don't have any data
276 * yet.
277 *
278 * If we don't have a sideband channel, there's no room in the
279 * protocol to say anything, so those clients are just out of
280 * luck.
281 */
282 if (!ret && use_sideband) {
283 static const char buf[] = "0005\1";
284 write_or_die(1, buf, 5);
285 }
4c324c00 286 }
b1c71b72 287
4c324c00 288 if (finish_command(&pack_objects)) {
7e44c935 289 error("git upload-pack: git-pack-objects died with error.");
4c324c00
JS
290 goto fail;
291 }
b1c71b72 292
4c324c00
JS
293 /* flush the data */
294 if (0 <= buffered) {
295 data[0] = buffered;
fcf0fe9e 296 send_client_data(1, data, 1);
4c324c00 297 fprintf(stderr, "flushed.\n");
b1c71b72 298 }
4c324c00
JS
299 if (use_sideband)
300 packet_flush(1);
301 return;
302
b1c71b72 303 fail:
583b7ea3 304 send_client_data(3, abort_msg, sizeof(abort_msg));
7e44c935 305 die("git upload-pack: %s", abort_msg);
fb9040cc
LT
306}
307
cf93982f 308static int got_oid(const char *hex, struct object_id *oid)
def88e9a 309{
b1e9fff7 310 struct object *o;
937a515a 311 int we_knew_they_have = 0;
b1e9fff7 312
cf93982f 313 if (get_oid_hex(hex, oid))
7e44c935 314 die("git upload-pack: expected SHA1 object, got '%s'", hex);
cf93982f 315 if (!has_object_file(oid))
937a515a 316 return -1;
b1e9fff7 317
c251c83d 318 o = parse_object(oid);
b1e9fff7 319 if (!o)
cf93982f 320 die("oops (%s)", oid_to_hex(oid));
182a8dab 321 if (o->type == OBJ_COMMIT) {
b1e9fff7 322 struct commit_list *parents;
937a515a 323 struct commit *commit = (struct commit *)o;
b1e9fff7 324 if (o->flags & THEY_HAVE)
937a515a
JH
325 we_knew_they_have = 1;
326 else
327 o->flags |= THEY_HAVE;
328 if (!oldest_have || (commit->date < oldest_have))
329 oldest_have = commit->date;
330 for (parents = commit->parents;
b1e9fff7
JH
331 parents;
332 parents = parents->next)
333 parents->item->object.flags |= THEY_HAVE;
fb9040cc 334 }
937a515a
JH
335 if (!we_knew_they_have) {
336 add_object_array(o, NULL, &have_obj);
337 return 1;
338 }
339 return 0;
340}
341
342static int reachable(struct commit *want)
343{
5411b10c 344 struct prio_queue work = { compare_commits_by_commit_date };
937a515a 345
5411b10c
JK
346 prio_queue_put(&work, want);
347 while (work.nr) {
e510ab89 348 struct commit_list *list;
5411b10c 349 struct commit *commit = prio_queue_get(&work);
937a515a
JH
350
351 if (commit->object.flags & THEY_HAVE) {
352 want->object.flags |= COMMON_KNOWN;
353 break;
354 }
355 if (!commit->object.parsed)
c251c83d 356 parse_object(&commit->object.oid);
937a515a
JH
357 if (commit->object.flags & REACHABLE)
358 continue;
359 commit->object.flags |= REACHABLE;
360 if (commit->date < oldest_have)
361 continue;
362 for (list = commit->parents; list; list = list->next) {
363 struct commit *parent = list->item;
364 if (!(parent->object.flags & REACHABLE))
5411b10c 365 prio_queue_put(&work, parent);
937a515a
JH
366 }
367 }
368 want->object.flags |= REACHABLE;
369 clear_commit_marks(want, REACHABLE);
5411b10c 370 clear_prio_queue(&work);
937a515a
JH
371 return (want->object.flags & COMMON_KNOWN);
372}
373
374static int ok_to_give_up(void)
375{
376 int i;
377
378 if (!have_obj.nr)
379 return 0;
380
381 for (i = 0; i < want_obj.nr; i++) {
382 struct object *want = want_obj.objects[i].item;
383
384 if (want->flags & COMMON_KNOWN)
385 continue;
386 want = deref_tag(want, "a want line", 0);
387 if (!want || want->type != OBJ_COMMIT) {
388 /* no way to tell if this is reachable by
389 * looking at the ancestry chain alone, so
390 * leave a note to ourselves not to worry about
391 * this object anymore.
392 */
393 want_obj.objects[i].item->flags |= COMMON_KNOWN;
394 continue;
395 }
396 if (!reachable((struct commit *)want))
397 return 0;
398 }
fb9040cc 399 return 1;
def88e9a
LT
400}
401
402static int get_common_commits(void)
403{
cf93982f 404 struct object_id oid;
405 char last_hex[GIT_MAX_HEXSZ + 1];
49bee717
SP
406 int got_common = 0;
407 int got_other = 0;
4e10cf9a 408 int sent_ready = 0;
def88e9a 409
f0243f26
JS
410 save_commit_buffer = 0;
411
eeefa7c9 412 for (;;) {
74543a04 413 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
414 const char *arg;
415
960deccb 416 reset_timeout();
def88e9a 417
74543a04 418 if (!line) {
49bee717 419 if (multi_ack == 2 && got_common
4e10cf9a
JH
420 && !got_other && ok_to_give_up()) {
421 sent_ready = 1;
81c634e9 422 packet_write_fmt(1, "ACK %s ready\n", last_hex);
4e10cf9a 423 }
b1e9fff7 424 if (have_obj.nr == 0 || multi_ack)
81c634e9 425 packet_write_fmt(1, "NAK\n");
4e10cf9a
JH
426
427 if (no_done && sent_ready) {
81c634e9 428 packet_write_fmt(1, "ACK %s\n", last_hex);
4e10cf9a
JH
429 return 0;
430 }
42526b47
SP
431 if (stateless_rpc)
432 exit(0);
49bee717
SP
433 got_common = 0;
434 got_other = 0;
def88e9a
LT
435 continue;
436 }
8bf3b758 437 if (skip_prefix(line, "have ", &arg)) {
cf93982f 438 switch (got_oid(arg, &oid)) {
937a515a 439 case -1: /* they have what we do not */
49bee717 440 got_other = 1;
78affc49 441 if (multi_ack && ok_to_give_up()) {
cf93982f 442 const char *hex = oid_to_hex(&oid);
4e10cf9a
JH
443 if (multi_ack == 2) {
444 sent_ready = 1;
81c634e9 445 packet_write_fmt(1, "ACK %s ready\n", hex);
4e10cf9a 446 } else
81c634e9 447 packet_write_fmt(1, "ACK %s continue\n", hex);
78affc49 448 }
937a515a
JH
449 break;
450 default:
49bee717 451 got_common = 1;
cf93982f 452 memcpy(last_hex, oid_to_hex(&oid), 41);
78affc49 453 if (multi_ack == 2)
81c634e9 454 packet_write_fmt(1, "ACK %s common\n", last_hex);
78affc49 455 else if (multi_ack)
81c634e9 456 packet_write_fmt(1, "ACK %s continue\n", last_hex);
c04c4e57 457 else if (have_obj.nr == 1)
81c634e9 458 packet_write_fmt(1, "ACK %s\n", last_hex);
937a515a 459 break;
af2d3aa4 460 }
def88e9a
LT
461 continue;
462 }
463 if (!strcmp(line, "done")) {
b1e9fff7 464 if (have_obj.nr > 0) {
1bd8c8f0 465 if (multi_ack)
81c634e9 466 packet_write_fmt(1, "ACK %s\n", last_hex);
1bd8c8f0
JS
467 return 0;
468 }
81c634e9 469 packet_write_fmt(1, "NAK\n");
def88e9a
LT
470 return -1;
471 }
7e44c935 472 die("git upload-pack: expected SHA1 list, got '%s'", line);
def88e9a 473 }
def88e9a
LT
474}
475
390eb36b
JH
476static int is_our_ref(struct object *o)
477{
68ee6289
FM
478 int allow_hidden_ref = (allow_unadvertised_object_request &
479 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
7199c093 480 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
390eb36b
JH
481}
482
2997178e
NTND
483/*
484 * on successful case, it's up to the caller to close cmd->out
485 */
486static int do_reachable_revlist(struct child_process *cmd,
079aa97e
NTND
487 struct object_array *src,
488 struct object_array *reachable)
051e4005
JH
489{
490 static const char *argv[] = {
491 "rev-list", "--stdin", NULL,
492 };
051e4005
JH
493 struct object *o;
494 char namebuf[42]; /* ^ + SHA-1 + LF */
495 int i;
496
2997178e
NTND
497 cmd->argv = argv;
498 cmd->git_cmd = 1;
499 cmd->no_stderr = 1;
500 cmd->in = -1;
501 cmd->out = -1;
051e4005
JH
502
503 /*
7fcbd37f
NTND
504 * If the next rev-list --stdin encounters an unknown commit,
505 * it terminates, which will cause SIGPIPE in the write loop
051e4005
JH
506 * below.
507 */
508 sigchain_push(SIGPIPE, SIG_IGN);
509
2997178e 510 if (start_command(cmd))
7fcbd37f
NTND
511 goto error;
512
051e4005 513 namebuf[0] = '^';
cf93982f 514 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
051e4005
JH
515 for (i = get_max_object_index(); 0 < i; ) {
516 o = get_indexed_object(--i);
2a745324
BH
517 if (!o)
518 continue;
079aa97e
NTND
519 if (reachable && o->type == OBJ_COMMIT)
520 o->flags &= ~TMP_MARK;
390eb36b 521 if (!is_our_ref(o))
051e4005 522 continue;
f2fd0760 523 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
cf93982f 524 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
051e4005
JH
525 goto error;
526 }
cf93982f 527 namebuf[GIT_SHA1_HEXSZ] = '\n';
3f0f6624
NTND
528 for (i = 0; i < src->nr; i++) {
529 o = src->objects[i].item;
079aa97e
NTND
530 if (is_our_ref(o)) {
531 if (reachable)
532 add_object_array(o, NULL, reachable);
051e4005 533 continue;
079aa97e
NTND
534 }
535 if (reachable && o->type == OBJ_COMMIT)
536 o->flags |= TMP_MARK;
f2fd0760 537 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
cf93982f 538 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
051e4005
JH
539 goto error;
540 }
2997178e
NTND
541 close(cmd->in);
542 cmd->in = -1;
543 sigchain_pop(SIGPIPE);
051e4005 544
2997178e
NTND
545 return 0;
546
547error:
051e4005
JH
548 sigchain_pop(SIGPIPE);
549
2997178e
NTND
550 if (cmd->in >= 0)
551 close(cmd->in);
552 if (cmd->out >= 0)
553 close(cmd->out);
554 return -1;
555}
556
079aa97e
NTND
557static int get_reachable_list(struct object_array *src,
558 struct object_array *reachable)
559{
560 struct child_process cmd = CHILD_PROCESS_INIT;
561 int i;
562 struct object *o;
563 char namebuf[42]; /* ^ + SHA-1 + LF */
564
565 if (do_reachable_revlist(&cmd, src, reachable) < 0)
566 return -1;
567
568 while ((i = read_in_full(cmd.out, namebuf, 41)) == 41) {
569 struct object_id sha1;
570
571 if (namebuf[40] != '\n' || get_oid_hex(namebuf, &sha1))
572 break;
573
574 o = lookup_object(sha1.hash);
575 if (o && o->type == OBJ_COMMIT) {
576 o->flags &= ~TMP_MARK;
577 }
578 }
579 for (i = get_max_object_index(); 0 < i; i--) {
580 o = get_indexed_object(i - 1);
581 if (o && o->type == OBJ_COMMIT &&
582 (o->flags & TMP_MARK)) {
583 add_object_array(o, NULL, reachable);
584 o->flags &= ~TMP_MARK;
585 }
586 }
587 close(cmd.out);
588
589 if (finish_command(&cmd))
590 return -1;
591
592 return 0;
593}
594
2997178e
NTND
595static int has_unreachable(struct object_array *src)
596{
597 struct child_process cmd = CHILD_PROCESS_INIT;
598 char buf[1];
599 int i;
600
079aa97e 601 if (do_reachable_revlist(&cmd, src, NULL) < 0)
2997178e 602 return 1;
051e4005
JH
603
604 /*
605 * The commits out of the rev-list are not ancestors of
606 * our ref.
607 */
2997178e 608 i = read_in_full(cmd.out, buf, 1);
051e4005
JH
609 if (i)
610 goto error;
611 close(cmd.out);
7fcbd37f 612 cmd.out = -1;
051e4005
JH
613
614 /*
615 * rev-list may have died by encountering a bad commit
616 * in the history, in which case we do want to bail out
617 * even when it showed no commit.
618 */
619 if (finish_command(&cmd))
620 goto error;
621
622 /* All the non-tip ones are ancestors of what we advertised */
3f0f6624 623 return 0;
051e4005
JH
624
625error:
7fcbd37f 626 sigchain_pop(SIGPIPE);
7fcbd37f
NTND
627 if (cmd.out >= 0)
628 close(cmd.out);
3f0f6624
NTND
629 return 1;
630}
7fcbd37f 631
3f0f6624
NTND
632static void check_non_tip(void)
633{
634 int i;
635
636 /*
637 * In the normal in-process case without
638 * uploadpack.allowReachableSHA1InWant,
639 * non-tip requests can never happen.
640 */
641 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
642 goto error;
643 if (!has_unreachable(&want_obj))
644 /* All the non-tip ones are ancestors of what we advertised */
645 return;
051e4005
JH
646
647error:
648 /* Pick one of them (we know there at least is one) */
649 for (i = 0; i < want_obj.nr; i++) {
3f0f6624 650 struct object *o = want_obj.objects[i].item;
390eb36b 651 if (!is_our_ref(o))
051e4005 652 die("git upload-pack: not our ref %s",
f2fd0760 653 oid_to_hex(&o->oid));
051e4005
JH
654 }
655}
656
5c24cdea
NTND
657static void send_shallow(struct commit_list *result)
658{
659 while (result) {
660 struct object *object = &result->item->object;
661 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
dbaa6bdc
JH
662 packet_write_fmt(1, "shallow %s",
663 oid_to_hex(&object->oid));
e92b848c 664 register_shallow(&object->oid);
5c24cdea
NTND
665 shallow_nr++;
666 }
667 result = result->next;
668 }
669}
670
873700c9 671static void send_unshallow(const struct object_array *shallows)
e8e44de7 672{
e8e44de7 673 int i;
873700c9 674
e8e44de7
NTND
675 for (i = 0; i < shallows->nr; i++) {
676 struct object *object = shallows->objects[i].item;
677 if (object->flags & NOT_SHALLOW) {
678 struct commit_list *parents;
dbaa6bdc
JH
679 packet_write_fmt(1, "unshallow %s",
680 oid_to_hex(&object->oid));
e8e44de7 681 object->flags &= ~CLIENT_SHALLOW;
873700c9
NTND
682 /*
683 * We want to _register_ "object" as shallow, but we
684 * also need to traverse object's parents to deepen a
685 * shallow clone. Unregister it for now so we can
686 * parse and add the parents to the want list, then
687 * re-register it.
688 */
e92b848c 689 unregister_shallow(&object->oid);
e8e44de7
NTND
690 object->parsed = 0;
691 parse_commit_or_die((struct commit *)object);
692 parents = ((struct commit *)object)->parents;
693 while (parents) {
694 add_object_array(&parents->item->object,
695 NULL, &want_obj);
696 parents = parents->next;
697 }
698 add_object_array(object, NULL, &extra_edge_obj);
699 }
700 /* make sure commit traversal conforms to client */
e92b848c 701 register_shallow(&object->oid);
e8e44de7 702 }
873700c9
NTND
703}
704
cccf74e2
NTND
705static void deepen(int depth, int deepen_relative,
706 struct object_array *shallows)
873700c9
NTND
707{
708 if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
709 int i;
710
711 for (i = 0; i < shallows->nr; i++) {
712 struct object *object = shallows->objects[i].item;
713 object->flags |= NOT_SHALLOW;
714 }
cccf74e2
NTND
715 } else if (deepen_relative) {
716 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
717 struct commit_list *result;
718
719 get_reachable_list(shallows, &reachable_shallows);
720 result = get_shallow_commits(&reachable_shallows,
721 depth + 1,
722 SHALLOW, NOT_SHALLOW);
723 send_shallow(result);
724 free_commit_list(result);
725 object_array_clear(&reachable_shallows);
873700c9
NTND
726 } else {
727 struct commit_list *result;
728
729 result = get_shallow_commits(&want_obj, depth,
730 SHALLOW, NOT_SHALLOW);
731 send_shallow(result);
732 free_commit_list(result);
733 }
734
735 send_unshallow(shallows);
e8e44de7
NTND
736 packet_flush(1);
737}
738
569e554b
NTND
739static void deepen_by_rev_list(int ac, const char **av,
740 struct object_array *shallows)
741{
742 struct commit_list *result;
743
744 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
745 send_shallow(result);
746 free_commit_list(result);
747 send_unshallow(shallows);
748 packet_flush(1);
749}
750
b1e9fff7 751static void receive_needs(void)
fb9040cc 752{
3cd47459 753 struct object_array shallows = OBJECT_ARRAY_INIT;
269a7a83 754 struct string_list deepen_not = STRING_LIST_INIT_DUP;
74543a04 755 int depth = 0;
051e4005 756 int has_non_tip = 0;
dddbad72 757 timestamp_t deepen_since = 0;
569e554b 758 int deepen_rev_list = 0;
fb9040cc 759
f0cea83f 760 shallow_nr = 0;
fb9040cc 761 for (;;) {
565ebbf7 762 struct object *o;
f47182c8 763 const char *features;
cf93982f 764 struct object_id oid_buf;
74543a04 765 char *line = packet_read_line(0, NULL);
8bf3b758
NTND
766 const char *arg;
767
960deccb 768 reset_timeout();
74543a04 769 if (!line)
ed09aef0 770 break;
e091eb93 771
8bf3b758 772 if (skip_prefix(line, "shallow ", &arg)) {
cf93982f 773 struct object_id oid;
ed09aef0 774 struct object *object;
cf93982f 775 if (get_oid_hex(arg, &oid))
ed09aef0 776 die("invalid shallow line: %s", line);
c251c83d 777 object = parse_object(&oid);
ed09aef0 778 if (!object)
af04fa2a 779 continue;
6293ded3 780 if (object->type != OBJ_COMMIT)
cf93982f 781 die("invalid shallow object %s", oid_to_hex(&oid));
e58e57e4
JK
782 if (!(object->flags & CLIENT_SHALLOW)) {
783 object->flags |= CLIENT_SHALLOW;
784 add_object_array(object, NULL, &shallows);
785 }
ed09aef0
JS
786 continue;
787 }
8bf3b758 788 if (skip_prefix(line, "deepen ", &arg)) {
6e414e30 789 char *end = NULL;
8bf3b758 790 depth = strtol(arg, &end, 0);
6e414e30 791 if (!end || *end || depth <= 0)
016e6ccb
JS
792 die("Invalid deepen: %s", line);
793 continue;
794 }
569e554b
NTND
795 if (skip_prefix(line, "deepen-since ", &arg)) {
796 char *end = NULL;
1aeb7e75 797 deepen_since = parse_timestamp(arg, &end, 0);
569e554b
NTND
798 if (!end || *end || !deepen_since ||
799 /* revisions.c's max_age -1 is special */
800 deepen_since == -1)
801 die("Invalid deepen-since: %s", line);
802 deepen_rev_list = 1;
803 continue;
804 }
269a7a83
NTND
805 if (skip_prefix(line, "deepen-not ", &arg)) {
806 char *ref = NULL;
cf93982f 807 struct object_id oid;
808 if (expand_ref(arg, strlen(arg), oid.hash, &ref) != 1)
269a7a83
NTND
809 die("git upload-pack: ambiguous deepen-not: %s", line);
810 string_list_append(&deepen_not, ref);
811 free(ref);
812 deepen_rev_list = 1;
813 continue;
814 }
10ac85c7
JH
815 if (skip_prefix(line, "filter ", &arg)) {
816 if (!filter_capability_requested)
817 die("git upload-pack: filtering capability not negotiated");
818 parse_list_objects_filter(&filter_options, arg);
819 continue;
820 }
8bf3b758 821 if (!skip_prefix(line, "want ", &arg) ||
cf93982f 822 get_oid_hex(arg, &oid_buf))
7e44c935 823 die("git upload-pack: protocol error, "
e091eb93 824 "expected to get sha, not '%s'", line);
f47182c8 825
8bf3b758 826 features = arg + 40;
f47182c8 827
cccf74e2
NTND
828 if (parse_feature_request(features, "deepen-relative"))
829 deepen_relative = 1;
f47182c8 830 if (parse_feature_request(features, "multi_ack_detailed"))
78affc49 831 multi_ack = 2;
f47182c8 832 else if (parse_feature_request(features, "multi_ack"))
1bd8c8f0 833 multi_ack = 1;
f47182c8 834 if (parse_feature_request(features, "no-done"))
4e10cf9a 835 no_done = 1;
f47182c8 836 if (parse_feature_request(features, "thin-pack"))
b19696c2 837 use_thin_pack = 1;
f47182c8 838 if (parse_feature_request(features, "ofs-delta"))
e4fe4b8e 839 use_ofs_delta = 1;
f47182c8 840 if (parse_feature_request(features, "side-band-64k"))
d47f3db7 841 use_sideband = LARGE_PACKET_MAX;
f47182c8 842 else if (parse_feature_request(features, "side-band"))
d47f3db7 843 use_sideband = DEFAULT_PACKET_MAX;
f47182c8 844 if (parse_feature_request(features, "no-progress"))
b0e90897 845 no_progress = 1;
f47182c8 846 if (parse_feature_request(features, "include-tag"))
348e390b 847 use_include_tag = 1;
10ac85c7
JH
848 if (parse_feature_request(features, "filter"))
849 filter_capability_requested = 1;
565ebbf7 850
c251c83d 851 o = parse_object(&oid_buf);
bdb31ead
JT
852 if (!o) {
853 packet_write_fmt(1,
854 "ERR upload-pack: not our ref %s",
cf93982f 855 oid_to_hex(&oid_buf));
9f9aa761 856 die("git upload-pack: not our ref %s",
cf93982f 857 oid_to_hex(&oid_buf));
bdb31ead 858 }
565ebbf7
JH
859 if (!(o->flags & WANTED)) {
860 o->flags |= WANTED;
f8edeaa0
DT
861 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
862 || is_our_ref(o)))
051e4005 863 has_non_tip = 1;
b1e9fff7 864 add_object_array(o, NULL, &want_obj);
565ebbf7 865 }
fb9040cc 866 }
9462e3f5 867
051e4005
JH
868 /*
869 * We have sent all our refs already, and the other end
870 * should have chosen out of them. When we are operating
871 * in the stateless RPC mode, however, their choice may
872 * have been based on the set of older refs advertised
873 * by another process that handled the initial request.
874 */
875 if (has_non_tip)
876 check_non_tip();
877
9462e3f5
JS
878 if (!use_sideband && daemon_mode)
879 no_progress = 1;
880
569e554b 881 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
f53514bc 882 return;
569e554b 883 if (depth > 0 && deepen_rev_list)
269a7a83 884 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
e8e44de7 885 if (depth > 0)
cccf74e2 886 deepen(depth, deepen_relative, &shallows);
569e554b
NTND
887 else if (deepen_rev_list) {
888 struct argv_array av = ARGV_ARRAY_INIT;
f53514bc 889 int i;
569e554b
NTND
890
891 argv_array_push(&av, "rev-list");
892 if (deepen_since)
cb71f8bd 893 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
269a7a83
NTND
894 if (deepen_not.nr) {
895 argv_array_push(&av, "--not");
896 for (i = 0; i < deepen_not.nr; i++) {
897 struct string_list_item *s = deepen_not.items + i;
898 argv_array_push(&av, s->string);
f53514bc 899 }
269a7a83 900 argv_array_push(&av, "--not");
016e6ccb 901 }
569e554b
NTND
902 for (i = 0; i < want_obj.nr; i++) {
903 struct object *o = want_obj.objects[i].item;
904 argv_array_push(&av, oid_to_hex(&o->oid));
f53514bc 905 }
569e554b
NTND
906 deepen_by_rev_list(av.argc, av.argv, &shallows);
907 argv_array_clear(&av);
908 }
e8e44de7 909 else
f53514bc
JS
910 if (shallows.nr > 0) {
911 int i;
912 for (i = 0; i < shallows.nr; i++)
e92b848c 913 register_shallow(&shallows.objects[i].item->oid);
f53514bc 914 }
f0cea83f
NE
915
916 shallow_nr += shallows.nr;
dcb572ab 917 object_array_clear(&shallows);
fb9040cc
LT
918}
919
daebaa78 920/* return non-zero if the ref is hidden, otherwise 0 */
78a766ab
LF
921static int mark_our_ref(const char *refname, const char *refname_full,
922 const struct object_id *oid)
cbbe50db 923{
363e98bf 924 struct object *o = lookup_unknown_object(oid->hash);
daebaa78 925
78a766ab 926 if (ref_is_hidden(refname, refname_full)) {
390eb36b 927 o->flags |= HIDDEN_REF;
daebaa78 928 return 1;
390eb36b 929 }
3f1da57f 930 o->flags |= OUR_REF;
cbbe50db
JH
931 return 0;
932}
933
78a766ab 934static int check_ref(const char *refname_full, const struct object_id *oid,
363e98bf 935 int flag, void *cb_data)
e172755b 936{
78a766ab
LF
937 const char *refname = strip_namespace(refname_full);
938
939 mark_our_ref(refname, refname_full, oid);
e172755b
JK
940 return 0;
941}
942
7171d8c1
JH
943static void format_symref_info(struct strbuf *buf, struct string_list *symref)
944{
945 struct string_list_item *item;
946
947 if (!symref->nr)
948 return;
949 for_each_string_list_item(item, symref)
950 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
951}
952
363e98bf
MH
953static int send_ref(const char *refname, const struct object_id *oid,
954 int flag, void *cb_data)
def88e9a 955{
ed09aef0 956 static const char *capabilities = "multi_ack thin-pack side-band"
cccf74e2
NTND
957 " side-band-64k ofs-delta shallow deepen-since deepen-not"
958 " deepen-relative no-progress include-tag multi_ack_detailed";
6b01ecfe 959 const char *refname_nons = strip_namespace(refname);
21758aff 960 struct object_id peeled;
b5b16990 961
78a766ab 962 if (mark_our_ref(refname_nons, refname, oid))
daebaa78 963 return 0;
cbbe50db 964
7171d8c1
JH
965 if (capabilities) {
966 struct strbuf symref_info = STRBUF_INIT;
967
968 format_symref_info(&symref_info, cb_data);
10ac85c7 969 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
363e98bf 970 oid_to_hex(oid), refname_nons,
cf2ad8e6 971 0, capabilities,
7199c093
FM
972 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
973 " allow-tip-sha1-in-want" : "",
68ee6289
FM
974 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
975 " allow-reachable-sha1-in-want" : "",
ff5effdf 976 stateless_rpc ? " no-done" : "",
7171d8c1 977 symref_info.buf,
10ac85c7 978 filter_advertise ? " filter" : "",
ff5effdf 979 git_user_agent_sanitized());
7171d8c1
JH
980 strbuf_release(&symref_info);
981 } else {
81c634e9 982 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
7171d8c1 983 }
1f5881bb 984 capabilities = NULL;
21758aff 985 if (!peel_ref(refname, peeled.hash))
81c634e9 986 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
def88e9a
LT
987 return 0;
988}
989
7dabd056
MH
990static int find_symref(const char *refname, const struct object_id *oid,
991 int flag, void *cb_data)
7171d8c1
JH
992{
993 const char *symref_target;
994 struct string_list_item *item;
7171d8c1
JH
995
996 if ((flag & REF_ISSYMREF) == 0)
997 return 0;
744c040b 998 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
7171d8c1
JH
999 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1000 die("'%s' is a symref but it is not?", refname);
1001 item = string_list_append(cb_data, refname);
1002 item->util = xstrdup(symref_target);
1003 return 0;
1004}
1005
59076eba 1006static void upload_pack(void)
def88e9a 1007{
7171d8c1
JH
1008 struct string_list symref = STRING_LIST_INIT_DUP;
1009
1010 head_ref_namespaced(find_symref, &symref);
1011
42526b47
SP
1012 if (advertise_refs || !stateless_rpc) {
1013 reset_timeout();
7171d8c1
JH
1014 head_ref_namespaced(send_ref, &symref);
1015 for_each_namespaced_ref(send_ref, &symref);
ad491366 1016 advertise_shallow_grafts(1);
42526b47
SP
1017 packet_flush(1);
1018 } else {
e172755b
JK
1019 head_ref_namespaced(check_ref, NULL);
1020 for_each_namespaced_ref(check_ref, NULL);
42526b47 1021 }
7171d8c1 1022 string_list_clear(&symref, 1);
42526b47
SP
1023 if (advertise_refs)
1024 return;
1025
b1e9fff7 1026 receive_needs();
59076eba
DR
1027 if (want_obj.nr) {
1028 get_common_commits();
1029 create_pack_file();
1030 }
def88e9a
LT
1031}
1032
daebaa78
JH
1033static int upload_pack_config(const char *var, const char *value, void *unused)
1034{
7199c093
FM
1035 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1036 if (git_config_bool(var, value))
1037 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1038 else
1039 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
68ee6289
FM
1040 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1041 if (git_config_bool(var, value))
1042 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1043 else
1044 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
f8edeaa0
DT
1045 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1046 if (git_config_bool(var, value))
1047 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1048 else
1049 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
7199c093 1050 } else if (!strcmp("uploadpack.keepalive", var)) {
05e95155
JK
1051 keepalive = git_config_int(var, value);
1052 if (!keepalive)
1053 keepalive = -1;
20b20a22
JK
1054 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1055 if (!strcmp("uploadpack.packobjectshook", var))
1056 return git_config_string(&pack_objects_hook, var, value);
10ac85c7
JH
1057 } else if (!strcmp("uploadpack.allowfilter", var)) {
1058 filter_advertise = git_config_bool(var, value);
05e95155 1059 }
daebaa78
JH
1060 return parse_hide_refs_config(var, value, "uploadpack");
1061}
1062
3f2e2297 1063int cmd_main(int argc, const char **argv)
def88e9a 1064{
9812f213 1065 const char *dir;
960deccb 1066 int strict = 0;
9812f213
AQ
1067 struct option options[] = {
1068 OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
1069 N_("quit after a single request/response exchange")),
1070 OPT_BOOL(0, "advertise-refs", &advertise_refs,
2e3a16b2 1071 N_("exit immediately after initial ref advertisement")),
9812f213
AQ
1072 OPT_BOOL(0, "strict", &strict,
1073 N_("do not try <directory>/.git/ if <directory> is no Git directory")),
1074 OPT_INTEGER(0, "timeout", &timeout,
1075 N_("interrupt transfer after <n> seconds of inactivity")),
1076 OPT_END()
1077 };
960deccb 1078
bbc30f99 1079 packet_trace_identity("upload-pack");
afc711b8 1080 check_replace_refs = 0;
2fb3f6db 1081
9812f213 1082 argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0);
960deccb 1083
9812f213
AQ
1084 if (argc != 1)
1085 usage_with_options(upload_pack_usage, options);
a6080a0a 1086
9812f213
AQ
1087 if (timeout)
1088 daemon_mode = 1;
04b33055 1089
e1464ca7 1090 setup_path();
04b33055 1091
9812f213 1092 dir = argv[0];
113b9475 1093
8d630132 1094 if (!enter_repo(dir, strict))
05ac6b34 1095 die("'%s' does not appear to be a git repository", dir);
ad491366 1096
daebaa78 1097 git_config(upload_pack_config, NULL);
def88e9a
LT
1098 upload_pack();
1099 return 0;
1100}