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